1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
10 * normal.c: Contains the main routine for processing characters in command
11 * mode. Communicates closely with the code in ops.c to handle
19 * The Visual area is remembered for reselection.
21 static int resel_VIsual_mode
= NUL
; /* 'v', 'V', or Ctrl-V */
22 static linenr_T resel_VIsual_line_count
; /* number of lines */
23 static colnr_T resel_VIsual_col
; /* nr of cols or end col */
25 static int restart_VIsual_select
= 0;
32 nv_compare
__ARGS((const void *s1
, const void *s2
));
33 static int find_command
__ARGS((int cmdchar
));
34 static void op_colon
__ARGS((oparg_T
*oap
));
35 static void op_function
__ARGS((oparg_T
*oap
));
36 #if defined(FEAT_MOUSE) && defined(FEAT_VISUAL)
37 static void find_start_of_word
__ARGS((pos_T
*));
38 static void find_end_of_word
__ARGS((pos_T
*));
39 static int get_mouse_class
__ARGS((char_u
*p
));
41 static void prep_redo_cmd
__ARGS((cmdarg_T
*cap
));
42 static void prep_redo
__ARGS((int regname
, long, int, int, int, int, int));
43 static int checkclearop
__ARGS((oparg_T
*oap
));
44 static int checkclearopq
__ARGS((oparg_T
*oap
));
45 static void clearop
__ARGS((oparg_T
*oap
));
46 static void clearopbeep
__ARGS((oparg_T
*oap
));
48 static void unshift_special
__ARGS((cmdarg_T
*cap
));
51 static void del_from_showcmd
__ARGS((int));
55 * nv_*(): functions called to handle Normal and Visual mode commands.
56 * n_*(): functions called to handle Normal mode commands.
57 * v_*(): functions called to handle Visual mode commands.
59 static void nv_ignore
__ARGS((cmdarg_T
*cap
));
60 static void nv_nop
__ARGS((cmdarg_T
*cap
));
61 static void nv_error
__ARGS((cmdarg_T
*cap
));
62 static void nv_help
__ARGS((cmdarg_T
*cap
));
63 static void nv_addsub
__ARGS((cmdarg_T
*cap
));
64 static void nv_page
__ARGS((cmdarg_T
*cap
));
65 static void nv_gd
__ARGS((oparg_T
*oap
, int nchar
, int thisblock
));
66 static int nv_screengo
__ARGS((oparg_T
*oap
, int dir
, long dist
));
68 static void nv_mousescroll
__ARGS((cmdarg_T
*cap
));
69 static void nv_mouse
__ARGS((cmdarg_T
*cap
));
71 static void nv_scroll_line
__ARGS((cmdarg_T
*cap
));
72 static void nv_zet
__ARGS((cmdarg_T
*cap
));
74 static void nv_ver_scrollbar
__ARGS((cmdarg_T
*cap
));
75 static void nv_hor_scrollbar
__ARGS((cmdarg_T
*cap
));
77 #ifdef FEAT_GUI_TABLINE
78 static void nv_tabline
__ARGS((cmdarg_T
*cap
));
79 static void nv_tabmenu
__ARGS((cmdarg_T
*cap
));
81 static void nv_exmode
__ARGS((cmdarg_T
*cap
));
82 static void nv_colon
__ARGS((cmdarg_T
*cap
));
83 static void nv_ctrlg
__ARGS((cmdarg_T
*cap
));
84 static void nv_ctrlh
__ARGS((cmdarg_T
*cap
));
85 static void nv_clear
__ARGS((cmdarg_T
*cap
));
86 static void nv_ctrlo
__ARGS((cmdarg_T
*cap
));
87 static void nv_hat
__ARGS((cmdarg_T
*cap
));
88 static void nv_Zet
__ARGS((cmdarg_T
*cap
));
89 static void nv_ident
__ARGS((cmdarg_T
*cap
));
90 static void nv_tagpop
__ARGS((cmdarg_T
*cap
));
91 static void nv_scroll
__ARGS((cmdarg_T
*cap
));
92 static void nv_right
__ARGS((cmdarg_T
*cap
));
93 static void nv_left
__ARGS((cmdarg_T
*cap
));
94 static void nv_up
__ARGS((cmdarg_T
*cap
));
95 static void nv_down
__ARGS((cmdarg_T
*cap
));
96 #ifdef FEAT_SEARCHPATH
97 static void nv_gotofile
__ARGS((cmdarg_T
*cap
));
99 static void nv_end
__ARGS((cmdarg_T
*cap
));
100 static void nv_dollar
__ARGS((cmdarg_T
*cap
));
101 static void nv_search
__ARGS((cmdarg_T
*cap
));
102 static void nv_next
__ARGS((cmdarg_T
*cap
));
103 static void normal_search
__ARGS((cmdarg_T
*cap
, int dir
, char_u
*pat
, int opt
));
104 static void nv_csearch
__ARGS((cmdarg_T
*cap
));
105 static void nv_brackets
__ARGS((cmdarg_T
*cap
));
106 static void nv_percent
__ARGS((cmdarg_T
*cap
));
107 static void nv_brace
__ARGS((cmdarg_T
*cap
));
108 static void nv_mark
__ARGS((cmdarg_T
*cap
));
109 static void nv_findpar
__ARGS((cmdarg_T
*cap
));
110 static void nv_undo
__ARGS((cmdarg_T
*cap
));
111 static void nv_kundo
__ARGS((cmdarg_T
*cap
));
112 static void nv_Replace
__ARGS((cmdarg_T
*cap
));
114 static void nv_vreplace
__ARGS((cmdarg_T
*cap
));
117 static void v_swap_corners
__ARGS((int cmdchar
));
119 static void nv_replace
__ARGS((cmdarg_T
*cap
));
120 static void n_swapchar
__ARGS((cmdarg_T
*cap
));
121 static void nv_cursormark
__ARGS((cmdarg_T
*cap
, int flag
, pos_T
*pos
));
123 static void v_visop
__ARGS((cmdarg_T
*cap
));
125 static void nv_subst
__ARGS((cmdarg_T
*cap
));
126 static void nv_abbrev
__ARGS((cmdarg_T
*cap
));
127 static void nv_optrans
__ARGS((cmdarg_T
*cap
));
128 static void nv_gomark
__ARGS((cmdarg_T
*cap
));
129 static void nv_pcmark
__ARGS((cmdarg_T
*cap
));
130 static void nv_regname
__ARGS((cmdarg_T
*cap
));
132 static void nv_visual
__ARGS((cmdarg_T
*cap
));
133 static void n_start_visual_mode
__ARGS((int c
));
135 static void nv_window
__ARGS((cmdarg_T
*cap
));
136 static void nv_suspend
__ARGS((cmdarg_T
*cap
));
137 static void nv_g_cmd
__ARGS((cmdarg_T
*cap
));
138 static void n_opencmd
__ARGS((cmdarg_T
*cap
));
139 static void nv_dot
__ARGS((cmdarg_T
*cap
));
140 static void nv_redo
__ARGS((cmdarg_T
*cap
));
141 static void nv_Undo
__ARGS((cmdarg_T
*cap
));
142 static void nv_tilde
__ARGS((cmdarg_T
*cap
));
143 static void nv_operator
__ARGS((cmdarg_T
*cap
));
145 static void set_op_var
__ARGS((int optype
));
147 static void nv_lineop
__ARGS((cmdarg_T
*cap
));
148 static void nv_home
__ARGS((cmdarg_T
*cap
));
149 static void nv_pipe
__ARGS((cmdarg_T
*cap
));
150 static void nv_bck_word
__ARGS((cmdarg_T
*cap
));
151 static void nv_wordcmd
__ARGS((cmdarg_T
*cap
));
152 static void nv_beginline
__ARGS((cmdarg_T
*cap
));
153 static void adjust_cursor
__ARGS((oparg_T
*oap
));
155 static void adjust_for_sel
__ARGS((cmdarg_T
*cap
));
156 static int unadjust_for_sel
__ARGS((void));
157 static void nv_select
__ARGS((cmdarg_T
*cap
));
159 static void nv_goto
__ARGS((cmdarg_T
*cap
));
160 static void nv_normal
__ARGS((cmdarg_T
*cap
));
161 static void nv_esc
__ARGS((cmdarg_T
*oap
));
162 static void nv_edit
__ARGS((cmdarg_T
*cap
));
163 static void invoke_edit
__ARGS((cmdarg_T
*cap
, int repl
, int cmd
, int startln
));
165 static void nv_object
__ARGS((cmdarg_T
*cap
));
167 static void nv_record
__ARGS((cmdarg_T
*cap
));
168 static void nv_at
__ARGS((cmdarg_T
*cap
));
169 static void nv_halfpage
__ARGS((cmdarg_T
*cap
));
170 static void nv_join
__ARGS((cmdarg_T
*cap
));
171 static void nv_put
__ARGS((cmdarg_T
*cap
));
172 static void nv_open
__ARGS((cmdarg_T
*cap
));
174 static void nv_sniff
__ARGS((cmdarg_T
*cap
));
176 #ifdef FEAT_NETBEANS_INTG
177 static void nv_nbcmd
__ARGS((cmdarg_T
*cap
));
180 static void nv_drop
__ARGS((cmdarg_T
*cap
));
183 static void nv_cursorhold
__ARGS((cmdarg_T
*cap
));
186 static char *e_noident
= N_("E349: No identifier under cursor");
189 * Function to be called for a Normal or Visual mode command.
190 * The argument is a cmdarg_T.
192 typedef void (*nv_func_T
) __ARGS((cmdarg_T
*cap
));
194 /* Values for cmd_flags. */
195 #define NV_NCH 0x01 /* may need to get a second char */
196 #define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
197 #define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
198 #define NV_LANG 0x08 /* second char needs language adjustment */
200 #define NV_SS 0x10 /* may start selection */
201 #define NV_SSS 0x20 /* may start selection with shift modifier */
202 #define NV_STS 0x40 /* may stop selection without shift modif. */
203 #define NV_RL 0x80 /* 'rightleft' modifies command */
204 #define NV_KEEPREG 0x100 /* don't clear regname */
205 #define NV_NCW 0x200 /* not allowed in command-line window */
208 * Generally speaking, every Normal mode command should either clear any
209 * pending operator (with *clearop*()), or set the motion type variable
212 * When a cursor motion command is made, it is marked as being a character or
213 * line oriented motion. Then, if an operator is in effect, the operation
214 * becomes character or line oriented accordingly.
218 * This table contains one entry for every Normal or Visual mode command.
219 * The order doesn't matter, init_normal_cmds() will create a sorted index.
220 * It is faster when all keys from zero to '~' are present.
222 static const struct nv_cmd
224 int cmd_char
; /* (first) command character */
225 nv_func_T cmd_func
; /* function for this command */
226 short_u cmd_flags
; /* NV_ flags */
227 short cmd_arg
; /* value for ca.arg */
230 {NUL
, nv_error
, 0, 0},
231 {Ctrl_A
, nv_addsub
, 0, 0},
232 {Ctrl_B
, nv_page
, NV_STS
, BACKWARD
},
233 {Ctrl_C
, nv_esc
, 0, TRUE
},
234 {Ctrl_D
, nv_halfpage
, 0, 0},
235 {Ctrl_E
, nv_scroll_line
, 0, TRUE
},
236 {Ctrl_F
, nv_page
, NV_STS
, FORWARD
},
237 {Ctrl_G
, nv_ctrlg
, 0, 0},
238 {Ctrl_H
, nv_ctrlh
, 0, 0},
239 {Ctrl_I
, nv_pcmark
, 0, 0},
240 {NL
, nv_down
, 0, FALSE
},
241 {Ctrl_K
, nv_error
, 0, 0},
242 {Ctrl_L
, nv_clear
, 0, 0},
243 {Ctrl_M
, nv_down
, 0, TRUE
},
244 {Ctrl_N
, nv_down
, NV_STS
, FALSE
},
245 {Ctrl_O
, nv_ctrlo
, 0, 0},
246 {Ctrl_P
, nv_up
, NV_STS
, FALSE
},
248 {Ctrl_Q
, nv_visual
, 0, FALSE
},
250 {Ctrl_Q
, nv_ignore
, 0, 0},
252 {Ctrl_R
, nv_redo
, 0, 0},
253 {Ctrl_S
, nv_ignore
, 0, 0},
254 {Ctrl_T
, nv_tagpop
, NV_NCW
, 0},
255 {Ctrl_U
, nv_halfpage
, 0, 0},
257 {Ctrl_V
, nv_visual
, 0, FALSE
},
258 {'V', nv_visual
, 0, FALSE
},
259 {'v', nv_visual
, 0, FALSE
},
261 {Ctrl_V
, nv_error
, 0, 0},
262 {'V', nv_error
, 0, 0},
263 {'v', nv_error
, 0, 0},
265 {Ctrl_W
, nv_window
, 0, 0},
266 {Ctrl_X
, nv_addsub
, 0, 0},
267 {Ctrl_Y
, nv_scroll_line
, 0, FALSE
},
268 {Ctrl_Z
, nv_suspend
, 0, 0},
269 {ESC
, nv_esc
, 0, FALSE
},
270 {Ctrl_BSL
, nv_normal
, NV_NCH_ALW
, 0},
271 {Ctrl_RSB
, nv_ident
, NV_NCW
, 0},
272 {Ctrl_HAT
, nv_hat
, NV_NCW
, 0},
273 {Ctrl__
, nv_error
, 0, 0},
274 {' ', nv_right
, 0, 0},
275 {'!', nv_operator
, 0, 0},
276 {'"', nv_regname
, NV_NCH_NOP
|NV_KEEPREG
, 0},
277 {'#', nv_ident
, 0, 0},
278 {'$', nv_dollar
, 0, 0},
279 {'%', nv_percent
, 0, 0},
280 {'&', nv_optrans
, 0, 0},
281 {'\'', nv_gomark
, NV_NCH_ALW
, TRUE
},
282 {'(', nv_brace
, 0, BACKWARD
},
283 {')', nv_brace
, 0, FORWARD
},
284 {'*', nv_ident
, 0, 0},
285 {'+', nv_down
, 0, TRUE
},
286 {',', nv_csearch
, 0, TRUE
},
287 {'-', nv_up
, 0, TRUE
},
288 {'.', nv_dot
, NV_KEEPREG
, 0},
289 {'/', nv_search
, 0, FALSE
},
290 {'0', nv_beginline
, 0, 0},
291 {'1', nv_ignore
, 0, 0},
292 {'2', nv_ignore
, 0, 0},
293 {'3', nv_ignore
, 0, 0},
294 {'4', nv_ignore
, 0, 0},
295 {'5', nv_ignore
, 0, 0},
296 {'6', nv_ignore
, 0, 0},
297 {'7', nv_ignore
, 0, 0},
298 {'8', nv_ignore
, 0, 0},
299 {'9', nv_ignore
, 0, 0},
300 {':', nv_colon
, 0, 0},
301 {';', nv_csearch
, 0, FALSE
},
302 {'<', nv_operator
, NV_RL
, 0},
303 {'=', nv_operator
, 0, 0},
304 {'>', nv_operator
, NV_RL
, 0},
305 {'?', nv_search
, 0, FALSE
},
306 {'@', nv_at
, NV_NCH_NOP
, FALSE
},
307 {'A', nv_edit
, 0, 0},
308 {'B', nv_bck_word
, 0, 1},
309 {'C', nv_abbrev
, NV_KEEPREG
, 0},
310 {'D', nv_abbrev
, NV_KEEPREG
, 0},
311 {'E', nv_wordcmd
, 0, TRUE
},
312 {'F', nv_csearch
, NV_NCH_ALW
|NV_LANG
, BACKWARD
},
313 {'G', nv_goto
, 0, TRUE
},
314 {'H', nv_scroll
, 0, 0},
315 {'I', nv_edit
, 0, 0},
316 {'J', nv_join
, 0, 0},
317 {'K', nv_ident
, 0, 0},
318 {'L', nv_scroll
, 0, 0},
319 {'M', nv_scroll
, 0, 0},
320 {'N', nv_next
, 0, SEARCH_REV
},
321 {'O', nv_open
, 0, 0},
323 {'Q', nv_exmode
, NV_NCW
, 0},
324 {'R', nv_Replace
, 0, FALSE
},
325 {'S', nv_subst
, NV_KEEPREG
, 0},
326 {'T', nv_csearch
, NV_NCH_ALW
|NV_LANG
, BACKWARD
},
327 {'U', nv_Undo
, 0, 0},
328 {'W', nv_wordcmd
, 0, TRUE
},
329 {'X', nv_abbrev
, NV_KEEPREG
, 0},
330 {'Y', nv_abbrev
, NV_KEEPREG
, 0},
331 {'Z', nv_Zet
, NV_NCH_NOP
|NV_NCW
, 0},
332 {'[', nv_brackets
, NV_NCH_ALW
, BACKWARD
},
333 {'\\', nv_error
, 0, 0},
334 {']', nv_brackets
, NV_NCH_ALW
, FORWARD
},
335 {'^', nv_beginline
, 0, BL_WHITE
| BL_FIX
},
336 {'_', nv_lineop
, 0, 0},
337 {'`', nv_gomark
, NV_NCH_ALW
, FALSE
},
338 {'a', nv_edit
, NV_NCH
, 0},
339 {'b', nv_bck_word
, 0, 0},
340 {'c', nv_operator
, 0, 0},
341 {'d', nv_operator
, 0, 0},
342 {'e', nv_wordcmd
, 0, FALSE
},
343 {'f', nv_csearch
, NV_NCH_ALW
|NV_LANG
, FORWARD
},
344 {'g', nv_g_cmd
, NV_NCH_ALW
, FALSE
},
345 {'h', nv_left
, NV_RL
, 0},
346 {'i', nv_edit
, NV_NCH
, 0},
347 {'j', nv_down
, 0, FALSE
},
348 {'k', nv_up
, 0, FALSE
},
349 {'l', nv_right
, NV_RL
, 0},
350 {'m', nv_mark
, NV_NCH_NOP
, 0},
351 {'n', nv_next
, 0, 0},
352 {'o', nv_open
, 0, 0},
354 {'q', nv_record
, NV_NCH
, 0},
355 {'r', nv_replace
, NV_NCH_NOP
|NV_LANG
, 0},
356 {'s', nv_subst
, NV_KEEPREG
, 0},
357 {'t', nv_csearch
, NV_NCH_ALW
|NV_LANG
, FORWARD
},
358 {'u', nv_undo
, 0, 0},
359 {'w', nv_wordcmd
, 0, FALSE
},
360 {'x', nv_abbrev
, NV_KEEPREG
, 0},
361 {'y', nv_operator
, 0, 0},
362 {'z', nv_zet
, NV_NCH_ALW
, 0},
363 {'{', nv_findpar
, 0, BACKWARD
},
364 {'|', nv_pipe
, 0, 0},
365 {'}', nv_findpar
, 0, FORWARD
},
366 {'~', nv_tilde
, 0, 0},
369 {POUND
, nv_ident
, 0, 0},
371 {K_MOUSEUP
, nv_mousescroll
, 0, TRUE
},
372 {K_MOUSEDOWN
, nv_mousescroll
, 0, FALSE
},
373 {K_LEFTMOUSE
, nv_mouse
, 0, 0},
374 {K_LEFTMOUSE_NM
, nv_mouse
, 0, 0},
375 {K_LEFTDRAG
, nv_mouse
, 0, 0},
376 {K_LEFTRELEASE
, nv_mouse
, 0, 0},
377 {K_LEFTRELEASE_NM
, nv_mouse
, 0, 0},
378 {K_MIDDLEMOUSE
, nv_mouse
, 0, 0},
379 {K_MIDDLEDRAG
, nv_mouse
, 0, 0},
380 {K_MIDDLERELEASE
, nv_mouse
, 0, 0},
381 {K_RIGHTMOUSE
, nv_mouse
, 0, 0},
382 {K_RIGHTDRAG
, nv_mouse
, 0, 0},
383 {K_RIGHTRELEASE
, nv_mouse
, 0, 0},
384 {K_X1MOUSE
, nv_mouse
, 0, 0},
385 {K_X1DRAG
, nv_mouse
, 0, 0},
386 {K_X1RELEASE
, nv_mouse
, 0, 0},
387 {K_X2MOUSE
, nv_mouse
, 0, 0},
388 {K_X2DRAG
, nv_mouse
, 0, 0},
389 {K_X2RELEASE
, nv_mouse
, 0, 0},
391 {K_IGNORE
, nv_ignore
, NV_KEEPREG
, 0},
392 {K_NOP
, nv_nop
, 0, 0},
393 {K_INS
, nv_edit
, 0, 0},
394 {K_KINS
, nv_edit
, 0, 0},
395 {K_BS
, nv_ctrlh
, 0, 0},
396 {K_UP
, nv_up
, NV_SSS
|NV_STS
, FALSE
},
397 {K_S_UP
, nv_page
, NV_SS
, BACKWARD
},
398 {K_DOWN
, nv_down
, NV_SSS
|NV_STS
, FALSE
},
399 {K_S_DOWN
, nv_page
, NV_SS
, FORWARD
},
400 {K_LEFT
, nv_left
, NV_SSS
|NV_STS
|NV_RL
, 0},
401 {K_S_LEFT
, nv_bck_word
, NV_SS
|NV_RL
, 0},
402 {K_C_LEFT
, nv_bck_word
, NV_SSS
|NV_RL
|NV_STS
, 1},
403 {K_RIGHT
, nv_right
, NV_SSS
|NV_STS
|NV_RL
, 0},
404 {K_S_RIGHT
, nv_wordcmd
, NV_SS
|NV_RL
, FALSE
},
405 {K_C_RIGHT
, nv_wordcmd
, NV_SSS
|NV_RL
|NV_STS
, TRUE
},
406 {K_PAGEUP
, nv_page
, NV_SSS
|NV_STS
, BACKWARD
},
407 {K_KPAGEUP
, nv_page
, NV_SSS
|NV_STS
, BACKWARD
},
408 {K_PAGEDOWN
, nv_page
, NV_SSS
|NV_STS
, FORWARD
},
409 {K_KPAGEDOWN
, nv_page
, NV_SSS
|NV_STS
, FORWARD
},
410 {K_END
, nv_end
, NV_SSS
|NV_STS
, FALSE
},
411 {K_KEND
, nv_end
, NV_SSS
|NV_STS
, FALSE
},
412 {K_S_END
, nv_end
, NV_SS
, FALSE
},
413 {K_C_END
, nv_end
, NV_SSS
|NV_STS
, TRUE
},
414 {K_HOME
, nv_home
, NV_SSS
|NV_STS
, 0},
415 {K_KHOME
, nv_home
, NV_SSS
|NV_STS
, 0},
416 {K_S_HOME
, nv_home
, NV_SS
, 0},
417 {K_C_HOME
, nv_goto
, NV_SSS
|NV_STS
, FALSE
},
418 {K_DEL
, nv_abbrev
, 0, 0},
419 {K_KDEL
, nv_abbrev
, 0, 0},
420 {K_UNDO
, nv_kundo
, 0, 0},
421 {K_HELP
, nv_help
, NV_NCW
, 0},
422 {K_F1
, nv_help
, NV_NCW
, 0},
423 {K_XF1
, nv_help
, NV_NCW
, 0},
425 {K_SELECT
, nv_select
, 0, 0},
428 {K_VER_SCROLLBAR
, nv_ver_scrollbar
, 0, 0},
429 {K_HOR_SCROLLBAR
, nv_hor_scrollbar
, 0, 0},
431 #ifdef FEAT_GUI_TABLINE
432 {K_TABLINE
, nv_tabline
, 0, 0},
433 {K_TABMENU
, nv_tabmenu
, 0, 0},
436 {K_F8
, farsi_fkey
, 0, 0},
437 {K_F9
, farsi_fkey
, 0, 0},
440 {K_SNIFF
, nv_sniff
, 0, 0},
442 #ifdef FEAT_NETBEANS_INTG
443 {K_F21
, nv_nbcmd
, NV_NCH_ALW
, 0},
446 {K_DROP
, nv_drop
, NV_STS
, 0},
449 {K_CURSORHOLD
, nv_cursorhold
, NV_KEEPREG
, 0},
453 /* Number of commands in nv_cmds[]. */
454 #define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
456 /* Sorted index of commands in nv_cmds[]. */
457 static short nv_cmd_idx
[NV_CMDS_SIZE
];
459 /* The highest index for which
460 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
461 static int nv_max_linear
;
464 * Compare functions for qsort() below, that checks the command character
465 * through the index in nv_cmd_idx[].
477 /* The commands are sorted on absolute value. */
478 c1
= nv_cmds
[*(const short *)s1
].cmd_char
;
479 c2
= nv_cmds
[*(const short *)s2
].cmd_char
;
488 * Initialize the nv_cmd_idx[] table.
495 /* Fill the index table with a one to one relation. */
496 for (i
= 0; i
< NV_CMDS_SIZE
; ++i
)
499 /* Sort the commands by the command character. */
500 qsort((void *)&nv_cmd_idx
, (size_t)NV_CMDS_SIZE
, sizeof(short), nv_compare
);
502 /* Find the first entry that can't be indexed by the command character. */
503 for (i
= 0; i
< NV_CMDS_SIZE
; ++i
)
504 if (i
!= nv_cmds
[nv_cmd_idx
[i
]].cmd_char
)
506 nv_max_linear
= i
- 1;
510 * Search for a command in the commands table.
511 * Returns -1 for invalid command.
514 find_command(cmdchar
)
523 /* A multi-byte character is never a command. */
524 if (cmdchar
>= 0x100)
528 /* We use the absolute value of the character. Special keys have a
529 * negative value, but are sorted on their absolute value. */
533 /* If the character is in the first part: The character is the index into
535 if (cmdchar
<= nv_max_linear
)
536 return nv_cmd_idx
[cmdchar
];
538 /* Perform a binary search. */
539 bot
= nv_max_linear
+ 1;
540 top
= NV_CMDS_SIZE
- 1;
545 c
= nv_cmds
[nv_cmd_idx
[i
]].cmd_char
;
562 * Execute a command in Normal mode.
566 normal_cmd(oap
, toplevel
)
568 int toplevel
; /* TRUE when called from main() */
570 cmdarg_T ca
; /* command arguments */
572 int ctrl_w
= FALSE
; /* got CTRL-W command */
573 int old_col
= curwin
->w_curswant
;
574 #ifdef FEAT_CMDL_INFO
575 int need_flushbuf
; /* need to call out_flush() */
578 pos_T old_pos
; /* cursor position before command */
580 static int old_mapped_len
= 0;
584 int set_prevcount
= FALSE
;
587 vim_memset(&ca
, 0, sizeof(ca
)); /* also resets ca.retval */
590 /* Use a count remembered from before entering an operator. After typing
591 * "3d" we return from normal_cmd() and come back here, the "3" is
592 * remembered in "opcount". */
593 ca
.opcount
= opcount
;
596 want_sniff_request
= sniff_connected
;
600 * If there is an operator pending, then the command we take this time
601 * will terminate it. Finish_op tells us to finish the operation before
602 * returning this time (unless the operation was cancelled).
607 finish_op
= (oap
->op_type
!= OP_NOP
);
611 ui_cursor_shape(); /* may show different cursor shape */
612 # ifdef FEAT_MOUSESHAPE
613 update_mouseshape(-1);
618 /* When not finishing an operator and no register name typed, reset the
620 if (!finish_op
&& !oap
->regname
)
624 set_prevcount
= TRUE
;
629 /* Restore counts from before receiving K_CURSORHOLD. This means after
630 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
632 if (oap
->prev_opcount
> 0 || oap
->prev_count0
> 0)
634 ca
.opcount
= oap
->prev_opcount
;
635 ca
.count0
= oap
->prev_count0
;
636 oap
->prev_opcount
= 0;
637 oap
->prev_count0
= 0;
642 mapped_len
= typebuf_maplen();
646 #ifdef USE_ON_FLY_SCROLL
647 dont_scroll
= FALSE
; /* allow scrolling here */
651 * Get the command character from the user.
656 LANGMAP_ADJUST(c
, TRUE
);
661 * If a mapping was started in Visual or Select mode, remember the length
662 * of the mapping. This is used below to not return to Insert mode for as
663 * long as the mapping is being executed.
665 if (restart_edit
== 0)
667 else if (old_mapped_len
668 || (VIsual_active
&& mapped_len
== 0 && typebuf_maplen() > 0))
669 old_mapped_len
= typebuf_maplen();
677 * In Select mode, typed text replaces the selection.
681 && (vim_isprintc(c
) || c
== NL
|| c
== CAR
|| c
== K_KENTER
))
683 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
684 * 'insertmode' is set) fake a "d"elete command, Insert mode will
685 * restart automatically.
686 * Insert the typed character in the typeahead buffer, so that it can
687 * be mapped in Insert mode. Required for ":lmap" to work. */
689 if (restart_edit
!= 0)
693 msg_nowait
= TRUE
; /* don't delay going to insert mode */
697 #ifdef FEAT_CMDL_INFO
698 need_flushbuf
= add_to_showcmd(c
);
703 if (!(VIsual_active
&& VIsual_select
))
707 * Handle a count before a command and compute ca.count0.
708 * Note that '0' is a command and not the start of a count, but it's
709 * part of a count after other digits.
711 while ( (c
>= '1' && c
<= '9')
712 || (ca
.count0
!= 0 && (c
== K_DEL
|| c
== K_KDEL
|| c
== '0')))
714 if (c
== K_DEL
|| c
== K_KDEL
)
717 #ifdef FEAT_CMDL_INFO
718 del_from_showcmd(4); /* delete the digit and ~@% */
722 ca
.count0
= ca
.count0
* 10 + (c
- '0');
723 if (ca
.count0
< 0) /* got too large! */
724 ca
.count0
= 999999999L;
726 /* Set v:count here, when called from main() and not a stuffed
727 * command, so that v:count can be used in an expression mapping
728 * right after the count. */
729 if (toplevel
&& stuff_empty())
731 long count
= ca
.count0
;
733 /* multiply with ca.opcount the same way as below */
735 count
= ca
.opcount
* (count
== 0 ? 1 : count
);
736 set_vcount(count
, count
== 0 ? 1 : count
, set_prevcount
);
737 set_prevcount
= FALSE
; /* only set v:prevcount once */
743 ++allow_keys
; /* no mapping for nchar, but keys */
745 ++no_zero_mapping
; /* don't map zero here */
748 LANGMAP_ADJUST(c
, TRUE
);
756 #ifdef FEAT_CMDL_INFO
757 need_flushbuf
|= add_to_showcmd(c
);
762 * If we got CTRL-W there may be a/another count
764 if (c
== Ctrl_W
&& !ctrl_w
&& oap
->op_type
== OP_NOP
)
767 ca
.opcount
= ca
.count0
; /* remember first count */
770 ++allow_keys
; /* no mapping for nchar, but keys */
771 c
= plain_vgetc(); /* get next character */
773 LANGMAP_ADJUST(c
, TRUE
);
777 #ifdef FEAT_CMDL_INFO
778 need_flushbuf
|= add_to_showcmd(c
);
780 goto getcount
; /* jump back */
785 if (c
== K_CURSORHOLD
)
787 /* Save the count values so that ca.opcount and ca.count0 are exactly
788 * the same when coming back here after handling K_CURSORHOLD. */
789 oap
->prev_opcount
= ca
.opcount
;
790 oap
->prev_count0
= ca
.count0
;
797 * If we're in the middle of an operator (including after entering a
798 * yank buffer with '"') AND we had a count before the operator, then
799 * that count overrides the current value of ca.count0.
800 * What this means effectively, is that commands like "3dw" get turned
801 * into "d3w" which makes things fall into place pretty neatly.
802 * If you give a count before AND after the operator, they are
806 ca
.count0
*= ca
.opcount
;
808 ca
.count0
= ca
.opcount
;
812 * Always remember the count. It will be set to zero (on the next call,
813 * above) when there is no pending operator.
814 * When called from main(), save the count for use by the "count" built-in
817 ca
.opcount
= ca
.count0
;
818 ca
.count1
= (ca
.count0
== 0 ? 1 : ca
.count0
);
822 * Only set v:count when called from main() and not a stuffed command.
824 if (toplevel
&& stuff_empty())
825 set_vcount(ca
.count0
, ca
.count1
, set_prevcount
);
829 * Find the command character in the table of commands.
830 * For CTRL-W we already got nchar when looking for a count.
839 idx
= find_command(ca
.cmdchar
);
842 /* Not a known command: beep. */
847 if (text_locked() && (nv_cmds
[idx
].cmd_flags
& NV_NCW
))
849 /* This command is not allowed while editing a ccmdline: beep. */
855 if ((nv_cmds
[idx
].cmd_flags
& NV_NCW
) && curbuf_locked())
861 * In Visual/Select mode, a few keys are handled in a special way.
865 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
867 && (nv_cmds
[idx
].cmd_flags
& NV_STS
)
868 && !(mod_mask
& MOD_MASK_SHIFT
))
871 redraw_curbuf_later(INVERTED
);
874 /* Keys that work different when 'keymodel' contains "startsel" */
877 if (nv_cmds
[idx
].cmd_flags
& NV_SS
)
879 unshift_special(&ca
);
880 idx
= find_command(ca
.cmdchar
);
888 else if ((nv_cmds
[idx
].cmd_flags
& NV_SSS
)
889 && (mod_mask
& MOD_MASK_SHIFT
))
891 mod_mask
&= ~MOD_MASK_SHIFT
;
897 #ifdef FEAT_RIGHTLEFT
898 if (curwin
->w_p_rl
&& KeyTyped
&& !KeyStuffed
899 && (nv_cmds
[idx
].cmd_flags
& NV_RL
))
901 /* Invert horizontal movements and operations. Only when typed by the
902 * user directly, not when the result of a mapping or "x" translated
906 case 'l': ca
.cmdchar
= 'h'; break;
907 case K_RIGHT
: ca
.cmdchar
= K_LEFT
; break;
908 case K_S_RIGHT
: ca
.cmdchar
= K_S_LEFT
; break;
909 case K_C_RIGHT
: ca
.cmdchar
= K_C_LEFT
; break;
910 case 'h': ca
.cmdchar
= 'l'; break;
911 case K_LEFT
: ca
.cmdchar
= K_RIGHT
; break;
912 case K_S_LEFT
: ca
.cmdchar
= K_S_RIGHT
; break;
913 case K_C_LEFT
: ca
.cmdchar
= K_C_RIGHT
; break;
914 case '>': ca
.cmdchar
= '<'; break;
915 case '<': ca
.cmdchar
= '>'; break;
917 idx
= find_command(ca
.cmdchar
);
922 * Get an additional character if we need one.
924 if ((nv_cmds
[idx
].cmd_flags
& NV_NCH
)
925 && (((nv_cmds
[idx
].cmd_flags
& NV_NCH_NOP
) == NV_NCH_NOP
926 && oap
->op_type
== OP_NOP
)
927 || (nv_cmds
[idx
].cmd_flags
& NV_NCH_ALW
) == NV_NCH_ALW
928 || (ca
.cmdchar
== 'q'
929 && oap
->op_type
== OP_NOP
932 || ((ca
.cmdchar
== 'a' || ca
.cmdchar
== 'i')
933 && (oap
->op_type
!= OP_NOP
940 int repl
= FALSE
; /* get character for replace mode */
941 int lit
= FALSE
; /* get extra character literally */
942 int langmap_active
= FALSE
; /* using :lmap mappings */
943 int lang
; /* getting a text character */
944 #ifdef USE_IM_CONTROL
945 int save_smd
; /* saved value of p_smd */
949 ++allow_keys
; /* no mapping for nchar, but allow key codes */
951 /* Don't generate a CursorHold event here, most commands can't handle
952 * it, e.g., nv_replace(), nv_csearch(). */
953 did_cursorhold
= TRUE
;
955 if (ca
.cmdchar
== 'g')
958 * For 'g' get the next character now, so that we can check for
959 * "gr", "g'" and "g`".
961 ca
.nchar
= plain_vgetc();
963 LANGMAP_ADJUST(ca
.nchar
, TRUE
);
965 #ifdef FEAT_CMDL_INFO
966 need_flushbuf
|= add_to_showcmd(ca
.nchar
);
968 if (ca
.nchar
== 'r' || ca
.nchar
== '\'' || ca
.nchar
== '`'
969 || ca
.nchar
== Ctrl_BSL
)
971 cp
= &ca
.extra_char
; /* need to get a third character */
973 lit
= TRUE
; /* get it literally */
975 repl
= TRUE
; /* get it in replace mode */
978 cp
= NULL
; /* no third character needed */
982 if (ca
.cmdchar
== 'r') /* get it in replace mode */
986 lang
= (repl
|| (nv_cmds
[idx
].cmd_flags
& NV_LANG
));
989 * Get a second or third character.
996 State
= REPLACE
; /* pretend Replace mode */
997 ui_cursor_shape(); /* show different cursor shape */
1000 if (lang
&& curbuf
->b_p_iminsert
== B_IMODE_LMAP
)
1002 /* Allow mappings defined with ":lmap". */
1009 langmap_active
= TRUE
;
1011 #ifdef USE_IM_CONTROL
1013 p_smd
= FALSE
; /* Don't let the IM code show the mode here */
1014 if (lang
&& curbuf
->b_p_iminsert
== B_IMODE_IM
)
1015 im_set_active(TRUE
);
1018 *cp
= plain_vgetc();
1022 /* Undo the decrement done above */
1025 State
= NORMAL_BUSY
;
1027 #ifdef USE_IM_CONTROL
1030 if (curbuf
->b_p_iminsert
!= B_IMODE_LMAP
)
1031 im_save_status(&curbuf
->b_p_iminsert
);
1032 im_set_active(FALSE
);
1037 State
= NORMAL_BUSY
;
1039 #ifdef FEAT_CMDL_INFO
1040 need_flushbuf
|= add_to_showcmd(*cp
);
1045 #ifdef FEAT_DIGRAPHS
1046 /* Typing CTRL-K gets a digraph. */
1048 && ((nv_cmds
[idx
].cmd_flags
& NV_LANG
)
1049 || cp
== &ca
.extra_char
)
1050 && vim_strchr(p_cpo
, CPO_DIGRAPH
) == NULL
)
1052 c
= get_digraph(FALSE
);
1056 # ifdef FEAT_CMDL_INFO
1057 /* Guessing how to update showcmd here... */
1058 del_from_showcmd(3);
1059 need_flushbuf
|= add_to_showcmd(*cp
);
1066 /* adjust chars > 127, except after "tTfFr" commands */
1067 LANGMAP_ADJUST(*cp
, !lang
);
1069 #ifdef FEAT_RIGHTLEFT
1070 /* adjust Hebrew mapped char */
1071 if (p_hkmap
&& lang
&& KeyTyped
)
1074 /* adjust Farsi mapped char */
1075 if (p_fkmap
&& lang
&& KeyTyped
)
1082 * When the next character is CTRL-\ a following CTRL-N means the
1083 * command is aborted and we go to Normal mode.
1085 if (cp
== &ca
.extra_char
1086 && ca
.nchar
== Ctrl_BSL
1087 && (ca
.extra_char
== Ctrl_N
|| ca
.extra_char
== Ctrl_G
))
1089 ca
.cmdchar
= Ctrl_BSL
;
1090 ca
.nchar
= ca
.extra_char
;
1091 idx
= find_command(ca
.cmdchar
);
1093 else if (*cp
== Ctrl_BSL
)
1095 long towait
= (p_ttm
>= 0 ? p_ttm
: p_tm
);
1097 /* There is a busy wait here when typing "f<C-\>" and then
1098 * something different from CTRL-N. Can't be avoided. */
1099 while ((c
= vpeekc()) <= 0 && towait
> 0L)
1101 do_sleep(towait
> 50L ? 50L : towait
);
1107 if (c
!= Ctrl_N
&& c
!= Ctrl_G
)
1111 ca
.cmdchar
= Ctrl_BSL
;
1113 idx
= find_command(ca
.cmdchar
);
1119 /* When getting a text character and the next character is a
1120 * multi-byte character, it could be a composing character.
1121 * However, don't wait for it to arrive. */
1122 while (enc_utf8
&& lang
&& (c
= vpeekc()) > 0
1123 && (c
>= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1126 if (!utf_iscomposing(c
))
1128 vungetc(c
); /* it wasn't, put it back */
1131 else if (ca
.ncharC1
== 0)
1142 #ifdef FEAT_CMDL_INFO
1144 * Flush the showcmd characters onto the screen so we can see them while
1145 * the command is being executed. Only do this when the shown command was
1146 * actually displayed, otherwise this will slow down a lot when executing
1153 if (ca
.cmdchar
!= K_IGNORE
)
1154 did_cursorhold
= FALSE
;
1159 if (ca
.nchar
== ESC
)
1162 if (restart_edit
== 0 && goto_im())
1167 if (ca
.cmdchar
!= K_IGNORE
)
1169 msg_didout
= FALSE
; /* don't scroll screen up for normal command */
1174 old_pos
= curwin
->w_cursor
; /* remember where cursor was */
1176 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1178 if (!VIsual_active
&& km_startsel
)
1180 if (nv_cmds
[idx
].cmd_flags
& NV_SS
)
1183 unshift_special(&ca
);
1184 idx
= find_command(ca
.cmdchar
);
1186 else if ((nv_cmds
[idx
].cmd_flags
& NV_SSS
)
1187 && (mod_mask
& MOD_MASK_SHIFT
))
1190 mod_mask
&= ~MOD_MASK_SHIFT
;
1196 * Execute the command!
1197 * Call the command function found in the commands table.
1199 ca
.arg
= nv_cmds
[idx
].cmd_arg
;
1200 (nv_cmds
[idx
].cmd_func
)(&ca
);
1203 * If we didn't start or finish an operator, reset oap->regname, unless we
1208 && (idx
< 0 || !(nv_cmds
[idx
].cmd_flags
& NV_KEEPREG
)))
1217 /* Get the length of mapped chars again after typing a count, second
1218 * character or "z333<cr>". */
1219 if (old_mapped_len
> 0)
1220 old_mapped_len
= typebuf_maplen();
1224 * If an operation is pending, handle it...
1226 do_pending_operator(&ca
, old_col
, FALSE
);
1229 * Wait for a moment when a message is displayed that will be overwritten
1230 * by the mode message.
1231 * In Visual mode and with "^O" in Insert mode, a short message will be
1232 * overwritten by the mode message. Wait a bit, until a key is hit.
1233 * In Visual mode, it's more important to keep the Visual area updated
1234 * than keeping a message (e.g. from a /pat search).
1235 * Only do this if the command was typed, not from a mapping.
1236 * Don't wait when emsg_silent is non-zero.
1237 * Also wait a bit after an error message, e.g. for "^O:".
1238 * Don't redraw the screen, it would remove the message.
1242 && (restart_edit
!= 0
1245 && old_pos
.lnum
== curwin
->w_cursor
.lnum
1246 && old_pos
.col
== curwin
->w_cursor
.col
)
1251 && (msg_didout
|| (msg_didany
&& msg_scroll
))
1254 || (restart_edit
!= 0
1259 || emsg_on_display
)))
1260 && oap
->regname
== 0
1261 && !(ca
.retval
& CA_COMMAND_BUSY
)
1266 && oap
->op_type
== OP_NOP
)
1268 int save_State
= State
;
1270 /* Draw the cursor with the right shape here */
1271 if (restart_edit
!= 0)
1274 /* If need to redraw, and there is a "keep_msg", redraw before the
1276 if (must_redraw
&& keep_msg
!= NULL
&& !emsg_on_display
)
1282 /* showmode() will clear keep_msg, but we want to use it anyway */
1284 /* now reset it, otherwise it's put in the history again */
1286 msg_attr(kmsg
, keep_msg_attr
);
1292 if (msg_scroll
|| emsg_on_display
)
1293 ui_delay(1000L, TRUE
); /* wait at least one second */
1294 ui_delay(3000L, FALSE
); /* wait up to three seconds */
1298 emsg_on_display
= FALSE
;
1302 * Finish up after executing a Normal mode command.
1308 /* Reset finish_op, in case it was set */
1314 /* Redraw the cursor with another shape, if we were in Operator-pending
1315 * mode or did a replace command. */
1316 if (c
|| ca
.cmdchar
== 'r')
1318 ui_cursor_shape(); /* may show different cursor shape */
1319 # ifdef FEAT_MOUSESHAPE
1320 update_mouseshape(-1);
1325 #ifdef FEAT_CMDL_INFO
1326 if (oap
->op_type
== OP_NOP
&& oap
->regname
== 0
1327 # ifdef FEAT_AUTOCMD
1328 && ca
.cmdchar
!= K_CURSORHOLD
1334 checkpcmark(); /* check if we moved since setting pcmark */
1335 vim_free(ca
.searchbuf
);
1342 #ifdef FEAT_SCROLLBIND
1343 if (curwin
->w_p_scb
&& toplevel
)
1345 validate_cursor(); /* may need to update w_leftcol */
1346 do_check_scrollbind(TRUE
);
1351 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1352 * if still inside a mapping that started in Visual mode).
1353 * May switch from Visual to Select mode after CTRL-O command.
1355 if ( oap
->op_type
== OP_NOP
1357 && ((restart_edit
!= 0 && !VIsual_active
&& old_mapped_len
== 0)
1358 || restart_VIsual_select
== 1)
1360 && restart_edit
!= 0
1362 && !(ca
.retval
& CA_COMMAND_BUSY
)
1364 && oap
->regname
== 0)
1367 if (restart_VIsual_select
== 1)
1369 VIsual_select
= TRUE
;
1371 restart_VIsual_select
= 0;
1374 if (restart_edit
!= 0
1376 && !VIsual_active
&& old_mapped_len
== 0
1379 (void)edit(restart_edit
, FALSE
, 1L);
1383 if (restart_VIsual_select
== 2)
1384 restart_VIsual_select
= 1;
1387 /* Save count before an operator for next time. */
1388 opcount
= ca
.opcount
;
1392 * Handle an operator after visual mode or when the movement is finished
1395 do_pending_operator(cap
, old_col
, gui_yank
)
1400 oparg_T
*oap
= cap
->oap
;
1402 int empty_region_error
;
1403 int restart_edit_save
;
1406 /* The visual area is remembered for redo */
1407 static int redo_VIsual_mode
= NUL
; /* 'v', 'V', or Ctrl-V */
1408 static linenr_T redo_VIsual_line_count
; /* number of lines */
1409 static colnr_T redo_VIsual_col
; /* number of cols or end column */
1410 static long redo_VIsual_count
; /* count for Visual operator */
1411 # ifdef FEAT_VIRTUALEDIT
1412 int include_line_break
= FALSE
;
1416 #if defined(FEAT_CLIPBOARD)
1418 * Yank the visual area into the GUI selection register before we operate
1419 * on it and lose it forever.
1420 * Don't do it if a specific register was specified, so that ""x"*P works.
1421 * This could call do_pending_operator() recursively, but that's OK
1422 * because gui_yank will be TRUE for the nested call.
1424 if (clip_star
.available
1425 && oap
->op_type
!= OP_NOP
1429 && !redo_VIsual_busy
1431 && oap
->regname
== 0)
1434 old_cursor
= curwin
->w_cursor
;
1437 * If an operation is pending, handle it...
1443 ) && oap
->op_type
!= OP_NOP
)
1446 oap
->is_VIsual
= VIsual_active
;
1447 if (oap
->motion_force
== 'V')
1448 oap
->motion_type
= MLINE
;
1449 else if (oap
->motion_force
== 'v')
1451 /* If the motion was linewise, "inclusive" will not have been set.
1452 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1453 if (oap
->motion_type
== MLINE
)
1454 oap
->inclusive
= FALSE
;
1455 /* If the motion already was characterwise, toggle "inclusive" */
1456 else if (oap
->motion_type
== MCHAR
)
1457 oap
->inclusive
= !oap
->inclusive
;
1458 oap
->motion_type
= MCHAR
;
1460 else if (oap
->motion_force
== Ctrl_V
)
1462 /* Change line- or characterwise motion into Visual block mode. */
1463 VIsual_active
= TRUE
;
1464 VIsual
= oap
->start
;
1465 VIsual_mode
= Ctrl_V
;
1466 VIsual_select
= FALSE
;
1467 VIsual_reselect
= FALSE
;
1471 /* only redo yank when 'y' flag is in 'cpoptions' */
1472 /* never redo "zf" (define fold) */
1473 if ((vim_strchr(p_cpo
, CPO_YANK
) != NULL
|| oap
->op_type
!= OP_YANK
)
1475 && (!VIsual_active
|| oap
->motion_force
)
1477 && cap
->cmdchar
!= 'D'
1479 && oap
->op_type
!= OP_FOLD
1480 && oap
->op_type
!= OP_FOLDOPEN
1481 && oap
->op_type
!= OP_FOLDOPENREC
1482 && oap
->op_type
!= OP_FOLDCLOSE
1483 && oap
->op_type
!= OP_FOLDCLOSEREC
1484 && oap
->op_type
!= OP_FOLDDEL
1485 && oap
->op_type
!= OP_FOLDDELREC
1489 prep_redo(oap
->regname
, cap
->count0
,
1490 get_op_char(oap
->op_type
), get_extra_op_char(oap
->op_type
),
1491 oap
->motion_force
, cap
->cmdchar
, cap
->nchar
);
1492 if (cap
->cmdchar
== '/' || cap
->cmdchar
== '?') /* was a search */
1495 * If 'cpoptions' does not contain 'r', insert the search
1496 * pattern to really repeat the same command.
1498 if (vim_strchr(p_cpo
, CPO_REDO
) == NULL
)
1499 AppendToRedobuffLit(cap
->searchbuf
, -1);
1500 AppendToRedobuff(NL_STR
);
1502 else if (cap
->cmdchar
== ':')
1504 /* do_cmdline() has stored the first typed line in
1505 * "repeat_cmdline". When several lines are typed repeating
1506 * won't be possible. */
1507 if (repeat_cmdline
== NULL
)
1511 AppendToRedobuffLit(repeat_cmdline
, -1);
1512 AppendToRedobuff(NL_STR
);
1513 vim_free(repeat_cmdline
);
1514 repeat_cmdline
= NULL
;
1520 if (redo_VIsual_busy
)
1522 oap
->start
= curwin
->w_cursor
;
1523 curwin
->w_cursor
.lnum
+= redo_VIsual_line_count
- 1;
1524 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
1525 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
1526 VIsual_mode
= redo_VIsual_mode
;
1527 if (VIsual_mode
== 'v')
1529 if (redo_VIsual_line_count
<= 1)
1530 curwin
->w_cursor
.col
+= redo_VIsual_col
- 1;
1532 curwin
->w_cursor
.col
= redo_VIsual_col
;
1534 if (redo_VIsual_col
== MAXCOL
)
1536 curwin
->w_curswant
= MAXCOL
;
1537 coladvance((colnr_T
)MAXCOL
);
1539 cap
->count0
= redo_VIsual_count
;
1540 if (redo_VIsual_count
!= 0)
1541 cap
->count1
= redo_VIsual_count
;
1545 else if (VIsual_active
)
1549 /* Save the current VIsual area for '< and '> marks, and "gv" */
1550 curbuf
->b_visual
.vi_start
= VIsual
;
1551 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
1552 curbuf
->b_visual
.vi_mode
= VIsual_mode
;
1553 curbuf
->b_visual
.vi_curswant
= curwin
->w_curswant
;
1555 curbuf
->b_visual_mode_eval
= VIsual_mode
;
1559 /* In Select mode, a linewise selection is operated upon like a
1560 * characterwise selection. */
1561 if (VIsual_select
&& VIsual_mode
== 'V')
1563 if (lt(VIsual
, curwin
->w_cursor
))
1566 curwin
->w_cursor
.col
=
1567 (colnr_T
)STRLEN(ml_get(curwin
->w_cursor
.lnum
));
1571 curwin
->w_cursor
.col
= 0;
1572 VIsual
.col
= (colnr_T
)STRLEN(ml_get(VIsual
.lnum
));
1576 /* If 'selection' is "exclusive", backup one character for
1577 * charwise selections. */
1578 else if (VIsual_mode
== 'v')
1580 # ifdef FEAT_VIRTUALEDIT
1581 include_line_break
=
1586 oap
->start
= VIsual
;
1587 if (VIsual_mode
== 'V')
1590 #endif /* FEAT_VISUAL */
1593 * Set oap->start to the first position of the operated text, oap->end
1594 * to the end of the operated text. w_cursor is equal to oap->start.
1596 if (lt(oap
->start
, curwin
->w_cursor
))
1599 /* Include folded lines completely. */
1602 if (hasFolding(oap
->start
.lnum
, &oap
->start
.lnum
, NULL
))
1604 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
,
1605 &curwin
->w_cursor
.lnum
))
1606 curwin
->w_cursor
.col
= (colnr_T
)STRLEN(ml_get_curline());
1609 oap
->end
= curwin
->w_cursor
;
1610 curwin
->w_cursor
= oap
->start
;
1612 /* w_virtcol may have been updated; if the cursor goes back to its
1613 * previous position w_virtcol becomes invalid and isn't updated
1615 curwin
->w_valid
&= ~VALID_VIRTCOL
;
1620 /* Include folded lines completely. */
1621 if (!VIsual_active
&& oap
->motion_type
== MLINE
)
1623 if (hasFolding(curwin
->w_cursor
.lnum
, &curwin
->w_cursor
.lnum
,
1625 curwin
->w_cursor
.col
= 0;
1626 if (hasFolding(oap
->start
.lnum
, NULL
, &oap
->start
.lnum
))
1627 oap
->start
.col
= (colnr_T
)STRLEN(ml_get(oap
->start
.lnum
));
1630 oap
->end
= oap
->start
;
1631 oap
->start
= curwin
->w_cursor
;
1634 oap
->line_count
= oap
->end
.lnum
- oap
->start
.lnum
+ 1;
1636 #ifdef FEAT_VIRTUALEDIT
1637 /* Set "virtual_op" before resetting VIsual_active. */
1638 virtual_op
= virtual_active();
1642 if (VIsual_active
|| redo_VIsual_busy
)
1644 if (VIsual_mode
== Ctrl_V
) /* block mode */
1648 oap
->block_mode
= TRUE
;
1650 getvvcol(curwin
, &(oap
->start
),
1651 &oap
->start_vcol
, NULL
, &oap
->end_vcol
);
1652 if (!redo_VIsual_busy
)
1654 getvvcol(curwin
, &(oap
->end
), &start
, NULL
, &end
);
1656 if (start
< oap
->start_vcol
)
1657 oap
->start_vcol
= start
;
1658 if (end
> oap
->end_vcol
)
1660 if (*p_sel
== 'e' && start
>= 1
1661 && start
- 1 >= oap
->end_vcol
)
1662 oap
->end_vcol
= start
- 1;
1664 oap
->end_vcol
= end
;
1668 /* if '$' was used, get oap->end_vcol from longest line */
1669 if (curwin
->w_curswant
== MAXCOL
)
1671 curwin
->w_cursor
.col
= MAXCOL
;
1673 for (curwin
->w_cursor
.lnum
= oap
->start
.lnum
;
1674 curwin
->w_cursor
.lnum
<= oap
->end
.lnum
;
1675 ++curwin
->w_cursor
.lnum
)
1677 getvvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &end
);
1678 if (end
> oap
->end_vcol
)
1679 oap
->end_vcol
= end
;
1682 else if (redo_VIsual_busy
)
1683 oap
->end_vcol
= oap
->start_vcol
+ redo_VIsual_col
- 1;
1685 * Correct oap->end.col and oap->start.col to be the
1686 * upper-left and lower-right corner of the block area.
1688 * (Actually, this does convert column positions into character
1691 curwin
->w_cursor
.lnum
= oap
->end
.lnum
;
1692 coladvance(oap
->end_vcol
);
1693 oap
->end
= curwin
->w_cursor
;
1695 curwin
->w_cursor
= oap
->start
;
1696 coladvance(oap
->start_vcol
);
1697 oap
->start
= curwin
->w_cursor
;
1700 if (!redo_VIsual_busy
&& !gui_yank
)
1703 * Prepare to reselect and redo Visual: this is based on the
1704 * size of the Visual text
1706 resel_VIsual_mode
= VIsual_mode
;
1707 if (curwin
->w_curswant
== MAXCOL
)
1708 resel_VIsual_col
= MAXCOL
;
1709 else if (VIsual_mode
== Ctrl_V
)
1710 resel_VIsual_col
= oap
->end_vcol
- oap
->start_vcol
+ 1;
1711 else if (oap
->line_count
> 1)
1712 resel_VIsual_col
= oap
->end
.col
;
1714 resel_VIsual_col
= oap
->end
.col
- oap
->start
.col
+ 1;
1715 resel_VIsual_line_count
= oap
->line_count
;
1718 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1719 if ((vim_strchr(p_cpo
, CPO_YANK
) != NULL
|| oap
->op_type
!= OP_YANK
)
1720 && oap
->op_type
!= OP_COLON
1722 && oap
->op_type
!= OP_FOLD
1723 && oap
->op_type
!= OP_FOLDOPEN
1724 && oap
->op_type
!= OP_FOLDOPENREC
1725 && oap
->op_type
!= OP_FOLDCLOSE
1726 && oap
->op_type
!= OP_FOLDCLOSEREC
1727 && oap
->op_type
!= OP_FOLDDEL
1728 && oap
->op_type
!= OP_FOLDDELREC
1730 && oap
->motion_force
== NUL
1733 /* Prepare for redoing. Only use the nchar field for "r",
1734 * otherwise it might be the second char of the operator. */
1735 prep_redo(oap
->regname
, 0L, NUL
, 'v',
1736 get_op_char(oap
->op_type
),
1737 get_extra_op_char(oap
->op_type
),
1738 oap
->op_type
== OP_REPLACE
? cap
->nchar
: NUL
);
1739 if (!redo_VIsual_busy
)
1741 redo_VIsual_mode
= resel_VIsual_mode
;
1742 redo_VIsual_col
= resel_VIsual_col
;
1743 redo_VIsual_line_count
= resel_VIsual_line_count
;
1744 redo_VIsual_count
= cap
->count0
;
1749 * oap->inclusive defaults to TRUE.
1750 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1751 * FALSE. This makes "d}P" and "v}dP" work the same.
1753 if (oap
->motion_force
== NUL
|| oap
->motion_type
== MLINE
)
1754 oap
->inclusive
= TRUE
;
1755 if (VIsual_mode
== 'V')
1756 oap
->motion_type
= MLINE
;
1759 oap
->motion_type
= MCHAR
;
1760 if (VIsual_mode
!= Ctrl_V
&& *ml_get_pos(&(oap
->end
)) == NUL
1761 # ifdef FEAT_VIRTUALEDIT
1762 && (include_line_break
|| !virtual_op
)
1766 oap
->inclusive
= FALSE
;
1767 /* Try to include the newline, unless it's an operator
1768 * that works on lines only */
1770 && !op_on_lines(oap
->op_type
)
1771 && oap
->end
.lnum
< curbuf
->b_ml
.ml_line_count
)
1775 # ifdef FEAT_VIRTUALEDIT
1776 oap
->end
.coladd
= 0;
1783 redo_VIsual_busy
= FALSE
;
1786 * Switch Visual off now, so screen updating does
1787 * not show inverted text when the screen is redrawn.
1788 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1789 * no screen redraw, so it is done here to remove the inverted
1794 VIsual_active
= FALSE
;
1800 clear_cmdline
= TRUE
; /* unshow visual mode later */
1801 #ifdef FEAT_CMDL_INFO
1805 if ((oap
->op_type
== OP_YANK
1806 || oap
->op_type
== OP_COLON
1807 || oap
->op_type
== OP_FUNCTION
1808 || oap
->op_type
== OP_FILTER
)
1809 && oap
->motion_force
== NUL
)
1810 redraw_curbuf_later(INVERTED
);
1816 /* Include the trailing byte of a multi-byte char. */
1817 if (has_mbyte
&& oap
->inclusive
)
1821 l
= (*mb_ptr2len
)(ml_get_pos(&oap
->end
));
1823 oap
->end
.col
+= l
- 1;
1826 curwin
->w_set_curswant
= TRUE
;
1829 * oap->empty is set when start and end are the same. The inclusive
1830 * flag affects this too, unless yanking and the end is on a NUL.
1832 oap
->empty
= (oap
->motion_type
== MCHAR
1834 || (oap
->op_type
== OP_YANK
1835 && gchar_pos(&oap
->end
) == NUL
))
1836 && equalpos(oap
->start
, oap
->end
)
1837 #ifdef FEAT_VIRTUALEDIT
1838 && !(virtual_op
&& oap
->start
.coladd
!= oap
->end
.coladd
)
1842 * For delete, change and yank, it's an error to operate on an
1843 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1845 empty_region_error
= (oap
->empty
1846 && vim_strchr(p_cpo
, CPO_EMPTYREGION
) != NULL
);
1849 /* Force a redraw when operating on an empty Visual region, when
1850 * 'modifiable is off or creating a fold. */
1851 if (oap
->is_VIsual
&& (oap
->empty
|| !curbuf
->b_p_ma
1852 # ifdef FEAT_FOLDING
1853 || oap
->op_type
== OP_FOLD
1856 redraw_curbuf_later(INVERTED
);
1860 * If the end of an operator is in column one while oap->motion_type
1861 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1862 * character in the previous line. If op_start is on or before the
1863 * first non-blank in the line, the operator becomes linewise
1864 * (strange, but that's the way vi does it).
1866 if ( oap
->motion_type
== MCHAR
1867 && oap
->inclusive
== FALSE
1868 && !(cap
->retval
& CA_NO_ADJ_OP_END
)
1869 && oap
->end
.col
== 0
1871 && (!oap
->is_VIsual
|| *p_sel
== 'o')
1874 && oap
->line_count
> 1)
1876 oap
->end_adjusted
= TRUE
; /* remember that we did this */
1880 oap
->motion_type
= MLINE
;
1883 oap
->end
.col
= (colnr_T
)STRLEN(ml_get(oap
->end
.lnum
));
1887 oap
->inclusive
= TRUE
;
1892 oap
->end_adjusted
= FALSE
;
1894 switch (oap
->op_type
)
1900 oap
->is_VIsual
? (int)cap
->count1
:
1903 auto_format(FALSE
, TRUE
);
1908 if (oap
->line_count
< 2)
1909 oap
->line_count
= 2;
1910 if (curwin
->w_cursor
.lnum
+ oap
->line_count
- 1 >
1911 curbuf
->b_ml
.ml_line_count
)
1915 do_do_join(oap
->line_count
, oap
->op_type
== OP_JOIN
);
1916 auto_format(FALSE
, TRUE
);
1922 VIsual_reselect
= FALSE
; /* don't reselect now */
1924 if (empty_region_error
)
1928 (void)op_delete(oap
);
1929 if (oap
->motion_type
== MLINE
&& has_format_option(FO_AUTO
))
1930 u_save_cursor(); /* cursor line wasn't saved yet */
1931 auto_format(FALSE
, TRUE
);
1936 if (empty_region_error
)
1942 (void)op_yank(oap
, FALSE
, !gui_yank
);
1948 VIsual_reselect
= FALSE
; /* don't reselect now */
1950 if (empty_region_error
)
1954 /* This is a new edit command, not a restart. Need to
1955 * remember it to make 'insertmode' work with mappings for
1956 * Visual mode. But do this only once and not when typed and
1957 * 'insertmode' isn't set. */
1958 if (p_im
|| !KeyTyped
)
1959 restart_edit_save
= restart_edit
;
1961 restart_edit_save
= 0;
1963 /* Reset finish_op now, don't want it set inside edit(). */
1965 if (op_change(oap
)) /* will call edit() */
1966 cap
->retval
|= CA_COMMAND_BUSY
;
1967 if (restart_edit
== 0)
1968 restart_edit
= restart_edit_save
;
1973 if (vim_strchr(p_cpo
, CPO_FILTER
) != NULL
)
1974 AppendToRedobuff((char_u
*)"!\r"); /* use any last used !cmd */
1976 bangredo
= TRUE
; /* do_bang() will put cmd in redo buffer */
1981 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1983 * If 'equalprg' is empty, do the indenting internally.
1985 if (oap
->op_type
== OP_INDENT
&& *get_equalprg() == NUL
)
1988 if (curbuf
->b_p_lisp
)
1990 op_reindent(oap
, get_lisp_indent
);
1994 # ifdef FEAT_CINDENT
1997 *curbuf
->b_p_inde
!= NUL
? get_expr_indent
:
2012 if (empty_region_error
)
2020 #if defined(FEAT_EVAL)
2021 if (*curbuf
->b_p_fex
!= NUL
)
2022 op_formatexpr(oap
); /* use expression */
2026 op_colon(oap
); /* use external command */
2028 op_format(oap
, FALSE
); /* use internal function */
2032 op_format(oap
, TRUE
); /* use internal function */
2036 op_function(oap
); /* call 'operatorfunc' */
2042 VIsual_reselect
= FALSE
; /* don't reselect now */
2044 #ifdef FEAT_VISUALEXTRA
2045 if (empty_region_error
)
2049 /* This is a new edit command, not a restart. Need to
2050 * remember it to make 'insertmode' work with mappings for
2051 * Visual mode. But do this only once. */
2052 restart_edit_save
= restart_edit
;
2055 op_insert(oap
, cap
->count1
);
2057 /* TODO: when inserting in several lines, should format all
2059 auto_format(FALSE
, TRUE
);
2061 if (restart_edit
== 0)
2062 restart_edit
= restart_edit_save
;
2071 VIsual_reselect
= FALSE
; /* don't reselect now */
2073 #ifdef FEAT_VISUALEXTRA
2074 if (empty_region_error
)
2077 #ifdef FEAT_VISUALEXTRA
2079 op_replace(oap
, cap
->nchar
);
2085 VIsual_reselect
= FALSE
; /* don't reselect now */
2086 foldCreate(oap
->start
.lnum
, oap
->end
.lnum
);
2090 case OP_FOLDOPENREC
:
2092 case OP_FOLDCLOSEREC
:
2093 VIsual_reselect
= FALSE
; /* don't reselect now */
2094 opFoldRange(oap
->start
.lnum
, oap
->end
.lnum
,
2095 oap
->op_type
== OP_FOLDOPEN
2096 || oap
->op_type
== OP_FOLDOPENREC
,
2097 oap
->op_type
== OP_FOLDOPENREC
2098 || oap
->op_type
== OP_FOLDCLOSEREC
,
2104 VIsual_reselect
= FALSE
; /* don't reselect now */
2105 deleteFold(oap
->start
.lnum
, oap
->end
.lnum
,
2106 oap
->op_type
== OP_FOLDDELREC
, oap
->is_VIsual
);
2112 #ifdef FEAT_VIRTUALEDIT
2118 * if 'sol' not set, go back to old column for some commands
2120 if (!p_sol
&& oap
->motion_type
== MLINE
&& !oap
->end_adjusted
2121 && (oap
->op_type
== OP_LSHIFT
|| oap
->op_type
== OP_RSHIFT
2122 || oap
->op_type
== OP_DELETE
))
2123 coladvance(curwin
->w_curswant
= old_col
);
2127 curwin
->w_cursor
= old_cursor
;
2130 oap
->block_mode
= FALSE
;
2137 * Handle indent and format operators and visual mode ":".
2143 stuffcharReadbuff(':');
2146 stuffReadbuff((char_u
*)"'<,'>");
2151 * Make the range look nice, so it can be repeated.
2153 if (oap
->start
.lnum
== curwin
->w_cursor
.lnum
)
2154 stuffcharReadbuff('.');
2156 stuffnumReadbuff((long)oap
->start
.lnum
);
2157 if (oap
->end
.lnum
!= oap
->start
.lnum
)
2159 stuffcharReadbuff(',');
2160 if (oap
->end
.lnum
== curwin
->w_cursor
.lnum
)
2161 stuffcharReadbuff('.');
2162 else if (oap
->end
.lnum
== curbuf
->b_ml
.ml_line_count
)
2163 stuffcharReadbuff('$');
2164 else if (oap
->start
.lnum
== curwin
->w_cursor
.lnum
)
2166 stuffReadbuff((char_u
*)".+");
2167 stuffnumReadbuff((long)oap
->line_count
- 1);
2170 stuffnumReadbuff((long)oap
->end
.lnum
);
2173 if (oap
->op_type
!= OP_COLON
)
2174 stuffReadbuff((char_u
*)"!");
2175 if (oap
->op_type
== OP_INDENT
)
2177 #ifndef FEAT_CINDENT
2178 if (*get_equalprg() == NUL
)
2179 stuffReadbuff((char_u
*)"indent");
2182 stuffReadbuff(get_equalprg());
2183 stuffReadbuff((char_u
*)"\n");
2185 else if (oap
->op_type
== OP_FORMAT
)
2188 stuffReadbuff((char_u
*)"fmt");
2190 stuffReadbuff(p_fp
);
2191 stuffReadbuff((char_u
*)"\n']");
2195 * do_cmdline() does the rest
2200 * Handle the "g@" operator: call 'operatorfunc'.
2210 if (*p_opfunc
== NUL
)
2211 EMSG(_("E774: 'operatorfunc' is empty"));
2214 /* Set '[ and '] marks to text to be operated on. */
2215 curbuf
->b_op_start
= oap
->start
;
2216 curbuf
->b_op_end
= oap
->end
;
2217 if (oap
->motion_type
!= MLINE
&& !oap
->inclusive
)
2218 /* Exclude the end position. */
2219 decl(&curbuf
->b_op_end
);
2221 if (oap
->block_mode
)
2222 argv
[0] = (char_u
*)"block";
2223 else if (oap
->motion_type
== MLINE
)
2224 argv
[0] = (char_u
*)"line";
2226 argv
[0] = (char_u
*)"char";
2227 (void)call_func_retnr(p_opfunc
, 1, argv
, FALSE
);
2230 EMSG(_("E775: Eval feature not available"));
2234 #if defined(FEAT_MOUSE) || defined(PROTO)
2236 * Do the appropriate action for the current mouse click in the current mode.
2237 * Not used for Command-line mode.
2240 * event modi- position visual change action
2241 * fier cursor window
2242 * left press - yes end yes
2243 * left press C yes end yes "^]" (2)
2244 * left press S yes end yes "*" (2)
2245 * left drag - yes start if moved no
2246 * left relse - yes start if moved no
2247 * middle press - yes if not active no put register
2248 * middle press - yes if active no yank and put
2249 * right press - yes start or extend yes
2250 * right press S yes no change yes "#" (2)
2251 * right drag - yes extend no
2252 * right relse - yes extend no
2254 * Insert or Replace Mode:
2255 * event modi- position visual change action
2256 * fier cursor window
2257 * left press - yes (cannot be active) yes
2258 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2259 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2260 * left drag - yes start or extend (1) no CTRL-O (1)
2261 * left relse - yes start or extend (1) no CTRL-O (1)
2262 * middle press - no (cannot be active) no put register
2263 * right press - yes start or extend yes CTRL-O
2264 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2266 * (1) only if mouse pointer moved since press
2267 * (2) only if click is in same buffer
2269 * Return TRUE if start_arrow() should be called for edit mode.
2272 do_mouse(oap
, c
, dir
, count
, fixindent
)
2273 oparg_T
*oap
; /* operator argument, can be NULL */
2274 int c
; /* K_LEFTMOUSE, etc */
2275 int dir
; /* Direction to 'put' if necessary */
2277 int fixindent
; /* PUT_FIXINDENT if fixing indent necessary */
2279 static int do_always
= FALSE
; /* ignore 'mouse' setting next time */
2280 static int got_click
= FALSE
; /* got a click some time back */
2282 int which_button
; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2283 int is_click
; /* If FALSE it's a drag or release event */
2284 int is_drag
; /* If TRUE it's a drag event */
2285 int jump_flags
= 0; /* flags for jump_to_mouse() */
2287 int moved
; /* Has cursor moved? */
2288 int in_status_line
; /* mouse in status line */
2289 #ifdef FEAT_VERTSPLIT
2290 int in_sep_line
; /* mouse in vertical separator line */
2293 #if defined(FEAT_FOLDING)
2296 win_T
*old_curwin
= curwin
;
2298 static pos_T orig_cursor
;
2299 colnr_T leftcol
, rightcol
;
2302 int old_active
= VIsual_active
;
2303 int old_mode
= VIsual_mode
;
2307 #if defined(FEAT_FOLDING)
2308 save_cursor
= curwin
->w_cursor
;
2312 * When GUI is active, always recognize mouse events, otherwise:
2313 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2314 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2315 * - For command line and insert mode 'mouse' is checked before calling
2328 if (!mouse_has(MOUSE_VISUAL
))
2333 if (State
== NORMAL
&& !mouse_has(MOUSE_NORMAL
))
2337 which_button
= get_mouse_button(KEY2TERMCAP1(c
), &is_click
, &is_drag
);
2339 #ifdef FEAT_MOUSESHAPE
2340 /* May have stopped dragging the status or separator line. The pointer is
2341 * most likely still on the status or separator line. */
2342 if (!is_drag
&& drag_status_line
)
2344 drag_status_line
= FALSE
;
2345 update_mouseshape(SHAPE_IDX_STATUS
);
2347 # ifdef FEAT_VERTSPLIT
2348 if (!is_drag
&& drag_sep_line
)
2350 drag_sep_line
= FALSE
;
2351 update_mouseshape(SHAPE_IDX_VSEP
);
2357 * Ignore drag and release events if we didn't get a click.
2363 if (!got_click
) /* didn't get click, ignore */
2365 if (!is_drag
) /* release, reset got_click */
2371 * ALT is only used for starging/extending Visual mode.
2373 if ((mod_mask
& MOD_MASK_ALT
))
2378 * CTRL right mouse button does CTRL-T
2380 if (is_click
&& (mod_mask
& MOD_MASK_CTRL
) && which_button
== MOUSE_RIGHT
)
2383 stuffcharReadbuff(Ctrl_O
);
2385 stuffnumReadbuff(count
);
2386 stuffcharReadbuff(Ctrl_T
);
2387 got_click
= FALSE
; /* ignore drag&release now */
2392 * CTRL only works with left mouse button
2394 if ((mod_mask
& MOD_MASK_CTRL
) && which_button
!= MOUSE_LEFT
)
2398 * When a modifier is down, ignore drag and release events, as well as
2399 * multiple clicks and the middle mouse button.
2400 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2402 if ((mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
| MOD_MASK_ALT
2405 || (mod_mask
& MOD_MASK_MULTI_CLICK
)
2406 || which_button
== MOUSE_MIDDLE
)
2407 && !((mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_ALT
))
2408 && mouse_model_popup()
2409 && which_button
== MOUSE_LEFT
)
2410 && !((mod_mask
& MOD_MASK_ALT
)
2411 && !mouse_model_popup()
2412 && which_button
== MOUSE_RIGHT
)
2417 * If the button press was used as the movement command for an operator
2418 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2419 * drag/release events.
2421 if (!is_click
&& which_button
== MOUSE_MIDDLE
)
2425 regname
= oap
->regname
;
2430 * Middle mouse button does a 'put' of the selected text
2432 if (which_button
== MOUSE_MIDDLE
)
2434 if (State
== NORMAL
)
2437 * If an operator was pending, we don't know what the user wanted
2438 * to do. Go back to normal mode: Clear the operator and beep().
2440 if (oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2448 * If visual was active, yank the highlighted text and put it
2449 * before the mouse pointer position.
2450 * In Select mode replace the highlighted text with the clipboard.
2456 stuffcharReadbuff(Ctrl_G
);
2457 stuffReadbuff((char_u
*)"\"+p");
2461 stuffcharReadbuff('y');
2462 stuffcharReadbuff(K_MIDDLEMOUSE
);
2464 do_always
= TRUE
; /* ignore 'mouse' setting next time */
2469 * The rest is below jump_to_mouse()
2473 else if ((State
& INSERT
) == 0)
2477 * Middle click in insert mode doesn't move the mouse, just insert the
2478 * contents of a register. '.' register is special, can't insert that
2480 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2481 * happens for the GUI).
2483 if ((State
& INSERT
) || !mouse_has(MOUSE_NORMAL
))
2486 insert_reg(regname
, TRUE
);
2489 #ifdef FEAT_CLIPBOARD
2490 if (clip_star
.available
&& regname
== 0)
2493 if ((State
& REPLACE_FLAG
) && !yank_register_mline(regname
))
2494 insert_reg(regname
, TRUE
);
2497 do_put(regname
, BACKWARD
, 1L, fixindent
| PUT_CURSEND
);
2499 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2500 AppendCharToRedobuff(Ctrl_R
);
2501 AppendCharToRedobuff(fixindent
? Ctrl_P
: Ctrl_O
);
2502 AppendCharToRedobuff(regname
== 0 ? '"' : regname
);
2509 /* When dragging or button-up stay in the same window. */
2511 jump_flags
|= MOUSE_FOCUS
| MOUSE_DID_MOVE
;
2513 start_visual
.lnum
= 0;
2516 /* Check for clicking in the tab page line. */
2517 if (mouse_row
== 0 && firstwin
->w_winrow
> 0)
2521 got_click
= FALSE
; /* ignore mouse-up and drag events */
2523 /* click in a tab selects that tab page */
2528 && mouse_col
< Columns
)
2530 c1
= TabPageIdxs
[mouse_col
];
2533 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2535 /* double click opens new page */
2538 tabpage_move(c1
== 0 ? 9999 : c1
- 1);
2542 /* Go to specified tab page, or next one if not clicking
2546 /* It's like clicking on the status line of a window. */
2547 if (curwin
!= old_curwin
)
2555 /* Close the current or specified tab page. */
2559 tp
= find_tabpage(-c1
);
2562 if (first_tabpage
->tp_next
!= NULL
)
2563 tabpage_close(FALSE
);
2565 else if (tp
!= NULL
)
2566 tabpage_close_other(tp
, FALSE
);
2574 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2575 * right button up -> pop-up menu
2576 * shift-left button -> right button
2577 * alt-left button -> alt-right button
2579 if (mouse_model_popup())
2581 if (which_button
== MOUSE_RIGHT
2582 && !(mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
)))
2585 * NOTE: Ignore right button down and drag mouse events.
2586 * Windows only shows the popup menu on the button up event.
2588 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2589 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2593 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2594 if (is_click
|| is_drag
)
2597 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2598 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2599 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2603 if (STRCMP(p_mousem
, "popup_setpos") == 0)
2605 /* First set the cursor position before showing the popup
2613 * set MOUSE_MAY_STOP_VIS if we are outside the
2614 * selection or the current window (might have false
2617 if (mouse_row
< W_WINROW(curwin
)
2619 > (W_WINROW(curwin
) + curwin
->w_height
))
2620 jump_flags
= MOUSE_MAY_STOP_VIS
;
2621 else if (get_fpos_of_mouse(&m_pos
) != IN_BUFFER
)
2622 jump_flags
= MOUSE_MAY_STOP_VIS
;
2625 if ((lt(curwin
->w_cursor
, VIsual
)
2626 && (lt(m_pos
, curwin
->w_cursor
)
2627 || lt(VIsual
, m_pos
)))
2628 || (lt(VIsual
, curwin
->w_cursor
)
2629 && (lt(m_pos
, VIsual
)
2630 || lt(curwin
->w_cursor
, m_pos
))))
2632 jump_flags
= MOUSE_MAY_STOP_VIS
;
2634 else if (VIsual_mode
== Ctrl_V
)
2636 getvcols(curwin
, &curwin
->w_cursor
, &VIsual
,
2637 &leftcol
, &rightcol
);
2638 getvcol(curwin
, &m_pos
, NULL
, &m_pos
.col
, NULL
);
2639 if (m_pos
.col
< leftcol
|| m_pos
.col
> rightcol
)
2640 jump_flags
= MOUSE_MAY_STOP_VIS
;
2645 jump_flags
= MOUSE_MAY_STOP_VIS
;
2650 jump_flags
= jump_to_mouse(jump_flags
, NULL
, which_button
);
2653 VIsual_active
? INVERTED
:
2657 out_flush(); /* Update before showing popup menu */
2660 gui_show_popupmenu();
2662 return (jump_flags
& CURSOR_MOVED
) != 0;
2670 if (which_button
== MOUSE_LEFT
2671 && (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_ALT
)))
2673 which_button
= MOUSE_RIGHT
;
2674 mod_mask
&= ~MOD_MASK_SHIFT
;
2679 if ((State
& (NORMAL
| INSERT
))
2680 && !(mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
)))
2682 if (which_button
== MOUSE_LEFT
)
2686 /* stop Visual mode for a left click in a window, but not when
2687 * on a status line */
2689 jump_flags
|= MOUSE_MAY_STOP_VIS
;
2691 else if (mouse_has(MOUSE_VISUAL
))
2692 jump_flags
|= MOUSE_MAY_VIS
;
2694 else if (which_button
== MOUSE_RIGHT
)
2696 if (is_click
&& VIsual_active
)
2699 * Remember the start and end of visual before moving the
2702 if (lt(curwin
->w_cursor
, VIsual
))
2704 start_visual
= curwin
->w_cursor
;
2705 end_visual
= VIsual
;
2709 start_visual
= VIsual
;
2710 end_visual
= curwin
->w_cursor
;
2713 jump_flags
|= MOUSE_FOCUS
;
2714 if (mouse_has(MOUSE_VISUAL
))
2715 jump_flags
|= MOUSE_MAY_VIS
;
2721 * If an operator is pending, ignore all drags and releases until the
2724 if (!is_drag
&& oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2727 oap
->motion_type
= MCHAR
;
2730 /* When releasing the button let jump_to_mouse() know. */
2731 if (!is_click
&& !is_drag
)
2732 jump_flags
|= MOUSE_RELEASED
;
2737 jump_flags
= jump_to_mouse(jump_flags
,
2738 oap
== NULL
? NULL
: &(oap
->inclusive
), which_button
);
2739 moved
= (jump_flags
& CURSOR_MOVED
);
2740 in_status_line
= (jump_flags
& IN_STATUS_LINE
);
2741 #ifdef FEAT_VERTSPLIT
2742 in_sep_line
= (jump_flags
& IN_SEP_LINE
);
2745 #ifdef FEAT_NETBEANS_INTG
2746 if (usingNetbeans
&& isNetbeansBuffer(curbuf
)
2747 && !(jump_flags
& (IN_STATUS_LINE
| IN_SEP_LINE
)))
2749 int key
= KEY2TERMCAP1(c
);
2751 if (key
== (int)KE_LEFTRELEASE
|| key
== (int)KE_MIDDLERELEASE
2752 || key
== (int)KE_RIGHTRELEASE
)
2753 netbeans_button_release(which_button
);
2757 /* When jumping to another window, clear a pending operator. That's a bit
2758 * friendlier than beeping and not jumping to that window. */
2759 if (curwin
!= old_curwin
&& oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2765 && (jump_flags
& (MOUSE_FOLD_CLOSE
| MOUSE_FOLD_OPEN
))
2766 && which_button
== MOUSE_LEFT
)
2768 /* open or close a fold at this line */
2769 if (jump_flags
& MOUSE_FOLD_OPEN
)
2770 openFold(curwin
->w_cursor
.lnum
, 1L);
2772 closeFold(curwin
->w_cursor
.lnum
, 1L);
2773 /* don't move the cursor if still in the same window */
2774 if (curwin
== old_curwin
)
2775 curwin
->w_cursor
= save_cursor
;
2779 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2780 if ((jump_flags
& IN_OTHER_WIN
) && !VIsual_active
&& clip_star
.available
)
2782 clip_modeless(which_button
, is_click
, is_drag
);
2788 /* Set global flag that we are extending the Visual area with mouse
2789 * dragging; temporarily minimize 'scrolloff'. */
2790 if (VIsual_active
&& is_drag
&& p_so
)
2792 /* In the very first line, allow scrolling one line */
2799 /* When dragging the mouse above the window, scroll down. */
2800 if (is_drag
&& mouse_row
< 0 && !in_status_line
)
2802 scroll_redraw(FALSE
, 1L);
2806 if (start_visual
.lnum
) /* right click in visual mode */
2808 /* When ALT is pressed make Visual mode blockwise. */
2809 if (mod_mask
& MOD_MASK_ALT
)
2810 VIsual_mode
= Ctrl_V
;
2813 * In Visual-block mode, divide the area in four, pick up the corner
2814 * that is in the quarter that the cursor is in.
2816 if (VIsual_mode
== Ctrl_V
)
2818 getvcols(curwin
, &start_visual
, &end_visual
, &leftcol
, &rightcol
);
2819 if (curwin
->w_curswant
> (leftcol
+ rightcol
) / 2)
2820 end_visual
.col
= leftcol
;
2822 end_visual
.col
= rightcol
;
2823 if (curwin
->w_cursor
.lnum
<
2824 (start_visual
.lnum
+ end_visual
.lnum
) / 2)
2825 end_visual
.lnum
= end_visual
.lnum
;
2827 end_visual
.lnum
= start_visual
.lnum
;
2829 /* move VIsual to the right column */
2830 start_visual
= curwin
->w_cursor
; /* save the cursor pos */
2831 curwin
->w_cursor
= end_visual
;
2832 coladvance(end_visual
.col
);
2833 VIsual
= curwin
->w_cursor
;
2834 curwin
->w_cursor
= start_visual
; /* restore the cursor */
2839 * If the click is before the start of visual, change the start.
2840 * If the click is after the end of visual, change the end. If
2841 * the click is inside the visual, change the closest side.
2843 if (lt(curwin
->w_cursor
, start_visual
))
2844 VIsual
= end_visual
;
2845 else if (lt(end_visual
, curwin
->w_cursor
))
2846 VIsual
= start_visual
;
2849 /* In the same line, compare column number */
2850 if (end_visual
.lnum
== start_visual
.lnum
)
2852 if (curwin
->w_cursor
.col
- start_visual
.col
>
2853 end_visual
.col
- curwin
->w_cursor
.col
)
2854 VIsual
= start_visual
;
2856 VIsual
= end_visual
;
2859 /* In different lines, compare line number */
2862 diff
= (curwin
->w_cursor
.lnum
- start_visual
.lnum
) -
2863 (end_visual
.lnum
- curwin
->w_cursor
.lnum
);
2865 if (diff
> 0) /* closest to end */
2866 VIsual
= start_visual
;
2867 else if (diff
< 0) /* closest to start */
2868 VIsual
= end_visual
;
2869 else /* in the middle line */
2871 if (curwin
->w_cursor
.col
<
2872 (start_visual
.col
+ end_visual
.col
) / 2)
2873 VIsual
= end_visual
;
2875 VIsual
= start_visual
;
2882 * If Visual mode started in insert mode, execute "CTRL-O"
2884 else if ((State
& INSERT
) && VIsual_active
)
2885 stuffcharReadbuff(Ctrl_O
);
2889 * Middle mouse click: Put text before cursor.
2891 if (which_button
== MOUSE_MIDDLE
)
2893 #ifdef FEAT_CLIPBOARD
2894 if (clip_star
.available
&& regname
== 0)
2897 if (yank_register_mline(regname
))
2899 if (mouse_past_bottom
)
2902 else if (mouse_past_eol
)
2907 c1
= (dir
== BACKWARD
) ? '[' : ']';
2912 c1
= (dir
== FORWARD
) ? 'p' : 'P';
2915 prep_redo(regname
, count
, NUL
, c1
, NUL
, c2
, NUL
);
2918 * Remember where the paste started, so in edit() Insstart can be set
2921 if (restart_edit
!= 0)
2922 where_paste_started
= curwin
->w_cursor
;
2923 do_put(regname
, dir
, count
, fixindent
| PUT_CURSEND
);
2926 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2928 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2929 * error under the mouse pointer.
2931 else if (((mod_mask
& MOD_MASK_CTRL
)
2932 || (mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2933 && bt_quickfix(curbuf
))
2936 stuffcharReadbuff(Ctrl_O
);
2937 if (curwin
->w_llist_ref
== NULL
) /* quickfix window */
2938 stuffReadbuff((char_u
*)":.cc\n");
2939 else /* location list window */
2940 stuffReadbuff((char_u
*)":.ll\n");
2941 got_click
= FALSE
; /* ignore drag&release now */
2946 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2947 * under the mouse pointer.
2949 else if ((mod_mask
& MOD_MASK_CTRL
) || (curbuf
->b_help
2950 && (mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
))
2953 stuffcharReadbuff(Ctrl_O
);
2954 stuffcharReadbuff(Ctrl_RSB
);
2955 got_click
= FALSE
; /* ignore drag&release now */
2959 * Shift-Mouse click searches for the next occurrence of the word under
2962 else if ((mod_mask
& MOD_MASK_SHIFT
))
2966 || (VIsual_active
&& VIsual_select
)
2969 stuffcharReadbuff(Ctrl_O
);
2970 if (which_button
== MOUSE_LEFT
)
2971 stuffcharReadbuff('*');
2972 else /* MOUSE_RIGHT */
2973 stuffcharReadbuff('#');
2976 /* Handle double clicks, unless on status line */
2977 else if (in_status_line
)
2979 #ifdef FEAT_MOUSESHAPE
2980 if ((is_drag
|| is_click
) && !drag_status_line
)
2982 drag_status_line
= TRUE
;
2983 update_mouseshape(-1);
2987 #ifdef FEAT_VERTSPLIT
2988 else if (in_sep_line
)
2990 # ifdef FEAT_MOUSESHAPE
2991 if ((is_drag
|| is_click
) && !drag_sep_line
)
2993 drag_sep_line
= TRUE
;
2994 update_mouseshape(-1);
3000 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) && (State
& (NORMAL
| INSERT
))
3001 && mouse_has(MOUSE_VISUAL
))
3003 if (is_click
|| !VIsual_active
)
3006 orig_cursor
= VIsual
;
3009 check_visual_highlight();
3010 VIsual
= curwin
->w_cursor
;
3011 orig_cursor
= VIsual
;
3012 VIsual_active
= TRUE
;
3013 VIsual_reselect
= TRUE
;
3014 /* start Select mode if 'selectmode' contains "mouse" */
3015 may_start_select('o');
3018 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
3020 /* Double click with ALT pressed makes it blockwise. */
3021 if (mod_mask
& MOD_MASK_ALT
)
3022 VIsual_mode
= Ctrl_V
;
3026 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_3CLICK
)
3028 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_4CLICK
)
3029 VIsual_mode
= Ctrl_V
;
3030 #ifdef FEAT_CLIPBOARD
3031 /* Make sure the clipboard gets updated. Needed because start and
3032 * end may still be the same, and the selection needs to be owned */
3033 clip_star
.vmode
= NUL
;
3037 * A double click selects a word or a block.
3039 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
3046 /* If the character under the cursor (skipping white space) is
3047 * not a word character, try finding a match and select a (),
3048 * {}, [], #if/#endif, etc. block. */
3049 end_visual
= curwin
->w_cursor
;
3050 while (gc
= gchar_pos(&end_visual
), vim_iswhite(gc
))
3053 oap
->motion_type
= MCHAR
;
3055 && VIsual_mode
== 'v'
3056 && !vim_iswordc(gchar_pos(&end_visual
))
3057 && equalpos(curwin
->w_cursor
, VIsual
)
3058 && (pos
= findmatch(oap
, NUL
)) != NULL
)
3060 curwin
->w_cursor
= *pos
;
3061 if (oap
->motion_type
== MLINE
)
3063 else if (*p_sel
== 'e')
3065 if (lt(curwin
->w_cursor
, VIsual
))
3068 ++curwin
->w_cursor
.col
;
3073 if (pos
== NULL
&& (is_click
|| is_drag
))
3075 /* When not found a match or when dragging: extend to include
3077 if (lt(curwin
->w_cursor
, orig_cursor
))
3079 find_start_of_word(&curwin
->w_cursor
);
3080 find_end_of_word(&VIsual
);
3084 find_start_of_word(&VIsual
);
3085 if (*p_sel
== 'e' && *ml_get_cursor() != NUL
)
3087 curwin
->w_cursor
.col
+=
3088 (*mb_ptr2len
)(ml_get_cursor());
3090 ++curwin
->w_cursor
.col
;
3092 find_end_of_word(&curwin
->w_cursor
);
3095 curwin
->w_set_curswant
= TRUE
;
3098 redraw_curbuf_later(INVERTED
); /* update the inversion */
3100 else if (VIsual_active
&& !old_active
)
3102 if (mod_mask
& MOD_MASK_ALT
)
3103 VIsual_mode
= Ctrl_V
;
3108 /* If Visual mode changed show it later. */
3109 if ((!VIsual_active
&& old_active
&& mode_displayed
)
3110 || (VIsual_active
&& p_smd
&& msg_silent
== 0
3111 && (!old_active
|| VIsual_mode
!= old_mode
)))
3112 redraw_cmdline
= TRUE
;
3120 * Move "pos" back to the start of the word it's in.
3123 find_start_of_word(pos
)
3130 line
= ml_get(pos
->lnum
);
3131 cclass
= get_mouse_class(line
+ pos
->col
);
3133 while (pos
->col
> 0)
3137 col
-= (*mb_head_off
)(line
, line
+ col
);
3139 if (get_mouse_class(line
+ col
) != cclass
)
3146 * Move "pos" forward to the end of the word it's in.
3147 * When 'selection' is "exclusive", the position is just after the word.
3150 find_end_of_word(pos
)
3157 line
= ml_get(pos
->lnum
);
3158 if (*p_sel
== 'e' && pos
->col
> 0)
3162 pos
->col
-= (*mb_head_off
)(line
, line
+ pos
->col
);
3165 cclass
= get_mouse_class(line
+ pos
->col
);
3166 while (line
[pos
->col
] != NUL
)
3169 col
= pos
->col
+ (*mb_ptr2len
)(line
+ pos
->col
);
3173 if (get_mouse_class(line
+ col
) != cclass
)
3184 * Get class of a character for selection: same class means same word.
3186 * 1: punctuation groups
3187 * 2: normal word character
3188 * >2: multi-byte word character.
3197 if (has_mbyte
&& MB_BYTE2LEN(p
[0]) > 1)
3198 return mb_get_class(p
);
3202 if (c
== ' ' || c
== '\t')
3209 * There are a few special cases where we want certain combinations of
3210 * characters to be considered as a single word. These are things like
3211 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3212 * character is in it's own class.
3214 if (c
!= NUL
&& vim_strchr((char_u
*)"-+*/%<>&|^!=", c
) != NULL
)
3218 #endif /* FEAT_VISUAL */
3219 #endif /* FEAT_MOUSE */
3221 #if defined(FEAT_VISUAL) || defined(PROTO)
3223 * Check if highlighting for visual mode is possible, give a warning message
3227 check_visual_highlight()
3229 static int did_check
= FALSE
;
3233 if (!did_check
&& hl_attr(HLF_V
) == 0)
3234 MSG(_("Warning: terminal cannot highlight"));
3241 * This function should ALWAYS be called to end Visual mode, except from
3242 * do_pending_operator().
3247 #ifdef FEAT_CLIPBOARD
3249 * If we are using the clipboard, then remember what was selected in case
3250 * we need to paste it somewhere while we still own the selection.
3251 * Only do this when the clipboard is already owned. Don't want to grab
3252 * the selection when hitting ESC.
3254 if (clip_star
.available
&& clip_star
.owned
)
3258 VIsual_active
= FALSE
;
3264 /* Save the current VIsual area for '< and '> marks, and "gv" */
3265 curbuf
->b_visual
.vi_mode
= VIsual_mode
;
3266 curbuf
->b_visual
.vi_start
= VIsual
;
3267 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
3268 curbuf
->b_visual
.vi_curswant
= curwin
->w_curswant
;
3270 curbuf
->b_visual_mode_eval
= VIsual_mode
;
3272 #ifdef FEAT_VIRTUALEDIT
3273 if (!virtual_active())
3274 curwin
->w_cursor
.coladd
= 0;
3278 clear_cmdline
= TRUE
; /* unshow visual mode later */
3279 #ifdef FEAT_CMDL_INFO
3284 adjust_cursor_eol();
3288 * Reset VIsual_active and VIsual_reselect.
3291 reset_VIsual_and_resel()
3296 redraw_curbuf_later(INVERTED
); /* delete the inversion later */
3298 VIsual_reselect
= FALSE
;
3302 * Reset VIsual_active and VIsual_reselect if it's set.
3310 redraw_curbuf_later(INVERTED
); /* delete the inversion later */
3311 VIsual_reselect
= FALSE
;
3314 #endif /* FEAT_VISUAL */
3316 #if defined(FEAT_BEVAL)
3317 static int find_is_eval_item
__ARGS((char_u
*ptr
, int *colp
, int *nbp
, int dir
));
3320 * Check for a balloon-eval special item to include when searching for an
3321 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3322 * Returns TRUE if the character at "*ptr" should be included.
3323 * "dir" is FORWARD or BACKWARD, the direction of searching.
3324 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3325 * "bnp" points to a counter for square brackets.
3328 find_is_eval_item(ptr
, colp
, bnp
, dir
)
3334 /* Accept everything inside []. */
3335 if ((*ptr
== ']' && dir
== BACKWARD
) || (*ptr
== '[' && dir
== FORWARD
))
3339 if ((*ptr
== '[' && dir
== BACKWARD
) || (*ptr
== ']' && dir
== FORWARD
))
3344 /* skip over "s.var" */
3348 /* two-character item: s->var */
3349 if (ptr
[dir
== BACKWARD
? 0 : 1] == '>'
3350 && ptr
[dir
== BACKWARD
? -1 : 0] == '-')
3360 * Find the identifier under or to the right of the cursor.
3361 * "find_type" can have one of three values:
3362 * FIND_IDENT: find an identifier (keyword)
3363 * FIND_STRING: find any non-white string
3364 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3365 * FIND_EVAL: find text useful for C program debugging
3367 * There are three steps:
3368 * 1. Search forward for the start of an identifier/string. Doesn't move if
3370 * 2. Search backward for the start of this identifier/string.
3371 * This doesn't match the real Vi but I like it a little better and it
3372 * shouldn't bother anyone.
3373 * 3. Search forward to the end of this identifier/string.
3374 * When FIND_IDENT isn't defined, we backup until a blank.
3376 * Returns the length of the string, or zero if no string is found.
3377 * If a string is found, a pointer to the string is put in "*string". This
3378 * string is not always NUL terminated.
3381 find_ident_under_cursor(string
, find_type
)
3385 return find_ident_at_pos(curwin
, curwin
->w_cursor
.lnum
,
3386 curwin
->w_cursor
.col
, string
, find_type
);
3390 * Like find_ident_under_cursor(), but for any window and any position.
3391 * However: Uses 'iskeyword' from the current window!.
3394 find_ident_at_pos(wp
, lnum
, startcol
, string
, find_type
)
3402 int col
= 0; /* init to shut up GCC */
3409 #if defined(FEAT_BEVAL)
3410 int bn
= 0; /* bracket nesting */
3414 * if i == 0: try to find an identifier
3415 * if i == 1: try to find any non-white string
3417 ptr
= ml_get_buf(wp
->w_buffer
, lnum
, FALSE
);
3418 for (i
= (find_type
& FIND_IDENT
) ? 0 : 1; i
< 2; ++i
)
3421 * 1. skip to start of identifier/string
3427 while (ptr
[col
] != NUL
)
3429 # if defined(FEAT_BEVAL)
3430 /* Stop at a ']' to evaluate "a[x]". */
3431 if ((find_type
& FIND_EVAL
) && ptr
[col
] == ']')
3434 this_class
= mb_get_class(ptr
+ col
);
3435 if (this_class
!= 0 && (i
== 1 || this_class
!= 1))
3437 col
+= (*mb_ptr2len
)(ptr
+ col
);
3442 while (ptr
[col
] != NUL
3443 && (i
== 0 ? !vim_iswordc(ptr
[col
]) : vim_iswhite(ptr
[col
]))
3444 # if defined(FEAT_BEVAL)
3445 && (!(find_type
& FIND_EVAL
) || ptr
[col
] != ']')
3450 #if defined(FEAT_BEVAL)
3451 /* When starting on a ']' count it, so that we include the '['. */
3452 bn
= ptr
[col
] == ']';
3456 * 2. Back up to start of identifier/string.
3461 /* Remember class of character under cursor. */
3462 # if defined(FEAT_BEVAL)
3463 if ((find_type
& FIND_EVAL
) && ptr
[col
] == ']')
3464 this_class
= mb_get_class((char_u
*)"a");
3467 this_class
= mb_get_class(ptr
+ col
);
3468 while (col
> 0 && this_class
!= 0)
3470 prevcol
= col
- 1 - (*mb_head_off
)(ptr
, ptr
+ col
- 1);
3471 prev_class
= mb_get_class(ptr
+ prevcol
);
3472 if (this_class
!= prev_class
3475 || (find_type
& FIND_IDENT
))
3476 # if defined(FEAT_BEVAL)
3477 && (!(find_type
& FIND_EVAL
)
3479 || !find_is_eval_item(ptr
+ prevcol
, &prevcol
,
3487 /* If we don't want just any old string, or we've found an
3488 * identifier, stop searching. */
3491 if (!(find_type
& FIND_STRING
) || this_class
== 2)
3499 ? vim_iswordc(ptr
[col
- 1])
3500 : (!vim_iswhite(ptr
[col
- 1])
3501 && (!(find_type
& FIND_IDENT
)
3502 || !vim_iswordc(ptr
[col
- 1]))))
3503 #if defined(FEAT_BEVAL)
3504 || ((find_type
& FIND_EVAL
)
3506 && find_is_eval_item(ptr
+ col
- 1, &col
,
3512 /* If we don't want just any old string, or we've found an
3513 * identifier, stop searching. */
3514 if (!(find_type
& FIND_STRING
) || vim_iswordc(ptr
[col
]))
3519 if (ptr
[col
] == NUL
|| (i
== 0 && (
3521 has_mbyte
? this_class
!= 2 :
3523 !vim_iswordc(ptr
[col
]))))
3526 * didn't find an identifier or string
3528 if (find_type
& FIND_STRING
)
3529 EMSG(_("E348: No string under cursor"));
3538 * 3. Find the end if the identifier/string.
3540 #if defined(FEAT_BEVAL)
3548 /* Search for point of changing multibyte character class. */
3549 this_class
= mb_get_class(ptr
);
3550 while (ptr
[col
] != NUL
3551 && ((i
== 0 ? mb_get_class(ptr
+ col
) == this_class
3552 : mb_get_class(ptr
+ col
) != 0)
3553 # if defined(FEAT_BEVAL)
3554 || ((find_type
& FIND_EVAL
)
3555 && col
<= (int)startcol
3556 && find_is_eval_item(ptr
+ col
, &col
, &bn
, FORWARD
))
3559 col
+= (*mb_ptr2len
)(ptr
+ col
);
3563 while ((i
== 0 ? vim_iswordc(ptr
[col
])
3564 : (ptr
[col
] != NUL
&& !vim_iswhite(ptr
[col
])))
3565 # if defined(FEAT_BEVAL)
3566 || ((find_type
& FIND_EVAL
)
3567 && col
<= (int)startcol
3568 && find_is_eval_item(ptr
+ col
, &col
, &bn
, FORWARD
))
3579 * Prepare for redo of a normal command.
3585 prep_redo(cap
->oap
->regname
, cap
->count0
,
3586 NUL
, cap
->cmdchar
, NUL
, NUL
, cap
->nchar
);
3590 * Prepare for redo of any command.
3591 * Note that only the last argument can be a multi-byte char.
3594 prep_redo(regname
, num
, cmd1
, cmd2
, cmd3
, cmd4
, cmd5
)
3604 if (regname
!= 0) /* yank from specified buffer */
3606 AppendCharToRedobuff('"');
3607 AppendCharToRedobuff(regname
);
3610 AppendNumberToRedobuff(num
);
3613 AppendCharToRedobuff(cmd1
);
3615 AppendCharToRedobuff(cmd2
);
3617 AppendCharToRedobuff(cmd3
);
3619 AppendCharToRedobuff(cmd4
);
3621 AppendCharToRedobuff(cmd5
);
3625 * check for operator active and clear it
3627 * return TRUE if operator was active
3633 if (oap
->op_type
== OP_NOP
)
3640 * Check for operator or Visual active. Clear active operator.
3642 * Return TRUE if operator or Visual was active.
3648 if (oap
->op_type
== OP_NOP
3662 oap
->op_type
= OP_NOP
;
3664 oap
->motion_force
= NUL
;
3665 oap
->use_reg_one
= FALSE
;
3678 * Remove the shift modifier from a special key.
3681 unshift_special(cap
)
3684 switch (cap
->cmdchar
)
3686 case K_S_RIGHT
: cap
->cmdchar
= K_RIGHT
; break;
3687 case K_S_LEFT
: cap
->cmdchar
= K_LEFT
; break;
3688 case K_S_UP
: cap
->cmdchar
= K_UP
; break;
3689 case K_S_DOWN
: cap
->cmdchar
= K_DOWN
; break;
3690 case K_S_HOME
: cap
->cmdchar
= K_HOME
; break;
3691 case K_S_END
: cap
->cmdchar
= K_END
; break;
3693 cap
->cmdchar
= simplify_key(cap
->cmdchar
, &mod_mask
);
3697 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3699 * Routines for displaying a partly typed command
3702 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3703 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3705 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3707 static char_u showcmd_buf
[SHOWCMD_BUFLEN
];
3708 static char_u old_showcmd_buf
[SHOWCMD_BUFLEN
]; /* For push_showcmd() */
3709 static int showcmd_is_clear
= TRUE
;
3710 static int showcmd_visual
= FALSE
;
3712 static void display_showcmd
__ARGS((void));
3721 if (VIsual_active
&& !char_avail())
3723 int i
= lt(VIsual
, curwin
->w_cursor
);
3725 colnr_T leftcol
, rightcol
;
3728 /* Show the size of the Visual area. */
3732 bot
= curwin
->w_cursor
.lnum
;
3736 top
= curwin
->w_cursor
.lnum
;
3739 # ifdef FEAT_FOLDING
3740 /* Include closed folds as a whole. */
3741 hasFolding(top
, &top
, NULL
);
3742 hasFolding(bot
, NULL
, &bot
);
3744 lines
= bot
- top
+ 1;
3746 if (VIsual_mode
== Ctrl_V
)
3748 getvcols(curwin
, &curwin
->w_cursor
, &VIsual
, &leftcol
, &rightcol
);
3749 sprintf((char *)showcmd_buf
, "%ldx%ld", lines
,
3750 (long)(rightcol
- leftcol
+ 1));
3752 else if (VIsual_mode
== 'V' || VIsual
.lnum
!= curwin
->w_cursor
.lnum
)
3753 sprintf((char *)showcmd_buf
, "%ld", lines
);
3755 sprintf((char *)showcmd_buf
, "%ld", (long)(i
3756 ? curwin
->w_cursor
.col
- VIsual
.col
3757 : VIsual
.col
- curwin
->w_cursor
.col
) + (*p_sel
!= 'e'));
3758 showcmd_buf
[SHOWCMD_COLS
] = NUL
; /* truncate */
3759 showcmd_visual
= TRUE
;
3764 showcmd_buf
[0] = NUL
;
3765 showcmd_visual
= FALSE
;
3767 /* Don't actually display something if there is nothing to clear. */
3768 if (showcmd_is_clear
)
3776 * Add 'c' to string of shown command chars.
3777 * Return TRUE if output has been written (and setcursor() has been called).
3787 #if defined(FEAT_MOUSE)
3789 static int ignore
[] =
3792 K_VER_SCROLLBAR
, K_HOR_SCROLLBAR
,
3793 K_LEFTMOUSE_NM
, K_LEFTRELEASE_NM
,
3796 K_LEFTMOUSE
, K_LEFTDRAG
, K_LEFTRELEASE
,
3797 K_MIDDLEMOUSE
, K_MIDDLEDRAG
, K_MIDDLERELEASE
,
3798 K_RIGHTMOUSE
, K_RIGHTDRAG
, K_RIGHTRELEASE
,
3799 K_MOUSEDOWN
, K_MOUSEUP
,
3800 K_X1MOUSE
, K_X1DRAG
, K_X1RELEASE
, K_X2MOUSE
, K_X2DRAG
, K_X2RELEASE
,
3806 if (!p_sc
|| msg_silent
!= 0)
3811 showcmd_buf
[0] = NUL
;
3812 showcmd_visual
= FALSE
;
3815 #if defined(FEAT_MOUSE)
3816 /* Ignore keys that are scrollbar updates and mouse clicks */
3818 for (i
= 0; ignore
[i
] != 0; ++i
)
3824 old_len
= (int)STRLEN(showcmd_buf
);
3825 extra_len
= (int)STRLEN(p
);
3826 overflow
= old_len
+ extra_len
- SHOWCMD_COLS
;
3828 mch_memmove(showcmd_buf
, showcmd_buf
+ overflow
,
3829 old_len
- overflow
+ 1);
3830 STRCAT(showcmd_buf
, p
);
3844 if (!add_to_showcmd(c
))
3849 * Delete 'len' characters from the end of the shown command.
3852 del_from_showcmd(len
)
3860 old_len
= (int)STRLEN(showcmd_buf
);
3863 showcmd_buf
[old_len
- len
] = NUL
;
3870 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3871 * something and there is a partial mapping.
3877 STRCPY(old_showcmd_buf
, showcmd_buf
);
3886 STRCPY(showcmd_buf
, old_showcmd_buf
);
3898 len
= (int)STRLEN(showcmd_buf
);
3900 showcmd_is_clear
= TRUE
;
3903 screen_puts(showcmd_buf
, (int)Rows
- 1, sc_col
, 0);
3904 showcmd_is_clear
= FALSE
;
3908 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3911 screen_puts((char_u
*)" " + len
, (int)Rows
- 1, sc_col
+ len
, 0);
3913 setcursor(); /* put cursor back where it belongs */
3917 #ifdef FEAT_SCROLLBIND
3919 * When "check" is FALSE, prepare for commands that scroll the window.
3920 * When "check" is TRUE, take care of scroll-binding after the window has
3921 * scrolled. Called from normal_cmd() and edit().
3924 do_check_scrollbind(check
)
3927 static win_T
*old_curwin
= NULL
;
3928 static linenr_T old_topline
= 0;
3930 static int old_topfill
= 0;
3932 static buf_T
*old_buf
= NULL
;
3933 static colnr_T old_leftcol
= 0;
3935 if (check
&& curwin
->w_p_scb
)
3937 /* If a ":syncbind" command was just used, don't scroll, only reset
3940 did_syncbind
= FALSE
;
3941 else if (curwin
== old_curwin
)
3944 * Synchronize other windows, as necessary according to
3945 * 'scrollbind'. Don't do this after an ":edit" command, except
3946 * when 'diff' is set.
3948 if ((curwin
->w_buffer
== old_buf
3953 && (curwin
->w_topline
!= old_topline
3955 || curwin
->w_topfill
!= old_topfill
3957 || curwin
->w_leftcol
!= old_leftcol
))
3959 check_scrollbind(curwin
->w_topline
- old_topline
,
3960 (long)(curwin
->w_leftcol
- old_leftcol
));
3963 else if (vim_strchr(p_sbo
, 'j')) /* jump flag set in 'scrollopt' */
3966 * When switching between windows, make sure that the relative
3967 * vertical offset is valid for the new window. The relative
3968 * offset is invalid whenever another 'scrollbind' window has
3969 * scrolled to a point that would force the current window to
3970 * scroll past the beginning or end of its buffer. When the
3971 * resync is performed, some of the other 'scrollbind' windows may
3972 * need to jump so that the current window's relative position is
3973 * visible on-screen.
3975 check_scrollbind(curwin
->w_topline
- curwin
->w_scbind_pos
, 0L);
3977 curwin
->w_scbind_pos
= curwin
->w_topline
;
3980 old_curwin
= curwin
;
3981 old_topline
= curwin
->w_topline
;
3983 old_topfill
= curwin
->w_topfill
;
3985 old_buf
= curwin
->w_buffer
;
3986 old_leftcol
= curwin
->w_leftcol
;
3990 * Synchronize any windows that have "scrollbind" set, based on the
3991 * number of rows by which the current window has changed
3992 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3995 check_scrollbind(topline_diff
, leftcol_diff
)
3996 linenr_T topline_diff
;
4001 win_T
*old_curwin
= curwin
;
4002 buf_T
*old_curbuf
= curbuf
;
4004 int old_VIsual_select
= VIsual_select
;
4005 int old_VIsual_active
= VIsual_active
;
4007 colnr_T tgt_leftcol
= curwin
->w_leftcol
;
4012 * check 'scrollopt' string for vertical and horizontal scroll options
4014 want_ver
= (vim_strchr(p_sbo
, 'v') && topline_diff
!= 0);
4016 want_ver
|= old_curwin
->w_p_diff
;
4018 want_hor
= (vim_strchr(p_sbo
, 'h') && (leftcol_diff
|| topline_diff
!= 0));
4021 * loop through the scrollbound windows and scroll accordingly
4024 VIsual_select
= VIsual_active
= 0;
4026 for (curwin
= firstwin
; curwin
; curwin
= curwin
->w_next
)
4028 curbuf
= curwin
->w_buffer
;
4029 /* skip original window and windows with 'noscrollbind' */
4030 if (curwin
!= old_curwin
&& curwin
->w_p_scb
)
4033 * do the vertical scroll
4038 if (old_curwin
->w_p_diff
&& curwin
->w_p_diff
)
4040 diff_set_topline(old_curwin
, curwin
);
4045 curwin
->w_scbind_pos
+= topline_diff
;
4046 topline
= curwin
->w_scbind_pos
;
4047 if (topline
> curbuf
->b_ml
.ml_line_count
)
4048 topline
= curbuf
->b_ml
.ml_line_count
;
4052 y
= topline
- curwin
->w_topline
;
4056 scrolldown(-y
, FALSE
);
4059 redraw_later(VALID
);
4062 curwin
->w_redr_status
= TRUE
;
4067 * do the horizontal scroll
4069 if (want_hor
&& curwin
->w_leftcol
!= tgt_leftcol
)
4071 curwin
->w_leftcol
= tgt_leftcol
;
4078 * reset current-window
4081 VIsual_select
= old_VIsual_select
;
4082 VIsual_active
= old_VIsual_active
;
4084 curwin
= old_curwin
;
4085 curbuf
= old_curbuf
;
4087 #endif /* #ifdef FEAT_SCROLLBIND */
4090 * Command character that's ignored.
4091 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4098 cap
->retval
|= CA_COMMAND_BUSY
; /* don't call edit() now */
4102 * Command character that doesn't do anything, but unlike nv_ignore() does
4103 * start edit(). Used for "startinsert" executed while starting up.
4113 * Command character doesn't exist.
4119 clearopbeep(cap
->oap
);
4123 * <Help> and <F1> commands.
4129 if (!checkclearopq(cap
->oap
))
4134 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4140 if (!checkclearopq(cap
->oap
)
4141 && do_addsub((int)cap
->cmdchar
, cap
->count1
) == OK
)
4146 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4152 if (!checkclearop(cap
->oap
))
4155 if (mod_mask
& MOD_MASK_CTRL
)
4157 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4158 if (cap
->arg
== BACKWARD
)
4159 goto_tabpage(-(int)cap
->count1
);
4161 goto_tabpage((int)cap
->count0
);
4165 (void)onepage(cap
->arg
, cap
->count1
);
4170 * Implementation of "gd" and "gD" command.
4173 nv_gd(oap
, nchar
, thisblock
)
4176 int thisblock
; /* 1 for "1gd" and "1gD" */
4181 if ((len
= find_ident_under_cursor(&ptr
, FIND_IDENT
)) == 0
4182 || find_decl(ptr
, len
, nchar
== 'd', thisblock
, 0) == FAIL
)
4185 else if ((fdo_flags
& FDO_SEARCH
) && KeyTyped
&& oap
->op_type
== OP_NOP
)
4191 * Search for variable declaration of "ptr[len]".
4192 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4193 * current file ("gD").
4194 * When "thisblock" is TRUE check the {} block scope.
4195 * Return FAIL when not found.
4198 find_decl(ptr
, len
, locally
, thisblock
, searchflags
)
4203 int searchflags
; /* flags passed to searchit() */
4215 if ((pat
= alloc(len
+ 7)) == NULL
)
4218 /* Put "\V" before the pattern to avoid that the special meaning of "."
4219 * and "~" causes trouble. */
4220 sprintf((char *)pat
, vim_iswordp(ptr
) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4222 old_pos
= curwin
->w_cursor
;
4225 p_ws
= FALSE
; /* don't wrap around end of file now */
4226 p_scs
= FALSE
; /* don't switch ignorecase off now */
4229 * With "gD" go to line 1.
4230 * With "gd" Search back for the start of the current function, then go
4231 * back until a blank line. If this fails go to line 1.
4233 if (!locally
|| !findpar(&incll
, BACKWARD
, 1L, '{', FALSE
))
4235 setpcmark(); /* Set in findpar() otherwise */
4236 curwin
->w_cursor
.lnum
= 1;
4237 par_pos
= curwin
->w_cursor
;
4241 par_pos
= curwin
->w_cursor
;
4242 while (curwin
->w_cursor
.lnum
> 1 && *skipwhite(ml_get_curline()) != NUL
)
4243 --curwin
->w_cursor
.lnum
;
4245 curwin
->w_cursor
.col
= 0;
4247 /* Search forward for the identifier, ignore comment lines. */
4248 clearpos(&found_pos
);
4251 t
= searchit(curwin
, curbuf
, &curwin
->w_cursor
, FORWARD
,
4252 pat
, 1L, searchflags
, RE_LAST
, (linenr_T
)0, NULL
);
4253 if (curwin
->w_cursor
.lnum
>= old_pos
.lnum
)
4254 t
= FAIL
; /* match after start is failure too */
4256 if (thisblock
&& t
!= FAIL
)
4260 /* Check that the block the match is in doesn't end before the
4261 * position where we started the search from. */
4262 if ((pos
= findmatchlimit(NULL
, '}', FM_FORWARD
,
4263 (int)(old_pos
.lnum
- curwin
->w_cursor
.lnum
+ 1))) != NULL
4264 && pos
->lnum
< old_pos
.lnum
)
4270 /* If we previously found a valid position, use it. */
4271 if (found_pos
.lnum
!= 0)
4273 curwin
->w_cursor
= found_pos
;
4278 #ifdef FEAT_COMMENTS
4279 if (get_leader_len(ml_get_curline(), NULL
, FALSE
) > 0)
4281 /* Ignore this line, continue at start of next line. */
4282 ++curwin
->w_cursor
.lnum
;
4283 curwin
->w_cursor
.col
= 0;
4287 if (!locally
) /* global search: use first match found */
4289 if (curwin
->w_cursor
.lnum
>= par_pos
.lnum
)
4291 /* If we previously found a valid position, use it. */
4292 if (found_pos
.lnum
!= 0)
4293 curwin
->w_cursor
= found_pos
;
4297 /* For finding a local variable and the match is before the "{" search
4298 * to find a later match. For K&R style function declarations this
4299 * skips the function header without types. */
4300 found_pos
= curwin
->w_cursor
;
4306 curwin
->w_cursor
= old_pos
;
4310 curwin
->w_set_curswant
= TRUE
;
4311 /* "n" searches forward now */
4323 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4324 * lines rather than lines in the file.
4325 * 'dist' must be positive.
4327 * Return OK if able to move cursor, FAIL otherwise.
4330 nv_screengo(oap
, dir
, dist
)
4335 int linelen
= linetabsize(ml_get_curline());
4339 int col_off1
; /* margin offset for first screen line */
4340 int col_off2
; /* margin offset for wrapped screen line */
4341 int width1
; /* text width for first screen line */
4342 int width2
; /* test width for wrapped screen line */
4344 oap
->motion_type
= MCHAR
;
4345 oap
->inclusive
= FALSE
;
4347 col_off1
= curwin_col_off();
4348 col_off2
= col_off1
- curwin_col_off2();
4349 width1
= W_WIDTH(curwin
) - col_off1
;
4350 width2
= W_WIDTH(curwin
) - col_off2
;
4352 #ifdef FEAT_VERTSPLIT
4353 if (curwin
->w_width
!= 0)
4357 * Instead of sticking at the last character of the buffer line we
4358 * try to stick in the last column of the screen.
4360 if (curwin
->w_curswant
== MAXCOL
)
4365 curwin
->w_curswant
= 0;
4368 curwin
->w_curswant
= width1
- 1;
4369 if (curwin
->w_virtcol
> curwin
->w_curswant
)
4370 curwin
->w_curswant
+= ((curwin
->w_virtcol
4371 - curwin
->w_curswant
- 1) / width2
+ 1) * width2
;
4376 if (linelen
> width1
)
4377 n
= ((linelen
- width1
- 1) / width2
+ 1) * width2
+ width1
;
4380 if (curwin
->w_curswant
> (colnr_T
)n
+ 1)
4381 curwin
->w_curswant
-= ((curwin
->w_curswant
- n
) / width2
+ 1)
4387 if (dir
== BACKWARD
)
4389 if ((long)curwin
->w_curswant
>= width2
)
4390 /* move back within line */
4391 curwin
->w_curswant
-= width2
;
4394 /* to previous line */
4395 if (curwin
->w_cursor
.lnum
== 1)
4400 --curwin
->w_cursor
.lnum
;
4402 /* Move to the start of a closed fold. Don't do that when
4403 * 'foldopen' contains "all": it will open in a moment. */
4404 if (!(fdo_flags
& FDO_ALL
))
4405 (void)hasFolding(curwin
->w_cursor
.lnum
,
4406 &curwin
->w_cursor
.lnum
, NULL
);
4408 linelen
= linetabsize(ml_get_curline());
4409 if (linelen
> width1
)
4410 curwin
->w_curswant
+= (((linelen
- width1
- 1) / width2
)
4414 else /* dir == FORWARD */
4416 if (linelen
> width1
)
4417 n
= ((linelen
- width1
- 1) / width2
+ 1) * width2
+ width1
;
4420 if (curwin
->w_curswant
+ width2
< (colnr_T
)n
)
4421 /* move forward within line */
4422 curwin
->w_curswant
+= width2
;
4427 /* Move to the end of a closed fold. */
4428 (void)hasFolding(curwin
->w_cursor
.lnum
, NULL
,
4429 &curwin
->w_cursor
.lnum
);
4431 if (curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
)
4436 curwin
->w_cursor
.lnum
++;
4437 curwin
->w_curswant
%= width2
;
4441 #ifdef FEAT_VERTSPLIT
4445 coladvance(curwin
->w_curswant
);
4447 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4448 if (curwin
->w_cursor
.col
> 0 && curwin
->w_p_wrap
)
4451 * Check for landing on a character that got split at the end of the
4452 * last line. We want to advance a screenline, not end up in the same
4453 * screenline or move two screenlines.
4456 if (curwin
->w_virtcol
> curwin
->w_curswant
4457 && (curwin
->w_curswant
< (colnr_T
)width1
4458 ? (curwin
->w_curswant
> (colnr_T
)width1
/ 2)
4459 : ((curwin
->w_curswant
- width1
) % width2
4460 > (colnr_T
)width2
/ 2)))
4461 --curwin
->w_cursor
.col
;
4466 curwin
->w_curswant
= MAXCOL
; /* stick in the last column */
4473 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4474 * when Shift or Ctrl is used.
4475 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4481 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4482 win_T
*old_curwin
= curwin
;
4484 /* Currently we only get the mouse coordinates in the GUI. */
4485 if (gui
.in_use
&& mouse_row
>= 0 && mouse_col
>= 0)
4492 /* find the window at the pointer coordinates */
4493 curwin
= mouse_find_win(&row
, &col
);
4494 curbuf
= curwin
->w_buffer
;
4498 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
4500 (void)onepage(cap
->arg
? FORWARD
: BACKWARD
, 1L);
4506 nv_scroll_line(cap
);
4509 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4510 curwin
->w_redr_status
= TRUE
;
4512 curwin
= old_curwin
;
4513 curbuf
= curwin
->w_buffer
;
4518 * Mouse clicks and drags.
4524 (void)do_mouse(cap
->oap
, cap
->cmdchar
, BACKWARD
, cap
->count1
, 0);
4529 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4530 * cap->arg must be TRUE for CTRL-E.
4536 if (!checkclearop(cap
->oap
))
4537 scroll_redraw(cap
->arg
, cap
->count1
);
4541 * Scroll "count" lines up or down, and redraw.
4544 scroll_redraw(up
, count
)
4548 linenr_T prev_topline
= curwin
->w_topline
;
4550 int prev_topfill
= curwin
->w_topfill
;
4552 linenr_T prev_lnum
= curwin
->w_cursor
.lnum
;
4555 scrollup(count
, TRUE
);
4557 scrolldown(count
, TRUE
);
4560 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4561 * valid, otherwise the screen jumps back at the end of the file. */
4563 check_cursor_moved(curwin
);
4564 curwin
->w_valid
|= VALID_TOPLINE
;
4566 /* If moved back to where we were, at least move the cursor, otherwise
4567 * we get stuck at one position. Don't move the cursor up if the
4568 * first line of the buffer is already on the screen */
4569 while (curwin
->w_topline
== prev_topline
4571 && curwin
->w_topfill
== prev_topfill
4577 if (curwin
->w_cursor
.lnum
> prev_lnum
4578 || cursor_down(1L, FALSE
) == FAIL
)
4583 if (curwin
->w_cursor
.lnum
< prev_lnum
4584 || prev_topline
== 1L
4585 || cursor_up(1L, FALSE
) == FAIL
)
4588 /* Mark w_topline as valid, otherwise the screen jumps back at the
4589 * end of the file. */
4590 check_cursor_moved(curwin
);
4591 curwin
->w_valid
|= VALID_TOPLINE
;
4594 if (curwin
->w_cursor
.lnum
!= prev_lnum
)
4595 coladvance(curwin
->w_curswant
);
4596 redraw_later(VALID
);
4600 * Commands that start with "z".
4608 int nchar
= cap
->nchar
;
4610 long old_fdl
= curwin
->w_p_fdl
;
4611 int old_fen
= curwin
->w_p_fen
;
4617 if (VIM_ISDIGIT(nchar
))
4620 * "z123{nchar}": edit the count before obtaining {nchar}
4622 if (checkclearop(cap
->oap
))
4627 #ifdef USE_ON_FLY_SCROLL
4628 dont_scroll
= TRUE
; /* disallow scrolling here */
4631 ++allow_keys
; /* no mapping for nchar, but allow key codes */
4632 nchar
= plain_vgetc();
4634 LANGMAP_ADJUST(nchar
, TRUE
);
4638 #ifdef FEAT_CMDL_INFO
4639 (void)add_to_showcmd(nchar
);
4641 if (nchar
== K_DEL
|| nchar
== K_KDEL
)
4643 else if (VIM_ISDIGIT(nchar
))
4644 n
= n
* 10 + (nchar
- '0');
4645 else if (nchar
== CAR
)
4648 need_mouse_correct
= TRUE
;
4650 win_setheight((int)n
);
4653 else if (nchar
== 'l'
4656 || nchar
== K_RIGHT
)
4658 cap
->count1
= n
? n
* cap
->count1
: cap
->count1
;
4663 clearopbeep(cap
->oap
);
4667 cap
->oap
->op_type
= OP_NOP
;
4674 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4675 * and "zC" only in Visual mode. "zj" and "zk" are motion
4677 cap
->nchar
!= 'f' && cap
->nchar
!= 'F'
4678 && !(VIsual_active
&& vim_strchr((char_u
*)"dcCoO", cap
->nchar
))
4679 && cap
->nchar
!= 'j' && cap
->nchar
!= 'k'
4682 checkclearop(cap
->oap
))
4686 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4687 * If line number given, set cursor.
4689 if ((vim_strchr((char_u
*)"+\r\nt.z^-b", nchar
) != NULL
)
4691 && cap
->count0
!= curwin
->w_cursor
.lnum
)
4694 if (cap
->count0
> curbuf
->b_ml
.ml_line_count
)
4695 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
4697 curwin
->w_cursor
.lnum
= cap
->count0
;
4703 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4705 if (cap
->count0
== 0)
4707 /* No count given: put cursor at the line below screen */
4708 validate_botline(); /* make sure w_botline is valid */
4709 if (curwin
->w_botline
> curbuf
->b_ml
.ml_line_count
)
4710 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
4712 curwin
->w_cursor
.lnum
= curwin
->w_botline
;
4718 beginline(BL_WHITE
| BL_FIX
);
4721 case 't': scroll_cursor_top(0, TRUE
);
4722 redraw_later(VALID
);
4725 /* "z." and "zz": put cursor in middle of screen */
4726 case '.': beginline(BL_WHITE
| BL_FIX
);
4729 case 'z': scroll_cursor_halfway(TRUE
);
4730 redraw_later(VALID
);
4733 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4734 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4735 * when <count> is at bottom of window, and puts that one at
4736 * bottom of window. */
4737 if (cap
->count0
!= 0)
4739 scroll_cursor_bot(0, TRUE
);
4740 curwin
->w_cursor
.lnum
= curwin
->w_topline
;
4742 else if (curwin
->w_topline
== 1)
4743 curwin
->w_cursor
.lnum
= 1;
4745 curwin
->w_cursor
.lnum
= curwin
->w_topline
- 1;
4748 beginline(BL_WHITE
| BL_FIX
);
4751 case 'b': scroll_cursor_bot(0, TRUE
);
4752 redraw_later(VALID
);
4755 /* "zH" - scroll screen right half-page */
4757 cap
->count1
*= W_WIDTH(curwin
) / 2;
4760 /* "zh" - scroll screen to the right */
4763 if (!curwin
->w_p_wrap
)
4765 if ((colnr_T
)cap
->count1
> curwin
->w_leftcol
)
4766 curwin
->w_leftcol
= 0;
4768 curwin
->w_leftcol
-= (colnr_T
)cap
->count1
;
4773 /* "zL" - scroll screen left half-page */
4774 case 'L': cap
->count1
*= W_WIDTH(curwin
) / 2;
4777 /* "zl" - scroll screen to the left */
4780 if (!curwin
->w_p_wrap
)
4782 /* scroll the window left */
4783 curwin
->w_leftcol
+= (colnr_T
)cap
->count1
;
4788 /* "zs" - scroll screen, cursor at the start */
4789 case 's': if (!curwin
->w_p_wrap
)
4792 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4793 col
= 0; /* like the cursor is in col 0 */
4796 getvcol(curwin
, &curwin
->w_cursor
, &col
, NULL
, NULL
);
4797 if ((long)col
> p_siso
)
4801 if (curwin
->w_leftcol
!= col
)
4803 curwin
->w_leftcol
= col
;
4804 redraw_later(NOT_VALID
);
4809 /* "ze" - scroll screen, cursor at the end */
4810 case 'e': if (!curwin
->w_p_wrap
)
4813 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4814 col
= 0; /* like the cursor is in col 0 */
4817 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &col
);
4818 n
= W_WIDTH(curwin
) - curwin_col_off();
4819 if ((long)col
+ p_siso
< n
)
4822 col
= col
+ p_siso
- n
+ 1;
4823 if (curwin
->w_leftcol
!= col
)
4825 curwin
->w_leftcol
= col
;
4826 redraw_later(NOT_VALID
);
4832 /* "zF": create fold command */
4833 /* "zf": create fold operator */
4835 case 'f': if (foldManualAllowed(TRUE
))
4839 curwin
->w_p_fen
= TRUE
;
4841 /* "zF" is like "zfzf" */
4842 if (nchar
== 'F' && cap
->oap
->op_type
== OP_FOLD
)
4849 clearopbeep(cap
->oap
);
4852 /* "zd": delete fold at cursor */
4853 /* "zD": delete fold at cursor recursively */
4855 case 'D': if (foldManualAllowed(FALSE
))
4860 deleteFold(curwin
->w_cursor
.lnum
,
4861 curwin
->w_cursor
.lnum
, nchar
== 'D', FALSE
);
4865 /* "zE": erease all folds */
4866 case 'E': if (foldmethodIsManual(curwin
))
4868 clearFolding(curwin
);
4869 changed_window_setting();
4871 else if (foldmethodIsMarker(curwin
))
4872 deleteFold((linenr_T
)1, curbuf
->b_ml
.ml_line_count
,
4875 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4878 /* "zn": fold none: reset 'foldenable' */
4879 case 'n': curwin
->w_p_fen
= FALSE
;
4882 /* "zN": fold Normal: set 'foldenable' */
4883 case 'N': curwin
->w_p_fen
= TRUE
;
4886 /* "zi": invert folding: toggle 'foldenable' */
4887 case 'i': curwin
->w_p_fen
= !curwin
->w_p_fen
;
4890 /* "za": open closed fold or close open fold at cursor */
4891 case 'a': if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4892 openFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4895 closeFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4896 curwin
->w_p_fen
= TRUE
;
4900 /* "zA": open fold at cursor recursively */
4901 case 'A': if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4902 openFoldRecurse(curwin
->w_cursor
.lnum
);
4905 closeFoldRecurse(curwin
->w_cursor
.lnum
);
4906 curwin
->w_p_fen
= TRUE
;
4910 /* "zo": open fold at cursor or Visual area */
4911 case 'o': if (VIsual_active
)
4914 openFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4917 /* "zO": open fold recursively */
4918 case 'O': if (VIsual_active
)
4921 openFoldRecurse(curwin
->w_cursor
.lnum
);
4924 /* "zc": close fold at cursor or Visual area */
4925 case 'c': if (VIsual_active
)
4928 closeFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4929 curwin
->w_p_fen
= TRUE
;
4932 /* "zC": close fold recursively */
4933 case 'C': if (VIsual_active
)
4936 closeFoldRecurse(curwin
->w_cursor
.lnum
);
4937 curwin
->w_p_fen
= TRUE
;
4940 /* "zv": open folds at the cursor */
4941 case 'v': foldOpenCursor();
4944 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4945 case 'x': curwin
->w_p_fen
= TRUE
;
4946 newFoldLevel(); /* update right now */
4950 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4951 case 'X': curwin
->w_p_fen
= TRUE
;
4952 old_fdl
= -1; /* force an update */
4955 /* "zm": fold more */
4956 case 'm': if (curwin
->w_p_fdl
> 0)
4958 old_fdl
= -1; /* force an update */
4959 curwin
->w_p_fen
= TRUE
;
4962 /* "zM": close all folds */
4963 case 'M': curwin
->w_p_fdl
= 0;
4964 old_fdl
= -1; /* force an update */
4965 curwin
->w_p_fen
= TRUE
;
4968 /* "zr": reduce folding */
4969 case 'r': ++curwin
->w_p_fdl
;
4972 /* "zR": open all folds */
4973 case 'R': curwin
->w_p_fdl
= getDeepestNesting();
4974 old_fdl
= -1; /* force an update */
4977 case 'j': /* "zj" move to next fold downwards */
4978 case 'k': /* "zk" move to next fold upwards */
4979 if (foldMoveTo(TRUE
, nchar
== 'j' ? FORWARD
: BACKWARD
,
4980 cap
->count1
) == FAIL
)
4981 clearopbeep(cap
->oap
);
4984 #endif /* FEAT_FOLDING */
4987 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4989 ++allow_keys
; /* no mapping for nchar, but allow key codes */
4990 nchar
= plain_vgetc();
4992 LANGMAP_ADJUST(nchar
, TRUE
);
4996 #ifdef FEAT_CMDL_INFO
4997 (void)add_to_showcmd(nchar
);
4999 if (vim_strchr((char_u
*)"gGwW", nchar
) == NULL
)
5001 clearopbeep(cap
->oap
);
5007 case 'g': /* "zg": add good word to word list */
5008 case 'w': /* "zw": add wrong word to word list */
5009 case 'G': /* "zG": add good word to temp word list */
5010 case 'W': /* "zW": add wrong word to temp word list */
5015 if (checkclearop(cap
->oap
))
5018 if (VIsual_active
&& get_visual_text(cap
, &ptr
, &len
)
5024 pos_T pos
= curwin
->w_cursor
;
5026 /* Find bad word under the cursor. */
5027 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, NULL
);
5028 if (len
!= 0 && curwin
->w_cursor
.col
<= pos
.col
)
5029 ptr
= ml_get_pos(&curwin
->w_cursor
);
5030 curwin
->w_cursor
= pos
;
5033 if (ptr
== NULL
&& (len
= find_ident_under_cursor(&ptr
,
5036 spell_add_word(ptr
, len
, nchar
== 'w' || nchar
== 'W',
5037 (nchar
== 'G' || nchar
== 'W')
5038 ? 0 : (int)cap
->count1
,
5043 case '=': /* "z=": suggestions for a badly spelled word */
5044 if (!checkclearop(cap
->oap
))
5045 spell_suggest((int)cap
->count0
);
5049 default: clearopbeep(cap
->oap
);
5053 /* Redraw when 'foldenable' changed */
5054 if (old_fen
!= curwin
->w_p_fen
)
5059 if (foldmethodIsDiff(curwin
) && curwin
->w_p_scb
)
5061 /* Adjust 'foldenable' in diff-synced windows. */
5064 if (wp
!= curwin
&& foldmethodIsDiff(wp
) && wp
->w_p_scb
)
5066 wp
->w_p_fen
= curwin
->w_p_fen
;
5067 changed_window_setting_win(wp
);
5072 changed_window_setting();
5075 /* Redraw when 'foldlevel' changed. */
5076 if (old_fdl
!= curwin
->w_p_fdl
)
5083 * Vertical scrollbar movement.
5086 nv_ver_scrollbar(cap
)
5089 if (cap
->oap
->op_type
!= OP_NOP
)
5090 clearopbeep(cap
->oap
);
5092 /* Even if an operator was pending, we still want to scroll */
5097 * Horizontal scrollbar movement.
5100 nv_hor_scrollbar(cap
)
5103 if (cap
->oap
->op_type
!= OP_NOP
)
5104 clearopbeep(cap
->oap
);
5106 /* Even if an operator was pending, we still want to scroll */
5107 gui_do_horiz_scroll();
5111 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5119 if (cap
->oap
->op_type
!= OP_NOP
)
5120 clearopbeep(cap
->oap
);
5122 /* Even if an operator was pending, we still want to jump tabs. */
5123 goto_tabpage(current_tab
);
5127 * Selected item in tab line menu.
5133 if (cap
->oap
->op_type
!= OP_NOP
)
5134 clearopbeep(cap
->oap
);
5136 /* Even if an operator was pending, we still want to jump tabs. */
5141 * Handle selecting an item of the GUI tab line menu.
5142 * Used in Normal and Insert mode.
5147 switch (current_tabmenu
)
5149 case TABLINE_MENU_CLOSE
:
5150 if (current_tab
== 0)
5151 do_cmdline_cmd((char_u
*)"tabclose");
5154 vim_snprintf((char *)IObuff
, IOSIZE
, "tabclose %d",
5156 do_cmdline_cmd(IObuff
);
5160 case TABLINE_MENU_NEW
:
5161 vim_snprintf((char *)IObuff
, IOSIZE
, "%dtabnew",
5162 current_tab
> 0 ? current_tab
- 1 : 999);
5163 do_cmdline_cmd(IObuff
);
5166 case TABLINE_MENU_OPEN
:
5167 vim_snprintf((char *)IObuff
, IOSIZE
, "browse %dtabnew",
5168 current_tab
> 0 ? current_tab
- 1 : 999);
5169 do_cmdline_cmd(IObuff
);
5183 * Ignore 'Q' in Visual mode, just give a beep.
5190 if (!checkclearop(cap
->oap
))
5195 * Handle a ":" command.
5209 if (cap
->oap
->op_type
!= OP_NOP
)
5211 /* Using ":" as a movement is characterwise exclusive. */
5212 cap
->oap
->motion_type
= MCHAR
;
5213 cap
->oap
->inclusive
= FALSE
;
5215 else if (cap
->count0
)
5217 /* translate "count:" into ":.,.+(count - 1)" */
5218 stuffcharReadbuff('.');
5219 if (cap
->count0
> 1)
5221 stuffReadbuff((char_u
*)",.+");
5222 stuffnumReadbuff((long)cap
->count0
- 1L);
5226 /* When typing, don't type below an old message */
5232 /* get a command line and execute it */
5233 do_cmdline(NULL
, getexline
, NULL
,
5234 cap
->oap
->op_type
!= OP_NOP
? DOCMD_KEEPLINE
: 0);
5236 /* If 'insertmode' changed, enter or exit Insert mode */
5237 if (p_im
!= old_p_im
)
5245 /* The start of the operator may have become invalid by the Ex
5247 if (cap
->oap
->op_type
!= OP_NOP
5248 && (cap
->oap
->start
.lnum
> curbuf
->b_ml
.ml_line_count
5249 || cap
->oap
->start
.col
>
5250 STRLEN(ml_get(cap
->oap
->start
.lnum
))))
5251 clearopbeep(cap
->oap
);
5256 * Handle CTRL-G command.
5263 if (VIsual_active
) /* toggle Selection/Visual mode */
5265 VIsual_select
= !VIsual_select
;
5270 if (!checkclearop(cap
->oap
))
5271 /* print full name if count given or :cd used */
5272 fileinfo((int)cap
->count0
, FALSE
, TRUE
);
5276 * Handle CTRL-H <Backspace> command.
5283 if (VIsual_active
&& VIsual_select
)
5285 cap
->cmdchar
= 'x'; /* BS key behaves like 'x' in Select mode */
5294 * CTRL-L: clear screen and redraw.
5300 if (!checkclearop(cap
->oap
))
5302 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5304 * Right now, the BeBox doesn't seem to have an easy way to detect
5305 * window resizing, so we cheat and make the user detect it
5306 * manually with CTRL-L instead
5311 /* Clear all syntax states to force resyncing. */
5312 syn_stack_free_all(curbuf
);
5314 redraw_later(CLEAR
);
5319 * CTRL-O: In Select mode: switch to Visual mode for one command.
5320 * Otherwise: Go to older pcmark.
5327 if (VIsual_active
&& VIsual_select
)
5329 VIsual_select
= FALSE
;
5331 restart_VIsual_select
= 2; /* restart Select mode later */
5336 cap
->count1
= -cap
->count1
;
5342 * CTRL-^ command, short for ":e #"
5348 if (!checkclearopq(cap
->oap
))
5349 (void)buflist_getfile((int)cap
->count0
, (linenr_T
)0,
5350 GETF_SETMARK
|GETF_ALT
, FALSE
);
5360 if (!checkclearopq(cap
->oap
))
5364 /* "ZZ": equivalent to ":x". */
5365 case 'Z': do_cmdline_cmd((char_u
*)"x");
5368 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5369 case 'Q': do_cmdline_cmd((char_u
*)"q!");
5372 default: clearopbeep(cap
->oap
);
5377 #if defined(FEAT_WINDOWS) || defined(PROTO)
5379 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5390 vim_memset(&ca
, 0, sizeof(ca
));
5399 * Handle the commands that use the word under the cursor.
5400 * [g] CTRL-] :ta to current identifier
5401 * [g] 'K' run program for current identifier
5402 * [g] '*' / to current identifier or string
5403 * [g] '#' ? to current identifier or string
5404 * g ']' :tselect for current identifier
5413 char_u
*kp
; /* value of 'keywordprg' */
5414 int kp_help
; /* 'keywordprg' is ":help" */
5415 int n
= 0; /* init for GCC */
5417 int g_cmd
; /* "g" command */
5422 if (cap
->cmdchar
== 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5424 cmdchar
= cap
->nchar
;
5429 cmdchar
= cap
->cmdchar
;
5433 if (cmdchar
== POUND
) /* the pound sign, '#' for English keyboards */
5437 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5439 if (cmdchar
== ']' || cmdchar
== Ctrl_RSB
|| cmdchar
== 'K')
5442 if (VIsual_active
&& get_visual_text(cap
, &ptr
, &n
) == FAIL
)
5445 if (checkclearopq(cap
->oap
))
5449 if (ptr
== NULL
&& (n
= find_ident_under_cursor(&ptr
,
5450 (cmdchar
== '*' || cmdchar
== '#')
5451 ? FIND_IDENT
|FIND_STRING
: FIND_IDENT
)) == 0)
5457 /* Allocate buffer to put the command in. Inserting backslashes can
5458 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5459 * and some numbers. */
5460 kp
= (*curbuf
->b_p_kp
== NUL
? p_kp
: curbuf
->b_p_kp
);
5461 kp_help
= (*kp
== NUL
|| STRCMP(kp
, ":he") == 0
5462 || STRCMP(kp
, ":help") == 0);
5463 buf
= alloc((unsigned)(n
* 2 + 30 + STRLEN(kp
)));
5473 * Put cursor at start of word, makes search skip the word
5475 * Call setpcmark() first, so "*``" puts the cursor back where
5479 curwin
->w_cursor
.col
= (colnr_T
) (ptr
- ml_get_curline());
5481 if (!g_cmd
&& vim_iswordp(ptr
))
5483 no_smartcase
= TRUE
; /* don't use 'smartcase' now */
5488 STRCPY(buf
, "he! ");
5491 /* An external command will probably use an argument starting
5492 * with "-" as an option. To avoid trouble we skip the "-". */
5493 while (*ptr
== '-' && n
> 0)
5500 EMSG(_(e_noident
)); /* found dashes only */
5505 /* When a count is given, turn it into a range. Is this
5506 * really what we want? */
5507 isman
= (STRCMP(kp
, "man") == 0);
5508 isman_s
= (STRCMP(kp
, "man -s") == 0);
5509 if (cap
->count0
!= 0 && !(isman
|| isman_s
))
5510 sprintf((char *)buf
, ".,.+%ld", cap
->count0
- 1);
5513 if (cap
->count0
== 0 && isman_s
)
5518 if (cap
->count0
!= 0 && (isman
|| isman_s
))
5520 sprintf((char *)buf
+ STRLEN(buf
), "%ld", cap
->count0
);
5529 STRCPY(buf
, "cstag ");
5537 STRCPY(buf
, "he! ");
5541 sprintf((char *)buf
, "%ldta ", cap
->count0
);
5545 * Now grab the chars in the identifier
5547 if (cmdchar
== 'K' && !kp_help
)
5549 /* Escape the argument properly for a shell command */
5550 ptr
= vim_strnsave(ptr
, n
);
5551 p
= vim_strsave_shellescape(ptr
, TRUE
);
5558 buf
= (char_u
*)vim_realloc(buf
, STRLEN(buf
) + STRLEN(p
) + 1);
5571 aux_ptr
= (char_u
*)(p_magic
? "/.*~[^$\\" : "/^$\\");
5572 else if (cmdchar
== '#')
5573 aux_ptr
= (char_u
*)(p_magic
? "/?.*~[^$\\" : "/?^$\\");
5575 /* Don't escape spaces and Tabs in a tag with a backslash */
5576 aux_ptr
= (char_u
*)"\\|\"\n*?[";
5578 p
= buf
+ STRLEN(buf
);
5581 /* put a backslash before \ and some others */
5582 if (vim_strchr(aux_ptr
, *ptr
) != NULL
)
5585 /* When current byte is a part of multibyte character, copy all
5586 * bytes of that character. */
5590 int len
= (*mb_ptr2len
)(ptr
) - 1;
5592 for (i
= 0; i
< len
&& n
>= 1; ++i
, --n
)
5602 * Execute the command.
5604 if (cmdchar
== '*' || cmdchar
== '#')
5608 has_mbyte
? vim_iswordp(mb_prevptr(ml_get_curline(), ptr
)) :
5610 vim_iswordc(ptr
[-1])))
5613 /* put pattern in search history */
5614 add_to_history(HIST_SEARCH
, buf
, TRUE
, NUL
);
5616 normal_search(cap
, cmdchar
== '*' ? '/' : '?', buf
, 0);
5619 do_cmdline_cmd(buf
);
5624 #if defined(FEAT_VISUAL) || defined(PROTO)
5626 * Get visually selected text, within one line only.
5627 * Returns FAIL if more than one line selected.
5630 get_visual_text(cap
, pp
, lenp
)
5632 char_u
**pp
; /* return: start of selected text */
5633 int *lenp
; /* return: length of selected text */
5635 if (VIsual_mode
!= 'V')
5637 if (VIsual
.lnum
!= curwin
->w_cursor
.lnum
)
5640 clearopbeep(cap
->oap
);
5643 if (VIsual_mode
== 'V')
5645 *pp
= ml_get_curline();
5646 *lenp
= (int)STRLEN(*pp
);
5650 if (lt(curwin
->w_cursor
, VIsual
))
5652 *pp
= ml_get_pos(&curwin
->w_cursor
);
5653 *lenp
= VIsual
.col
- curwin
->w_cursor
.col
+ 1;
5657 *pp
= ml_get_pos(&VIsual
);
5658 *lenp
= curwin
->w_cursor
.col
- VIsual
.col
+ 1;
5662 /* Correct the length to include the whole last character. */
5663 *lenp
+= (*mb_ptr2len
)(*pp
+ (*lenp
- 1)) - 1;
5666 reset_VIsual_and_resel();
5672 * CTRL-T: backwards in tag stack
5678 if (!checkclearopq(cap
->oap
))
5679 do_tag((char_u
*)"", DT_POP
, (int)cap
->count1
, FALSE
, TRUE
);
5683 * Handle scrolling command 'H', 'L' and 'M'.
5696 cap
->oap
->motion_type
= MLINE
;
5699 if (cap
->cmdchar
== 'L')
5701 validate_botline(); /* make sure curwin->w_botline is valid */
5702 curwin
->w_cursor
.lnum
= curwin
->w_botline
- 1;
5703 if (cap
->count1
- 1 >= curwin
->w_cursor
.lnum
)
5704 curwin
->w_cursor
.lnum
= 1;
5708 if (hasAnyFolding(curwin
))
5710 /* Count a fold for one screen line. */
5711 for (n
= cap
->count1
- 1; n
> 0
5712 && curwin
->w_cursor
.lnum
> curwin
->w_topline
; --n
)
5714 (void)hasFolding(curwin
->w_cursor
.lnum
,
5715 &curwin
->w_cursor
.lnum
, NULL
);
5716 --curwin
->w_cursor
.lnum
;
5721 curwin
->w_cursor
.lnum
-= cap
->count1
- 1;
5726 if (cap
->cmdchar
== 'M')
5729 /* Don't count filler lines above the window. */
5730 used
-= diff_check_fill(curwin
, curwin
->w_topline
)
5731 - curwin
->w_topfill
;
5733 validate_botline(); /* make sure w_empty_rows is valid */
5734 half
= (curwin
->w_height
- curwin
->w_empty_rows
+ 1) / 2;
5735 for (n
= 0; curwin
->w_topline
+ n
< curbuf
->b_ml
.ml_line_count
; ++n
)
5738 /* Count half he number of filler lines to be "below this
5739 * line" and half to be "above the next line". */
5740 if (n
> 0 && used
+ diff_check_fill(curwin
, curwin
->w_topline
5747 used
+= plines(curwin
->w_topline
+ n
);
5751 if (hasFolding(curwin
->w_topline
+ n
, NULL
, &lnum
))
5752 n
= lnum
- curwin
->w_topline
;
5755 if (n
> 0 && used
> curwin
->w_height
)
5758 else /* (cap->cmdchar == 'H') */
5760 n
= cap
->count1
- 1;
5762 if (hasAnyFolding(curwin
))
5764 /* Count a fold for one screen line. */
5765 lnum
= curwin
->w_topline
;
5766 while (n
-- > 0 && lnum
< curwin
->w_botline
- 1)
5768 hasFolding(lnum
, NULL
, &lnum
);
5771 n
= lnum
- curwin
->w_topline
;
5775 curwin
->w_cursor
.lnum
= curwin
->w_topline
+ n
;
5776 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
5777 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
5780 cursor_correct(); /* correct for 'so' */
5781 beginline(BL_SOL
| BL_FIX
);
5785 * Cursor right commands.
5795 # define PAST_LINE 0
5798 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
5800 /* <C-Right> and <S-Right> move a word or WORD right */
5801 if (mod_mask
& MOD_MASK_CTRL
)
5807 cap
->oap
->motion_type
= MCHAR
;
5808 cap
->oap
->inclusive
= FALSE
;
5810 PAST_LINE
= (VIsual_active
&& *p_sel
!= 'o');
5812 # ifdef FEAT_VIRTUALEDIT
5814 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5815 * (theoretically) infinitely long.
5817 if (virtual_active())
5822 for (n
= cap
->count1
; n
> 0; --n
)
5824 if ((!PAST_LINE
&& oneright() == FAIL
)
5825 || (PAST_LINE
&& *ml_get_cursor() == NUL
))
5828 * <Space> wraps to next line if 'whichwrap' has 's'.
5829 * 'l' wraps to next line if 'whichwrap' has 'l'.
5830 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5832 if ( ((cap
->cmdchar
== ' '
5833 && vim_strchr(p_ww
, 's') != NULL
)
5834 || (cap
->cmdchar
== 'l'
5835 && vim_strchr(p_ww
, 'l') != NULL
)
5836 || (cap
->cmdchar
== K_RIGHT
5837 && vim_strchr(p_ww
, '>') != NULL
))
5838 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
5840 /* When deleting we also count the NL as a character.
5841 * Set cap->oap->inclusive when last char in the line is
5842 * included, move to next line after that */
5843 if ( cap
->oap
->op_type
!= OP_NOP
5844 && !cap
->oap
->inclusive
5845 && !lineempty(curwin
->w_cursor
.lnum
))
5846 cap
->oap
->inclusive
= TRUE
;
5849 ++curwin
->w_cursor
.lnum
;
5850 curwin
->w_cursor
.col
= 0;
5851 #ifdef FEAT_VIRTUALEDIT
5852 curwin
->w_cursor
.coladd
= 0;
5854 curwin
->w_set_curswant
= TRUE
;
5855 cap
->oap
->inclusive
= FALSE
;
5859 if (cap
->oap
->op_type
== OP_NOP
)
5861 /* Only beep and flush if not moved at all */
5862 if (n
== cap
->count1
)
5867 if (!lineempty(curwin
->w_cursor
.lnum
))
5868 cap
->oap
->inclusive
= TRUE
;
5875 curwin
->w_set_curswant
= TRUE
;
5876 # ifdef FEAT_VIRTUALEDIT
5877 if (virtual_active())
5884 curwin
->w_cursor
.col
+=
5885 (*mb_ptr2len
)(ml_get_cursor());
5888 ++curwin
->w_cursor
.col
;
5894 if (n
!= cap
->count1
&& (fdo_flags
& FDO_HOR
) && KeyTyped
5895 && cap
->oap
->op_type
== OP_NOP
)
5901 * Cursor left commands.
5903 * Returns TRUE when operator end should not be adjusted.
5911 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
5913 /* <C-Left> and <S-Left> move a word or WORD left */
5914 if (mod_mask
& MOD_MASK_CTRL
)
5920 cap
->oap
->motion_type
= MCHAR
;
5921 cap
->oap
->inclusive
= FALSE
;
5922 for (n
= cap
->count1
; n
> 0; --n
)
5924 if (oneleft() == FAIL
)
5926 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5927 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5928 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5930 if ( (((cap
->cmdchar
== K_BS
5931 || cap
->cmdchar
== Ctrl_H
)
5932 && vim_strchr(p_ww
, 'b') != NULL
)
5933 || (cap
->cmdchar
== 'h'
5934 && vim_strchr(p_ww
, 'h') != NULL
)
5935 || (cap
->cmdchar
== K_LEFT
5936 && vim_strchr(p_ww
, '<') != NULL
))
5937 && curwin
->w_cursor
.lnum
> 1)
5939 --(curwin
->w_cursor
.lnum
);
5940 coladvance((colnr_T
)MAXCOL
);
5941 curwin
->w_set_curswant
= TRUE
;
5943 /* When the NL before the first char has to be deleted we
5944 * put the cursor on the NUL after the previous line.
5945 * This is a very special case, be careful!
5946 * Don't adjust op_end now, otherwise it won't work. */
5947 if ( (cap
->oap
->op_type
== OP_DELETE
5948 || cap
->oap
->op_type
== OP_CHANGE
)
5949 && !lineempty(curwin
->w_cursor
.lnum
))
5951 if (*ml_get_cursor() != NUL
)
5952 ++curwin
->w_cursor
.col
;
5953 cap
->retval
|= CA_NO_ADJ_OP_END
;
5957 /* Only beep and flush if not moved at all */
5958 else if (cap
->oap
->op_type
== OP_NOP
&& n
== cap
->count1
)
5964 if (n
!= cap
->count1
&& (fdo_flags
& FDO_HOR
) && KeyTyped
5965 && cap
->oap
->op_type
== OP_NOP
)
5971 * Cursor up commands.
5972 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5978 if (mod_mask
& MOD_MASK_SHIFT
)
5980 /* <S-Up> is page up */
5981 cap
->arg
= BACKWARD
;
5986 cap
->oap
->motion_type
= MLINE
;
5987 if (cursor_up(cap
->count1
, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
5988 clearopbeep(cap
->oap
);
5990 beginline(BL_WHITE
| BL_FIX
);
5995 * Cursor down commands.
5996 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6002 if (mod_mask
& MOD_MASK_SHIFT
)
6004 /* <S-Down> is page down */
6009 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6010 /* In a quickfix window a <CR> jumps to the error under the cursor. */
6011 if (bt_quickfix(curbuf
) && cap
->cmdchar
== CAR
)
6012 if (curwin
->w_llist_ref
== NULL
)
6013 do_cmdline_cmd((char_u
*)".cc"); /* quickfix window */
6015 do_cmdline_cmd((char_u
*)".ll"); /* location list window */
6020 /* In the cmdline window a <CR> executes the command. */
6021 if (cmdwin_type
!= 0 && cap
->cmdchar
== CAR
)
6022 cmdwin_result
= CAR
;
6026 cap
->oap
->motion_type
= MLINE
;
6027 if (cursor_down(cap
->count1
, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
6028 clearopbeep(cap
->oap
);
6030 beginline(BL_WHITE
| BL_FIX
);
6035 #ifdef FEAT_SEARCHPATH
6037 * Grab the file name under the cursor and edit it.
6048 clearopbeep(cap
->oap
);
6053 if (curbuf_locked())
6060 ptr
= grab_file_name(cap
->count1
, &lnum
);
6064 /* do autowrite if necessary */
6065 if (curbufIsChanged() && curbuf
->b_nwindows
<= 1 && !P_HID(curbuf
))
6066 autowrite(curbuf
, FALSE
);
6068 (void)do_ecmd(0, ptr
, NULL
, NULL
, ECMD_LAST
,
6069 P_HID(curbuf
) ? ECMD_HIDE
: 0, curwin
);
6070 if (cap
->nchar
== 'F' && lnum
>= 0)
6072 curwin
->w_cursor
.lnum
= lnum
;
6073 check_cursor_lnum();
6074 beginline(BL_SOL
| BL_FIX
);
6084 * <End> command: to end of current line or last line.
6090 if (cap
->arg
|| (mod_mask
& MOD_MASK_CTRL
)) /* CTRL-END = goto last line */
6094 cap
->count1
= 1; /* to end of current line */
6100 * Handle the "$" command.
6106 cap
->oap
->motion_type
= MCHAR
;
6107 cap
->oap
->inclusive
= TRUE
;
6108 #ifdef FEAT_VIRTUALEDIT
6109 /* In virtual mode when off the edge of a line and an operator
6110 * is pending (whew!) keep the cursor where it is.
6111 * Otherwise, send it to the end of the line. */
6112 if (!virtual_active() || gchar_cursor() != NUL
6113 || cap
->oap
->op_type
== OP_NOP
)
6115 curwin
->w_curswant
= MAXCOL
; /* so we stay at the end */
6116 if (cursor_down((long)(cap
->count1
- 1),
6117 cap
->oap
->op_type
== OP_NOP
) == FAIL
)
6118 clearopbeep(cap
->oap
);
6120 else if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6126 * Implementation of '?' and '/' commands.
6127 * If cap->arg is TRUE don't set PC mark.
6133 oparg_T
*oap
= cap
->oap
;
6135 if (cap
->cmdchar
== '?' && cap
->oap
->op_type
== OP_ROT13
)
6137 /* Translate "g??" to "g?g?" */
6144 cap
->searchbuf
= getcmdline(cap
->cmdchar
, cap
->count1
, 0);
6146 if (cap
->searchbuf
== NULL
)
6152 normal_search(cap
, cap
->cmdchar
, cap
->searchbuf
,
6153 (cap
->arg
? 0 : SEARCH_MARK
));
6157 * Handle "N" and "n" commands.
6158 * cap->arg is SEARCH_REV for "N", 0 for "n".
6164 normal_search(cap
, 0, NULL
, SEARCH_MARK
| cap
->arg
);
6168 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6169 * Uses only cap->count1 and cap->oap from "cap".
6172 normal_search(cap
, dir
, pat
, opt
)
6176 int opt
; /* extra flags for do_search() */
6180 cap
->oap
->motion_type
= MCHAR
;
6181 cap
->oap
->inclusive
= FALSE
;
6182 cap
->oap
->use_reg_one
= TRUE
;
6183 curwin
->w_set_curswant
= TRUE
;
6185 i
= do_search(cap
->oap
, dir
, pat
, cap
->count1
,
6186 opt
| SEARCH_OPT
| SEARCH_ECHO
| SEARCH_MSG
, NULL
);
6192 cap
->oap
->motion_type
= MLINE
;
6193 #ifdef FEAT_VIRTUALEDIT
6194 curwin
->w_cursor
.coladd
= 0;
6197 if (cap
->oap
->op_type
== OP_NOP
&& (fdo_flags
& FDO_SEARCH
) && KeyTyped
)
6202 /* "/$" will put the cursor after the end of the line, may need to
6203 * correct that here */
6208 * Character search commands.
6209 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6210 * ',' and FALSE for ';'.
6211 * cap->nchar is NUL for ',' and ';' (repeat the search)
6219 if (cap
->cmdchar
== 't' || cap
->cmdchar
== 'T')
6224 cap
->oap
->motion_type
= MCHAR
;
6225 if (IS_SPECIAL(cap
->nchar
) || searchc(cap
, t_cmd
) == FAIL
)
6226 clearopbeep(cap
->oap
);
6229 curwin
->w_set_curswant
= TRUE
;
6230 #ifdef FEAT_VIRTUALEDIT
6231 /* Include a Tab for "tx" and for "dfx". */
6232 if (gchar_cursor() == TAB
&& virtual_active() && cap
->arg
== FORWARD
6233 && (t_cmd
|| cap
->oap
->op_type
!= OP_NOP
))
6237 getvcol(curwin
, &curwin
->w_cursor
, &scol
, NULL
, &ecol
);
6238 curwin
->w_cursor
.coladd
= ecol
- scol
;
6241 curwin
->w_cursor
.coladd
= 0;
6244 adjust_for_sel(cap
);
6247 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6254 * "[" and "]" commands.
6255 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6263 pos_T
*pos
= NULL
; /* init for GCC */
6264 pos_T old_pos
; /* cursor position before command */
6270 cap
->oap
->motion_type
= MCHAR
;
6271 cap
->oap
->inclusive
= FALSE
;
6272 old_pos
= curwin
->w_cursor
;
6273 #ifdef FEAT_VIRTUALEDIT
6274 curwin
->w_cursor
.coladd
= 0; /* TODO: don't do this for an error. */
6277 #ifdef FEAT_SEARCHPATH
6279 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6281 if (cap
->nchar
== 'f')
6288 * Find the occurrence(s) of the identifier or define under cursor
6289 * in current and included files or jump to the first occurrence.
6292 * fwd bwd fwd bwd fwd bwd
6293 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6294 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6296 if (vim_strchr((char_u
*)
6302 cap
->nchar
) != NULL
)
6307 if ((len
= find_ident_under_cursor(&ptr
, FIND_IDENT
)) == 0)
6311 find_pattern_in_path(ptr
, 0, len
, TRUE
,
6312 cap
->count0
== 0 ? !isupper(cap
->nchar
) : FALSE
,
6313 ((cap
->nchar
& 0xf) == ('d' & 0xf)) ? FIND_DEFINE
: FIND_ANY
,
6315 isupper(cap
->nchar
) ? ACTION_SHOW_ALL
:
6316 islower(cap
->nchar
) ? ACTION_SHOW
: ACTION_GOTO
,
6317 cap
->cmdchar
== ']' ? curwin
->w_cursor
.lnum
+ 1 : (linenr_T
)1,
6319 curwin
->w_set_curswant
= TRUE
;
6326 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6327 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6328 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6329 * "[m" or "]m" search for prev/next start of (Java) method.
6330 * "[M" or "]M" search for prev/next end of (Java) method.
6332 if ( (cap
->cmdchar
== '['
6333 && vim_strchr((char_u
*)"{(*/#mM", cap
->nchar
) != NULL
)
6334 || (cap
->cmdchar
== ']'
6335 && vim_strchr((char_u
*)"})*/#mM", cap
->nchar
) != NULL
))
6337 if (cap
->nchar
== '*')
6341 if (cap
->nchar
== 'm' || cap
->nchar
== 'M')
6343 if (cap
->cmdchar
== '[')
6356 if ((pos
= findmatchlimit(cap
->oap
, findc
,
6357 (cap
->cmdchar
== '[') ? FM_BACKWARD
: FM_FORWARD
, 0)) == NULL
)
6359 if (new_pos
.lnum
== 0) /* nothing found */
6361 if (cap
->nchar
!= 'm' && cap
->nchar
!= 'M')
6362 clearopbeep(cap
->oap
);
6365 pos
= &new_pos
; /* use last one found */
6369 curwin
->w_cursor
= *pos
;
6372 curwin
->w_cursor
= old_pos
;
6375 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6376 * brought us to the match for "[m" and "]M" when inside a method.
6377 * Try finding the '{' or '}' we want to be at.
6378 * Also repeat for the given count.
6380 if (cap
->nchar
== 'm' || cap
->nchar
== 'M')
6382 /* norm is TRUE for "]M" and "[m" */
6383 int norm
= ((findc
== '{') == (cap
->nchar
== 'm'));
6386 /* found a match: we were inside a method */
6387 if (prev_pos
.lnum
!= 0)
6390 curwin
->w_cursor
= prev_pos
;
6400 if ((findc
== '{' ? dec_cursor() : inc_cursor()) < 0)
6402 /* if not found anything, that's an error */
6404 clearopbeep(cap
->oap
);
6409 if (c
== '{' || c
== '}')
6411 /* Must have found end/start of class: use it.
6412 * Or found the place to be at. */
6413 if ((c
== findc
&& norm
) || (n
== 1 && !norm
))
6415 new_pos
= curwin
->w_cursor
;
6419 /* if no match found at all, we started outside of the
6420 * class and we're inside now. Just go on. */
6421 else if (new_pos
.lnum
== 0)
6423 new_pos
= curwin
->w_cursor
;
6426 /* found start/end of other method: go to match */
6427 else if ((pos
= findmatchlimit(cap
->oap
, findc
,
6428 (cap
->cmdchar
== '[') ? FM_BACKWARD
: FM_FORWARD
,
6432 curwin
->w_cursor
= *pos
;
6438 curwin
->w_cursor
= old_pos
;
6439 if (pos
== NULL
&& new_pos
.lnum
!= 0)
6440 clearopbeep(cap
->oap
);
6445 curwin
->w_cursor
= *pos
;
6446 curwin
->w_set_curswant
= TRUE
;
6448 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
6449 && cap
->oap
->op_type
== OP_NOP
)
6456 * "[[", "[]", "]]" and "][": move to start or end of function
6458 else if (cap
->nchar
== '[' || cap
->nchar
== ']')
6460 if (cap
->nchar
== cap
->cmdchar
) /* "]]" or "[[" */
6463 flag
= '}'; /* "][" or "[]" */
6465 curwin
->w_set_curswant
= TRUE
;
6467 * Imitate strange Vi behaviour: When using "]]" with an operator
6468 * we also stop at '}'.
6470 if (!findpar(&cap
->oap
->inclusive
, cap
->arg
, cap
->count1
, flag
,
6471 (cap
->oap
->op_type
!= OP_NOP
6472 && cap
->arg
== FORWARD
&& flag
== '{')))
6473 clearopbeep(cap
->oap
);
6476 if (cap
->oap
->op_type
== OP_NOP
)
6477 beginline(BL_WHITE
| BL_FIX
);
6479 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6486 * "[p", "[P", "]P" and "]p": put with indent adjustment
6488 else if (cap
->nchar
== 'p' || cap
->nchar
== 'P')
6490 if (!checkclearop(cap
->oap
))
6493 do_put(cap
->oap
->regname
,
6494 (cap
->cmdchar
== ']' && cap
->nchar
== 'p') ? FORWARD
: BACKWARD
,
6495 cap
->count1
, PUT_FIXINDENT
);
6500 * "['", "[`", "]'" and "]`": jump to next mark
6502 else if (cap
->nchar
== '\'' || cap
->nchar
== '`')
6504 pos
= &curwin
->w_cursor
;
6505 for (n
= cap
->count1
; n
> 0; --n
)
6508 pos
= getnextmark(pos
, cap
->cmdchar
== '[' ? BACKWARD
: FORWARD
,
6509 cap
->nchar
== '\'');
6515 nv_cursormark(cap
, cap
->nchar
== '\'', pos
);
6520 * [ or ] followed by a middle mouse click: put selected text with
6521 * indent adjustment. Any other button just does as usual.
6523 else if (cap
->nchar
>= K_LEFTMOUSE
&& cap
->nchar
<= K_RIGHTRELEASE
)
6525 (void)do_mouse(cap
->oap
, cap
->nchar
,
6526 (cap
->cmdchar
== ']') ? FORWARD
: BACKWARD
,
6527 cap
->count1
, PUT_FIXINDENT
);
6529 #endif /* FEAT_MOUSE */
6533 * "[z" and "]z": move to start or end of open fold.
6535 else if (cap
->nchar
== 'z')
6537 if (foldMoveTo(FALSE
, cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6538 cap
->count1
) == FAIL
)
6539 clearopbeep(cap
->oap
);
6545 * "[c" and "]c": move to next or previous diff-change.
6547 else if (cap
->nchar
== 'c')
6549 if (diff_move_to(cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6550 cap
->count1
) == FAIL
)
6551 clearopbeep(cap
->oap
);
6557 * "[s", "[S", "]s" and "]S": move to next spell error.
6559 else if (cap
->nchar
== 's' || cap
->nchar
== 'S')
6562 for (n
= 0; n
< cap
->count1
; ++n
)
6563 if (spell_move_to(curwin
, cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6564 cap
->nchar
== 's' ? TRUE
: FALSE
, FALSE
, NULL
) == 0)
6566 clearopbeep(cap
->oap
);
6569 # ifdef FEAT_FOLDING
6570 if (cap
->oap
->op_type
== OP_NOP
&& (fdo_flags
& FDO_SEARCH
) && KeyTyped
)
6576 /* Not a valid cap->nchar. */
6578 clearopbeep(cap
->oap
);
6582 * Handle Normal mode "%" command.
6590 linenr_T lnum
= curwin
->w_cursor
.lnum
;
6593 cap
->oap
->inclusive
= TRUE
;
6594 if (cap
->count0
) /* {cnt}% : goto {cnt} percentage in file */
6596 if (cap
->count0
> 100)
6597 clearopbeep(cap
->oap
);
6600 cap
->oap
->motion_type
= MLINE
;
6602 /* Round up, so CTRL-G will give same value. Watch out for a
6603 * large line count, the line number must not go negative! */
6604 if (curbuf
->b_ml
.ml_line_count
> 1000000)
6605 curwin
->w_cursor
.lnum
= (curbuf
->b_ml
.ml_line_count
+ 99L)
6606 / 100L * cap
->count0
;
6608 curwin
->w_cursor
.lnum
= (curbuf
->b_ml
.ml_line_count
*
6609 cap
->count0
+ 99L) / 100L;
6610 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
6611 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6612 beginline(BL_SOL
| BL_FIX
);
6615 else /* "%" : go to matching paren */
6617 cap
->oap
->motion_type
= MCHAR
;
6618 cap
->oap
->use_reg_one
= TRUE
;
6619 if ((pos
= findmatch(cap
->oap
, NUL
)) == NULL
)
6620 clearopbeep(cap
->oap
);
6624 curwin
->w_cursor
= *pos
;
6625 curwin
->w_set_curswant
= TRUE
;
6626 #ifdef FEAT_VIRTUALEDIT
6627 curwin
->w_cursor
.coladd
= 0;
6630 adjust_for_sel(cap
);
6635 if (cap
->oap
->op_type
== OP_NOP
6636 && lnum
!= curwin
->w_cursor
.lnum
6637 && (fdo_flags
& FDO_PERCENT
)
6644 * Handle "(" and ")" commands.
6645 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6651 cap
->oap
->motion_type
= MCHAR
;
6652 cap
->oap
->use_reg_one
= TRUE
;
6653 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6654 cap
->oap
->inclusive
= FALSE
;
6655 curwin
->w_set_curswant
= TRUE
;
6657 if (findsent(cap
->arg
, cap
->count1
) == FAIL
)
6658 clearopbeep(cap
->oap
);
6661 /* Don't leave the cursor on the NUL past end of line. */
6662 adjust_cursor(cap
->oap
);
6663 #ifdef FEAT_VIRTUALEDIT
6664 curwin
->w_cursor
.coladd
= 0;
6667 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6674 * "m" command: Mark a position.
6680 if (!checkclearop(cap
->oap
))
6682 if (setmark(cap
->nchar
) == FAIL
)
6683 clearopbeep(cap
->oap
);
6688 * "{" and "}" commands.
6689 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6695 cap
->oap
->motion_type
= MCHAR
;
6696 cap
->oap
->inclusive
= FALSE
;
6697 cap
->oap
->use_reg_one
= TRUE
;
6698 curwin
->w_set_curswant
= TRUE
;
6699 if (!findpar(&cap
->oap
->inclusive
, cap
->arg
, cap
->count1
, NUL
, FALSE
))
6700 clearopbeep(cap
->oap
);
6703 #ifdef FEAT_VIRTUALEDIT
6704 curwin
->w_cursor
.coladd
= 0;
6707 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6714 * "u" command: Undo or make lower case.
6720 if (cap
->oap
->op_type
== OP_LOWER
6726 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6742 if (!checkclearopq(cap
->oap
))
6744 u_undo((int)cap
->count1
);
6745 curwin
->w_set_curswant
= TRUE
;
6750 * Handle the "r" command.
6760 if (checkclearop(cap
->oap
))
6763 /* get another character */
6764 if (cap
->nchar
== Ctrl_V
)
6766 had_ctrl_v
= Ctrl_V
;
6767 cap
->nchar
= get_literal();
6768 /* Don't redo a multibyte character with CTRL-V. */
6769 if (cap
->nchar
> DEL
)
6775 /* Abort if the character is a special key. */
6776 if (IS_SPECIAL(cap
->nchar
))
6778 clearopbeep(cap
->oap
);
6783 /* Visual mode "r" */
6793 #ifdef FEAT_VIRTUALEDIT
6794 /* Break tabs, etc. */
6795 if (virtual_active())
6797 if (u_save_cursor() == FAIL
)
6799 if (gchar_cursor() == NUL
)
6801 /* Add extra space and put the cursor on the first one. */
6802 coladvance_force((colnr_T
)(getviscol() + cap
->count1
));
6803 curwin
->w_cursor
.col
-= cap
->count1
;
6805 else if (gchar_cursor() == TAB
)
6806 coladvance_force(getviscol());
6810 /* Abort if not enough characters to replace. */
6811 ptr
= ml_get_cursor();
6812 if (STRLEN(ptr
) < (unsigned)cap
->count1
6814 || (has_mbyte
&& mb_charlen(ptr
) < cap
->count1
)
6818 clearopbeep(cap
->oap
);
6823 * Replacing with a TAB is done by edit() when it is complicated because
6824 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6825 * Other characters are done below to avoid problems with things like
6826 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6828 if (had_ctrl_v
!= Ctrl_V
&& cap
->nchar
== '\t' && (curbuf
->b_p_et
|| p_sta
))
6830 stuffnumReadbuff(cap
->count1
);
6831 stuffcharReadbuff('R');
6832 stuffcharReadbuff('\t');
6833 stuffcharReadbuff(ESC
);
6837 /* save line for undo */
6838 if (u_save_cursor() == FAIL
)
6841 if (had_ctrl_v
!= Ctrl_V
&& (cap
->nchar
== '\r' || cap
->nchar
== '\n'))
6844 * Replace character(s) by a single newline.
6845 * Strange vi behaviour: Only one newline is inserted.
6846 * Delete the characters here.
6847 * Insert the newline with an insert command, takes care of
6848 * autoindent. The insert command depends on being on the last
6849 * character of a line or not.
6852 (void)del_chars(cap
->count1
, FALSE
); /* delete the characters */
6854 (void)del_bytes(cap
->count1
, FALSE
, FALSE
); /* delete the characters */
6856 stuffcharReadbuff('\r');
6857 stuffcharReadbuff(ESC
);
6859 /* Give 'r' to edit(), to get the redo command right. */
6860 invoke_edit(cap
, TRUE
, 'r', FALSE
);
6864 prep_redo(cap
->oap
->regname
, cap
->count1
,
6865 NUL
, 'r', NUL
, had_ctrl_v
, cap
->nchar
);
6867 curbuf
->b_op_start
= curwin
->w_cursor
;
6871 int old_State
= State
;
6873 if (cap
->ncharC1
!= 0)
6874 AppendCharToRedobuff(cap
->ncharC1
);
6875 if (cap
->ncharC2
!= 0)
6876 AppendCharToRedobuff(cap
->ncharC2
);
6878 /* This is slow, but it handles replacing a single-byte with a
6879 * multi-byte and the other way around. Also handles adding
6880 * composing characters for utf-8. */
6881 for (n
= cap
->count1
; n
> 0; --n
)
6884 ins_char(cap
->nchar
);
6886 if (cap
->ncharC1
!= 0)
6887 ins_char(cap
->ncharC1
);
6888 if (cap
->ncharC2
!= 0)
6889 ins_char(cap
->ncharC2
);
6896 * Replace the characters within one line.
6898 for (n
= cap
->count1
; n
> 0; --n
)
6901 * Get ptr again, because u_save and/or showmatch() will have
6902 * released the line. At the same time we let know that the
6903 * line will be changed.
6905 ptr
= ml_get_buf(curbuf
, curwin
->w_cursor
.lnum
, TRUE
);
6906 ptr
[curwin
->w_cursor
.col
] = cap
->nchar
;
6907 if (p_sm
&& msg_silent
== 0)
6908 showmatch(cap
->nchar
);
6909 ++curwin
->w_cursor
.col
;
6911 #ifdef FEAT_NETBEANS_INTG
6914 colnr_T start
= (colnr_T
)(curwin
->w_cursor
.col
- cap
->count1
);
6916 netbeans_removed(curbuf
, curwin
->w_cursor
.lnum
, start
,
6918 netbeans_inserted(curbuf
, curwin
->w_cursor
.lnum
, start
,
6919 &ptr
[start
], (int)cap
->count1
);
6923 /* mark the buffer as changed and prepare for displaying */
6924 changed_bytes(curwin
->w_cursor
.lnum
,
6925 (colnr_T
)(curwin
->w_cursor
.col
- cap
->count1
));
6927 --curwin
->w_cursor
.col
; /* cursor on the last replaced char */
6929 /* if the character on the left of the current cursor is a multi-byte
6930 * character, move two characters left */
6934 curbuf
->b_op_end
= curwin
->w_cursor
;
6935 curwin
->w_set_curswant
= TRUE
;
6936 set_last_insert(cap
->nchar
);
6942 * 'o': Exchange start and end of Visual area.
6943 * 'O': same, but in block mode exchange left and right corners.
6946 v_swap_corners(cmdchar
)
6950 colnr_T left
, right
;
6952 if (cmdchar
== 'O' && VIsual_mode
== Ctrl_V
)
6954 old_cursor
= curwin
->w_cursor
;
6955 getvcols(curwin
, &old_cursor
, &VIsual
, &left
, &right
);
6956 curwin
->w_cursor
.lnum
= VIsual
.lnum
;
6958 VIsual
= curwin
->w_cursor
;
6960 curwin
->w_cursor
.lnum
= old_cursor
.lnum
;
6961 curwin
->w_curswant
= right
;
6962 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6963 * right one column */
6964 if (old_cursor
.lnum
>= VIsual
.lnum
&& *p_sel
== 'e')
6965 ++curwin
->w_curswant
;
6966 coladvance(curwin
->w_curswant
);
6967 if (curwin
->w_cursor
.col
== old_cursor
.col
6968 #ifdef FEAT_VIRTUALEDIT
6969 && (!virtual_active()
6970 || curwin
->w_cursor
.coladd
== old_cursor
.coladd
)
6974 curwin
->w_cursor
.lnum
= VIsual
.lnum
;
6975 if (old_cursor
.lnum
<= VIsual
.lnum
&& *p_sel
== 'e')
6978 VIsual
= curwin
->w_cursor
;
6980 curwin
->w_cursor
.lnum
= old_cursor
.lnum
;
6982 curwin
->w_curswant
= left
;
6987 old_cursor
= curwin
->w_cursor
;
6988 curwin
->w_cursor
= VIsual
;
6989 VIsual
= old_cursor
;
6990 curwin
->w_set_curswant
= TRUE
;
6993 #endif /* FEAT_VISUAL */
6996 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7003 if (VIsual_active
) /* "R" is replace lines */
7012 if (!checkclearopq(cap
->oap
))
7014 if (!curbuf
->b_p_ma
)
7015 EMSG(_(e_modifiable
));
7018 #ifdef FEAT_VIRTUALEDIT
7019 if (virtual_active())
7020 coladvance(getviscol());
7022 invoke_edit(cap
, FALSE
, cap
->arg
? 'V' : 'R', FALSE
);
7027 #ifdef FEAT_VREPLACE
7039 cap
->nchar
= cap
->extra_char
;
7040 nv_replace(cap
); /* Do same as "r" in Visual mode for now */
7044 if (!checkclearopq(cap
->oap
))
7046 if (!curbuf
->b_p_ma
)
7047 EMSG(_(e_modifiable
));
7050 if (cap
->extra_char
== Ctrl_V
) /* get another character */
7051 cap
->extra_char
= get_literal();
7052 stuffcharReadbuff(cap
->extra_char
);
7053 stuffcharReadbuff(ESC
);
7054 # ifdef FEAT_VIRTUALEDIT
7055 if (virtual_active())
7056 coladvance(getviscol());
7058 invoke_edit(cap
, TRUE
, 'v', FALSE
);
7065 * Swap case for "~" command, when it does not work like an operator.
7074 #ifdef FEAT_NETBEANS_INTG
7080 if (checkclearopq(cap
->oap
))
7083 if (lineempty(curwin
->w_cursor
.lnum
) && vim_strchr(p_ww
, '~') == NULL
)
7085 clearopbeep(cap
->oap
);
7091 if (u_save_cursor() == FAIL
)
7094 startpos
= curwin
->w_cursor
;
7095 #ifdef FEAT_NETBEANS_INTG
7098 for (n
= cap
->count1
; n
> 0; --n
)
7100 did_change
|= swapchar(cap
->oap
->op_type
, &curwin
->w_cursor
);
7102 if (gchar_cursor() == NUL
)
7104 if (vim_strchr(p_ww
, '~') != NULL
7105 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
7107 #ifdef FEAT_NETBEANS_INTG
7112 ptr
= ml_get(pos
.lnum
);
7113 count
= (int)STRLEN(ptr
) - pos
.col
;
7114 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
,
7116 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
,
7117 &ptr
[pos
.col
], count
);
7123 ++curwin
->w_cursor
.lnum
;
7124 curwin
->w_cursor
.col
= 0;
7127 if (u_savesub(curwin
->w_cursor
.lnum
) == FAIL
)
7136 #ifdef FEAT_NETBEANS_INTG
7137 if (did_change
&& usingNetbeans
)
7139 ptr
= ml_get(pos
.lnum
);
7140 count
= curwin
->w_cursor
.col
- pos
.col
;
7141 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
, (long)count
);
7142 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
, &ptr
[pos
.col
], count
);
7148 curwin
->w_set_curswant
= TRUE
;
7151 changed_lines(startpos
.lnum
, startpos
.col
, curwin
->w_cursor
.lnum
+ 1,
7153 curbuf
->b_op_start
= startpos
;
7154 curbuf
->b_op_end
= curwin
->w_cursor
;
7155 if (curbuf
->b_op_end
.col
> 0)
7156 --curbuf
->b_op_end
.col
;
7161 * Move cursor to mark.
7164 nv_cursormark(cap
, flag
, pos
)
7169 if (check_mark(pos
) == FAIL
)
7173 if (cap
->cmdchar
== '\''
7174 || cap
->cmdchar
== '`'
7175 || cap
->cmdchar
== '['
7176 || cap
->cmdchar
== ']')
7178 curwin
->w_cursor
= *pos
;
7180 beginline(BL_WHITE
| BL_FIX
);
7184 cap
->oap
->motion_type
= flag
? MLINE
: MCHAR
;
7185 if (cap
->cmdchar
== '`')
7186 cap
->oap
->use_reg_one
= TRUE
;
7187 cap
->oap
->inclusive
= FALSE
; /* ignored if not MCHAR */
7188 curwin
->w_set_curswant
= TRUE
;
7193 * Handle commands that are operators in Visual mode.
7199 static char_u trans
[] = "YyDdCcxdXdAAIIrr";
7201 /* Uppercase means linewise, except in block mode, then "D" deletes till
7202 * the end of the line, and "C" replaces til EOL */
7203 if (isupper(cap
->cmdchar
))
7205 if (VIsual_mode
!= Ctrl_V
)
7207 else if (cap
->cmdchar
== 'C' || cap
->cmdchar
== 'D')
7208 curwin
->w_curswant
= MAXCOL
;
7210 cap
->cmdchar
= *(vim_strchr(trans
, cap
->cmdchar
) + 1);
7216 * "s" and "S" commands.
7223 if (VIsual_active
) /* "vs" and "vS" are the same as "vc" */
7225 if (cap
->cmdchar
== 'S')
7236 * Abbreviated commands.
7242 if (cap
->cmdchar
== K_DEL
|| cap
->cmdchar
== K_KDEL
)
7243 cap
->cmdchar
= 'x'; /* DEL key behaves like 'x' */
7246 /* in Visual mode these commands are operators */
7255 * Translate a command into another command.
7261 static char_u
*(ar
[8]) = {(char_u
*)"dl", (char_u
*)"dh",
7262 (char_u
*)"d$", (char_u
*)"c$",
7263 (char_u
*)"cl", (char_u
*)"cc",
7264 (char_u
*)"yy", (char_u
*)":s\r"};
7265 static char_u
*str
= (char_u
*)"xXDCsSY&";
7267 if (!checkclearopq(cap
->oap
))
7269 /* In Vi "2D" doesn't delete the next line. Can't translate it
7270 * either, because "2." should also not use the count. */
7271 if (cap
->cmdchar
== 'D' && vim_strchr(p_cpo
, CPO_HASH
) != NULL
)
7273 cap
->oap
->start
= curwin
->w_cursor
;
7274 cap
->oap
->op_type
= OP_DELETE
;
7276 set_op_var(OP_DELETE
);
7282 AppendCharToRedobuff('D');
7287 stuffnumReadbuff(cap
->count0
);
7288 stuffReadbuff(ar
[(int)(vim_strchr(str
, cap
->cmdchar
) - str
)]);
7295 * "'" and "`" commands. Also for "g'" and "g`".
7296 * cap->arg is TRUE for "'" and "g'".
7305 linenr_T lnum
= curwin
->w_cursor
.lnum
;
7306 int old_KeyTyped
= KeyTyped
; /* getting file may reset it */
7309 if (cap
->cmdchar
== 'g')
7310 c
= cap
->extra_char
;
7313 pos
= getmark(c
, (cap
->oap
->op_type
== OP_NOP
));
7314 if (pos
== (pos_T
*)-1) /* jumped to other file */
7318 check_cursor_lnum();
7319 beginline(BL_WHITE
| BL_FIX
);
7325 nv_cursormark(cap
, cap
->arg
, pos
);
7327 #ifdef FEAT_VIRTUALEDIT
7328 /* May need to clear the coladd that a mark includes. */
7329 if (!virtual_active())
7330 curwin
->w_cursor
.coladd
= 0;
7333 if (cap
->oap
->op_type
== OP_NOP
7334 && (pos
== (pos_T
*)-1 || lnum
!= curwin
->w_cursor
.lnum
)
7335 && (fdo_flags
& FDO_MARK
)
7342 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7348 #ifdef FEAT_JUMPLIST
7350 # ifdef FEAT_FOLDING
7351 linenr_T lnum
= curwin
->w_cursor
.lnum
;
7352 int old_KeyTyped
= KeyTyped
; /* getting file may reset it */
7355 if (!checkclearopq(cap
->oap
))
7357 if (cap
->cmdchar
== 'g')
7358 pos
= movechangelist((int)cap
->count1
);
7360 pos
= movemark((int)cap
->count1
);
7361 if (pos
== (pos_T
*)-1) /* jump to other file */
7363 curwin
->w_set_curswant
= TRUE
;
7366 else if (pos
!= NULL
) /* can jump */
7367 nv_cursormark(cap
, FALSE
, pos
);
7368 else if (cap
->cmdchar
== 'g')
7370 if (curbuf
->b_changelistlen
== 0)
7371 EMSG(_("E664: changelist is empty"));
7372 else if (cap
->count1
< 0)
7373 EMSG(_("E662: At start of changelist"));
7375 EMSG(_("E663: At end of changelist"));
7378 clearopbeep(cap
->oap
);
7379 # ifdef FEAT_FOLDING
7380 if (cap
->oap
->op_type
== OP_NOP
7381 && (pos
== (pos_T
*)-1 || lnum
!= curwin
->w_cursor
.lnum
)
7382 && (fdo_flags
& FDO_MARK
)
7388 clearopbeep(cap
->oap
);
7393 * Handle '"' command.
7399 if (checkclearop(cap
->oap
))
7402 if (cap
->nchar
== '=')
7403 cap
->nchar
= get_expr_register();
7405 if (cap
->nchar
!= NUL
&& valid_yank_reg(cap
->nchar
, FALSE
))
7407 cap
->oap
->regname
= cap
->nchar
;
7408 cap
->opcount
= cap
->count0
; /* remember count before '"' */
7410 set_reg_var(cap
->oap
->regname
);
7414 clearopbeep(cap
->oap
);
7419 * Handle "v", "V" and "CTRL-V" commands.
7420 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7422 * Handle CTRL-Q just like CTRL-V.
7428 if (cap
->cmdchar
== Ctrl_Q
)
7429 cap
->cmdchar
= Ctrl_V
;
7431 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7432 * characterwise, linewise, or blockwise. */
7433 if (cap
->oap
->op_type
!= OP_NOP
)
7435 cap
->oap
->motion_force
= cap
->cmdchar
;
7436 finish_op
= FALSE
; /* operator doesn't finish now but later */
7440 VIsual_select
= cap
->arg
;
7441 if (VIsual_active
) /* change Visual mode */
7443 if (VIsual_mode
== cap
->cmdchar
) /* stop visual mode */
7445 else /* toggle char/block mode */
7446 { /* or char/line mode */
7447 VIsual_mode
= cap
->cmdchar
;
7450 redraw_curbuf_later(INVERTED
); /* update the inversion */
7452 else /* start Visual mode */
7454 check_visual_highlight();
7455 if (cap
->count0
) /* use previously selected part */
7457 if (resel_VIsual_mode
== NUL
) /* there is none */
7462 VIsual
= curwin
->w_cursor
;
7464 VIsual_active
= TRUE
;
7465 VIsual_reselect
= TRUE
;
7467 /* start Select mode when 'selectmode' contains "cmd" */
7468 may_start_select('c');
7472 if (p_smd
&& msg_silent
== 0)
7473 redraw_cmdline
= TRUE
; /* show visual mode later */
7475 * For V and ^V, we multiply the number of lines even if there
7476 * was only one -- webb
7478 if (resel_VIsual_mode
!= 'v' || resel_VIsual_line_count
> 1)
7480 curwin
->w_cursor
.lnum
+=
7481 resel_VIsual_line_count
* cap
->count0
- 1;
7482 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
7483 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
7485 VIsual_mode
= resel_VIsual_mode
;
7486 if (VIsual_mode
== 'v')
7488 if (resel_VIsual_line_count
<= 1)
7489 curwin
->w_cursor
.col
+= resel_VIsual_col
* cap
->count0
- 1;
7491 curwin
->w_cursor
.col
= resel_VIsual_col
;
7494 if (resel_VIsual_col
== MAXCOL
)
7496 curwin
->w_curswant
= MAXCOL
;
7497 coladvance((colnr_T
)MAXCOL
);
7499 else if (VIsual_mode
== Ctrl_V
)
7502 curwin
->w_curswant
= curwin
->w_virtcol
7503 + resel_VIsual_col
* cap
->count0
- 1;
7504 coladvance(curwin
->w_curswant
);
7507 curwin
->w_set_curswant
= TRUE
;
7508 redraw_curbuf_later(INVERTED
); /* show the inversion */
7513 /* start Select mode when 'selectmode' contains "cmd" */
7514 may_start_select('c');
7515 n_start_visual_mode(cap
->cmdchar
);
7521 * Start selection for Shift-movement keys.
7526 /* if 'selectmode' contains "key", start Select mode */
7527 may_start_select('k');
7528 n_start_visual_mode('v');
7532 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7538 VIsual_select
= (stuff_empty() && typebuf_typed()
7539 && (vim_strchr(p_slm
, c
) != NULL
));
7543 * Start Visual mode "c".
7544 * Should set VIsual_select before calling this.
7547 n_start_visual_mode(c
)
7551 VIsual_active
= TRUE
;
7552 VIsual_reselect
= TRUE
;
7553 #ifdef FEAT_VIRTUALEDIT
7554 /* Corner case: the 0 position in a tab may change when going into
7555 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7557 if (c
== Ctrl_V
&& (ve_flags
& VE_BLOCK
) && gchar_cursor() == TAB
)
7558 coladvance(curwin
->w_virtcol
);
7560 VIsual
= curwin
->w_cursor
;
7569 if (p_smd
&& msg_silent
== 0)
7570 redraw_cmdline
= TRUE
; /* show visual mode later */
7571 #ifdef FEAT_CLIPBOARD
7572 /* Make sure the clipboard gets updated. Needed because start and
7573 * end may still be the same, and the selection needs to be owned */
7574 clip_star
.vmode
= NUL
;
7577 /* Only need to redraw this line, unless still need to redraw an old
7578 * Visual area (when 'lazyredraw' is set). */
7579 if (curwin
->w_redr_type
< INVERTED
)
7581 curwin
->w_old_cursor_lnum
= curwin
->w_cursor
.lnum
;
7582 curwin
->w_old_visual_lnum
= curwin
->w_cursor
.lnum
;
7586 #endif /* FEAT_VISUAL */
7589 * CTRL-W: Window commands
7596 if (!checkclearop(cap
->oap
))
7597 do_window(cap
->nchar
, cap
->count0
, NUL
); /* everything is in window.c */
7599 (void)checkclearop(cap
->oap
);
7613 end_visual_mode(); /* stop Visual mode */
7615 do_cmdline_cmd((char_u
*)"st");
7619 * Commands starting with "g".
7625 oparg_T
*oap
= cap
->oap
;
7636 * "g^A": dump log of used memory.
7639 vim_mem_profile_dump();
7643 #ifdef FEAT_VREPLACE
7645 * "gR": Enter virtual replace mode.
7658 do_cmdline_cmd((char_u
*)"%s//~/&");
7663 * "gv": Reselect the previous Visual area. If Visual already active,
7664 * exchange previous and current Visual area.
7667 if (checkclearop(oap
))
7670 if ( curbuf
->b_visual
.vi_start
.lnum
== 0
7671 || curbuf
->b_visual
.vi_start
.lnum
> curbuf
->b_ml
.ml_line_count
7672 || curbuf
->b_visual
.vi_end
.lnum
== 0)
7676 /* set w_cursor to the start of the Visual area, tpos to the end */
7680 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
7681 curbuf
->b_visual
.vi_mode
= i
;
7683 curbuf
->b_visual_mode_eval
= i
;
7685 i
= curwin
->w_curswant
;
7686 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
7687 curbuf
->b_visual
.vi_curswant
= i
;
7689 tpos
= curbuf
->b_visual
.vi_end
;
7690 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
7691 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
7692 curbuf
->b_visual
.vi_start
= VIsual
;
7696 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
7697 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
7698 tpos
= curbuf
->b_visual
.vi_end
;
7699 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
7702 VIsual_active
= TRUE
;
7703 VIsual_reselect
= TRUE
;
7705 /* Set Visual to the start and w_cursor to the end of the Visual
7706 * area. Make sure they are on an existing character. */
7708 VIsual
= curwin
->w_cursor
;
7709 curwin
->w_cursor
= tpos
;
7713 * When called from normal "g" command: start Select mode when
7714 * 'selectmode' contains "cmd". When called for K_SELECT, always
7715 * start Select mode.
7718 VIsual_select
= TRUE
;
7720 may_start_select('c');
7724 #ifdef FEAT_CLIPBOARD
7725 /* Make sure the clipboard gets updated. Needed because start and
7726 * end are still the same, and the selection needs to be owned */
7727 clip_star
.vmode
= NUL
;
7729 redraw_curbuf_later(INVERTED
);
7734 * "gV": Don't reselect the previous Visual area after a Select mode
7738 VIsual_reselect
= FALSE
;
7742 * "gh": start Select mode.
7743 * "gH": start Select line mode.
7744 * "g^H": start Select block mode.
7747 cap
->nchar
= Ctrl_H
;
7753 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7754 if (cap
->nchar
== Ctrl_H
)
7755 cap
->cmdchar
= Ctrl_V
;
7758 cap
->cmdchar
= cap
->nchar
+ ('v' - 'h');
7762 #endif /* FEAT_VISUAL */
7765 * "gj" and "gk" two new funny movement keys -- up and down
7766 * movement based on *screen* line rather than *file* line.
7770 /* with 'nowrap' it works just like the normal "j" command; also when
7771 * in a closed fold */
7772 if (!curwin
->w_p_wrap
7774 || hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
)
7778 oap
->motion_type
= MLINE
;
7779 i
= cursor_down(cap
->count1
, oap
->op_type
== OP_NOP
);
7782 i
= nv_screengo(oap
, FORWARD
, cap
->count1
);
7789 /* with 'nowrap' it works just like the normal "k" command; also when
7790 * in a closed fold */
7791 if (!curwin
->w_p_wrap
7793 || hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
)
7797 oap
->motion_type
= MLINE
;
7798 i
= cursor_up(cap
->count1
, oap
->op_type
== OP_NOP
);
7801 i
= nv_screengo(oap
, BACKWARD
, cap
->count1
);
7807 * "gJ": join two lines without inserting a space.
7814 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7815 * "gm": middle of "g0" and "g$".
7825 oap
->motion_type
= MCHAR
;
7826 oap
->inclusive
= FALSE
;
7827 if (curwin
->w_p_wrap
7828 #ifdef FEAT_VERTSPLIT
7829 && curwin
->w_width
!= 0
7833 int width1
= W_WIDTH(curwin
) - curwin_col_off();
7834 int width2
= width1
+ curwin_col_off2();
7838 if (curwin
->w_virtcol
>= (colnr_T
)width1
&& width2
> 0)
7839 i
= (curwin
->w_virtcol
- width1
) / width2
* width2
+ width1
;
7842 i
= curwin
->w_leftcol
;
7843 /* Go to the middle of the screen line. When 'number' is on and lines
7844 * are wrapping the middle can be more to the left. */
7845 if (cap
->nchar
== 'm')
7846 i
+= (W_WIDTH(curwin
) - curwin_col_off()
7847 + ((curwin
->w_p_wrap
&& i
> 0)
7848 ? curwin_col_off2() : 0)) / 2;
7849 coladvance((colnr_T
)i
);
7854 while (vim_iswhite(i
) && oneright() == OK
);
7856 curwin
->w_set_curswant
= TRUE
;
7860 /* "g_": to the last non-blank character in the line or <count> lines
7862 cap
->oap
->motion_type
= MCHAR
;
7863 cap
->oap
->inclusive
= TRUE
;
7864 curwin
->w_curswant
= MAXCOL
;
7865 if (cursor_down((long)(cap
->count1
- 1),
7866 cap
->oap
->op_type
== OP_NOP
) == FAIL
)
7867 clearopbeep(cap
->oap
);
7870 char_u
*ptr
= ml_get_curline();
7872 /* In Visual mode we may end up after the line. */
7873 if (curwin
->w_cursor
.col
> 0 && ptr
[curwin
->w_cursor
.col
] == NUL
)
7874 --curwin
->w_cursor
.col
;
7876 /* Decrease the cursor column until it's on a non-blank. */
7877 while (curwin
->w_cursor
.col
> 0
7878 && vim_iswhite(ptr
[curwin
->w_cursor
.col
]))
7879 --curwin
->w_cursor
.col
;
7880 curwin
->w_set_curswant
= TRUE
;
7888 int col_off
= curwin_col_off();
7890 oap
->motion_type
= MCHAR
;
7891 oap
->inclusive
= TRUE
;
7892 if (curwin
->w_p_wrap
7893 #ifdef FEAT_VERTSPLIT
7894 && curwin
->w_width
!= 0
7898 curwin
->w_curswant
= MAXCOL
; /* so we stay at the end */
7899 if (cap
->count1
== 1)
7901 int width1
= W_WIDTH(curwin
) - col_off
;
7902 int width2
= width1
+ curwin_col_off2();
7906 if (curwin
->w_virtcol
>= (colnr_T
)width1
)
7907 i
+= ((curwin
->w_virtcol
- width1
) / width2
+ 1)
7909 coladvance((colnr_T
)i
);
7910 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7911 if (curwin
->w_cursor
.col
> 0 && curwin
->w_p_wrap
)
7914 * Check for landing on a character that got split at
7915 * the end of the line. We do not want to advance to
7916 * the next screen line.
7919 if (curwin
->w_virtcol
> (colnr_T
)i
)
7920 --curwin
->w_cursor
.col
;
7924 else if (nv_screengo(oap
, FORWARD
, cap
->count1
- 1) == FAIL
)
7929 i
= curwin
->w_leftcol
+ W_WIDTH(curwin
) - col_off
- 1;
7930 coladvance((colnr_T
)i
);
7932 /* Make sure we stick in this column. */
7934 curwin
->w_curswant
= curwin
->w_virtcol
;
7935 curwin
->w_set_curswant
= FALSE
;
7941 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7946 case POUND
: /* pound sign (sometimes equal to '#') */
7948 case Ctrl_RSB
: /* :tag or :tselect for current identifier */
7949 case ']': /* :tselect for current identifier */
7954 * ge and gE: go back to end of word
7958 oap
->motion_type
= MCHAR
;
7959 curwin
->w_set_curswant
= TRUE
;
7960 oap
->inclusive
= TRUE
;
7961 if (bckend_word(cap
->count1
, cap
->nchar
== 'E', FALSE
) == FAIL
)
7966 * "g CTRL-G": display info about cursor position
7973 * "gi": start Insert at the last position.
7976 if (curbuf
->b_last_insert
.lnum
!= 0)
7978 curwin
->w_cursor
= curbuf
->b_last_insert
;
7979 check_cursor_lnum();
7980 i
= (int)STRLEN(ml_get_curline());
7981 if (curwin
->w_cursor
.col
> (colnr_T
)i
)
7983 #ifdef FEAT_VIRTUALEDIT
7984 if (virtual_active())
7985 curwin
->w_cursor
.coladd
+= curwin
->w_cursor
.col
- i
;
7987 curwin
->w_cursor
.col
= i
;
7995 * "gI": Start insert in column 1.
7999 if (!checkclearopq(oap
))
8000 invoke_edit(cap
, FALSE
, 'g', FALSE
);
8003 #ifdef FEAT_SEARCHPATH
8005 * "gf": goto file, edit file under cursor
8006 * "]f" and "[f": can also be used.
8014 /* "g'm" and "g`m": jump to mark without setting pcmark */
8026 do_sleep(cap
->count1
* 1000L);
8030 * "ga": Display the ascii value of the character under the
8031 * cursor. It is displayed in decimal, hex, and octal. -- webb
8039 * "g8": Display the bytes used for the UTF-8 character under the
8040 * cursor. It is displayed in hex.
8041 * "8g8" finds illegal byte sequence.
8044 if (cap
->count0
== 8)
8056 * "gg": Goto the first line in file. With a count it goes to
8057 * that line number like for "G". -- webb
8065 * Two-character operators:
8067 * "gw" Format text and keep cursor position
8068 * "g~" Toggle the case of the text.
8069 * "gu" Change text to lower case.
8070 * "gU" Change text to upper case.
8071 * "g?" rot13 encoding
8072 * "g@" call 'operatorfunc'
8076 oap
->cursor_start
= curwin
->w_cursor
;
8087 * "gd": Find first occurrence of pattern under the cursor in the
8089 * "gD": idem, but in the current file.
8093 nv_gd(oap
, cap
->nchar
, (int)cap
->count0
);
8098 * g<*Mouse> : <C-*mouse>
8102 case K_MIDDLERELEASE
:
8108 case K_RIGHTRELEASE
:
8115 mod_mask
= MOD_MASK_CTRL
;
8116 (void)do_mouse(oap
, cap
->nchar
, BACKWARD
, cap
->count1
, 0);
8124 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8132 /* "go": goto byte count from start of buffer */
8134 goto_byte(cap
->count0
);
8138 /* "gQ": improved Ex mode */
8142 clearopbeep(cap
->oap
);
8147 if (!checkclearopq(oap
))
8151 #ifdef FEAT_JUMPLIST
8157 cap
->count1
= -cap
->count1
;
8164 goto_tabpage((int)cap
->count0
);
8167 goto_tabpage(-(int)cap
->count1
);
8172 case '-': /* "g+" and "g-": undo or redo along the timeline */
8173 if (!checkclearopq(oap
))
8174 undo_time(cap
->nchar
== '-' ? -cap
->count1
: cap
->count1
,
8185 * Handle "o" and "O" commands.
8191 if (!checkclearopq(cap
->oap
))
8194 if (cap
->cmdchar
== 'O')
8195 /* Open above the first line of a folded sequence of lines */
8196 (void)hasFolding(curwin
->w_cursor
.lnum
,
8197 &curwin
->w_cursor
.lnum
, NULL
);
8199 /* Open below the last line of a folded sequence of lines */
8200 (void)hasFolding(curwin
->w_cursor
.lnum
,
8201 NULL
, &curwin
->w_cursor
.lnum
);
8203 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
-
8204 (cap
->cmdchar
== 'O' ? 1 : 0)),
8205 (linenr_T
)(curwin
->w_cursor
.lnum
+
8206 (cap
->cmdchar
== 'o' ? 1 : 0))
8208 && open_line(cap
->cmdchar
== 'O' ? BACKWARD
: FORWARD
,
8209 #ifdef FEAT_COMMENTS
8210 has_format_option(FO_OPEN_COMS
) ? OPENLINE_DO_COM
:
8214 /* When '#' is in 'cpoptions' ignore the count. */
8215 if (vim_strchr(p_cpo
, CPO_HASH
) != NULL
)
8217 invoke_edit(cap
, FALSE
, cap
->cmdchar
, TRUE
);
8223 * "." command: redo last change.
8229 if (!checkclearopq(cap
->oap
))
8232 * If "restart_edit" is TRUE, the last but one command is repeated
8233 * instead of the last command (inserting text). This is used for
8234 * CTRL-O <.> in insert mode.
8236 if (start_redo(cap
->count0
, restart_edit
!= 0 && !arrow_used
) == FAIL
)
8237 clearopbeep(cap
->oap
);
8248 if (!checkclearopq(cap
->oap
))
8250 u_redo((int)cap
->count1
);
8251 curwin
->w_set_curswant
= TRUE
;
8256 * Handle "U" command.
8262 /* In Visual mode and typing "gUU" triggers an operator */
8263 if (cap
->oap
->op_type
== OP_UPPER
8269 /* translate "gUU" to "gUgU" */
8274 else if (!checkclearopq(cap
->oap
))
8277 curwin
->w_set_curswant
= TRUE
;
8282 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8293 && cap
->oap
->op_type
!= OP_TILDE
)
8300 * Handle an operator command.
8301 * The actual work is done by do_pending_operator().
8309 op_type
= get_op_type(cap
->cmdchar
, cap
->nchar
);
8311 if (op_type
== cap
->oap
->op_type
) /* double operator works on lines */
8313 else if (!checkclearop(cap
->oap
))
8315 cap
->oap
->start
= curwin
->w_cursor
;
8316 cap
->oap
->op_type
= op_type
;
8318 set_op_var(op_type
);
8325 * Set v:operator to the characters for "optype".
8333 if (optype
== OP_NOP
)
8334 set_vim_var_string(VV_OP
, NULL
, 0);
8337 opchars
[0] = get_op_char(optype
);
8338 opchars
[1] = get_extra_op_char(optype
);
8340 set_vim_var_string(VV_OP
, opchars
, -1);
8346 * Handle linewise operator "dd", "yy", etc.
8348 * "_" is is a strange motion command that helps make operators more logical.
8349 * It is actually implemented, but not documented in the real Vi. This motion
8350 * command actually refers to "the current line". Commands like "dd" and "yy"
8351 * are really an alternate form of "d_" and "y_". It does accept a count, so
8352 * "d3_" works to delete 3 lines.
8358 cap
->oap
->motion_type
= MLINE
;
8359 if (cursor_down(cap
->count1
- 1L, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
8360 clearopbeep(cap
->oap
);
8361 else if ( cap
->oap
->op_type
== OP_DELETE
8362 || cap
->oap
->op_type
== OP_LSHIFT
8363 || cap
->oap
->op_type
== OP_RSHIFT
)
8364 beginline(BL_SOL
| BL_FIX
);
8365 else if (cap
->oap
->op_type
!= OP_YANK
) /* 'Y' does not move cursor */
8366 beginline(BL_WHITE
| BL_FIX
);
8376 /* CTRL-HOME is like "gg" */
8377 if (mod_mask
& MOD_MASK_CTRL
)
8384 ins_at_eol
= FALSE
; /* Don't move cursor past eol (only necessary in a
8385 one-character line). */
8395 cap
->oap
->motion_type
= MCHAR
;
8396 cap
->oap
->inclusive
= FALSE
;
8398 if (cap
->count0
> 0)
8400 coladvance((colnr_T
)(cap
->count0
- 1));
8401 curwin
->w_curswant
= (colnr_T
)(cap
->count0
- 1);
8404 curwin
->w_curswant
= 0;
8405 /* keep curswant at the column where we wanted to go, not where
8406 we ended; differs if line is too short */
8407 curwin
->w_set_curswant
= FALSE
;
8411 * Handle back-word command "b" and "B".
8412 * cap->arg is 1 for "B"
8418 cap
->oap
->motion_type
= MCHAR
;
8419 cap
->oap
->inclusive
= FALSE
;
8420 curwin
->w_set_curswant
= TRUE
;
8421 if (bck_word(cap
->count1
, cap
->arg
, FALSE
) == FAIL
)
8422 clearopbeep(cap
->oap
);
8424 else if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8430 * Handle word motion commands "e", "E", "w" and "W".
8431 * cap->arg is TRUE for "E" and "W".
8440 pos_T startpos
= curwin
->w_cursor
;
8443 * Set inclusive for the "E" and "e" command.
8445 if (cap
->cmdchar
== 'e' || cap
->cmdchar
== 'E')
8449 cap
->oap
->inclusive
= word_end
;
8452 * "cw" and "cW" are a special case.
8454 if (!word_end
&& cap
->oap
->op_type
== OP_CHANGE
)
8457 if (n
!= NUL
) /* not an empty line */
8462 * Reproduce a funny Vi behaviour: "cw" on a blank only
8463 * changes one character, not all blanks until the start of
8464 * the next word. Only do this when the 'w' flag is included
8467 if (cap
->count1
== 1 && vim_strchr(p_cpo
, CPO_CW
) != NULL
)
8469 cap
->oap
->inclusive
= TRUE
;
8470 cap
->oap
->motion_type
= MCHAR
;
8477 * This is a little strange. To match what the real Vi does,
8478 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8479 * that we are not on a space or a TAB. This seems impolite
8480 * at first, but it's really more what we mean when we say
8482 * Another strangeness: When standing on the end of a word
8483 * "ce" will change until the end of the next wordt, but "cw"
8484 * will change only one character! This is done by setting
8487 cap
->oap
->inclusive
= TRUE
;
8494 cap
->oap
->motion_type
= MCHAR
;
8495 curwin
->w_set_curswant
= TRUE
;
8497 n
= end_word(cap
->count1
, cap
->arg
, flag
, FALSE
);
8499 n
= fwd_word(cap
->count1
, cap
->arg
, cap
->oap
->op_type
!= OP_NOP
);
8501 /* Don't leave the cursor on the NUL past the end of line. Unless we
8502 * didn't move it forward. */
8503 if (lt(startpos
, curwin
->w_cursor
))
8504 adjust_cursor(cap
->oap
);
8506 if (n
== FAIL
&& cap
->oap
->op_type
== OP_NOP
)
8507 clearopbeep(cap
->oap
);
8511 adjust_for_sel(cap
);
8514 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8521 * Used after a movement command: If the cursor ends up on the NUL after the
8522 * end of the line, may move it back to the last character and make the motion
8529 /* The cursor cannot remain on the NUL when:
8530 * - the column is > 0
8531 * - not in Visual mode or 'selection' is "o"
8532 * - 'virtualedit' is not "all" and not "onemore".
8534 if (curwin
->w_cursor
.col
> 0 && gchar_cursor() == NUL
8536 && (!VIsual_active
|| *p_sel
== 'o')
8538 #ifdef FEAT_VIRTUALEDIT
8539 && !virtual_active() && (ve_flags
& VE_ONEMORE
) == 0
8543 --curwin
->w_cursor
.col
;
8545 /* prevent cursor from moving on the trail byte */
8549 oap
->inclusive
= TRUE
;
8554 * "0" and "^" commands.
8555 * cap->arg is the argument for beginline().
8561 cap
->oap
->motion_type
= MCHAR
;
8562 cap
->oap
->inclusive
= FALSE
;
8563 beginline(cap
->arg
);
8565 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8568 ins_at_eol
= FALSE
; /* Don't move cursor past eol (only necessary in a
8569 one-character line). */
8574 * In exclusive Visual mode, may include the last character.
8580 if (VIsual_active
&& cap
->oap
->inclusive
&& *p_sel
== 'e'
8581 && gchar_cursor() != NUL
&& lt(VIsual
, curwin
->w_cursor
))
8588 ++curwin
->w_cursor
.col
;
8589 cap
->oap
->inclusive
= FALSE
;
8594 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8595 * Should check VIsual_mode before calling this.
8596 * Returns TRUE when backed up to the previous line.
8603 if (*p_sel
== 'e' && !equalpos(VIsual
, curwin
->w_cursor
))
8605 if (lt(VIsual
, curwin
->w_cursor
))
8606 pp
= &curwin
->w_cursor
;
8609 #ifdef FEAT_VIRTUALEDIT
8621 else if (pp
->lnum
> 1)
8624 pp
->col
= (colnr_T
)STRLEN(ml_get(pp
->lnum
));
8632 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8639 VIsual_select
= TRUE
;
8640 else if (VIsual_reselect
)
8642 cap
->nchar
= 'v'; /* fake "gv" command */
8651 * "G", "gg", CTRL-END, CTRL-HOME.
8652 * cap->arg is TRUE for "G".
8661 lnum
= curbuf
->b_ml
.ml_line_count
;
8664 cap
->oap
->motion_type
= MLINE
;
8667 /* When a count is given, use it instead of the default lnum */
8668 if (cap
->count0
!= 0)
8672 else if (lnum
> curbuf
->b_ml
.ml_line_count
)
8673 lnum
= curbuf
->b_ml
.ml_line_count
;
8674 curwin
->w_cursor
.lnum
= lnum
;
8675 beginline(BL_SOL
| BL_FIX
);
8677 if ((fdo_flags
& FDO_JUMP
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8683 * CTRL-\ in Normal mode.
8689 if (cap
->nchar
== Ctrl_N
|| cap
->nchar
== Ctrl_G
)
8692 if (restart_edit
!= 0 && mode_displayed
)
8693 clear_cmdline
= TRUE
; /* unshow mode later */
8696 if (cmdwin_type
!= 0)
8697 cmdwin_result
= Ctrl_C
;
8702 end_visual_mode(); /* stop Visual */
8703 redraw_curbuf_later(INVERTED
);
8706 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8707 if (cap
->nchar
== Ctrl_G
&& p_im
)
8711 clearopbeep(cap
->oap
);
8715 * ESC in Normal mode: beep, but don't flush buffers.
8716 * Don't even beep if we are canceling a command.
8724 no_reason
= (cap
->oap
->op_type
== OP_NOP
8725 && cap
->opcount
== 0
8727 && cap
->oap
->regname
== 0
8730 if (cap
->arg
) /* TRUE for CTRL-C */
8732 if (restart_edit
== 0
8740 MSG(_("Type :quit<Enter> to exit Vim"));
8742 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8743 * set again below when halfway a mapping. */
8747 if (cmdwin_type
!= 0)
8749 cmdwin_result
= K_IGNORE
;
8750 got_int
= FALSE
; /* don't stop executing autocommands et al. */
8759 end_visual_mode(); /* stop Visual */
8760 check_cursor_col(); /* make sure cursor is not beyond EOL */
8761 curwin
->w_set_curswant
= TRUE
;
8762 redraw_curbuf_later(INVERTED
);
8770 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8771 * set return to Insert mode afterwards. */
8772 if (restart_edit
== 0 && goto_im()
8773 #ifdef FEAT_EX_EXTRA
8774 && ex_normal_busy
== 0
8781 * Handle "A", "a", "I", "i" and <Insert> commands.
8787 /* <Insert> is equal to "i" */
8788 if (cap
->cmdchar
== K_INS
|| cap
->cmdchar
== K_KINS
)
8792 /* in Visual mode "A" and "I" are an operator */
8793 if (VIsual_active
&& (cap
->cmdchar
== 'A' || cap
->cmdchar
== 'I'))
8796 /* in Visual mode and after an operator "a" and "i" are for text objects */
8799 if ((cap
->cmdchar
== 'a' || cap
->cmdchar
== 'i')
8800 && (cap
->oap
->op_type
!= OP_NOP
8809 clearopbeep(cap
->oap
);
8812 else if (!curbuf
->b_p_ma
&& !p_im
)
8814 /* Only give this error when 'insertmode' is off. */
8815 EMSG(_(e_modifiable
));
8818 else if (!checkclearopq(cap
->oap
))
8820 switch (cap
->cmdchar
)
8822 case 'A': /* "A"ppend after the line */
8823 curwin
->w_set_curswant
= TRUE
;
8824 #ifdef FEAT_VIRTUALEDIT
8825 if (ve_flags
== VE_ALL
)
8827 int save_State
= State
;
8829 /* Pretent Insert mode here to allow the cursor on the
8830 * character past the end of the line */
8832 coladvance((colnr_T
)MAXCOL
);
8837 curwin
->w_cursor
.col
+= (colnr_T
)STRLEN(ml_get_cursor());
8840 case 'I': /* "I"nsert before the first non-blank */
8841 if (vim_strchr(p_cpo
, CPO_INSEND
) == NULL
)
8842 beginline(BL_WHITE
);
8844 beginline(BL_WHITE
|BL_FIX
);
8847 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8848 #ifdef FEAT_VIRTUALEDIT
8849 /* increment coladd when in virtual space, increment the
8850 * column otherwise, also to append after an unprintable char */
8851 if (virtual_active()
8852 && (curwin
->w_cursor
.coladd
> 0
8853 || *ml_get_cursor() == NUL
8854 || *ml_get_cursor() == TAB
))
8855 curwin
->w_cursor
.coladd
++;
8858 if (*ml_get_cursor() != NUL
)
8863 #ifdef FEAT_VIRTUALEDIT
8864 if (curwin
->w_cursor
.coladd
&& cap
->cmdchar
!= 'A')
8866 int save_State
= State
;
8868 /* Pretent Insert mode here to allow the cursor on the
8869 * character past the end of the line */
8871 coladvance(getviscol());
8876 invoke_edit(cap
, FALSE
, cap
->cmdchar
, FALSE
);
8881 * Invoke edit() and take care of "restart_edit" and the return value.
8884 invoke_edit(cap
, repl
, cmd
, startln
)
8886 int repl
; /* "r" or "gr" command */
8890 int restart_edit_save
= 0;
8892 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8893 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8895 if (repl
|| !stuff_empty())
8896 restart_edit_save
= restart_edit
;
8898 restart_edit_save
= 0;
8900 /* Always reset "restart_edit", this is not a restarted edit. */
8903 if (edit(cmd
, startln
, cap
->count1
))
8904 cap
->retval
|= CA_COMMAND_BUSY
;
8906 if (restart_edit
== 0)
8907 restart_edit
= restart_edit_save
;
8912 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8922 if (cap
->cmdchar
== 'i')
8923 include
= FALSE
; /* "ix" = inner object: exclude white space */
8925 include
= TRUE
; /* "ax" = an object: include white space */
8927 /* Make sure (), [], {} and <> are in 'matchpairs' */
8928 mps_save
= curbuf
->b_p_mps
;
8929 curbuf
->b_p_mps
= (char_u
*)"(:),{:},[:],<:>";
8933 case 'w': /* "aw" = a word */
8934 flag
= current_word(cap
->oap
, cap
->count1
, include
, FALSE
);
8936 case 'W': /* "aW" = a WORD */
8937 flag
= current_word(cap
->oap
, cap
->count1
, include
, TRUE
);
8939 case 'b': /* "ab" = a braces block */
8942 flag
= current_block(cap
->oap
, cap
->count1
, include
, '(', ')');
8944 case 'B': /* "aB" = a Brackets block */
8947 flag
= current_block(cap
->oap
, cap
->count1
, include
, '{', '}');
8949 case '[': /* "a[" = a [] block */
8951 flag
= current_block(cap
->oap
, cap
->count1
, include
, '[', ']');
8953 case '<': /* "a<" = a <> block */
8955 flag
= current_block(cap
->oap
, cap
->count1
, include
, '<', '>');
8957 case 't': /* "at" = a tag block (xml and html) */
8958 flag
= current_tagblock(cap
->oap
, cap
->count1
, include
);
8960 case 'p': /* "ap" = a paragraph */
8961 flag
= current_par(cap
->oap
, cap
->count1
, include
, 'p');
8963 case 's': /* "as" = a sentence */
8964 flag
= current_sent(cap
->oap
, cap
->count1
, include
);
8966 case '"': /* "a"" = a double quoted string */
8967 case '\'': /* "a'" = a single quoted string */
8968 case '`': /* "a`" = a backtick quoted string */
8969 flag
= current_quote(cap
->oap
, cap
->count1
, include
,
8973 case 'S': /* "aS" = a section */
8974 case 'f': /* "af" = a filename */
8975 case 'u': /* "au" = a URL */
8982 curbuf
->b_p_mps
= mps_save
;
8984 clearopbeep(cap
->oap
);
8985 adjust_cursor_col();
8986 curwin
->w_set_curswant
= TRUE
;
8991 * "q" command: Start/stop recording.
8992 * "q:", "q/", "q?": edit command-line in command-line window.
8998 if (cap
->oap
->op_type
== OP_FORMAT
)
9000 /* "gqq" is the same as "gqgq": format line */
9005 else if (!checkclearop(cap
->oap
))
9008 if (cap
->nchar
== ':' || cap
->nchar
== '/' || cap
->nchar
== '?')
9010 stuffcharReadbuff(cap
->nchar
);
9011 stuffcharReadbuff(K_CMDWIN
);
9015 /* (stop) recording into a named register, unless executing a
9017 if (!Exec_reg
&& do_record(cap
->nchar
) == FAIL
)
9018 clearopbeep(cap
->oap
);
9023 * Handle the "@r" command.
9029 if (checkclearop(cap
->oap
))
9032 if (cap
->nchar
== '=')
9034 if (get_expr_register() == NUL
)
9038 while (cap
->count1
-- && !got_int
)
9040 if (do_execreg(cap
->nchar
, FALSE
, FALSE
, FALSE
) == FAIL
)
9042 clearopbeep(cap
->oap
);
9050 * Handle the CTRL-U and CTRL-D commands.
9056 if ((cap
->cmdchar
== Ctrl_U
&& curwin
->w_cursor
.lnum
== 1)
9057 || (cap
->cmdchar
== Ctrl_D
9058 && curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
))
9059 clearopbeep(cap
->oap
);
9060 else if (!checkclearop(cap
->oap
))
9061 halfpage(cap
->cmdchar
== Ctrl_D
, cap
->count0
);
9065 * Handle "J" or "gJ" command.
9072 if (VIsual_active
) /* join the visual lines */
9076 if (!checkclearop(cap
->oap
))
9078 if (cap
->count0
<= 1)
9079 cap
->count0
= 2; /* default for join is two lines! */
9080 if (curwin
->w_cursor
.lnum
+ cap
->count0
- 1 >
9081 curbuf
->b_ml
.ml_line_count
)
9082 clearopbeep(cap
->oap
); /* beyond last line */
9085 prep_redo(cap
->oap
->regname
, cap
->count0
,
9086 NUL
, cap
->cmdchar
, NUL
, NUL
, cap
->nchar
);
9087 do_do_join(cap
->count0
, cap
->nchar
== NUL
);
9093 * "P", "gP", "p" and "gp" commands.
9101 void *reg1
= NULL
, *reg2
= NULL
;
9103 int was_visual
= FALSE
;
9108 if (cap
->oap
->op_type
!= OP_NOP
)
9111 /* "dp" is ":diffput" */
9112 if (cap
->oap
->op_type
== OP_DELETE
&& cap
->cmdchar
== 'p')
9115 nv_diffgetput(TRUE
);
9119 clearopbeep(cap
->oap
);
9123 dir
= (cap
->cmdchar
== 'P'
9124 || (cap
->cmdchar
== 'g' && cap
->nchar
== 'P'))
9125 ? BACKWARD
: FORWARD
;
9127 if (cap
->cmdchar
== 'g')
9128 flags
|= PUT_CURSEND
;
9133 /* Putting in Visual mode: The put text replaces the selected
9134 * text. First delete the selected text, then put the new text.
9135 * Need to save and restore the registers that the delete
9136 * overwrites if the old contents is being put.
9139 regname
= cap
->oap
->regname
;
9140 # ifdef FEAT_CLIPBOARD
9141 adjust_clip_reg(®name
);
9143 if (regname
== 0 || VIM_ISDIGIT(regname
)
9144 # ifdef FEAT_CLIPBOARD
9145 || (clip_unnamed
&& (regname
== '*' || regname
== '+'))
9150 /* the delete is going to overwrite the register we want to
9151 * put, save it first. */
9152 reg1
= get_register(regname
, TRUE
);
9155 /* Now delete the selected text. */
9158 cap
->oap
->regname
= NUL
;
9160 do_pending_operator(cap
, 0, FALSE
);
9161 empty
= (curbuf
->b_ml
.ml_flags
& ML_EMPTY
);
9163 /* delete PUT_LINE_BACKWARD; */
9164 cap
->oap
->regname
= regname
;
9168 /* Delete probably changed the register we want to put, save
9169 * it first. Then put back what was there before the delete. */
9170 reg2
= get_register(regname
, FALSE
);
9171 put_register(regname
, reg1
);
9174 /* When deleted a linewise Visual area, put the register as
9175 * lines to avoid it joined with the next line. When deletion was
9176 * characterwise, split a line when putting lines. */
9177 if (VIsual_mode
== 'V')
9179 else if (VIsual_mode
== 'v')
9180 flags
|= PUT_LINE_SPLIT
;
9181 if (VIsual_mode
== Ctrl_V
&& dir
== FORWARD
)
9182 flags
|= PUT_LINE_FORWARD
;
9184 if ((VIsual_mode
!= 'V'
9185 && curwin
->w_cursor
.col
< curbuf
->b_op_start
.col
)
9186 || (VIsual_mode
== 'V'
9187 && curwin
->w_cursor
.lnum
< curbuf
->b_op_start
.lnum
))
9188 /* cursor is at the end of the line or end of file, put
9193 do_put(cap
->oap
->regname
, dir
, cap
->count1
, flags
);
9196 /* If a register was saved, put it back now. */
9198 put_register(regname
, reg2
);
9200 /* What to reselect with "gv"? Selecting the just put text seems to
9201 * be the most useful, since the original text was removed. */
9204 curbuf
->b_visual
.vi_start
= curbuf
->b_op_start
;
9205 curbuf
->b_visual
.vi_end
= curbuf
->b_op_end
;
9208 /* When all lines were selected and deleted do_put() leaves an empty
9209 * line that needs to be deleted now. */
9210 if (empty
&& *ml_get(curbuf
->b_ml
.ml_line_count
) == NUL
)
9212 ml_delete(curbuf
->b_ml
.ml_line_count
, TRUE
);
9214 /* If the cursor was in that line, move it to the end of the last
9216 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
9218 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
9219 coladvance((colnr_T
)MAXCOL
);
9223 auto_format(FALSE
, TRUE
);
9228 * "o" and "O" commands.
9235 /* "do" is ":diffget" */
9236 if (cap
->oap
->op_type
== OP_DELETE
&& cap
->cmdchar
== 'o')
9239 nv_diffgetput(FALSE
);
9244 if (VIsual_active
) /* switch start and end of visual */
9245 v_swap_corners(cap
->cmdchar
);
9257 ProcessSniffRequests();
9261 #ifdef FEAT_NETBEANS_INTG
9266 netbeans_keycommand(cap
->nchar
);
9276 do_put('~', BACKWARD
, 1L, PUT_CURSEND
);
9282 * Trigger CursorHold event.
9283 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9284 * input buffer. "did_cursorhold" is set to avoid retriggering.
9291 apply_autocmds(EVENT_CURSORHOLD
, NULL
, NULL
, FALSE
, curbuf
);
9292 did_cursorhold
= TRUE
;
9293 cap
->retval
|= CA_COMMAND_BUSY
; /* don't call edit() now */