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
< (int)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
< (int)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.
565 normal_cmd(oap
, toplevel
)
567 int toplevel UNUSED
; /* TRUE when called from main() */
569 cmdarg_T ca
; /* command arguments */
571 int ctrl_w
= FALSE
; /* got CTRL-W command */
572 int old_col
= curwin
->w_curswant
;
573 #ifdef FEAT_CMDL_INFO
574 int need_flushbuf
; /* need to call out_flush() */
577 pos_T old_pos
; /* cursor position before command */
579 static int old_mapped_len
= 0;
583 int set_prevcount
= FALSE
;
586 vim_memset(&ca
, 0, sizeof(ca
)); /* also resets ca.retval */
589 /* Use a count remembered from before entering an operator. After typing
590 * "3d" we return from normal_cmd() and come back here, the "3" is
591 * remembered in "opcount". */
592 ca
.opcount
= opcount
;
595 want_sniff_request
= sniff_connected
;
599 * If there is an operator pending, then the command we take this time
600 * will terminate it. Finish_op tells us to finish the operation before
601 * returning this time (unless the operation was cancelled).
606 finish_op
= (oap
->op_type
!= OP_NOP
);
610 ui_cursor_shape(); /* may show different cursor shape */
611 # ifdef FEAT_MOUSESHAPE
612 update_mouseshape(-1);
617 /* When not finishing an operator and no register name typed, reset the
619 if (!finish_op
&& !oap
->regname
)
623 set_prevcount
= TRUE
;
628 /* Restore counts from before receiving K_CURSORHOLD. This means after
629 * typing "3", handling K_CURSORHOLD and then typing "2" we get "32", not
631 if (oap
->prev_opcount
> 0 || oap
->prev_count0
> 0)
633 ca
.opcount
= oap
->prev_opcount
;
634 ca
.count0
= oap
->prev_count0
;
635 oap
->prev_opcount
= 0;
636 oap
->prev_count0
= 0;
641 mapped_len
= typebuf_maplen();
645 #ifdef USE_ON_FLY_SCROLL
646 dont_scroll
= FALSE
; /* allow scrolling here */
650 * Get the command character from the user.
653 LANGMAP_ADJUST(c
, TRUE
);
657 * If a mapping was started in Visual or Select mode, remember the length
658 * of the mapping. This is used below to not return to Insert mode for as
659 * long as the mapping is being executed.
661 if (restart_edit
== 0)
663 else if (old_mapped_len
664 || (VIsual_active
&& mapped_len
== 0 && typebuf_maplen() > 0))
665 old_mapped_len
= typebuf_maplen();
673 * In Select mode, typed text replaces the selection.
677 && (vim_isprintc(c
) || c
== NL
|| c
== CAR
|| c
== K_KENTER
))
679 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
680 * 'insertmode' is set) fake a "d"elete command, Insert mode will
681 * restart automatically.
682 * Insert the typed character in the typeahead buffer, so that it can
683 * be mapped in Insert mode. Required for ":lmap" to work. */
685 if (restart_edit
!= 0)
689 msg_nowait
= TRUE
; /* don't delay going to insert mode */
693 #ifdef FEAT_CMDL_INFO
694 need_flushbuf
= add_to_showcmd(c
);
699 if (!(VIsual_active
&& VIsual_select
))
703 * Handle a count before a command and compute ca.count0.
704 * Note that '0' is a command and not the start of a count, but it's
705 * part of a count after other digits.
707 while ( (c
>= '1' && c
<= '9')
708 || (ca
.count0
!= 0 && (c
== K_DEL
|| c
== K_KDEL
|| c
== '0')))
710 if (c
== K_DEL
|| c
== K_KDEL
)
713 #ifdef FEAT_CMDL_INFO
714 del_from_showcmd(4); /* delete the digit and ~@% */
718 ca
.count0
= ca
.count0
* 10 + (c
- '0');
719 if (ca
.count0
< 0) /* got too large! */
720 ca
.count0
= 999999999L;
722 /* Set v:count here, when called from main() and not a stuffed
723 * command, so that v:count can be used in an expression mapping
724 * right after the count. */
725 if (toplevel
&& stuff_empty())
727 long count
= ca
.count0
;
729 /* multiply with ca.opcount the same way as below */
731 count
= ca
.opcount
* (count
== 0 ? 1 : count
);
732 set_vcount(count
, count
== 0 ? 1 : count
, set_prevcount
);
733 set_prevcount
= FALSE
; /* only set v:prevcount once */
739 ++allow_keys
; /* no mapping for nchar, but keys */
741 ++no_zero_mapping
; /* don't map zero here */
743 LANGMAP_ADJUST(c
, TRUE
);
750 #ifdef FEAT_CMDL_INFO
751 need_flushbuf
|= add_to_showcmd(c
);
756 * If we got CTRL-W there may be a/another count
758 if (c
== Ctrl_W
&& !ctrl_w
&& oap
->op_type
== OP_NOP
)
761 ca
.opcount
= ca
.count0
; /* remember first count */
764 ++allow_keys
; /* no mapping for nchar, but keys */
765 c
= plain_vgetc(); /* get next character */
766 LANGMAP_ADJUST(c
, TRUE
);
769 #ifdef FEAT_CMDL_INFO
770 need_flushbuf
|= add_to_showcmd(c
);
772 goto getcount
; /* jump back */
777 if (c
== K_CURSORHOLD
)
779 /* Save the count values so that ca.opcount and ca.count0 are exactly
780 * the same when coming back here after handling K_CURSORHOLD. */
781 oap
->prev_opcount
= ca
.opcount
;
782 oap
->prev_count0
= ca
.count0
;
789 * If we're in the middle of an operator (including after entering a
790 * yank buffer with '"') AND we had a count before the operator, then
791 * that count overrides the current value of ca.count0.
792 * What this means effectively, is that commands like "3dw" get turned
793 * into "d3w" which makes things fall into place pretty neatly.
794 * If you give a count before AND after the operator, they are
798 ca
.count0
*= ca
.opcount
;
800 ca
.count0
= ca
.opcount
;
804 * Always remember the count. It will be set to zero (on the next call,
805 * above) when there is no pending operator.
806 * When called from main(), save the count for use by the "count" built-in
809 ca
.opcount
= ca
.count0
;
810 ca
.count1
= (ca
.count0
== 0 ? 1 : ca
.count0
);
814 * Only set v:count when called from main() and not a stuffed command.
816 if (toplevel
&& stuff_empty())
817 set_vcount(ca
.count0
, ca
.count1
, set_prevcount
);
821 * Find the command character in the table of commands.
822 * For CTRL-W we already got nchar when looking for a count.
831 idx
= find_command(ca
.cmdchar
);
834 /* Not a known command: beep. */
839 if (text_locked() && (nv_cmds
[idx
].cmd_flags
& NV_NCW
))
841 /* This command is not allowed while editing a ccmdline: beep. */
847 if ((nv_cmds
[idx
].cmd_flags
& NV_NCW
) && curbuf_locked())
853 * In Visual/Select mode, a few keys are handled in a special way.
857 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
859 && (nv_cmds
[idx
].cmd_flags
& NV_STS
)
860 && !(mod_mask
& MOD_MASK_SHIFT
))
863 redraw_curbuf_later(INVERTED
);
866 /* Keys that work different when 'keymodel' contains "startsel" */
869 if (nv_cmds
[idx
].cmd_flags
& NV_SS
)
871 unshift_special(&ca
);
872 idx
= find_command(ca
.cmdchar
);
880 else if ((nv_cmds
[idx
].cmd_flags
& NV_SSS
)
881 && (mod_mask
& MOD_MASK_SHIFT
))
883 mod_mask
&= ~MOD_MASK_SHIFT
;
889 #ifdef FEAT_RIGHTLEFT
890 if (curwin
->w_p_rl
&& KeyTyped
&& !KeyStuffed
891 && (nv_cmds
[idx
].cmd_flags
& NV_RL
))
893 /* Invert horizontal movements and operations. Only when typed by the
894 * user directly, not when the result of a mapping or "x" translated
898 case 'l': ca
.cmdchar
= 'h'; break;
899 case K_RIGHT
: ca
.cmdchar
= K_LEFT
; break;
900 case K_S_RIGHT
: ca
.cmdchar
= K_S_LEFT
; break;
901 case K_C_RIGHT
: ca
.cmdchar
= K_C_LEFT
; break;
902 case 'h': ca
.cmdchar
= 'l'; break;
903 case K_LEFT
: ca
.cmdchar
= K_RIGHT
; break;
904 case K_S_LEFT
: ca
.cmdchar
= K_S_RIGHT
; break;
905 case K_C_LEFT
: ca
.cmdchar
= K_C_RIGHT
; break;
906 case '>': ca
.cmdchar
= '<'; break;
907 case '<': ca
.cmdchar
= '>'; break;
909 idx
= find_command(ca
.cmdchar
);
914 * Get an additional character if we need one.
916 if ((nv_cmds
[idx
].cmd_flags
& NV_NCH
)
917 && (((nv_cmds
[idx
].cmd_flags
& NV_NCH_NOP
) == NV_NCH_NOP
918 && oap
->op_type
== OP_NOP
)
919 || (nv_cmds
[idx
].cmd_flags
& NV_NCH_ALW
) == NV_NCH_ALW
920 || (ca
.cmdchar
== 'q'
921 && oap
->op_type
== OP_NOP
924 || ((ca
.cmdchar
== 'a' || ca
.cmdchar
== 'i')
925 && (oap
->op_type
!= OP_NOP
932 int repl
= FALSE
; /* get character for replace mode */
933 int lit
= FALSE
; /* get extra character literally */
934 int langmap_active
= FALSE
; /* using :lmap mappings */
935 int lang
; /* getting a text character */
936 #ifdef USE_IM_CONTROL
937 int save_smd
; /* saved value of p_smd */
941 ++allow_keys
; /* no mapping for nchar, but allow key codes */
943 /* Don't generate a CursorHold event here, most commands can't handle
944 * it, e.g., nv_replace(), nv_csearch(). */
945 did_cursorhold
= TRUE
;
947 if (ca
.cmdchar
== 'g')
950 * For 'g' get the next character now, so that we can check for
951 * "gr", "g'" and "g`".
953 ca
.nchar
= plain_vgetc();
954 LANGMAP_ADJUST(ca
.nchar
, TRUE
);
955 #ifdef FEAT_CMDL_INFO
956 need_flushbuf
|= add_to_showcmd(ca
.nchar
);
958 if (ca
.nchar
== 'r' || ca
.nchar
== '\'' || ca
.nchar
== '`'
959 || ca
.nchar
== Ctrl_BSL
)
961 cp
= &ca
.extra_char
; /* need to get a third character */
963 lit
= TRUE
; /* get it literally */
965 repl
= TRUE
; /* get it in replace mode */
968 cp
= NULL
; /* no third character needed */
972 if (ca
.cmdchar
== 'r') /* get it in replace mode */
976 lang
= (repl
|| (nv_cmds
[idx
].cmd_flags
& NV_LANG
));
979 * Get a second or third character.
986 State
= REPLACE
; /* pretend Replace mode */
987 ui_cursor_shape(); /* show different cursor shape */
990 if (lang
&& curbuf
->b_p_iminsert
== B_IMODE_LMAP
)
992 /* Allow mappings defined with ":lmap". */
999 langmap_active
= TRUE
;
1001 #ifdef USE_IM_CONTROL
1003 p_smd
= FALSE
; /* Don't let the IM code show the mode here */
1004 if (lang
&& curbuf
->b_p_iminsert
== B_IMODE_IM
)
1005 im_set_active(TRUE
);
1008 *cp
= plain_vgetc();
1012 /* Undo the decrement done above */
1015 State
= NORMAL_BUSY
;
1017 #ifdef USE_IM_CONTROL
1020 if (curbuf
->b_p_iminsert
!= B_IMODE_LMAP
)
1021 im_save_status(&curbuf
->b_p_iminsert
);
1022 im_set_active(FALSE
);
1027 State
= NORMAL_BUSY
;
1029 #ifdef FEAT_CMDL_INFO
1030 need_flushbuf
|= add_to_showcmd(*cp
);
1035 #ifdef FEAT_DIGRAPHS
1036 /* Typing CTRL-K gets a digraph. */
1038 && ((nv_cmds
[idx
].cmd_flags
& NV_LANG
)
1039 || cp
== &ca
.extra_char
)
1040 && vim_strchr(p_cpo
, CPO_DIGRAPH
) == NULL
)
1042 c
= get_digraph(FALSE
);
1046 # ifdef FEAT_CMDL_INFO
1047 /* Guessing how to update showcmd here... */
1048 del_from_showcmd(3);
1049 need_flushbuf
|= add_to_showcmd(*cp
);
1055 /* adjust chars > 127, except after "tTfFr" commands */
1056 LANGMAP_ADJUST(*cp
, !lang
);
1057 #ifdef FEAT_RIGHTLEFT
1058 /* adjust Hebrew mapped char */
1059 if (p_hkmap
&& lang
&& KeyTyped
)
1062 /* adjust Farsi mapped char */
1063 if (p_fkmap
&& lang
&& KeyTyped
)
1070 * When the next character is CTRL-\ a following CTRL-N means the
1071 * command is aborted and we go to Normal mode.
1073 if (cp
== &ca
.extra_char
1074 && ca
.nchar
== Ctrl_BSL
1075 && (ca
.extra_char
== Ctrl_N
|| ca
.extra_char
== Ctrl_G
))
1077 ca
.cmdchar
= Ctrl_BSL
;
1078 ca
.nchar
= ca
.extra_char
;
1079 idx
= find_command(ca
.cmdchar
);
1081 else if (*cp
== Ctrl_BSL
)
1083 long towait
= (p_ttm
>= 0 ? p_ttm
: p_tm
);
1085 /* There is a busy wait here when typing "f<C-\>" and then
1086 * something different from CTRL-N. Can't be avoided. */
1087 while ((c
= vpeekc()) <= 0 && towait
> 0L)
1089 do_sleep(towait
> 50L ? 50L : towait
);
1095 if (c
!= Ctrl_N
&& c
!= Ctrl_G
)
1099 ca
.cmdchar
= Ctrl_BSL
;
1101 idx
= find_command(ca
.cmdchar
);
1107 /* When getting a text character and the next character is a
1108 * multi-byte character, it could be a composing character.
1109 * However, don't wait for it to arrive. */
1110 while (enc_utf8
&& lang
&& (c
= vpeekc()) > 0
1111 && (c
>= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1114 if (!utf_iscomposing(c
))
1116 vungetc(c
); /* it wasn't, put it back */
1119 else if (ca
.ncharC1
== 0)
1130 #ifdef FEAT_CMDL_INFO
1132 * Flush the showcmd characters onto the screen so we can see them while
1133 * the command is being executed. Only do this when the shown command was
1134 * actually displayed, otherwise this will slow down a lot when executing
1141 if (ca
.cmdchar
!= K_IGNORE
)
1142 did_cursorhold
= FALSE
;
1147 if (ca
.nchar
== ESC
)
1150 if (restart_edit
== 0 && goto_im())
1155 if (ca
.cmdchar
!= K_IGNORE
)
1157 msg_didout
= FALSE
; /* don't scroll screen up for normal command */
1162 old_pos
= curwin
->w_cursor
; /* remember where cursor was */
1164 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1166 if (!VIsual_active
&& km_startsel
)
1168 if (nv_cmds
[idx
].cmd_flags
& NV_SS
)
1171 unshift_special(&ca
);
1172 idx
= find_command(ca
.cmdchar
);
1174 else if ((nv_cmds
[idx
].cmd_flags
& NV_SSS
)
1175 && (mod_mask
& MOD_MASK_SHIFT
))
1178 mod_mask
&= ~MOD_MASK_SHIFT
;
1184 * Execute the command!
1185 * Call the command function found in the commands table.
1187 ca
.arg
= nv_cmds
[idx
].cmd_arg
;
1188 (nv_cmds
[idx
].cmd_func
)(&ca
);
1191 * If we didn't start or finish an operator, reset oap->regname, unless we
1196 && (idx
< 0 || !(nv_cmds
[idx
].cmd_flags
& NV_KEEPREG
)))
1205 /* Get the length of mapped chars again after typing a count, second
1206 * character or "z333<cr>". */
1207 if (old_mapped_len
> 0)
1208 old_mapped_len
= typebuf_maplen();
1212 * If an operation is pending, handle it...
1214 do_pending_operator(&ca
, old_col
, FALSE
);
1217 * Wait for a moment when a message is displayed that will be overwritten
1218 * by the mode message.
1219 * In Visual mode and with "^O" in Insert mode, a short message will be
1220 * overwritten by the mode message. Wait a bit, until a key is hit.
1221 * In Visual mode, it's more important to keep the Visual area updated
1222 * than keeping a message (e.g. from a /pat search).
1223 * Only do this if the command was typed, not from a mapping.
1224 * Don't wait when emsg_silent is non-zero.
1225 * Also wait a bit after an error message, e.g. for "^O:".
1226 * Don't redraw the screen, it would remove the message.
1230 && (restart_edit
!= 0
1233 && old_pos
.lnum
== curwin
->w_cursor
.lnum
1234 && old_pos
.col
== curwin
->w_cursor
.col
)
1239 && (msg_didout
|| (msg_didany
&& msg_scroll
))
1242 || (restart_edit
!= 0
1247 || emsg_on_display
)))
1248 && oap
->regname
== 0
1249 && !(ca
.retval
& CA_COMMAND_BUSY
)
1254 && oap
->op_type
== OP_NOP
)
1256 int save_State
= State
;
1258 /* Draw the cursor with the right shape here */
1259 if (restart_edit
!= 0)
1262 /* If need to redraw, and there is a "keep_msg", redraw before the
1264 if (must_redraw
&& keep_msg
!= NULL
&& !emsg_on_display
)
1270 /* showmode() will clear keep_msg, but we want to use it anyway */
1272 /* now reset it, otherwise it's put in the history again */
1274 msg_attr(kmsg
, keep_msg_attr
);
1280 if (msg_scroll
|| emsg_on_display
)
1281 ui_delay(1000L, TRUE
); /* wait at least one second */
1282 ui_delay(3000L, FALSE
); /* wait up to three seconds */
1286 emsg_on_display
= FALSE
;
1290 * Finish up after executing a Normal mode command.
1296 /* Reset finish_op, in case it was set */
1302 /* Redraw the cursor with another shape, if we were in Operator-pending
1303 * mode or did a replace command. */
1304 if (c
|| ca
.cmdchar
== 'r')
1306 ui_cursor_shape(); /* may show different cursor shape */
1307 # ifdef FEAT_MOUSESHAPE
1308 update_mouseshape(-1);
1313 #ifdef FEAT_CMDL_INFO
1314 if (oap
->op_type
== OP_NOP
&& oap
->regname
== 0
1315 # ifdef FEAT_AUTOCMD
1316 && ca
.cmdchar
!= K_CURSORHOLD
1322 checkpcmark(); /* check if we moved since setting pcmark */
1323 vim_free(ca
.searchbuf
);
1330 #ifdef FEAT_SCROLLBIND
1331 if (curwin
->w_p_scb
&& toplevel
)
1333 validate_cursor(); /* may need to update w_leftcol */
1334 do_check_scrollbind(TRUE
);
1339 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1340 * if still inside a mapping that started in Visual mode).
1341 * May switch from Visual to Select mode after CTRL-O command.
1343 if ( oap
->op_type
== OP_NOP
1345 && ((restart_edit
!= 0 && !VIsual_active
&& old_mapped_len
== 0)
1346 || restart_VIsual_select
== 1)
1348 && restart_edit
!= 0
1350 && !(ca
.retval
& CA_COMMAND_BUSY
)
1352 && oap
->regname
== 0)
1355 if (restart_VIsual_select
== 1)
1357 VIsual_select
= TRUE
;
1359 restart_VIsual_select
= 0;
1362 if (restart_edit
!= 0
1364 && !VIsual_active
&& old_mapped_len
== 0
1367 (void)edit(restart_edit
, FALSE
, 1L);
1371 if (restart_VIsual_select
== 2)
1372 restart_VIsual_select
= 1;
1375 /* Save count before an operator for next time. */
1376 opcount
= ca
.opcount
;
1380 * Handle an operator after visual mode or when the movement is finished
1383 do_pending_operator(cap
, old_col
, gui_yank
)
1388 oparg_T
*oap
= cap
->oap
;
1390 int empty_region_error
;
1391 int restart_edit_save
;
1394 /* The visual area is remembered for redo */
1395 static int redo_VIsual_mode
= NUL
; /* 'v', 'V', or Ctrl-V */
1396 static linenr_T redo_VIsual_line_count
; /* number of lines */
1397 static colnr_T redo_VIsual_col
; /* number of cols or end column */
1398 static long redo_VIsual_count
; /* count for Visual operator */
1399 # ifdef FEAT_VIRTUALEDIT
1400 int include_line_break
= FALSE
;
1404 #if defined(FEAT_CLIPBOARD)
1406 * Yank the visual area into the GUI selection register before we operate
1407 * on it and lose it forever.
1408 * Don't do it if a specific register was specified, so that ""x"*P works.
1409 * This could call do_pending_operator() recursively, but that's OK
1410 * because gui_yank will be TRUE for the nested call.
1412 if (clip_star
.available
1413 && oap
->op_type
!= OP_NOP
1417 && !redo_VIsual_busy
1419 && oap
->regname
== 0)
1422 old_cursor
= curwin
->w_cursor
;
1425 * If an operation is pending, handle it...
1431 ) && oap
->op_type
!= OP_NOP
)
1434 oap
->is_VIsual
= VIsual_active
;
1435 if (oap
->motion_force
== 'V')
1436 oap
->motion_type
= MLINE
;
1437 else if (oap
->motion_force
== 'v')
1439 /* If the motion was linewise, "inclusive" will not have been set.
1440 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1441 if (oap
->motion_type
== MLINE
)
1442 oap
->inclusive
= FALSE
;
1443 /* If the motion already was characterwise, toggle "inclusive" */
1444 else if (oap
->motion_type
== MCHAR
)
1445 oap
->inclusive
= !oap
->inclusive
;
1446 oap
->motion_type
= MCHAR
;
1448 else if (oap
->motion_force
== Ctrl_V
)
1450 /* Change line- or characterwise motion into Visual block mode. */
1451 VIsual_active
= TRUE
;
1452 VIsual
= oap
->start
;
1453 VIsual_mode
= Ctrl_V
;
1454 VIsual_select
= FALSE
;
1455 VIsual_reselect
= FALSE
;
1459 /* only redo yank when 'y' flag is in 'cpoptions' */
1460 /* never redo "zf" (define fold) */
1461 if ((vim_strchr(p_cpo
, CPO_YANK
) != NULL
|| oap
->op_type
!= OP_YANK
)
1463 && (!VIsual_active
|| oap
->motion_force
)
1465 && cap
->cmdchar
!= 'D'
1467 && oap
->op_type
!= OP_FOLD
1468 && oap
->op_type
!= OP_FOLDOPEN
1469 && oap
->op_type
!= OP_FOLDOPENREC
1470 && oap
->op_type
!= OP_FOLDCLOSE
1471 && oap
->op_type
!= OP_FOLDCLOSEREC
1472 && oap
->op_type
!= OP_FOLDDEL
1473 && oap
->op_type
!= OP_FOLDDELREC
1477 prep_redo(oap
->regname
, cap
->count0
,
1478 get_op_char(oap
->op_type
), get_extra_op_char(oap
->op_type
),
1479 oap
->motion_force
, cap
->cmdchar
, cap
->nchar
);
1480 if (cap
->cmdchar
== '/' || cap
->cmdchar
== '?') /* was a search */
1483 * If 'cpoptions' does not contain 'r', insert the search
1484 * pattern to really repeat the same command.
1486 if (vim_strchr(p_cpo
, CPO_REDO
) == NULL
)
1487 AppendToRedobuffLit(cap
->searchbuf
, -1);
1488 AppendToRedobuff(NL_STR
);
1490 else if (cap
->cmdchar
== ':')
1492 /* do_cmdline() has stored the first typed line in
1493 * "repeat_cmdline". When several lines are typed repeating
1494 * won't be possible. */
1495 if (repeat_cmdline
== NULL
)
1499 AppendToRedobuffLit(repeat_cmdline
, -1);
1500 AppendToRedobuff(NL_STR
);
1501 vim_free(repeat_cmdline
);
1502 repeat_cmdline
= NULL
;
1508 if (redo_VIsual_busy
)
1510 oap
->start
= curwin
->w_cursor
;
1511 curwin
->w_cursor
.lnum
+= redo_VIsual_line_count
- 1;
1512 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
1513 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
1514 VIsual_mode
= redo_VIsual_mode
;
1515 if (VIsual_mode
== 'v')
1517 if (redo_VIsual_line_count
<= 1)
1518 curwin
->w_cursor
.col
+= redo_VIsual_col
- 1;
1520 curwin
->w_cursor
.col
= redo_VIsual_col
;
1522 if (redo_VIsual_col
== MAXCOL
)
1524 curwin
->w_curswant
= MAXCOL
;
1525 coladvance((colnr_T
)MAXCOL
);
1527 cap
->count0
= redo_VIsual_count
;
1528 if (redo_VIsual_count
!= 0)
1529 cap
->count1
= redo_VIsual_count
;
1533 else if (VIsual_active
)
1537 /* Save the current VIsual area for '< and '> marks, and "gv" */
1538 curbuf
->b_visual
.vi_start
= VIsual
;
1539 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
1540 curbuf
->b_visual
.vi_mode
= VIsual_mode
;
1541 curbuf
->b_visual
.vi_curswant
= curwin
->w_curswant
;
1543 curbuf
->b_visual_mode_eval
= VIsual_mode
;
1547 /* In Select mode, a linewise selection is operated upon like a
1548 * characterwise selection. */
1549 if (VIsual_select
&& VIsual_mode
== 'V')
1551 if (lt(VIsual
, curwin
->w_cursor
))
1554 curwin
->w_cursor
.col
=
1555 (colnr_T
)STRLEN(ml_get(curwin
->w_cursor
.lnum
));
1559 curwin
->w_cursor
.col
= 0;
1560 VIsual
.col
= (colnr_T
)STRLEN(ml_get(VIsual
.lnum
));
1564 /* If 'selection' is "exclusive", backup one character for
1565 * charwise selections. */
1566 else if (VIsual_mode
== 'v')
1568 # ifdef FEAT_VIRTUALEDIT
1569 include_line_break
=
1574 oap
->start
= VIsual
;
1575 if (VIsual_mode
== 'V')
1578 #endif /* FEAT_VISUAL */
1581 * Set oap->start to the first position of the operated text, oap->end
1582 * to the end of the operated text. w_cursor is equal to oap->start.
1584 if (lt(oap
->start
, curwin
->w_cursor
))
1587 /* Include folded lines completely. */
1590 if (hasFolding(oap
->start
.lnum
, &oap
->start
.lnum
, NULL
))
1592 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
,
1593 &curwin
->w_cursor
.lnum
))
1594 curwin
->w_cursor
.col
= (colnr_T
)STRLEN(ml_get_curline());
1597 oap
->end
= curwin
->w_cursor
;
1598 curwin
->w_cursor
= oap
->start
;
1600 /* w_virtcol may have been updated; if the cursor goes back to its
1601 * previous position w_virtcol becomes invalid and isn't updated
1603 curwin
->w_valid
&= ~VALID_VIRTCOL
;
1608 /* Include folded lines completely. */
1609 if (!VIsual_active
&& oap
->motion_type
== MLINE
)
1611 if (hasFolding(curwin
->w_cursor
.lnum
, &curwin
->w_cursor
.lnum
,
1613 curwin
->w_cursor
.col
= 0;
1614 if (hasFolding(oap
->start
.lnum
, NULL
, &oap
->start
.lnum
))
1615 oap
->start
.col
= (colnr_T
)STRLEN(ml_get(oap
->start
.lnum
));
1618 oap
->end
= oap
->start
;
1619 oap
->start
= curwin
->w_cursor
;
1622 oap
->line_count
= oap
->end
.lnum
- oap
->start
.lnum
+ 1;
1624 #ifdef FEAT_VIRTUALEDIT
1625 /* Set "virtual_op" before resetting VIsual_active. */
1626 virtual_op
= virtual_active();
1630 if (VIsual_active
|| redo_VIsual_busy
)
1632 if (VIsual_mode
== Ctrl_V
) /* block mode */
1636 oap
->block_mode
= TRUE
;
1638 getvvcol(curwin
, &(oap
->start
),
1639 &oap
->start_vcol
, NULL
, &oap
->end_vcol
);
1640 if (!redo_VIsual_busy
)
1642 getvvcol(curwin
, &(oap
->end
), &start
, NULL
, &end
);
1644 if (start
< oap
->start_vcol
)
1645 oap
->start_vcol
= start
;
1646 if (end
> oap
->end_vcol
)
1648 if (*p_sel
== 'e' && start
>= 1
1649 && start
- 1 >= oap
->end_vcol
)
1650 oap
->end_vcol
= start
- 1;
1652 oap
->end_vcol
= end
;
1656 /* if '$' was used, get oap->end_vcol from longest line */
1657 if (curwin
->w_curswant
== MAXCOL
)
1659 curwin
->w_cursor
.col
= MAXCOL
;
1661 for (curwin
->w_cursor
.lnum
= oap
->start
.lnum
;
1662 curwin
->w_cursor
.lnum
<= oap
->end
.lnum
;
1663 ++curwin
->w_cursor
.lnum
)
1665 getvvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &end
);
1666 if (end
> oap
->end_vcol
)
1667 oap
->end_vcol
= end
;
1670 else if (redo_VIsual_busy
)
1671 oap
->end_vcol
= oap
->start_vcol
+ redo_VIsual_col
- 1;
1673 * Correct oap->end.col and oap->start.col to be the
1674 * upper-left and lower-right corner of the block area.
1676 * (Actually, this does convert column positions into character
1679 curwin
->w_cursor
.lnum
= oap
->end
.lnum
;
1680 coladvance(oap
->end_vcol
);
1681 oap
->end
= curwin
->w_cursor
;
1683 curwin
->w_cursor
= oap
->start
;
1684 coladvance(oap
->start_vcol
);
1685 oap
->start
= curwin
->w_cursor
;
1688 if (!redo_VIsual_busy
&& !gui_yank
)
1691 * Prepare to reselect and redo Visual: this is based on the
1692 * size of the Visual text
1694 resel_VIsual_mode
= VIsual_mode
;
1695 if (curwin
->w_curswant
== MAXCOL
)
1696 resel_VIsual_col
= MAXCOL
;
1697 else if (VIsual_mode
== Ctrl_V
)
1698 resel_VIsual_col
= oap
->end_vcol
- oap
->start_vcol
+ 1;
1699 else if (oap
->line_count
> 1)
1700 resel_VIsual_col
= oap
->end
.col
;
1702 resel_VIsual_col
= oap
->end
.col
- oap
->start
.col
+ 1;
1703 resel_VIsual_line_count
= oap
->line_count
;
1706 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1707 if ((vim_strchr(p_cpo
, CPO_YANK
) != NULL
|| oap
->op_type
!= OP_YANK
)
1708 && oap
->op_type
!= OP_COLON
1710 && oap
->op_type
!= OP_FOLD
1711 && oap
->op_type
!= OP_FOLDOPEN
1712 && oap
->op_type
!= OP_FOLDOPENREC
1713 && oap
->op_type
!= OP_FOLDCLOSE
1714 && oap
->op_type
!= OP_FOLDCLOSEREC
1715 && oap
->op_type
!= OP_FOLDDEL
1716 && oap
->op_type
!= OP_FOLDDELREC
1718 && oap
->motion_force
== NUL
1721 /* Prepare for redoing. Only use the nchar field for "r",
1722 * otherwise it might be the second char of the operator. */
1723 prep_redo(oap
->regname
, 0L, NUL
, 'v',
1724 get_op_char(oap
->op_type
),
1725 get_extra_op_char(oap
->op_type
),
1726 oap
->op_type
== OP_REPLACE
? cap
->nchar
: NUL
);
1727 if (!redo_VIsual_busy
)
1729 redo_VIsual_mode
= resel_VIsual_mode
;
1730 redo_VIsual_col
= resel_VIsual_col
;
1731 redo_VIsual_line_count
= resel_VIsual_line_count
;
1732 redo_VIsual_count
= cap
->count0
;
1737 * oap->inclusive defaults to TRUE.
1738 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1739 * FALSE. This makes "d}P" and "v}dP" work the same.
1741 if (oap
->motion_force
== NUL
|| oap
->motion_type
== MLINE
)
1742 oap
->inclusive
= TRUE
;
1743 if (VIsual_mode
== 'V')
1744 oap
->motion_type
= MLINE
;
1747 oap
->motion_type
= MCHAR
;
1748 if (VIsual_mode
!= Ctrl_V
&& *ml_get_pos(&(oap
->end
)) == NUL
1749 # ifdef FEAT_VIRTUALEDIT
1750 && (include_line_break
|| !virtual_op
)
1754 oap
->inclusive
= FALSE
;
1755 /* Try to include the newline, unless it's an operator
1756 * that works on lines only */
1758 && !op_on_lines(oap
->op_type
)
1759 && oap
->end
.lnum
< curbuf
->b_ml
.ml_line_count
)
1763 # ifdef FEAT_VIRTUALEDIT
1764 oap
->end
.coladd
= 0;
1771 redo_VIsual_busy
= FALSE
;
1774 * Switch Visual off now, so screen updating does
1775 * not show inverted text when the screen is redrawn.
1776 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1777 * no screen redraw, so it is done here to remove the inverted
1782 VIsual_active
= FALSE
;
1788 clear_cmdline
= TRUE
; /* unshow visual mode later */
1789 #ifdef FEAT_CMDL_INFO
1793 if ((oap
->op_type
== OP_YANK
1794 || oap
->op_type
== OP_COLON
1795 || oap
->op_type
== OP_FUNCTION
1796 || oap
->op_type
== OP_FILTER
)
1797 && oap
->motion_force
== NUL
)
1798 redraw_curbuf_later(INVERTED
);
1804 /* Include the trailing byte of a multi-byte char. */
1805 if (has_mbyte
&& oap
->inclusive
)
1809 l
= (*mb_ptr2len
)(ml_get_pos(&oap
->end
));
1811 oap
->end
.col
+= l
- 1;
1814 curwin
->w_set_curswant
= TRUE
;
1817 * oap->empty is set when start and end are the same. The inclusive
1818 * flag affects this too, unless yanking and the end is on a NUL.
1820 oap
->empty
= (oap
->motion_type
== MCHAR
1822 || (oap
->op_type
== OP_YANK
1823 && gchar_pos(&oap
->end
) == NUL
))
1824 && equalpos(oap
->start
, oap
->end
)
1825 #ifdef FEAT_VIRTUALEDIT
1826 && !(virtual_op
&& oap
->start
.coladd
!= oap
->end
.coladd
)
1830 * For delete, change and yank, it's an error to operate on an
1831 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1833 empty_region_error
= (oap
->empty
1834 && vim_strchr(p_cpo
, CPO_EMPTYREGION
) != NULL
);
1837 /* Force a redraw when operating on an empty Visual region, when
1838 * 'modifiable is off or creating a fold. */
1839 if (oap
->is_VIsual
&& (oap
->empty
|| !curbuf
->b_p_ma
1840 # ifdef FEAT_FOLDING
1841 || oap
->op_type
== OP_FOLD
1844 redraw_curbuf_later(INVERTED
);
1848 * If the end of an operator is in column one while oap->motion_type
1849 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1850 * character in the previous line. If op_start is on or before the
1851 * first non-blank in the line, the operator becomes linewise
1852 * (strange, but that's the way vi does it).
1854 if ( oap
->motion_type
== MCHAR
1855 && oap
->inclusive
== FALSE
1856 && !(cap
->retval
& CA_NO_ADJ_OP_END
)
1857 && oap
->end
.col
== 0
1859 && (!oap
->is_VIsual
|| *p_sel
== 'o')
1862 && oap
->line_count
> 1)
1864 oap
->end_adjusted
= TRUE
; /* remember that we did this */
1868 oap
->motion_type
= MLINE
;
1871 oap
->end
.col
= (colnr_T
)STRLEN(ml_get(oap
->end
.lnum
));
1875 oap
->inclusive
= TRUE
;
1880 oap
->end_adjusted
= FALSE
;
1882 switch (oap
->op_type
)
1888 oap
->is_VIsual
? (int)cap
->count1
:
1891 auto_format(FALSE
, TRUE
);
1896 if (oap
->line_count
< 2)
1897 oap
->line_count
= 2;
1898 if (curwin
->w_cursor
.lnum
+ oap
->line_count
- 1 >
1899 curbuf
->b_ml
.ml_line_count
)
1903 do_do_join(oap
->line_count
, oap
->op_type
== OP_JOIN
);
1904 auto_format(FALSE
, TRUE
);
1910 VIsual_reselect
= FALSE
; /* don't reselect now */
1912 if (empty_region_error
)
1916 (void)op_delete(oap
);
1917 if (oap
->motion_type
== MLINE
&& has_format_option(FO_AUTO
))
1918 u_save_cursor(); /* cursor line wasn't saved yet */
1919 auto_format(FALSE
, TRUE
);
1924 if (empty_region_error
)
1930 (void)op_yank(oap
, FALSE
, !gui_yank
);
1936 VIsual_reselect
= FALSE
; /* don't reselect now */
1938 if (empty_region_error
)
1942 /* This is a new edit command, not a restart. Need to
1943 * remember it to make 'insertmode' work with mappings for
1944 * Visual mode. But do this only once and not when typed and
1945 * 'insertmode' isn't set. */
1946 if (p_im
|| !KeyTyped
)
1947 restart_edit_save
= restart_edit
;
1949 restart_edit_save
= 0;
1951 /* Reset finish_op now, don't want it set inside edit(). */
1953 if (op_change(oap
)) /* will call edit() */
1954 cap
->retval
|= CA_COMMAND_BUSY
;
1955 if (restart_edit
== 0)
1956 restart_edit
= restart_edit_save
;
1961 if (vim_strchr(p_cpo
, CPO_FILTER
) != NULL
)
1962 AppendToRedobuff((char_u
*)"!\r"); /* use any last used !cmd */
1964 bangredo
= TRUE
; /* do_bang() will put cmd in redo buffer */
1969 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1971 * If 'equalprg' is empty, do the indenting internally.
1973 if (oap
->op_type
== OP_INDENT
&& *get_equalprg() == NUL
)
1976 if (curbuf
->b_p_lisp
)
1978 op_reindent(oap
, get_lisp_indent
);
1982 # ifdef FEAT_CINDENT
1985 *curbuf
->b_p_inde
!= NUL
? get_expr_indent
:
2000 if (empty_region_error
)
2008 #if defined(FEAT_EVAL)
2009 if (*curbuf
->b_p_fex
!= NUL
)
2010 op_formatexpr(oap
); /* use expression */
2014 op_colon(oap
); /* use external command */
2016 op_format(oap
, FALSE
); /* use internal function */
2020 op_format(oap
, TRUE
); /* use internal function */
2024 op_function(oap
); /* call 'operatorfunc' */
2030 VIsual_reselect
= FALSE
; /* don't reselect now */
2032 #ifdef FEAT_VISUALEXTRA
2033 if (empty_region_error
)
2037 /* This is a new edit command, not a restart. Need to
2038 * remember it to make 'insertmode' work with mappings for
2039 * Visual mode. But do this only once. */
2040 restart_edit_save
= restart_edit
;
2043 op_insert(oap
, cap
->count1
);
2045 /* TODO: when inserting in several lines, should format all
2047 auto_format(FALSE
, TRUE
);
2049 if (restart_edit
== 0)
2050 restart_edit
= restart_edit_save
;
2059 VIsual_reselect
= FALSE
; /* don't reselect now */
2061 #ifdef FEAT_VISUALEXTRA
2062 if (empty_region_error
)
2065 #ifdef FEAT_VISUALEXTRA
2067 op_replace(oap
, cap
->nchar
);
2073 VIsual_reselect
= FALSE
; /* don't reselect now */
2074 foldCreate(oap
->start
.lnum
, oap
->end
.lnum
);
2078 case OP_FOLDOPENREC
:
2080 case OP_FOLDCLOSEREC
:
2081 VIsual_reselect
= FALSE
; /* don't reselect now */
2082 opFoldRange(oap
->start
.lnum
, oap
->end
.lnum
,
2083 oap
->op_type
== OP_FOLDOPEN
2084 || oap
->op_type
== OP_FOLDOPENREC
,
2085 oap
->op_type
== OP_FOLDOPENREC
2086 || oap
->op_type
== OP_FOLDCLOSEREC
,
2092 VIsual_reselect
= FALSE
; /* don't reselect now */
2093 deleteFold(oap
->start
.lnum
, oap
->end
.lnum
,
2094 oap
->op_type
== OP_FOLDDELREC
, oap
->is_VIsual
);
2100 #ifdef FEAT_VIRTUALEDIT
2106 * if 'sol' not set, go back to old column for some commands
2108 if (!p_sol
&& oap
->motion_type
== MLINE
&& !oap
->end_adjusted
2109 && (oap
->op_type
== OP_LSHIFT
|| oap
->op_type
== OP_RSHIFT
2110 || oap
->op_type
== OP_DELETE
))
2111 coladvance(curwin
->w_curswant
= old_col
);
2115 curwin
->w_cursor
= old_cursor
;
2118 oap
->block_mode
= FALSE
;
2125 * Handle indent and format operators and visual mode ":".
2131 stuffcharReadbuff(':');
2134 stuffReadbuff((char_u
*)"'<,'>");
2139 * Make the range look nice, so it can be repeated.
2141 if (oap
->start
.lnum
== curwin
->w_cursor
.lnum
)
2142 stuffcharReadbuff('.');
2144 stuffnumReadbuff((long)oap
->start
.lnum
);
2145 if (oap
->end
.lnum
!= oap
->start
.lnum
)
2147 stuffcharReadbuff(',');
2148 if (oap
->end
.lnum
== curwin
->w_cursor
.lnum
)
2149 stuffcharReadbuff('.');
2150 else if (oap
->end
.lnum
== curbuf
->b_ml
.ml_line_count
)
2151 stuffcharReadbuff('$');
2152 else if (oap
->start
.lnum
== curwin
->w_cursor
.lnum
)
2154 stuffReadbuff((char_u
*)".+");
2155 stuffnumReadbuff((long)oap
->line_count
- 1);
2158 stuffnumReadbuff((long)oap
->end
.lnum
);
2161 if (oap
->op_type
!= OP_COLON
)
2162 stuffReadbuff((char_u
*)"!");
2163 if (oap
->op_type
== OP_INDENT
)
2165 #ifndef FEAT_CINDENT
2166 if (*get_equalprg() == NUL
)
2167 stuffReadbuff((char_u
*)"indent");
2170 stuffReadbuff(get_equalprg());
2171 stuffReadbuff((char_u
*)"\n");
2173 else if (oap
->op_type
== OP_FORMAT
)
2176 stuffReadbuff((char_u
*)"fmt");
2178 stuffReadbuff(p_fp
);
2179 stuffReadbuff((char_u
*)"\n']");
2183 * do_cmdline() does the rest
2188 * Handle the "g@" operator: call 'operatorfunc'.
2192 oparg_T
*oap UNUSED
;
2197 if (*p_opfunc
== NUL
)
2198 EMSG(_("E774: 'operatorfunc' is empty"));
2201 /* Set '[ and '] marks to text to be operated on. */
2202 curbuf
->b_op_start
= oap
->start
;
2203 curbuf
->b_op_end
= oap
->end
;
2204 if (oap
->motion_type
!= MLINE
&& !oap
->inclusive
)
2205 /* Exclude the end position. */
2206 decl(&curbuf
->b_op_end
);
2208 if (oap
->block_mode
)
2209 argv
[0] = (char_u
*)"block";
2210 else if (oap
->motion_type
== MLINE
)
2211 argv
[0] = (char_u
*)"line";
2213 argv
[0] = (char_u
*)"char";
2214 (void)call_func_retnr(p_opfunc
, 1, argv
, FALSE
);
2217 EMSG(_("E775: Eval feature not available"));
2221 #if defined(FEAT_MOUSE) || defined(PROTO)
2223 * Do the appropriate action for the current mouse click in the current mode.
2224 * Not used for Command-line mode.
2227 * event modi- position visual change action
2228 * fier cursor window
2229 * left press - yes end yes
2230 * left press C yes end yes "^]" (2)
2231 * left press S yes end yes "*" (2)
2232 * left drag - yes start if moved no
2233 * left relse - yes start if moved no
2234 * middle press - yes if not active no put register
2235 * middle press - yes if active no yank and put
2236 * right press - yes start or extend yes
2237 * right press S yes no change yes "#" (2)
2238 * right drag - yes extend no
2239 * right relse - yes extend no
2241 * Insert or Replace Mode:
2242 * event modi- position visual change action
2243 * fier cursor window
2244 * left press - yes (cannot be active) yes
2245 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2246 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2247 * left drag - yes start or extend (1) no CTRL-O (1)
2248 * left relse - yes start or extend (1) no CTRL-O (1)
2249 * middle press - no (cannot be active) no put register
2250 * right press - yes start or extend yes CTRL-O
2251 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2253 * (1) only if mouse pointer moved since press
2254 * (2) only if click is in same buffer
2256 * Return TRUE if start_arrow() should be called for edit mode.
2259 do_mouse(oap
, c
, dir
, count
, fixindent
)
2260 oparg_T
*oap
; /* operator argument, can be NULL */
2261 int c
; /* K_LEFTMOUSE, etc */
2262 int dir
; /* Direction to 'put' if necessary */
2264 int fixindent
; /* PUT_FIXINDENT if fixing indent necessary */
2266 static int do_always
= FALSE
; /* ignore 'mouse' setting next time */
2267 static int got_click
= FALSE
; /* got a click some time back */
2269 int which_button
; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2270 int is_click
; /* If FALSE it's a drag or release event */
2271 int is_drag
; /* If TRUE it's a drag event */
2272 int jump_flags
= 0; /* flags for jump_to_mouse() */
2274 int moved
; /* Has cursor moved? */
2275 int in_status_line
; /* mouse in status line */
2276 #ifdef FEAT_VERTSPLIT
2277 int in_sep_line
; /* mouse in vertical separator line */
2280 #if defined(FEAT_FOLDING)
2283 win_T
*old_curwin
= curwin
;
2285 static pos_T orig_cursor
;
2286 colnr_T leftcol
, rightcol
;
2289 int old_active
= VIsual_active
;
2290 int old_mode
= VIsual_mode
;
2294 #if defined(FEAT_FOLDING)
2295 save_cursor
= curwin
->w_cursor
;
2299 * When GUI is active, always recognize mouse events, otherwise:
2300 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2301 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2302 * - For command line and insert mode 'mouse' is checked before calling
2315 if (!mouse_has(MOUSE_VISUAL
))
2320 if (State
== NORMAL
&& !mouse_has(MOUSE_NORMAL
))
2324 which_button
= get_mouse_button(KEY2TERMCAP1(c
), &is_click
, &is_drag
);
2326 #ifdef FEAT_MOUSESHAPE
2327 /* May have stopped dragging the status or separator line. The pointer is
2328 * most likely still on the status or separator line. */
2329 if (!is_drag
&& drag_status_line
)
2331 drag_status_line
= FALSE
;
2332 update_mouseshape(SHAPE_IDX_STATUS
);
2334 # ifdef FEAT_VERTSPLIT
2335 if (!is_drag
&& drag_sep_line
)
2337 drag_sep_line
= FALSE
;
2338 update_mouseshape(SHAPE_IDX_VSEP
);
2344 * Ignore drag and release events if we didn't get a click.
2350 if (!got_click
) /* didn't get click, ignore */
2352 if (!is_drag
) /* release, reset got_click */
2358 * ALT is only used for starging/extending Visual mode.
2360 if ((mod_mask
& MOD_MASK_ALT
))
2365 * CTRL right mouse button does CTRL-T
2367 if (is_click
&& (mod_mask
& MOD_MASK_CTRL
) && which_button
== MOUSE_RIGHT
)
2370 stuffcharReadbuff(Ctrl_O
);
2372 stuffnumReadbuff(count
);
2373 stuffcharReadbuff(Ctrl_T
);
2374 got_click
= FALSE
; /* ignore drag&release now */
2379 * CTRL only works with left mouse button
2381 if ((mod_mask
& MOD_MASK_CTRL
) && which_button
!= MOUSE_LEFT
)
2385 * When a modifier is down, ignore drag and release events, as well as
2386 * multiple clicks and the middle mouse button.
2387 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2389 if ((mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
| MOD_MASK_ALT
2392 || (mod_mask
& MOD_MASK_MULTI_CLICK
)
2393 || which_button
== MOUSE_MIDDLE
)
2394 && !((mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_ALT
))
2395 && mouse_model_popup()
2396 && which_button
== MOUSE_LEFT
)
2397 && !((mod_mask
& MOD_MASK_ALT
)
2398 && !mouse_model_popup()
2399 && which_button
== MOUSE_RIGHT
)
2404 * If the button press was used as the movement command for an operator
2405 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2406 * drag/release events.
2408 if (!is_click
&& which_button
== MOUSE_MIDDLE
)
2412 regname
= oap
->regname
;
2417 * Middle mouse button does a 'put' of the selected text
2419 if (which_button
== MOUSE_MIDDLE
)
2421 if (State
== NORMAL
)
2424 * If an operator was pending, we don't know what the user wanted
2425 * to do. Go back to normal mode: Clear the operator and beep().
2427 if (oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2435 * If visual was active, yank the highlighted text and put it
2436 * before the mouse pointer position.
2437 * In Select mode replace the highlighted text with the clipboard.
2443 stuffcharReadbuff(Ctrl_G
);
2444 stuffReadbuff((char_u
*)"\"+p");
2448 stuffcharReadbuff('y');
2449 stuffcharReadbuff(K_MIDDLEMOUSE
);
2451 do_always
= TRUE
; /* ignore 'mouse' setting next time */
2456 * The rest is below jump_to_mouse()
2460 else if ((State
& INSERT
) == 0)
2464 * Middle click in insert mode doesn't move the mouse, just insert the
2465 * contents of a register. '.' register is special, can't insert that
2467 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2468 * happens for the GUI).
2470 if ((State
& INSERT
) || !mouse_has(MOUSE_NORMAL
))
2473 insert_reg(regname
, TRUE
);
2476 #ifdef FEAT_CLIPBOARD
2477 if (clip_star
.available
&& regname
== 0)
2480 if ((State
& REPLACE_FLAG
) && !yank_register_mline(regname
))
2481 insert_reg(regname
, TRUE
);
2484 do_put(regname
, BACKWARD
, 1L, fixindent
| PUT_CURSEND
);
2486 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2487 AppendCharToRedobuff(Ctrl_R
);
2488 AppendCharToRedobuff(fixindent
? Ctrl_P
: Ctrl_O
);
2489 AppendCharToRedobuff(regname
== 0 ? '"' : regname
);
2496 /* When dragging or button-up stay in the same window. */
2498 jump_flags
|= MOUSE_FOCUS
| MOUSE_DID_MOVE
;
2500 start_visual
.lnum
= 0;
2503 /* Check for clicking in the tab page line. */
2504 if (mouse_row
== 0 && firstwin
->w_winrow
> 0)
2508 got_click
= FALSE
; /* ignore mouse-up and drag events */
2510 /* click in a tab selects that tab page */
2515 && mouse_col
< Columns
)
2517 c1
= TabPageIdxs
[mouse_col
];
2520 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2522 /* double click opens new page */
2525 tabpage_move(c1
== 0 ? 9999 : c1
- 1);
2529 /* Go to specified tab page, or next one if not clicking
2533 /* It's like clicking on the status line of a window. */
2534 if (curwin
!= old_curwin
)
2542 /* Close the current or specified tab page. */
2546 tp
= find_tabpage(-c1
);
2549 if (first_tabpage
->tp_next
!= NULL
)
2550 tabpage_close(FALSE
);
2552 else if (tp
!= NULL
)
2553 tabpage_close_other(tp
, FALSE
);
2561 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2562 * right button up -> pop-up menu
2563 * shift-left button -> right button
2564 * alt-left button -> alt-right button
2566 if (mouse_model_popup())
2568 if (which_button
== MOUSE_RIGHT
2569 && !(mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
)))
2572 * NOTE: Ignore right button down and drag mouse events.
2573 * Windows only shows the popup menu on the button up event.
2575 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2576 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC)
2580 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2581 if (is_click
|| is_drag
)
2584 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2585 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2586 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON)
2590 if (STRCMP(p_mousem
, "popup_setpos") == 0)
2592 /* First set the cursor position before showing the popup
2600 * set MOUSE_MAY_STOP_VIS if we are outside the
2601 * selection or the current window (might have false
2604 if (mouse_row
< W_WINROW(curwin
)
2606 > (W_WINROW(curwin
) + curwin
->w_height
))
2607 jump_flags
= MOUSE_MAY_STOP_VIS
;
2608 else if (get_fpos_of_mouse(&m_pos
) != IN_BUFFER
)
2609 jump_flags
= MOUSE_MAY_STOP_VIS
;
2612 if ((lt(curwin
->w_cursor
, VIsual
)
2613 && (lt(m_pos
, curwin
->w_cursor
)
2614 || lt(VIsual
, m_pos
)))
2615 || (lt(VIsual
, curwin
->w_cursor
)
2616 && (lt(m_pos
, VIsual
)
2617 || lt(curwin
->w_cursor
, m_pos
))))
2619 jump_flags
= MOUSE_MAY_STOP_VIS
;
2621 else if (VIsual_mode
== Ctrl_V
)
2623 getvcols(curwin
, &curwin
->w_cursor
, &VIsual
,
2624 &leftcol
, &rightcol
);
2625 getvcol(curwin
, &m_pos
, NULL
, &m_pos
.col
, NULL
);
2626 if (m_pos
.col
< leftcol
|| m_pos
.col
> rightcol
)
2627 jump_flags
= MOUSE_MAY_STOP_VIS
;
2632 jump_flags
= MOUSE_MAY_STOP_VIS
;
2637 jump_flags
= jump_to_mouse(jump_flags
, NULL
, which_button
);
2640 VIsual_active
? INVERTED
:
2644 out_flush(); /* Update before showing popup menu */
2647 gui_show_popupmenu();
2649 return (jump_flags
& CURSOR_MOVED
) != 0;
2657 if (which_button
== MOUSE_LEFT
2658 && (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_ALT
)))
2660 which_button
= MOUSE_RIGHT
;
2661 mod_mask
&= ~MOD_MASK_SHIFT
;
2666 if ((State
& (NORMAL
| INSERT
))
2667 && !(mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
)))
2669 if (which_button
== MOUSE_LEFT
)
2673 /* stop Visual mode for a left click in a window, but not when
2674 * on a status line */
2676 jump_flags
|= MOUSE_MAY_STOP_VIS
;
2678 else if (mouse_has(MOUSE_VISUAL
))
2679 jump_flags
|= MOUSE_MAY_VIS
;
2681 else if (which_button
== MOUSE_RIGHT
)
2683 if (is_click
&& VIsual_active
)
2686 * Remember the start and end of visual before moving the
2689 if (lt(curwin
->w_cursor
, VIsual
))
2691 start_visual
= curwin
->w_cursor
;
2692 end_visual
= VIsual
;
2696 start_visual
= VIsual
;
2697 end_visual
= curwin
->w_cursor
;
2700 jump_flags
|= MOUSE_FOCUS
;
2701 if (mouse_has(MOUSE_VISUAL
))
2702 jump_flags
|= MOUSE_MAY_VIS
;
2708 * If an operator is pending, ignore all drags and releases until the
2711 if (!is_drag
&& oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2714 oap
->motion_type
= MCHAR
;
2717 /* When releasing the button let jump_to_mouse() know. */
2718 if (!is_click
&& !is_drag
)
2719 jump_flags
|= MOUSE_RELEASED
;
2724 jump_flags
= jump_to_mouse(jump_flags
,
2725 oap
== NULL
? NULL
: &(oap
->inclusive
), which_button
);
2726 moved
= (jump_flags
& CURSOR_MOVED
);
2727 in_status_line
= (jump_flags
& IN_STATUS_LINE
);
2728 #ifdef FEAT_VERTSPLIT
2729 in_sep_line
= (jump_flags
& IN_SEP_LINE
);
2732 #ifdef FEAT_NETBEANS_INTG
2733 if (usingNetbeans
&& isNetbeansBuffer(curbuf
)
2734 && !(jump_flags
& (IN_STATUS_LINE
| IN_SEP_LINE
)))
2736 int key
= KEY2TERMCAP1(c
);
2738 if (key
== (int)KE_LEFTRELEASE
|| key
== (int)KE_MIDDLERELEASE
2739 || key
== (int)KE_RIGHTRELEASE
)
2740 netbeans_button_release(which_button
);
2744 /* When jumping to another window, clear a pending operator. That's a bit
2745 * friendlier than beeping and not jumping to that window. */
2746 if (curwin
!= old_curwin
&& oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2752 && (jump_flags
& (MOUSE_FOLD_CLOSE
| MOUSE_FOLD_OPEN
))
2753 && which_button
== MOUSE_LEFT
)
2755 /* open or close a fold at this line */
2756 if (jump_flags
& MOUSE_FOLD_OPEN
)
2757 openFold(curwin
->w_cursor
.lnum
, 1L);
2759 closeFold(curwin
->w_cursor
.lnum
, 1L);
2760 /* don't move the cursor if still in the same window */
2761 if (curwin
== old_curwin
)
2762 curwin
->w_cursor
= save_cursor
;
2766 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2767 if ((jump_flags
& IN_OTHER_WIN
) && !VIsual_active
&& clip_star
.available
)
2769 clip_modeless(which_button
, is_click
, is_drag
);
2775 /* Set global flag that we are extending the Visual area with mouse
2776 * dragging; temporarily minimize 'scrolloff'. */
2777 if (VIsual_active
&& is_drag
&& p_so
)
2779 /* In the very first line, allow scrolling one line */
2786 /* When dragging the mouse above the window, scroll down. */
2787 if (is_drag
&& mouse_row
< 0 && !in_status_line
)
2789 scroll_redraw(FALSE
, 1L);
2793 if (start_visual
.lnum
) /* right click in visual mode */
2795 /* When ALT is pressed make Visual mode blockwise. */
2796 if (mod_mask
& MOD_MASK_ALT
)
2797 VIsual_mode
= Ctrl_V
;
2800 * In Visual-block mode, divide the area in four, pick up the corner
2801 * that is in the quarter that the cursor is in.
2803 if (VIsual_mode
== Ctrl_V
)
2805 getvcols(curwin
, &start_visual
, &end_visual
, &leftcol
, &rightcol
);
2806 if (curwin
->w_curswant
> (leftcol
+ rightcol
) / 2)
2807 end_visual
.col
= leftcol
;
2809 end_visual
.col
= rightcol
;
2810 if (curwin
->w_cursor
.lnum
<
2811 (start_visual
.lnum
+ end_visual
.lnum
) / 2)
2812 end_visual
.lnum
= end_visual
.lnum
;
2814 end_visual
.lnum
= start_visual
.lnum
;
2816 /* move VIsual to the right column */
2817 start_visual
= curwin
->w_cursor
; /* save the cursor pos */
2818 curwin
->w_cursor
= end_visual
;
2819 coladvance(end_visual
.col
);
2820 VIsual
= curwin
->w_cursor
;
2821 curwin
->w_cursor
= start_visual
; /* restore the cursor */
2826 * If the click is before the start of visual, change the start.
2827 * If the click is after the end of visual, change the end. If
2828 * the click is inside the visual, change the closest side.
2830 if (lt(curwin
->w_cursor
, start_visual
))
2831 VIsual
= end_visual
;
2832 else if (lt(end_visual
, curwin
->w_cursor
))
2833 VIsual
= start_visual
;
2836 /* In the same line, compare column number */
2837 if (end_visual
.lnum
== start_visual
.lnum
)
2839 if (curwin
->w_cursor
.col
- start_visual
.col
>
2840 end_visual
.col
- curwin
->w_cursor
.col
)
2841 VIsual
= start_visual
;
2843 VIsual
= end_visual
;
2846 /* In different lines, compare line number */
2849 diff
= (curwin
->w_cursor
.lnum
- start_visual
.lnum
) -
2850 (end_visual
.lnum
- curwin
->w_cursor
.lnum
);
2852 if (diff
> 0) /* closest to end */
2853 VIsual
= start_visual
;
2854 else if (diff
< 0) /* closest to start */
2855 VIsual
= end_visual
;
2856 else /* in the middle line */
2858 if (curwin
->w_cursor
.col
<
2859 (start_visual
.col
+ end_visual
.col
) / 2)
2860 VIsual
= end_visual
;
2862 VIsual
= start_visual
;
2869 * If Visual mode started in insert mode, execute "CTRL-O"
2871 else if ((State
& INSERT
) && VIsual_active
)
2872 stuffcharReadbuff(Ctrl_O
);
2876 * Middle mouse click: Put text before cursor.
2878 if (which_button
== MOUSE_MIDDLE
)
2880 #ifdef FEAT_CLIPBOARD
2881 if (clip_star
.available
&& regname
== 0)
2884 if (yank_register_mline(regname
))
2886 if (mouse_past_bottom
)
2889 else if (mouse_past_eol
)
2894 c1
= (dir
== BACKWARD
) ? '[' : ']';
2899 c1
= (dir
== FORWARD
) ? 'p' : 'P';
2902 prep_redo(regname
, count
, NUL
, c1
, NUL
, c2
, NUL
);
2905 * Remember where the paste started, so in edit() Insstart can be set
2908 if (restart_edit
!= 0)
2909 where_paste_started
= curwin
->w_cursor
;
2910 do_put(regname
, dir
, count
, fixindent
| PUT_CURSEND
);
2913 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2915 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2916 * error under the mouse pointer.
2918 else if (((mod_mask
& MOD_MASK_CTRL
)
2919 || (mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2920 && bt_quickfix(curbuf
))
2923 stuffcharReadbuff(Ctrl_O
);
2924 if (curwin
->w_llist_ref
== NULL
) /* quickfix window */
2925 stuffReadbuff((char_u
*)":.cc\n");
2926 else /* location list window */
2927 stuffReadbuff((char_u
*)":.ll\n");
2928 got_click
= FALSE
; /* ignore drag&release now */
2933 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2934 * under the mouse pointer.
2936 else if ((mod_mask
& MOD_MASK_CTRL
) || (curbuf
->b_help
2937 && (mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
))
2940 stuffcharReadbuff(Ctrl_O
);
2941 stuffcharReadbuff(Ctrl_RSB
);
2942 got_click
= FALSE
; /* ignore drag&release now */
2946 * Shift-Mouse click searches for the next occurrence of the word under
2949 else if ((mod_mask
& MOD_MASK_SHIFT
))
2953 || (VIsual_active
&& VIsual_select
)
2956 stuffcharReadbuff(Ctrl_O
);
2957 if (which_button
== MOUSE_LEFT
)
2958 stuffcharReadbuff('*');
2959 else /* MOUSE_RIGHT */
2960 stuffcharReadbuff('#');
2963 /* Handle double clicks, unless on status line */
2964 else if (in_status_line
)
2966 #ifdef FEAT_MOUSESHAPE
2967 if ((is_drag
|| is_click
) && !drag_status_line
)
2969 drag_status_line
= TRUE
;
2970 update_mouseshape(-1);
2974 #ifdef FEAT_VERTSPLIT
2975 else if (in_sep_line
)
2977 # ifdef FEAT_MOUSESHAPE
2978 if ((is_drag
|| is_click
) && !drag_sep_line
)
2980 drag_sep_line
= TRUE
;
2981 update_mouseshape(-1);
2987 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) && (State
& (NORMAL
| INSERT
))
2988 && mouse_has(MOUSE_VISUAL
))
2990 if (is_click
|| !VIsual_active
)
2993 orig_cursor
= VIsual
;
2996 check_visual_highlight();
2997 VIsual
= curwin
->w_cursor
;
2998 orig_cursor
= VIsual
;
2999 VIsual_active
= TRUE
;
3000 VIsual_reselect
= TRUE
;
3001 /* start Select mode if 'selectmode' contains "mouse" */
3002 may_start_select('o');
3005 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
3007 /* Double click with ALT pressed makes it blockwise. */
3008 if (mod_mask
& MOD_MASK_ALT
)
3009 VIsual_mode
= Ctrl_V
;
3013 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_3CLICK
)
3015 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_4CLICK
)
3016 VIsual_mode
= Ctrl_V
;
3017 #ifdef FEAT_CLIPBOARD
3018 /* Make sure the clipboard gets updated. Needed because start and
3019 * end may still be the same, and the selection needs to be owned */
3020 clip_star
.vmode
= NUL
;
3024 * A double click selects a word or a block.
3026 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
3033 /* If the character under the cursor (skipping white space) is
3034 * not a word character, try finding a match and select a (),
3035 * {}, [], #if/#endif, etc. block. */
3036 end_visual
= curwin
->w_cursor
;
3037 while (gc
= gchar_pos(&end_visual
), vim_iswhite(gc
))
3040 oap
->motion_type
= MCHAR
;
3042 && VIsual_mode
== 'v'
3043 && !vim_iswordc(gchar_pos(&end_visual
))
3044 && equalpos(curwin
->w_cursor
, VIsual
)
3045 && (pos
= findmatch(oap
, NUL
)) != NULL
)
3047 curwin
->w_cursor
= *pos
;
3048 if (oap
->motion_type
== MLINE
)
3050 else if (*p_sel
== 'e')
3052 if (lt(curwin
->w_cursor
, VIsual
))
3055 ++curwin
->w_cursor
.col
;
3060 if (pos
== NULL
&& (is_click
|| is_drag
))
3062 /* When not found a match or when dragging: extend to include
3064 if (lt(curwin
->w_cursor
, orig_cursor
))
3066 find_start_of_word(&curwin
->w_cursor
);
3067 find_end_of_word(&VIsual
);
3071 find_start_of_word(&VIsual
);
3072 if (*p_sel
== 'e' && *ml_get_cursor() != NUL
)
3074 curwin
->w_cursor
.col
+=
3075 (*mb_ptr2len
)(ml_get_cursor());
3077 ++curwin
->w_cursor
.col
;
3079 find_end_of_word(&curwin
->w_cursor
);
3082 curwin
->w_set_curswant
= TRUE
;
3085 redraw_curbuf_later(INVERTED
); /* update the inversion */
3087 else if (VIsual_active
&& !old_active
)
3089 if (mod_mask
& MOD_MASK_ALT
)
3090 VIsual_mode
= Ctrl_V
;
3095 /* If Visual mode changed show it later. */
3096 if ((!VIsual_active
&& old_active
&& mode_displayed
)
3097 || (VIsual_active
&& p_smd
&& msg_silent
== 0
3098 && (!old_active
|| VIsual_mode
!= old_mode
)))
3099 redraw_cmdline
= TRUE
;
3107 * Move "pos" back to the start of the word it's in.
3110 find_start_of_word(pos
)
3117 line
= ml_get(pos
->lnum
);
3118 cclass
= get_mouse_class(line
+ pos
->col
);
3120 while (pos
->col
> 0)
3124 col
-= (*mb_head_off
)(line
, line
+ col
);
3126 if (get_mouse_class(line
+ col
) != cclass
)
3133 * Move "pos" forward to the end of the word it's in.
3134 * When 'selection' is "exclusive", the position is just after the word.
3137 find_end_of_word(pos
)
3144 line
= ml_get(pos
->lnum
);
3145 if (*p_sel
== 'e' && pos
->col
> 0)
3149 pos
->col
-= (*mb_head_off
)(line
, line
+ pos
->col
);
3152 cclass
= get_mouse_class(line
+ pos
->col
);
3153 while (line
[pos
->col
] != NUL
)
3156 col
= pos
->col
+ (*mb_ptr2len
)(line
+ pos
->col
);
3160 if (get_mouse_class(line
+ col
) != cclass
)
3171 * Get class of a character for selection: same class means same word.
3173 * 1: punctuation groups
3174 * 2: normal word character
3175 * >2: multi-byte word character.
3184 if (has_mbyte
&& MB_BYTE2LEN(p
[0]) > 1)
3185 return mb_get_class(p
);
3189 if (c
== ' ' || c
== '\t')
3196 * There are a few special cases where we want certain combinations of
3197 * characters to be considered as a single word. These are things like
3198 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3199 * character is in it's own class.
3201 if (c
!= NUL
&& vim_strchr((char_u
*)"-+*/%<>&|^!=", c
) != NULL
)
3205 #endif /* FEAT_VISUAL */
3206 #endif /* FEAT_MOUSE */
3208 #if defined(FEAT_VISUAL) || defined(PROTO)
3210 * Check if highlighting for visual mode is possible, give a warning message
3214 check_visual_highlight()
3216 static int did_check
= FALSE
;
3220 if (!did_check
&& hl_attr(HLF_V
) == 0)
3221 MSG(_("Warning: terminal cannot highlight"));
3228 * This function should ALWAYS be called to end Visual mode, except from
3229 * do_pending_operator().
3234 #ifdef FEAT_CLIPBOARD
3236 * If we are using the clipboard, then remember what was selected in case
3237 * we need to paste it somewhere while we still own the selection.
3238 * Only do this when the clipboard is already owned. Don't want to grab
3239 * the selection when hitting ESC.
3241 if (clip_star
.available
&& clip_star
.owned
)
3245 VIsual_active
= FALSE
;
3251 /* Save the current VIsual area for '< and '> marks, and "gv" */
3252 curbuf
->b_visual
.vi_mode
= VIsual_mode
;
3253 curbuf
->b_visual
.vi_start
= VIsual
;
3254 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
3255 curbuf
->b_visual
.vi_curswant
= curwin
->w_curswant
;
3257 curbuf
->b_visual_mode_eval
= VIsual_mode
;
3259 #ifdef FEAT_VIRTUALEDIT
3260 if (!virtual_active())
3261 curwin
->w_cursor
.coladd
= 0;
3265 clear_cmdline
= TRUE
; /* unshow visual mode later */
3266 #ifdef FEAT_CMDL_INFO
3271 adjust_cursor_eol();
3275 * Reset VIsual_active and VIsual_reselect.
3278 reset_VIsual_and_resel()
3283 redraw_curbuf_later(INVERTED
); /* delete the inversion later */
3285 VIsual_reselect
= FALSE
;
3289 * Reset VIsual_active and VIsual_reselect if it's set.
3297 redraw_curbuf_later(INVERTED
); /* delete the inversion later */
3298 VIsual_reselect
= FALSE
;
3301 #endif /* FEAT_VISUAL */
3303 #if defined(FEAT_BEVAL)
3304 static int find_is_eval_item
__ARGS((char_u
*ptr
, int *colp
, int *nbp
, int dir
));
3307 * Check for a balloon-eval special item to include when searching for an
3308 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3309 * Returns TRUE if the character at "*ptr" should be included.
3310 * "dir" is FORWARD or BACKWARD, the direction of searching.
3311 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3312 * "bnp" points to a counter for square brackets.
3315 find_is_eval_item(ptr
, colp
, bnp
, dir
)
3321 /* Accept everything inside []. */
3322 if ((*ptr
== ']' && dir
== BACKWARD
) || (*ptr
== '[' && dir
== FORWARD
))
3326 if ((*ptr
== '[' && dir
== BACKWARD
) || (*ptr
== ']' && dir
== FORWARD
))
3331 /* skip over "s.var" */
3335 /* two-character item: s->var */
3336 if (ptr
[dir
== BACKWARD
? 0 : 1] == '>'
3337 && ptr
[dir
== BACKWARD
? -1 : 0] == '-')
3347 * Find the identifier under or to the right of the cursor.
3348 * "find_type" can have one of three values:
3349 * FIND_IDENT: find an identifier (keyword)
3350 * FIND_STRING: find any non-white string
3351 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3352 * FIND_EVAL: find text useful for C program debugging
3354 * There are three steps:
3355 * 1. Search forward for the start of an identifier/string. Doesn't move if
3357 * 2. Search backward for the start of this identifier/string.
3358 * This doesn't match the real Vi but I like it a little better and it
3359 * shouldn't bother anyone.
3360 * 3. Search forward to the end of this identifier/string.
3361 * When FIND_IDENT isn't defined, we backup until a blank.
3363 * Returns the length of the string, or zero if no string is found.
3364 * If a string is found, a pointer to the string is put in "*string". This
3365 * string is not always NUL terminated.
3368 find_ident_under_cursor(string
, find_type
)
3372 return find_ident_at_pos(curwin
, curwin
->w_cursor
.lnum
,
3373 curwin
->w_cursor
.col
, string
, find_type
);
3377 * Like find_ident_under_cursor(), but for any window and any position.
3378 * However: Uses 'iskeyword' from the current window!.
3381 find_ident_at_pos(wp
, lnum
, startcol
, string
, find_type
)
3389 int col
= 0; /* init to shut up GCC */
3396 #if defined(FEAT_BEVAL)
3397 int bn
= 0; /* bracket nesting */
3401 * if i == 0: try to find an identifier
3402 * if i == 1: try to find any non-white string
3404 ptr
= ml_get_buf(wp
->w_buffer
, lnum
, FALSE
);
3405 for (i
= (find_type
& FIND_IDENT
) ? 0 : 1; i
< 2; ++i
)
3408 * 1. skip to start of identifier/string
3414 while (ptr
[col
] != NUL
)
3416 # if defined(FEAT_BEVAL)
3417 /* Stop at a ']' to evaluate "a[x]". */
3418 if ((find_type
& FIND_EVAL
) && ptr
[col
] == ']')
3421 this_class
= mb_get_class(ptr
+ col
);
3422 if (this_class
!= 0 && (i
== 1 || this_class
!= 1))
3424 col
+= (*mb_ptr2len
)(ptr
+ col
);
3429 while (ptr
[col
] != NUL
3430 && (i
== 0 ? !vim_iswordc(ptr
[col
]) : vim_iswhite(ptr
[col
]))
3431 # if defined(FEAT_BEVAL)
3432 && (!(find_type
& FIND_EVAL
) || ptr
[col
] != ']')
3437 #if defined(FEAT_BEVAL)
3438 /* When starting on a ']' count it, so that we include the '['. */
3439 bn
= ptr
[col
] == ']';
3443 * 2. Back up to start of identifier/string.
3448 /* Remember class of character under cursor. */
3449 # if defined(FEAT_BEVAL)
3450 if ((find_type
& FIND_EVAL
) && ptr
[col
] == ']')
3451 this_class
= mb_get_class((char_u
*)"a");
3454 this_class
= mb_get_class(ptr
+ col
);
3455 while (col
> 0 && this_class
!= 0)
3457 prevcol
= col
- 1 - (*mb_head_off
)(ptr
, ptr
+ col
- 1);
3458 prev_class
= mb_get_class(ptr
+ prevcol
);
3459 if (this_class
!= prev_class
3462 || (find_type
& FIND_IDENT
))
3463 # if defined(FEAT_BEVAL)
3464 && (!(find_type
& FIND_EVAL
)
3466 || !find_is_eval_item(ptr
+ prevcol
, &prevcol
,
3474 /* If we don't want just any old string, or we've found an
3475 * identifier, stop searching. */
3478 if (!(find_type
& FIND_STRING
) || this_class
== 2)
3486 ? vim_iswordc(ptr
[col
- 1])
3487 : (!vim_iswhite(ptr
[col
- 1])
3488 && (!(find_type
& FIND_IDENT
)
3489 || !vim_iswordc(ptr
[col
- 1]))))
3490 #if defined(FEAT_BEVAL)
3491 || ((find_type
& FIND_EVAL
)
3493 && find_is_eval_item(ptr
+ col
- 1, &col
,
3499 /* If we don't want just any old string, or we've found an
3500 * identifier, stop searching. */
3501 if (!(find_type
& FIND_STRING
) || vim_iswordc(ptr
[col
]))
3506 if (ptr
[col
] == NUL
|| (i
== 0 && (
3508 has_mbyte
? this_class
!= 2 :
3510 !vim_iswordc(ptr
[col
]))))
3513 * didn't find an identifier or string
3515 if (find_type
& FIND_STRING
)
3516 EMSG(_("E348: No string under cursor"));
3525 * 3. Find the end if the identifier/string.
3527 #if defined(FEAT_BEVAL)
3535 /* Search for point of changing multibyte character class. */
3536 this_class
= mb_get_class(ptr
);
3537 while (ptr
[col
] != NUL
3538 && ((i
== 0 ? mb_get_class(ptr
+ col
) == this_class
3539 : mb_get_class(ptr
+ col
) != 0)
3540 # if defined(FEAT_BEVAL)
3541 || ((find_type
& FIND_EVAL
)
3542 && col
<= (int)startcol
3543 && find_is_eval_item(ptr
+ col
, &col
, &bn
, FORWARD
))
3546 col
+= (*mb_ptr2len
)(ptr
+ col
);
3550 while ((i
== 0 ? vim_iswordc(ptr
[col
])
3551 : (ptr
[col
] != NUL
&& !vim_iswhite(ptr
[col
])))
3552 # if defined(FEAT_BEVAL)
3553 || ((find_type
& FIND_EVAL
)
3554 && col
<= (int)startcol
3555 && find_is_eval_item(ptr
+ col
, &col
, &bn
, FORWARD
))
3566 * Prepare for redo of a normal command.
3572 prep_redo(cap
->oap
->regname
, cap
->count0
,
3573 NUL
, cap
->cmdchar
, NUL
, NUL
, cap
->nchar
);
3577 * Prepare for redo of any command.
3578 * Note that only the last argument can be a multi-byte char.
3581 prep_redo(regname
, num
, cmd1
, cmd2
, cmd3
, cmd4
, cmd5
)
3591 if (regname
!= 0) /* yank from specified buffer */
3593 AppendCharToRedobuff('"');
3594 AppendCharToRedobuff(regname
);
3597 AppendNumberToRedobuff(num
);
3600 AppendCharToRedobuff(cmd1
);
3602 AppendCharToRedobuff(cmd2
);
3604 AppendCharToRedobuff(cmd3
);
3606 AppendCharToRedobuff(cmd4
);
3608 AppendCharToRedobuff(cmd5
);
3612 * check for operator active and clear it
3614 * return TRUE if operator was active
3620 if (oap
->op_type
== OP_NOP
)
3627 * Check for operator or Visual active. Clear active operator.
3629 * Return TRUE if operator or Visual was active.
3635 if (oap
->op_type
== OP_NOP
3649 oap
->op_type
= OP_NOP
;
3651 oap
->motion_force
= NUL
;
3652 oap
->use_reg_one
= FALSE
;
3665 * Remove the shift modifier from a special key.
3668 unshift_special(cap
)
3671 switch (cap
->cmdchar
)
3673 case K_S_RIGHT
: cap
->cmdchar
= K_RIGHT
; break;
3674 case K_S_LEFT
: cap
->cmdchar
= K_LEFT
; break;
3675 case K_S_UP
: cap
->cmdchar
= K_UP
; break;
3676 case K_S_DOWN
: cap
->cmdchar
= K_DOWN
; break;
3677 case K_S_HOME
: cap
->cmdchar
= K_HOME
; break;
3678 case K_S_END
: cap
->cmdchar
= K_END
; break;
3680 cap
->cmdchar
= simplify_key(cap
->cmdchar
, &mod_mask
);
3684 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3686 * Routines for displaying a partly typed command
3689 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3690 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3692 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3694 static char_u showcmd_buf
[SHOWCMD_BUFLEN
];
3695 static char_u old_showcmd_buf
[SHOWCMD_BUFLEN
]; /* For push_showcmd() */
3696 static int showcmd_is_clear
= TRUE
;
3697 static int showcmd_visual
= FALSE
;
3699 static void display_showcmd
__ARGS((void));
3708 if (VIsual_active
&& !char_avail())
3710 int cursor_bot
= lt(VIsual
, curwin
->w_cursor
);
3712 colnr_T leftcol
, rightcol
;
3715 /* Show the size of the Visual area. */
3719 bot
= curwin
->w_cursor
.lnum
;
3723 top
= curwin
->w_cursor
.lnum
;
3726 # ifdef FEAT_FOLDING
3727 /* Include closed folds as a whole. */
3728 hasFolding(top
, &top
, NULL
);
3729 hasFolding(bot
, NULL
, &bot
);
3731 lines
= bot
- top
+ 1;
3733 if (VIsual_mode
== Ctrl_V
)
3735 #ifdef FEAT_LINEBREAK
3736 char_u
*saved_sbr
= p_sbr
;
3738 /* Make 'sbr' empty for a moment to get the correct size. */
3739 p_sbr
= empty_option
;
3741 getvcols(curwin
, &curwin
->w_cursor
, &VIsual
, &leftcol
, &rightcol
);
3742 #ifdef FEAT_LINEBREAK
3745 sprintf((char *)showcmd_buf
, "%ldx%ld", lines
,
3746 (long)(rightcol
- leftcol
+ 1));
3748 else if (VIsual_mode
== 'V' || VIsual
.lnum
!= curwin
->w_cursor
.lnum
)
3749 sprintf((char *)showcmd_buf
, "%ld", lines
);
3751 sprintf((char *)showcmd_buf
, "%ld", (long)(cursor_bot
3752 ? curwin
->w_cursor
.col
- VIsual
.col
3753 : VIsual
.col
- curwin
->w_cursor
.col
) + (*p_sel
!= 'e'));
3754 showcmd_buf
[SHOWCMD_COLS
] = NUL
; /* truncate */
3755 showcmd_visual
= TRUE
;
3760 showcmd_buf
[0] = NUL
;
3761 showcmd_visual
= FALSE
;
3763 /* Don't actually display something if there is nothing to clear. */
3764 if (showcmd_is_clear
)
3772 * Add 'c' to string of shown command chars.
3773 * Return TRUE if output has been written (and setcursor() has been called).
3783 #if defined(FEAT_MOUSE)
3785 static int ignore
[] =
3788 K_VER_SCROLLBAR
, K_HOR_SCROLLBAR
,
3789 K_LEFTMOUSE_NM
, K_LEFTRELEASE_NM
,
3792 K_LEFTMOUSE
, K_LEFTDRAG
, K_LEFTRELEASE
,
3793 K_MIDDLEMOUSE
, K_MIDDLEDRAG
, K_MIDDLERELEASE
,
3794 K_RIGHTMOUSE
, K_RIGHTDRAG
, K_RIGHTRELEASE
,
3795 K_MOUSEDOWN
, K_MOUSEUP
,
3796 K_X1MOUSE
, K_X1DRAG
, K_X1RELEASE
, K_X2MOUSE
, K_X2DRAG
, K_X2RELEASE
,
3802 if (!p_sc
|| msg_silent
!= 0)
3807 showcmd_buf
[0] = NUL
;
3808 showcmd_visual
= FALSE
;
3811 #if defined(FEAT_MOUSE)
3812 /* Ignore keys that are scrollbar updates and mouse clicks */
3814 for (i
= 0; ignore
[i
] != 0; ++i
)
3820 old_len
= (int)STRLEN(showcmd_buf
);
3821 extra_len
= (int)STRLEN(p
);
3822 overflow
= old_len
+ extra_len
- SHOWCMD_COLS
;
3824 mch_memmove(showcmd_buf
, showcmd_buf
+ overflow
,
3825 old_len
- overflow
+ 1);
3826 STRCAT(showcmd_buf
, p
);
3840 if (!add_to_showcmd(c
))
3845 * Delete 'len' characters from the end of the shown command.
3848 del_from_showcmd(len
)
3856 old_len
= (int)STRLEN(showcmd_buf
);
3859 showcmd_buf
[old_len
- len
] = NUL
;
3866 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3867 * something and there is a partial mapping.
3873 STRCPY(old_showcmd_buf
, showcmd_buf
);
3882 STRCPY(showcmd_buf
, old_showcmd_buf
);
3894 len
= (int)STRLEN(showcmd_buf
);
3896 showcmd_is_clear
= TRUE
;
3899 screen_puts(showcmd_buf
, (int)Rows
- 1, sc_col
, 0);
3900 showcmd_is_clear
= FALSE
;
3904 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3907 screen_puts((char_u
*)" " + len
, (int)Rows
- 1, sc_col
+ len
, 0);
3909 setcursor(); /* put cursor back where it belongs */
3913 #ifdef FEAT_SCROLLBIND
3915 * When "check" is FALSE, prepare for commands that scroll the window.
3916 * When "check" is TRUE, take care of scroll-binding after the window has
3917 * scrolled. Called from normal_cmd() and edit().
3920 do_check_scrollbind(check
)
3923 static win_T
*old_curwin
= NULL
;
3924 static linenr_T old_topline
= 0;
3926 static int old_topfill
= 0;
3928 static buf_T
*old_buf
= NULL
;
3929 static colnr_T old_leftcol
= 0;
3931 if (check
&& curwin
->w_p_scb
)
3933 /* If a ":syncbind" command was just used, don't scroll, only reset
3936 did_syncbind
= FALSE
;
3937 else if (curwin
== old_curwin
)
3940 * Synchronize other windows, as necessary according to
3941 * 'scrollbind'. Don't do this after an ":edit" command, except
3942 * when 'diff' is set.
3944 if ((curwin
->w_buffer
== old_buf
3949 && (curwin
->w_topline
!= old_topline
3951 || curwin
->w_topfill
!= old_topfill
3953 || curwin
->w_leftcol
!= old_leftcol
))
3955 check_scrollbind(curwin
->w_topline
- old_topline
,
3956 (long)(curwin
->w_leftcol
- old_leftcol
));
3959 else if (vim_strchr(p_sbo
, 'j')) /* jump flag set in 'scrollopt' */
3962 * When switching between windows, make sure that the relative
3963 * vertical offset is valid for the new window. The relative
3964 * offset is invalid whenever another 'scrollbind' window has
3965 * scrolled to a point that would force the current window to
3966 * scroll past the beginning or end of its buffer. When the
3967 * resync is performed, some of the other 'scrollbind' windows may
3968 * need to jump so that the current window's relative position is
3969 * visible on-screen.
3971 check_scrollbind(curwin
->w_topline
- curwin
->w_scbind_pos
, 0L);
3973 curwin
->w_scbind_pos
= curwin
->w_topline
;
3976 old_curwin
= curwin
;
3977 old_topline
= curwin
->w_topline
;
3979 old_topfill
= curwin
->w_topfill
;
3981 old_buf
= curwin
->w_buffer
;
3982 old_leftcol
= curwin
->w_leftcol
;
3986 * Synchronize any windows that have "scrollbind" set, based on the
3987 * number of rows by which the current window has changed
3988 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3991 check_scrollbind(topline_diff
, leftcol_diff
)
3992 linenr_T topline_diff
;
3997 win_T
*old_curwin
= curwin
;
3998 buf_T
*old_curbuf
= curbuf
;
4000 int old_VIsual_select
= VIsual_select
;
4001 int old_VIsual_active
= VIsual_active
;
4003 colnr_T tgt_leftcol
= curwin
->w_leftcol
;
4008 * check 'scrollopt' string for vertical and horizontal scroll options
4010 want_ver
= (vim_strchr(p_sbo
, 'v') && topline_diff
!= 0);
4012 want_ver
|= old_curwin
->w_p_diff
;
4014 want_hor
= (vim_strchr(p_sbo
, 'h') && (leftcol_diff
|| topline_diff
!= 0));
4017 * loop through the scrollbound windows and scroll accordingly
4020 VIsual_select
= VIsual_active
= 0;
4022 for (curwin
= firstwin
; curwin
; curwin
= curwin
->w_next
)
4024 curbuf
= curwin
->w_buffer
;
4025 /* skip original window and windows with 'noscrollbind' */
4026 if (curwin
!= old_curwin
&& curwin
->w_p_scb
)
4029 * do the vertical scroll
4034 if (old_curwin
->w_p_diff
&& curwin
->w_p_diff
)
4036 diff_set_topline(old_curwin
, curwin
);
4041 curwin
->w_scbind_pos
+= topline_diff
;
4042 topline
= curwin
->w_scbind_pos
;
4043 if (topline
> curbuf
->b_ml
.ml_line_count
)
4044 topline
= curbuf
->b_ml
.ml_line_count
;
4048 y
= topline
- curwin
->w_topline
;
4052 scrolldown(-y
, FALSE
);
4055 redraw_later(VALID
);
4058 curwin
->w_redr_status
= TRUE
;
4063 * do the horizontal scroll
4065 if (want_hor
&& curwin
->w_leftcol
!= tgt_leftcol
)
4067 curwin
->w_leftcol
= tgt_leftcol
;
4074 * reset current-window
4077 VIsual_select
= old_VIsual_select
;
4078 VIsual_active
= old_VIsual_active
;
4080 curwin
= old_curwin
;
4081 curbuf
= old_curbuf
;
4083 #endif /* #ifdef FEAT_SCROLLBIND */
4086 * Command character that's ignored.
4087 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4094 cap
->retval
|= CA_COMMAND_BUSY
; /* don't call edit() now */
4098 * Command character that doesn't do anything, but unlike nv_ignore() does
4099 * start edit(). Used for "startinsert" executed while starting up.
4103 cmdarg_T
*cap UNUSED
;
4108 * Command character doesn't exist.
4114 clearopbeep(cap
->oap
);
4118 * <Help> and <F1> commands.
4124 if (!checkclearopq(cap
->oap
))
4129 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4135 if (!checkclearopq(cap
->oap
)
4136 && do_addsub((int)cap
->cmdchar
, cap
->count1
) == OK
)
4141 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4147 if (!checkclearop(cap
->oap
))
4150 if (mod_mask
& MOD_MASK_CTRL
)
4152 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4153 if (cap
->arg
== BACKWARD
)
4154 goto_tabpage(-(int)cap
->count1
);
4156 goto_tabpage((int)cap
->count0
);
4160 (void)onepage(cap
->arg
, cap
->count1
);
4165 * Implementation of "gd" and "gD" command.
4168 nv_gd(oap
, nchar
, thisblock
)
4171 int thisblock
; /* 1 for "1gd" and "1gD" */
4176 if ((len
= find_ident_under_cursor(&ptr
, FIND_IDENT
)) == 0
4177 || find_decl(ptr
, len
, nchar
== 'd', thisblock
, 0) == FAIL
)
4180 else if ((fdo_flags
& FDO_SEARCH
) && KeyTyped
&& oap
->op_type
== OP_NOP
)
4186 * Search for variable declaration of "ptr[len]".
4187 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4188 * current file ("gD").
4189 * When "thisblock" is TRUE check the {} block scope.
4190 * Return FAIL when not found.
4193 find_decl(ptr
, len
, locally
, thisblock
, searchflags
)
4198 int searchflags
; /* flags passed to searchit() */
4210 if ((pat
= alloc(len
+ 7)) == NULL
)
4213 /* Put "\V" before the pattern to avoid that the special meaning of "."
4214 * and "~" causes trouble. */
4215 sprintf((char *)pat
, vim_iswordp(ptr
) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4217 old_pos
= curwin
->w_cursor
;
4220 p_ws
= FALSE
; /* don't wrap around end of file now */
4221 p_scs
= FALSE
; /* don't switch ignorecase off now */
4224 * With "gD" go to line 1.
4225 * With "gd" Search back for the start of the current function, then go
4226 * back until a blank line. If this fails go to line 1.
4228 if (!locally
|| !findpar(&incll
, BACKWARD
, 1L, '{', FALSE
))
4230 setpcmark(); /* Set in findpar() otherwise */
4231 curwin
->w_cursor
.lnum
= 1;
4232 par_pos
= curwin
->w_cursor
;
4236 par_pos
= curwin
->w_cursor
;
4237 while (curwin
->w_cursor
.lnum
> 1 && *skipwhite(ml_get_curline()) != NUL
)
4238 --curwin
->w_cursor
.lnum
;
4240 curwin
->w_cursor
.col
= 0;
4242 /* Search forward for the identifier, ignore comment lines. */
4243 clearpos(&found_pos
);
4246 t
= searchit(curwin
, curbuf
, &curwin
->w_cursor
, FORWARD
,
4247 pat
, 1L, searchflags
, RE_LAST
, (linenr_T
)0, NULL
);
4248 if (curwin
->w_cursor
.lnum
>= old_pos
.lnum
)
4249 t
= FAIL
; /* match after start is failure too */
4251 if (thisblock
&& t
!= FAIL
)
4255 /* Check that the block the match is in doesn't end before the
4256 * position where we started the search from. */
4257 if ((pos
= findmatchlimit(NULL
, '}', FM_FORWARD
,
4258 (int)(old_pos
.lnum
- curwin
->w_cursor
.lnum
+ 1))) != NULL
4259 && pos
->lnum
< old_pos
.lnum
)
4265 /* If we previously found a valid position, use it. */
4266 if (found_pos
.lnum
!= 0)
4268 curwin
->w_cursor
= found_pos
;
4273 #ifdef FEAT_COMMENTS
4274 if (get_leader_len(ml_get_curline(), NULL
, FALSE
) > 0)
4276 /* Ignore this line, continue at start of next line. */
4277 ++curwin
->w_cursor
.lnum
;
4278 curwin
->w_cursor
.col
= 0;
4282 if (!locally
) /* global search: use first match found */
4284 if (curwin
->w_cursor
.lnum
>= par_pos
.lnum
)
4286 /* If we previously found a valid position, use it. */
4287 if (found_pos
.lnum
!= 0)
4288 curwin
->w_cursor
= found_pos
;
4292 /* For finding a local variable and the match is before the "{" search
4293 * to find a later match. For K&R style function declarations this
4294 * skips the function header without types. */
4295 found_pos
= curwin
->w_cursor
;
4301 curwin
->w_cursor
= old_pos
;
4305 curwin
->w_set_curswant
= TRUE
;
4306 /* "n" searches forward now */
4318 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4319 * lines rather than lines in the file.
4320 * 'dist' must be positive.
4322 * Return OK if able to move cursor, FAIL otherwise.
4325 nv_screengo(oap
, dir
, dist
)
4330 int linelen
= linetabsize(ml_get_curline());
4334 int col_off1
; /* margin offset for first screen line */
4335 int col_off2
; /* margin offset for wrapped screen line */
4336 int width1
; /* text width for first screen line */
4337 int width2
; /* test width for wrapped screen line */
4339 oap
->motion_type
= MCHAR
;
4340 oap
->inclusive
= FALSE
;
4342 col_off1
= curwin_col_off();
4343 col_off2
= col_off1
- curwin_col_off2();
4344 width1
= W_WIDTH(curwin
) - col_off1
;
4345 width2
= W_WIDTH(curwin
) - col_off2
;
4347 #ifdef FEAT_VERTSPLIT
4348 if (curwin
->w_width
!= 0)
4352 * Instead of sticking at the last character of the buffer line we
4353 * try to stick in the last column of the screen.
4355 if (curwin
->w_curswant
== MAXCOL
)
4360 curwin
->w_curswant
= 0;
4363 curwin
->w_curswant
= width1
- 1;
4364 if (curwin
->w_virtcol
> curwin
->w_curswant
)
4365 curwin
->w_curswant
+= ((curwin
->w_virtcol
4366 - curwin
->w_curswant
- 1) / width2
+ 1) * width2
;
4371 if (linelen
> width1
)
4372 n
= ((linelen
- width1
- 1) / width2
+ 1) * width2
+ width1
;
4375 if (curwin
->w_curswant
> (colnr_T
)n
+ 1)
4376 curwin
->w_curswant
-= ((curwin
->w_curswant
- n
) / width2
+ 1)
4382 if (dir
== BACKWARD
)
4384 if ((long)curwin
->w_curswant
>= width2
)
4385 /* move back within line */
4386 curwin
->w_curswant
-= width2
;
4389 /* to previous line */
4390 if (curwin
->w_cursor
.lnum
== 1)
4395 --curwin
->w_cursor
.lnum
;
4397 /* Move to the start of a closed fold. Don't do that when
4398 * 'foldopen' contains "all": it will open in a moment. */
4399 if (!(fdo_flags
& FDO_ALL
))
4400 (void)hasFolding(curwin
->w_cursor
.lnum
,
4401 &curwin
->w_cursor
.lnum
, NULL
);
4403 linelen
= linetabsize(ml_get_curline());
4404 if (linelen
> width1
)
4405 curwin
->w_curswant
+= (((linelen
- width1
- 1) / width2
)
4409 else /* dir == FORWARD */
4411 if (linelen
> width1
)
4412 n
= ((linelen
- width1
- 1) / width2
+ 1) * width2
+ width1
;
4415 if (curwin
->w_curswant
+ width2
< (colnr_T
)n
)
4416 /* move forward within line */
4417 curwin
->w_curswant
+= width2
;
4422 /* Move to the end of a closed fold. */
4423 (void)hasFolding(curwin
->w_cursor
.lnum
, NULL
,
4424 &curwin
->w_cursor
.lnum
);
4426 if (curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
)
4431 curwin
->w_cursor
.lnum
++;
4432 curwin
->w_curswant
%= width2
;
4436 #ifdef FEAT_VERTSPLIT
4440 coladvance(curwin
->w_curswant
);
4442 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4443 if (curwin
->w_cursor
.col
> 0 && curwin
->w_p_wrap
)
4446 * Check for landing on a character that got split at the end of the
4447 * last line. We want to advance a screenline, not end up in the same
4448 * screenline or move two screenlines.
4451 if (curwin
->w_virtcol
> curwin
->w_curswant
4452 && (curwin
->w_curswant
< (colnr_T
)width1
4453 ? (curwin
->w_curswant
> (colnr_T
)width1
/ 2)
4454 : ((curwin
->w_curswant
- width1
) % width2
4455 > (colnr_T
)width2
/ 2)))
4456 --curwin
->w_cursor
.col
;
4461 curwin
->w_curswant
= MAXCOL
; /* stick in the last column */
4468 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4469 * when Shift or Ctrl is used.
4470 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4476 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4477 win_T
*old_curwin
= curwin
;
4479 /* Currently we only get the mouse coordinates in the GUI. */
4480 if (gui
.in_use
&& mouse_row
>= 0 && mouse_col
>= 0)
4487 /* find the window at the pointer coordinates */
4488 curwin
= mouse_find_win(&row
, &col
);
4489 curbuf
= curwin
->w_buffer
;
4493 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
4495 (void)onepage(cap
->arg
? FORWARD
: BACKWARD
, 1L);
4501 nv_scroll_line(cap
);
4504 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4505 curwin
->w_redr_status
= TRUE
;
4507 curwin
= old_curwin
;
4508 curbuf
= curwin
->w_buffer
;
4513 * Mouse clicks and drags.
4519 (void)do_mouse(cap
->oap
, cap
->cmdchar
, BACKWARD
, cap
->count1
, 0);
4524 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4525 * cap->arg must be TRUE for CTRL-E.
4531 if (!checkclearop(cap
->oap
))
4532 scroll_redraw(cap
->arg
, cap
->count1
);
4536 * Scroll "count" lines up or down, and redraw.
4539 scroll_redraw(up
, count
)
4543 linenr_T prev_topline
= curwin
->w_topline
;
4545 int prev_topfill
= curwin
->w_topfill
;
4547 linenr_T prev_lnum
= curwin
->w_cursor
.lnum
;
4550 scrollup(count
, TRUE
);
4552 scrolldown(count
, TRUE
);
4555 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4556 * valid, otherwise the screen jumps back at the end of the file. */
4558 check_cursor_moved(curwin
);
4559 curwin
->w_valid
|= VALID_TOPLINE
;
4561 /* If moved back to where we were, at least move the cursor, otherwise
4562 * we get stuck at one position. Don't move the cursor up if the
4563 * first line of the buffer is already on the screen */
4564 while (curwin
->w_topline
== prev_topline
4566 && curwin
->w_topfill
== prev_topfill
4572 if (curwin
->w_cursor
.lnum
> prev_lnum
4573 || cursor_down(1L, FALSE
) == FAIL
)
4578 if (curwin
->w_cursor
.lnum
< prev_lnum
4579 || prev_topline
== 1L
4580 || cursor_up(1L, FALSE
) == FAIL
)
4583 /* Mark w_topline as valid, otherwise the screen jumps back at the
4584 * end of the file. */
4585 check_cursor_moved(curwin
);
4586 curwin
->w_valid
|= VALID_TOPLINE
;
4589 if (curwin
->w_cursor
.lnum
!= prev_lnum
)
4590 coladvance(curwin
->w_curswant
);
4591 redraw_later(VALID
);
4595 * Commands that start with "z".
4603 int nchar
= cap
->nchar
;
4605 long old_fdl
= curwin
->w_p_fdl
;
4606 int old_fen
= curwin
->w_p_fen
;
4612 if (VIM_ISDIGIT(nchar
))
4615 * "z123{nchar}": edit the count before obtaining {nchar}
4617 if (checkclearop(cap
->oap
))
4622 #ifdef USE_ON_FLY_SCROLL
4623 dont_scroll
= TRUE
; /* disallow scrolling here */
4626 ++allow_keys
; /* no mapping for nchar, but allow key codes */
4627 nchar
= plain_vgetc();
4628 LANGMAP_ADJUST(nchar
, TRUE
);
4631 #ifdef FEAT_CMDL_INFO
4632 (void)add_to_showcmd(nchar
);
4634 if (nchar
== K_DEL
|| nchar
== K_KDEL
)
4636 else if (VIM_ISDIGIT(nchar
))
4637 n
= n
* 10 + (nchar
- '0');
4638 else if (nchar
== CAR
)
4641 need_mouse_correct
= TRUE
;
4643 win_setheight((int)n
);
4646 else if (nchar
== 'l'
4649 || nchar
== K_RIGHT
)
4651 cap
->count1
= n
? n
* cap
->count1
: cap
->count1
;
4656 clearopbeep(cap
->oap
);
4660 cap
->oap
->op_type
= OP_NOP
;
4667 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4668 * and "zC" only in Visual mode. "zj" and "zk" are motion
4670 cap
->nchar
!= 'f' && cap
->nchar
!= 'F'
4671 && !(VIsual_active
&& vim_strchr((char_u
*)"dcCoO", cap
->nchar
))
4672 && cap
->nchar
!= 'j' && cap
->nchar
!= 'k'
4675 checkclearop(cap
->oap
))
4679 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4680 * If line number given, set cursor.
4682 if ((vim_strchr((char_u
*)"+\r\nt.z^-b", nchar
) != NULL
)
4684 && cap
->count0
!= curwin
->w_cursor
.lnum
)
4687 if (cap
->count0
> curbuf
->b_ml
.ml_line_count
)
4688 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
4690 curwin
->w_cursor
.lnum
= cap
->count0
;
4696 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4698 if (cap
->count0
== 0)
4700 /* No count given: put cursor at the line below screen */
4701 validate_botline(); /* make sure w_botline is valid */
4702 if (curwin
->w_botline
> curbuf
->b_ml
.ml_line_count
)
4703 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
4705 curwin
->w_cursor
.lnum
= curwin
->w_botline
;
4711 beginline(BL_WHITE
| BL_FIX
);
4714 case 't': scroll_cursor_top(0, TRUE
);
4715 redraw_later(VALID
);
4718 /* "z." and "zz": put cursor in middle of screen */
4719 case '.': beginline(BL_WHITE
| BL_FIX
);
4722 case 'z': scroll_cursor_halfway(TRUE
);
4723 redraw_later(VALID
);
4726 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4727 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4728 * when <count> is at bottom of window, and puts that one at
4729 * bottom of window. */
4730 if (cap
->count0
!= 0)
4732 scroll_cursor_bot(0, TRUE
);
4733 curwin
->w_cursor
.lnum
= curwin
->w_topline
;
4735 else if (curwin
->w_topline
== 1)
4736 curwin
->w_cursor
.lnum
= 1;
4738 curwin
->w_cursor
.lnum
= curwin
->w_topline
- 1;
4741 beginline(BL_WHITE
| BL_FIX
);
4744 case 'b': scroll_cursor_bot(0, TRUE
);
4745 redraw_later(VALID
);
4748 /* "zH" - scroll screen right half-page */
4750 cap
->count1
*= W_WIDTH(curwin
) / 2;
4753 /* "zh" - scroll screen to the right */
4756 if (!curwin
->w_p_wrap
)
4758 if ((colnr_T
)cap
->count1
> curwin
->w_leftcol
)
4759 curwin
->w_leftcol
= 0;
4761 curwin
->w_leftcol
-= (colnr_T
)cap
->count1
;
4766 /* "zL" - scroll screen left half-page */
4767 case 'L': cap
->count1
*= W_WIDTH(curwin
) / 2;
4770 /* "zl" - scroll screen to the left */
4773 if (!curwin
->w_p_wrap
)
4775 /* scroll the window left */
4776 curwin
->w_leftcol
+= (colnr_T
)cap
->count1
;
4781 /* "zs" - scroll screen, cursor at the start */
4782 case 's': if (!curwin
->w_p_wrap
)
4785 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4786 col
= 0; /* like the cursor is in col 0 */
4789 getvcol(curwin
, &curwin
->w_cursor
, &col
, NULL
, NULL
);
4790 if ((long)col
> p_siso
)
4794 if (curwin
->w_leftcol
!= col
)
4796 curwin
->w_leftcol
= col
;
4797 redraw_later(NOT_VALID
);
4802 /* "ze" - scroll screen, cursor at the end */
4803 case 'e': if (!curwin
->w_p_wrap
)
4806 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4807 col
= 0; /* like the cursor is in col 0 */
4810 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &col
);
4811 n
= W_WIDTH(curwin
) - curwin_col_off();
4812 if ((long)col
+ p_siso
< n
)
4815 col
= col
+ p_siso
- n
+ 1;
4816 if (curwin
->w_leftcol
!= col
)
4818 curwin
->w_leftcol
= col
;
4819 redraw_later(NOT_VALID
);
4825 /* "zF": create fold command */
4826 /* "zf": create fold operator */
4828 case 'f': if (foldManualAllowed(TRUE
))
4832 curwin
->w_p_fen
= TRUE
;
4834 /* "zF" is like "zfzf" */
4835 if (nchar
== 'F' && cap
->oap
->op_type
== OP_FOLD
)
4842 clearopbeep(cap
->oap
);
4845 /* "zd": delete fold at cursor */
4846 /* "zD": delete fold at cursor recursively */
4848 case 'D': if (foldManualAllowed(FALSE
))
4853 deleteFold(curwin
->w_cursor
.lnum
,
4854 curwin
->w_cursor
.lnum
, nchar
== 'D', FALSE
);
4858 /* "zE": erease all folds */
4859 case 'E': if (foldmethodIsManual(curwin
))
4861 clearFolding(curwin
);
4862 changed_window_setting();
4864 else if (foldmethodIsMarker(curwin
))
4865 deleteFold((linenr_T
)1, curbuf
->b_ml
.ml_line_count
,
4868 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4871 /* "zn": fold none: reset 'foldenable' */
4872 case 'n': curwin
->w_p_fen
= FALSE
;
4875 /* "zN": fold Normal: set 'foldenable' */
4876 case 'N': curwin
->w_p_fen
= TRUE
;
4879 /* "zi": invert folding: toggle 'foldenable' */
4880 case 'i': curwin
->w_p_fen
= !curwin
->w_p_fen
;
4883 /* "za": open closed fold or close open fold at cursor */
4884 case 'a': if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4885 openFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4888 closeFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4889 curwin
->w_p_fen
= TRUE
;
4893 /* "zA": open fold at cursor recursively */
4894 case 'A': if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4895 openFoldRecurse(curwin
->w_cursor
.lnum
);
4898 closeFoldRecurse(curwin
->w_cursor
.lnum
);
4899 curwin
->w_p_fen
= TRUE
;
4903 /* "zo": open fold at cursor or Visual area */
4904 case 'o': if (VIsual_active
)
4907 openFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4910 /* "zO": open fold recursively */
4911 case 'O': if (VIsual_active
)
4914 openFoldRecurse(curwin
->w_cursor
.lnum
);
4917 /* "zc": close fold at cursor or Visual area */
4918 case 'c': if (VIsual_active
)
4921 closeFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4922 curwin
->w_p_fen
= TRUE
;
4925 /* "zC": close fold recursively */
4926 case 'C': if (VIsual_active
)
4929 closeFoldRecurse(curwin
->w_cursor
.lnum
);
4930 curwin
->w_p_fen
= TRUE
;
4933 /* "zv": open folds at the cursor */
4934 case 'v': foldOpenCursor();
4937 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4938 case 'x': curwin
->w_p_fen
= TRUE
;
4939 newFoldLevel(); /* update right now */
4943 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4944 case 'X': curwin
->w_p_fen
= TRUE
;
4945 old_fdl
= -1; /* force an update */
4948 /* "zm": fold more */
4949 case 'm': if (curwin
->w_p_fdl
> 0)
4951 old_fdl
= -1; /* force an update */
4952 curwin
->w_p_fen
= TRUE
;
4955 /* "zM": close all folds */
4956 case 'M': curwin
->w_p_fdl
= 0;
4957 old_fdl
= -1; /* force an update */
4958 curwin
->w_p_fen
= TRUE
;
4961 /* "zr": reduce folding */
4962 case 'r': ++curwin
->w_p_fdl
;
4965 /* "zR": open all folds */
4966 case 'R': curwin
->w_p_fdl
= getDeepestNesting();
4967 old_fdl
= -1; /* force an update */
4970 case 'j': /* "zj" move to next fold downwards */
4971 case 'k': /* "zk" move to next fold upwards */
4972 if (foldMoveTo(TRUE
, nchar
== 'j' ? FORWARD
: BACKWARD
,
4973 cap
->count1
) == FAIL
)
4974 clearopbeep(cap
->oap
);
4977 #endif /* FEAT_FOLDING */
4980 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4982 ++allow_keys
; /* no mapping for nchar, but allow key codes */
4983 nchar
= plain_vgetc();
4984 LANGMAP_ADJUST(nchar
, TRUE
);
4987 #ifdef FEAT_CMDL_INFO
4988 (void)add_to_showcmd(nchar
);
4990 if (vim_strchr((char_u
*)"gGwW", nchar
) == NULL
)
4992 clearopbeep(cap
->oap
);
4998 case 'g': /* "zg": add good word to word list */
4999 case 'w': /* "zw": add wrong word to word list */
5000 case 'G': /* "zG": add good word to temp word list */
5001 case 'W': /* "zW": add wrong word to temp word list */
5006 if (checkclearop(cap
->oap
))
5009 if (VIsual_active
&& get_visual_text(cap
, &ptr
, &len
)
5015 pos_T pos
= curwin
->w_cursor
;
5017 /* Find bad word under the cursor. */
5018 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, NULL
);
5019 if (len
!= 0 && curwin
->w_cursor
.col
<= pos
.col
)
5020 ptr
= ml_get_pos(&curwin
->w_cursor
);
5021 curwin
->w_cursor
= pos
;
5024 if (ptr
== NULL
&& (len
= find_ident_under_cursor(&ptr
,
5027 spell_add_word(ptr
, len
, nchar
== 'w' || nchar
== 'W',
5028 (nchar
== 'G' || nchar
== 'W')
5029 ? 0 : (int)cap
->count1
,
5034 case '=': /* "z=": suggestions for a badly spelled word */
5035 if (!checkclearop(cap
->oap
))
5036 spell_suggest((int)cap
->count0
);
5040 default: clearopbeep(cap
->oap
);
5044 /* Redraw when 'foldenable' changed */
5045 if (old_fen
!= curwin
->w_p_fen
)
5050 if (foldmethodIsDiff(curwin
) && curwin
->w_p_scb
)
5052 /* Adjust 'foldenable' in diff-synced windows. */
5055 if (wp
!= curwin
&& foldmethodIsDiff(wp
) && wp
->w_p_scb
)
5057 wp
->w_p_fen
= curwin
->w_p_fen
;
5058 changed_window_setting_win(wp
);
5063 changed_window_setting();
5066 /* Redraw when 'foldlevel' changed. */
5067 if (old_fdl
!= curwin
->w_p_fdl
)
5074 * Vertical scrollbar movement.
5077 nv_ver_scrollbar(cap
)
5080 if (cap
->oap
->op_type
!= OP_NOP
)
5081 clearopbeep(cap
->oap
);
5083 /* Even if an operator was pending, we still want to scroll */
5088 * Horizontal scrollbar movement.
5091 nv_hor_scrollbar(cap
)
5094 if (cap
->oap
->op_type
!= OP_NOP
)
5095 clearopbeep(cap
->oap
);
5097 /* Even if an operator was pending, we still want to scroll */
5098 gui_do_horiz_scroll();
5102 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5110 if (cap
->oap
->op_type
!= OP_NOP
)
5111 clearopbeep(cap
->oap
);
5113 /* Even if an operator was pending, we still want to jump tabs. */
5114 goto_tabpage(current_tab
);
5118 * Selected item in tab line menu.
5124 if (cap
->oap
->op_type
!= OP_NOP
)
5125 clearopbeep(cap
->oap
);
5127 /* Even if an operator was pending, we still want to jump tabs. */
5132 * Handle selecting an item of the GUI tab line menu.
5133 * Used in Normal and Insert mode.
5138 switch (current_tabmenu
)
5140 case TABLINE_MENU_CLOSE
:
5141 if (current_tab
== 0)
5142 do_cmdline_cmd((char_u
*)"tabclose");
5145 vim_snprintf((char *)IObuff
, IOSIZE
, "tabclose %d",
5147 do_cmdline_cmd(IObuff
);
5151 case TABLINE_MENU_NEW
:
5152 vim_snprintf((char *)IObuff
, IOSIZE
, "%dtabnew",
5153 current_tab
> 0 ? current_tab
- 1 : 999);
5154 do_cmdline_cmd(IObuff
);
5157 case TABLINE_MENU_OPEN
:
5158 vim_snprintf((char *)IObuff
, IOSIZE
, "browse %dtabnew",
5159 current_tab
> 0 ? current_tab
- 1 : 999);
5160 do_cmdline_cmd(IObuff
);
5174 * Ignore 'Q' in Visual mode, just give a beep.
5181 if (!checkclearop(cap
->oap
))
5186 * Handle a ":" command.
5200 if (cap
->oap
->op_type
!= OP_NOP
)
5202 /* Using ":" as a movement is characterwise exclusive. */
5203 cap
->oap
->motion_type
= MCHAR
;
5204 cap
->oap
->inclusive
= FALSE
;
5206 else if (cap
->count0
)
5208 /* translate "count:" into ":.,.+(count - 1)" */
5209 stuffcharReadbuff('.');
5210 if (cap
->count0
> 1)
5212 stuffReadbuff((char_u
*)",.+");
5213 stuffnumReadbuff((long)cap
->count0
- 1L);
5217 /* When typing, don't type below an old message */
5223 /* get a command line and execute it */
5224 do_cmdline(NULL
, getexline
, NULL
,
5225 cap
->oap
->op_type
!= OP_NOP
? DOCMD_KEEPLINE
: 0);
5227 /* If 'insertmode' changed, enter or exit Insert mode */
5228 if (p_im
!= old_p_im
)
5236 /* The start of the operator may have become invalid by the Ex
5238 if (cap
->oap
->op_type
!= OP_NOP
5239 && (cap
->oap
->start
.lnum
> curbuf
->b_ml
.ml_line_count
5240 || cap
->oap
->start
.col
>
5241 (colnr_T
)STRLEN(ml_get(cap
->oap
->start
.lnum
))))
5242 clearopbeep(cap
->oap
);
5247 * Handle CTRL-G command.
5254 if (VIsual_active
) /* toggle Selection/Visual mode */
5256 VIsual_select
= !VIsual_select
;
5261 if (!checkclearop(cap
->oap
))
5262 /* print full name if count given or :cd used */
5263 fileinfo((int)cap
->count0
, FALSE
, TRUE
);
5267 * Handle CTRL-H <Backspace> command.
5274 if (VIsual_active
&& VIsual_select
)
5276 cap
->cmdchar
= 'x'; /* BS key behaves like 'x' in Select mode */
5285 * CTRL-L: clear screen and redraw.
5291 if (!checkclearop(cap
->oap
))
5293 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5295 * Right now, the BeBox doesn't seem to have an easy way to detect
5296 * window resizing, so we cheat and make the user detect it
5297 * manually with CTRL-L instead
5302 /* Clear all syntax states to force resyncing. */
5303 syn_stack_free_all(curbuf
);
5305 redraw_later(CLEAR
);
5310 * CTRL-O: In Select mode: switch to Visual mode for one command.
5311 * Otherwise: Go to older pcmark.
5318 if (VIsual_active
&& VIsual_select
)
5320 VIsual_select
= FALSE
;
5322 restart_VIsual_select
= 2; /* restart Select mode later */
5327 cap
->count1
= -cap
->count1
;
5333 * CTRL-^ command, short for ":e #"
5339 if (!checkclearopq(cap
->oap
))
5340 (void)buflist_getfile((int)cap
->count0
, (linenr_T
)0,
5341 GETF_SETMARK
|GETF_ALT
, FALSE
);
5351 if (!checkclearopq(cap
->oap
))
5355 /* "ZZ": equivalent to ":x". */
5356 case 'Z': do_cmdline_cmd((char_u
*)"x");
5359 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5360 case 'Q': do_cmdline_cmd((char_u
*)"q!");
5363 default: clearopbeep(cap
->oap
);
5368 #if defined(FEAT_WINDOWS) || defined(PROTO)
5370 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5381 vim_memset(&ca
, 0, sizeof(ca
));
5390 * Handle the commands that use the word under the cursor.
5391 * [g] CTRL-] :ta to current identifier
5392 * [g] 'K' run program for current identifier
5393 * [g] '*' / to current identifier or string
5394 * [g] '#' ? to current identifier or string
5395 * g ']' :tselect for current identifier
5404 char_u
*kp
; /* value of 'keywordprg' */
5405 int kp_help
; /* 'keywordprg' is ":help" */
5406 int n
= 0; /* init for GCC */
5408 int g_cmd
; /* "g" command */
5409 int tag_cmd
= FALSE
;
5414 if (cap
->cmdchar
== 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5416 cmdchar
= cap
->nchar
;
5421 cmdchar
= cap
->cmdchar
;
5425 if (cmdchar
== POUND
) /* the pound sign, '#' for English keyboards */
5429 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5431 if (cmdchar
== ']' || cmdchar
== Ctrl_RSB
|| cmdchar
== 'K')
5434 if (VIsual_active
&& get_visual_text(cap
, &ptr
, &n
) == FAIL
)
5437 if (checkclearopq(cap
->oap
))
5441 if (ptr
== NULL
&& (n
= find_ident_under_cursor(&ptr
,
5442 (cmdchar
== '*' || cmdchar
== '#')
5443 ? FIND_IDENT
|FIND_STRING
: FIND_IDENT
)) == 0)
5449 /* Allocate buffer to put the command in. Inserting backslashes can
5450 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5451 * and some numbers. */
5452 kp
= (*curbuf
->b_p_kp
== NUL
? p_kp
: curbuf
->b_p_kp
);
5453 kp_help
= (*kp
== NUL
|| STRCMP(kp
, ":he") == 0
5454 || STRCMP(kp
, ":help") == 0);
5455 buf
= alloc((unsigned)(n
* 2 + 30 + STRLEN(kp
)));
5465 * Put cursor at start of word, makes search skip the word
5467 * Call setpcmark() first, so "*``" puts the cursor back where
5471 curwin
->w_cursor
.col
= (colnr_T
) (ptr
- ml_get_curline());
5473 if (!g_cmd
&& vim_iswordp(ptr
))
5475 no_smartcase
= TRUE
; /* don't use 'smartcase' now */
5480 STRCPY(buf
, "he! ");
5483 /* An external command will probably use an argument starting
5484 * with "-" as an option. To avoid trouble we skip the "-". */
5485 while (*ptr
== '-' && n
> 0)
5492 EMSG(_(e_noident
)); /* found dashes only */
5497 /* When a count is given, turn it into a range. Is this
5498 * really what we want? */
5499 isman
= (STRCMP(kp
, "man") == 0);
5500 isman_s
= (STRCMP(kp
, "man -s") == 0);
5501 if (cap
->count0
!= 0 && !(isman
|| isman_s
))
5502 sprintf((char *)buf
, ".,.+%ld", cap
->count0
- 1);
5505 if (cap
->count0
== 0 && isman_s
)
5510 if (cap
->count0
!= 0 && (isman
|| isman_s
))
5512 sprintf((char *)buf
+ STRLEN(buf
), "%ld", cap
->count0
);
5522 STRCPY(buf
, "cstag ");
5530 STRCPY(buf
, "he! ");
5537 sprintf((char *)buf
, "%ldta ", cap
->count0
);
5542 * Now grab the chars in the identifier
5544 if (cmdchar
== 'K' && !kp_help
)
5546 /* Escape the argument properly for a shell command */
5547 ptr
= vim_strnsave(ptr
, n
);
5548 p
= vim_strsave_shellescape(ptr
, TRUE
);
5555 buf
= (char_u
*)vim_realloc(buf
, STRLEN(buf
) + STRLEN(p
) + 1);
5568 aux_ptr
= (char_u
*)(p_magic
? "/.*~[^$\\" : "/^$\\");
5569 else if (cmdchar
== '#')
5570 aux_ptr
= (char_u
*)(p_magic
? "/?.*~[^$\\" : "/?^$\\");
5572 /* Don't escape spaces and Tabs in a tag with a backslash */
5573 aux_ptr
= (char_u
*)"\\|\"\n[";
5575 aux_ptr
= (char_u
*)"\\|\"\n*?[";
5577 p
= buf
+ STRLEN(buf
);
5580 /* put a backslash before \ and some others */
5581 if (vim_strchr(aux_ptr
, *ptr
) != NULL
)
5584 /* When current byte is a part of multibyte character, copy all
5585 * bytes of that character. */
5589 int len
= (*mb_ptr2len
)(ptr
) - 1;
5591 for (i
= 0; i
< len
&& n
>= 1; ++i
, --n
)
5601 * Execute the command.
5603 if (cmdchar
== '*' || cmdchar
== '#')
5607 has_mbyte
? vim_iswordp(mb_prevptr(ml_get_curline(), ptr
)) :
5609 vim_iswordc(ptr
[-1])))
5612 /* 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
)
5826 || (PAST_LINE
&& *ml_get_cursor() == NUL
)
5831 * <Space> wraps to next line if 'whichwrap' has 's'.
5832 * 'l' wraps to next line if 'whichwrap' has 'l'.
5833 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5835 if ( ((cap
->cmdchar
== ' '
5836 && vim_strchr(p_ww
, 's') != NULL
)
5837 || (cap
->cmdchar
== 'l'
5838 && vim_strchr(p_ww
, 'l') != NULL
)
5839 || (cap
->cmdchar
== K_RIGHT
5840 && vim_strchr(p_ww
, '>') != NULL
))
5841 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
5843 /* When deleting we also count the NL as a character.
5844 * Set cap->oap->inclusive when last char in the line is
5845 * included, move to next line after that */
5846 if ( cap
->oap
->op_type
!= OP_NOP
5847 && !cap
->oap
->inclusive
5848 && !lineempty(curwin
->w_cursor
.lnum
))
5849 cap
->oap
->inclusive
= TRUE
;
5852 ++curwin
->w_cursor
.lnum
;
5853 curwin
->w_cursor
.col
= 0;
5854 #ifdef FEAT_VIRTUALEDIT
5855 curwin
->w_cursor
.coladd
= 0;
5857 curwin
->w_set_curswant
= TRUE
;
5858 cap
->oap
->inclusive
= FALSE
;
5862 if (cap
->oap
->op_type
== OP_NOP
)
5864 /* Only beep and flush if not moved at all */
5865 if (n
== cap
->count1
)
5870 if (!lineempty(curwin
->w_cursor
.lnum
))
5871 cap
->oap
->inclusive
= TRUE
;
5878 curwin
->w_set_curswant
= TRUE
;
5879 # ifdef FEAT_VIRTUALEDIT
5880 if (virtual_active())
5887 curwin
->w_cursor
.col
+=
5888 (*mb_ptr2len
)(ml_get_cursor());
5891 ++curwin
->w_cursor
.col
;
5897 if (n
!= cap
->count1
&& (fdo_flags
& FDO_HOR
) && KeyTyped
5898 && cap
->oap
->op_type
== OP_NOP
)
5904 * Cursor left commands.
5906 * Returns TRUE when operator end should not be adjusted.
5914 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
5916 /* <C-Left> and <S-Left> move a word or WORD left */
5917 if (mod_mask
& MOD_MASK_CTRL
)
5923 cap
->oap
->motion_type
= MCHAR
;
5924 cap
->oap
->inclusive
= FALSE
;
5925 for (n
= cap
->count1
; n
> 0; --n
)
5927 if (oneleft() == FAIL
)
5929 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5930 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5931 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5933 if ( (((cap
->cmdchar
== K_BS
5934 || cap
->cmdchar
== Ctrl_H
)
5935 && vim_strchr(p_ww
, 'b') != NULL
)
5936 || (cap
->cmdchar
== 'h'
5937 && vim_strchr(p_ww
, 'h') != NULL
)
5938 || (cap
->cmdchar
== K_LEFT
5939 && vim_strchr(p_ww
, '<') != NULL
))
5940 && curwin
->w_cursor
.lnum
> 1)
5942 --(curwin
->w_cursor
.lnum
);
5943 coladvance((colnr_T
)MAXCOL
);
5944 curwin
->w_set_curswant
= TRUE
;
5946 /* When the NL before the first char has to be deleted we
5947 * put the cursor on the NUL after the previous line.
5948 * This is a very special case, be careful!
5949 * Don't adjust op_end now, otherwise it won't work. */
5950 if ( (cap
->oap
->op_type
== OP_DELETE
5951 || cap
->oap
->op_type
== OP_CHANGE
)
5952 && !lineempty(curwin
->w_cursor
.lnum
))
5954 if (*ml_get_cursor() != NUL
)
5955 ++curwin
->w_cursor
.col
;
5956 cap
->retval
|= CA_NO_ADJ_OP_END
;
5960 /* Only beep and flush if not moved at all */
5961 else if (cap
->oap
->op_type
== OP_NOP
&& n
== cap
->count1
)
5967 if (n
!= cap
->count1
&& (fdo_flags
& FDO_HOR
) && KeyTyped
5968 && cap
->oap
->op_type
== OP_NOP
)
5974 * Cursor up commands.
5975 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5981 if (mod_mask
& MOD_MASK_SHIFT
)
5983 /* <S-Up> is page up */
5984 cap
->arg
= BACKWARD
;
5989 cap
->oap
->motion_type
= MLINE
;
5990 if (cursor_up(cap
->count1
, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
5991 clearopbeep(cap
->oap
);
5993 beginline(BL_WHITE
| BL_FIX
);
5998 * Cursor down commands.
5999 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
6005 if (mod_mask
& MOD_MASK_SHIFT
)
6007 /* <S-Down> is page down */
6012 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
6013 /* In a quickfix window a <CR> jumps to the error under the cursor. */
6014 if (bt_quickfix(curbuf
) && cap
->cmdchar
== CAR
)
6015 if (curwin
->w_llist_ref
== NULL
)
6016 do_cmdline_cmd((char_u
*)".cc"); /* quickfix window */
6018 do_cmdline_cmd((char_u
*)".ll"); /* location list window */
6023 /* In the cmdline window a <CR> executes the command. */
6024 if (cmdwin_type
!= 0 && cap
->cmdchar
== CAR
)
6025 cmdwin_result
= CAR
;
6029 cap
->oap
->motion_type
= MLINE
;
6030 if (cursor_down(cap
->count1
, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
6031 clearopbeep(cap
->oap
);
6033 beginline(BL_WHITE
| BL_FIX
);
6038 #ifdef FEAT_SEARCHPATH
6040 * Grab the file name under the cursor and edit it.
6051 clearopbeep(cap
->oap
);
6056 if (curbuf_locked())
6063 ptr
= grab_file_name(cap
->count1
, &lnum
);
6067 /* do autowrite if necessary */
6068 if (curbufIsChanged() && curbuf
->b_nwindows
<= 1 && !P_HID(curbuf
))
6069 autowrite(curbuf
, FALSE
);
6071 (void)do_ecmd(0, ptr
, NULL
, NULL
, ECMD_LAST
,
6072 P_HID(curbuf
) ? ECMD_HIDE
: 0, curwin
);
6073 if (cap
->nchar
== 'F' && lnum
>= 0)
6075 curwin
->w_cursor
.lnum
= lnum
;
6076 check_cursor_lnum();
6077 beginline(BL_SOL
| BL_FIX
);
6087 * <End> command: to end of current line or last line.
6093 if (cap
->arg
|| (mod_mask
& MOD_MASK_CTRL
)) /* CTRL-END = goto last line */
6097 cap
->count1
= 1; /* to end of current line */
6103 * Handle the "$" command.
6109 cap
->oap
->motion_type
= MCHAR
;
6110 cap
->oap
->inclusive
= TRUE
;
6111 #ifdef FEAT_VIRTUALEDIT
6112 /* In virtual mode when off the edge of a line and an operator
6113 * is pending (whew!) keep the cursor where it is.
6114 * Otherwise, send it to the end of the line. */
6115 if (!virtual_active() || gchar_cursor() != NUL
6116 || cap
->oap
->op_type
== OP_NOP
)
6118 curwin
->w_curswant
= MAXCOL
; /* so we stay at the end */
6119 if (cursor_down((long)(cap
->count1
- 1),
6120 cap
->oap
->op_type
== OP_NOP
) == FAIL
)
6121 clearopbeep(cap
->oap
);
6123 else if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6129 * Implementation of '?' and '/' commands.
6130 * If cap->arg is TRUE don't set PC mark.
6136 oparg_T
*oap
= cap
->oap
;
6138 if (cap
->cmdchar
== '?' && cap
->oap
->op_type
== OP_ROT13
)
6140 /* Translate "g??" to "g?g?" */
6147 cap
->searchbuf
= getcmdline(cap
->cmdchar
, cap
->count1
, 0);
6149 if (cap
->searchbuf
== NULL
)
6155 normal_search(cap
, cap
->cmdchar
, cap
->searchbuf
,
6156 (cap
->arg
? 0 : SEARCH_MARK
));
6160 * Handle "N" and "n" commands.
6161 * cap->arg is SEARCH_REV for "N", 0 for "n".
6167 normal_search(cap
, 0, NULL
, SEARCH_MARK
| cap
->arg
);
6171 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6172 * Uses only cap->count1 and cap->oap from "cap".
6175 normal_search(cap
, dir
, pat
, opt
)
6179 int opt
; /* extra flags for do_search() */
6183 cap
->oap
->motion_type
= MCHAR
;
6184 cap
->oap
->inclusive
= FALSE
;
6185 cap
->oap
->use_reg_one
= TRUE
;
6186 curwin
->w_set_curswant
= TRUE
;
6188 i
= do_search(cap
->oap
, dir
, pat
, cap
->count1
,
6189 opt
| SEARCH_OPT
| SEARCH_ECHO
| SEARCH_MSG
, NULL
);
6195 cap
->oap
->motion_type
= MLINE
;
6196 #ifdef FEAT_VIRTUALEDIT
6197 curwin
->w_cursor
.coladd
= 0;
6200 if (cap
->oap
->op_type
== OP_NOP
&& (fdo_flags
& FDO_SEARCH
) && KeyTyped
)
6205 /* "/$" will put the cursor after the end of the line, may need to
6206 * correct that here */
6211 * Character search commands.
6212 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6213 * ',' and FALSE for ';'.
6214 * cap->nchar is NUL for ',' and ';' (repeat the search)
6222 if (cap
->cmdchar
== 't' || cap
->cmdchar
== 'T')
6227 cap
->oap
->motion_type
= MCHAR
;
6228 if (IS_SPECIAL(cap
->nchar
) || searchc(cap
, t_cmd
) == FAIL
)
6229 clearopbeep(cap
->oap
);
6232 curwin
->w_set_curswant
= TRUE
;
6233 #ifdef FEAT_VIRTUALEDIT
6234 /* Include a Tab for "tx" and for "dfx". */
6235 if (gchar_cursor() == TAB
&& virtual_active() && cap
->arg
== FORWARD
6236 && (t_cmd
|| cap
->oap
->op_type
!= OP_NOP
))
6240 getvcol(curwin
, &curwin
->w_cursor
, &scol
, NULL
, &ecol
);
6241 curwin
->w_cursor
.coladd
= ecol
- scol
;
6244 curwin
->w_cursor
.coladd
= 0;
6247 adjust_for_sel(cap
);
6250 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6257 * "[" and "]" commands.
6258 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6266 pos_T
*pos
= NULL
; /* init for GCC */
6267 pos_T old_pos
; /* cursor position before command */
6273 cap
->oap
->motion_type
= MCHAR
;
6274 cap
->oap
->inclusive
= FALSE
;
6275 old_pos
= curwin
->w_cursor
;
6276 #ifdef FEAT_VIRTUALEDIT
6277 curwin
->w_cursor
.coladd
= 0; /* TODO: don't do this for an error. */
6280 #ifdef FEAT_SEARCHPATH
6282 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6284 if (cap
->nchar
== 'f')
6291 * Find the occurrence(s) of the identifier or define under cursor
6292 * in current and included files or jump to the first occurrence.
6295 * fwd bwd fwd bwd fwd bwd
6296 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6297 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6299 if (vim_strchr((char_u
*)
6305 cap
->nchar
) != NULL
)
6310 if ((len
= find_ident_under_cursor(&ptr
, FIND_IDENT
)) == 0)
6314 find_pattern_in_path(ptr
, 0, len
, TRUE
,
6315 cap
->count0
== 0 ? !isupper(cap
->nchar
) : FALSE
,
6316 ((cap
->nchar
& 0xf) == ('d' & 0xf)) ? FIND_DEFINE
: FIND_ANY
,
6318 isupper(cap
->nchar
) ? ACTION_SHOW_ALL
:
6319 islower(cap
->nchar
) ? ACTION_SHOW
: ACTION_GOTO
,
6320 cap
->cmdchar
== ']' ? curwin
->w_cursor
.lnum
+ 1 : (linenr_T
)1,
6322 curwin
->w_set_curswant
= TRUE
;
6329 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6330 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6331 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6332 * "[m" or "]m" search for prev/next start of (Java) method.
6333 * "[M" or "]M" search for prev/next end of (Java) method.
6335 if ( (cap
->cmdchar
== '['
6336 && vim_strchr((char_u
*)"{(*/#mM", cap
->nchar
) != NULL
)
6337 || (cap
->cmdchar
== ']'
6338 && vim_strchr((char_u
*)"})*/#mM", cap
->nchar
) != NULL
))
6340 if (cap
->nchar
== '*')
6344 if (cap
->nchar
== 'm' || cap
->nchar
== 'M')
6346 if (cap
->cmdchar
== '[')
6359 if ((pos
= findmatchlimit(cap
->oap
, findc
,
6360 (cap
->cmdchar
== '[') ? FM_BACKWARD
: FM_FORWARD
, 0)) == NULL
)
6362 if (new_pos
.lnum
== 0) /* nothing found */
6364 if (cap
->nchar
!= 'm' && cap
->nchar
!= 'M')
6365 clearopbeep(cap
->oap
);
6368 pos
= &new_pos
; /* use last one found */
6372 curwin
->w_cursor
= *pos
;
6375 curwin
->w_cursor
= old_pos
;
6378 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6379 * brought us to the match for "[m" and "]M" when inside a method.
6380 * Try finding the '{' or '}' we want to be at.
6381 * Also repeat for the given count.
6383 if (cap
->nchar
== 'm' || cap
->nchar
== 'M')
6385 /* norm is TRUE for "]M" and "[m" */
6386 int norm
= ((findc
== '{') == (cap
->nchar
== 'm'));
6389 /* found a match: we were inside a method */
6390 if (prev_pos
.lnum
!= 0)
6393 curwin
->w_cursor
= prev_pos
;
6403 if ((findc
== '{' ? dec_cursor() : inc_cursor()) < 0)
6405 /* if not found anything, that's an error */
6407 clearopbeep(cap
->oap
);
6412 if (c
== '{' || c
== '}')
6414 /* Must have found end/start of class: use it.
6415 * Or found the place to be at. */
6416 if ((c
== findc
&& norm
) || (n
== 1 && !norm
))
6418 new_pos
= curwin
->w_cursor
;
6422 /* if no match found at all, we started outside of the
6423 * class and we're inside now. Just go on. */
6424 else if (new_pos
.lnum
== 0)
6426 new_pos
= curwin
->w_cursor
;
6429 /* found start/end of other method: go to match */
6430 else if ((pos
= findmatchlimit(cap
->oap
, findc
,
6431 (cap
->cmdchar
== '[') ? FM_BACKWARD
: FM_FORWARD
,
6435 curwin
->w_cursor
= *pos
;
6441 curwin
->w_cursor
= old_pos
;
6442 if (pos
== NULL
&& new_pos
.lnum
!= 0)
6443 clearopbeep(cap
->oap
);
6448 curwin
->w_cursor
= *pos
;
6449 curwin
->w_set_curswant
= TRUE
;
6451 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
6452 && cap
->oap
->op_type
== OP_NOP
)
6459 * "[[", "[]", "]]" and "][": move to start or end of function
6461 else if (cap
->nchar
== '[' || cap
->nchar
== ']')
6463 if (cap
->nchar
== cap
->cmdchar
) /* "]]" or "[[" */
6466 flag
= '}'; /* "][" or "[]" */
6468 curwin
->w_set_curswant
= TRUE
;
6470 * Imitate strange Vi behaviour: When using "]]" with an operator
6471 * we also stop at '}'.
6473 if (!findpar(&cap
->oap
->inclusive
, cap
->arg
, cap
->count1
, flag
,
6474 (cap
->oap
->op_type
!= OP_NOP
6475 && cap
->arg
== FORWARD
&& flag
== '{')))
6476 clearopbeep(cap
->oap
);
6479 if (cap
->oap
->op_type
== OP_NOP
)
6480 beginline(BL_WHITE
| BL_FIX
);
6482 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6489 * "[p", "[P", "]P" and "]p": put with indent adjustment
6491 else if (cap
->nchar
== 'p' || cap
->nchar
== 'P')
6493 if (!checkclearop(cap
->oap
))
6496 do_put(cap
->oap
->regname
,
6497 (cap
->cmdchar
== ']' && cap
->nchar
== 'p') ? FORWARD
: BACKWARD
,
6498 cap
->count1
, PUT_FIXINDENT
);
6503 * "['", "[`", "]'" and "]`": jump to next mark
6505 else if (cap
->nchar
== '\'' || cap
->nchar
== '`')
6507 pos
= &curwin
->w_cursor
;
6508 for (n
= cap
->count1
; n
> 0; --n
)
6511 pos
= getnextmark(pos
, cap
->cmdchar
== '[' ? BACKWARD
: FORWARD
,
6512 cap
->nchar
== '\'');
6518 nv_cursormark(cap
, cap
->nchar
== '\'', pos
);
6523 * [ or ] followed by a middle mouse click: put selected text with
6524 * indent adjustment. Any other button just does as usual.
6526 else if (cap
->nchar
>= K_LEFTMOUSE
&& cap
->nchar
<= K_RIGHTRELEASE
)
6528 (void)do_mouse(cap
->oap
, cap
->nchar
,
6529 (cap
->cmdchar
== ']') ? FORWARD
: BACKWARD
,
6530 cap
->count1
, PUT_FIXINDENT
);
6532 #endif /* FEAT_MOUSE */
6536 * "[z" and "]z": move to start or end of open fold.
6538 else if (cap
->nchar
== 'z')
6540 if (foldMoveTo(FALSE
, cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6541 cap
->count1
) == FAIL
)
6542 clearopbeep(cap
->oap
);
6548 * "[c" and "]c": move to next or previous diff-change.
6550 else if (cap
->nchar
== 'c')
6552 if (diff_move_to(cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6553 cap
->count1
) == FAIL
)
6554 clearopbeep(cap
->oap
);
6560 * "[s", "[S", "]s" and "]S": move to next spell error.
6562 else if (cap
->nchar
== 's' || cap
->nchar
== 'S')
6565 for (n
= 0; n
< cap
->count1
; ++n
)
6566 if (spell_move_to(curwin
, cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6567 cap
->nchar
== 's' ? TRUE
: FALSE
, FALSE
, NULL
) == 0)
6569 clearopbeep(cap
->oap
);
6572 # ifdef FEAT_FOLDING
6573 if (cap
->oap
->op_type
== OP_NOP
&& (fdo_flags
& FDO_SEARCH
) && KeyTyped
)
6579 /* Not a valid cap->nchar. */
6581 clearopbeep(cap
->oap
);
6585 * Handle Normal mode "%" command.
6593 linenr_T lnum
= curwin
->w_cursor
.lnum
;
6596 cap
->oap
->inclusive
= TRUE
;
6597 if (cap
->count0
) /* {cnt}% : goto {cnt} percentage in file */
6599 if (cap
->count0
> 100)
6600 clearopbeep(cap
->oap
);
6603 cap
->oap
->motion_type
= MLINE
;
6605 /* Round up, so CTRL-G will give same value. Watch out for a
6606 * large line count, the line number must not go negative! */
6607 if (curbuf
->b_ml
.ml_line_count
> 1000000)
6608 curwin
->w_cursor
.lnum
= (curbuf
->b_ml
.ml_line_count
+ 99L)
6609 / 100L * cap
->count0
;
6611 curwin
->w_cursor
.lnum
= (curbuf
->b_ml
.ml_line_count
*
6612 cap
->count0
+ 99L) / 100L;
6613 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
6614 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6615 beginline(BL_SOL
| BL_FIX
);
6618 else /* "%" : go to matching paren */
6620 cap
->oap
->motion_type
= MCHAR
;
6621 cap
->oap
->use_reg_one
= TRUE
;
6622 if ((pos
= findmatch(cap
->oap
, NUL
)) == NULL
)
6623 clearopbeep(cap
->oap
);
6627 curwin
->w_cursor
= *pos
;
6628 curwin
->w_set_curswant
= TRUE
;
6629 #ifdef FEAT_VIRTUALEDIT
6630 curwin
->w_cursor
.coladd
= 0;
6633 adjust_for_sel(cap
);
6638 if (cap
->oap
->op_type
== OP_NOP
6639 && lnum
!= curwin
->w_cursor
.lnum
6640 && (fdo_flags
& FDO_PERCENT
)
6647 * Handle "(" and ")" commands.
6648 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6654 cap
->oap
->motion_type
= MCHAR
;
6655 cap
->oap
->use_reg_one
= TRUE
;
6656 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6657 cap
->oap
->inclusive
= FALSE
;
6658 curwin
->w_set_curswant
= TRUE
;
6660 if (findsent(cap
->arg
, cap
->count1
) == FAIL
)
6661 clearopbeep(cap
->oap
);
6664 /* Don't leave the cursor on the NUL past end of line. */
6665 adjust_cursor(cap
->oap
);
6666 #ifdef FEAT_VIRTUALEDIT
6667 curwin
->w_cursor
.coladd
= 0;
6670 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6677 * "m" command: Mark a position.
6683 if (!checkclearop(cap
->oap
))
6685 if (setmark(cap
->nchar
) == FAIL
)
6686 clearopbeep(cap
->oap
);
6691 * "{" and "}" commands.
6692 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6698 cap
->oap
->motion_type
= MCHAR
;
6699 cap
->oap
->inclusive
= FALSE
;
6700 cap
->oap
->use_reg_one
= TRUE
;
6701 curwin
->w_set_curswant
= TRUE
;
6702 if (!findpar(&cap
->oap
->inclusive
, cap
->arg
, cap
->count1
, NUL
, FALSE
))
6703 clearopbeep(cap
->oap
);
6706 #ifdef FEAT_VIRTUALEDIT
6707 curwin
->w_cursor
.coladd
= 0;
6710 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6717 * "u" command: Undo or make lower case.
6723 if (cap
->oap
->op_type
== OP_LOWER
6729 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6745 if (!checkclearopq(cap
->oap
))
6747 u_undo((int)cap
->count1
);
6748 curwin
->w_set_curswant
= TRUE
;
6753 * Handle the "r" command.
6763 if (checkclearop(cap
->oap
))
6766 /* get another character */
6767 if (cap
->nchar
== Ctrl_V
)
6769 had_ctrl_v
= Ctrl_V
;
6770 cap
->nchar
= get_literal();
6771 /* Don't redo a multibyte character with CTRL-V. */
6772 if (cap
->nchar
> DEL
)
6778 /* Abort if the character is a special key. */
6779 if (IS_SPECIAL(cap
->nchar
))
6781 clearopbeep(cap
->oap
);
6786 /* Visual mode "r" */
6796 #ifdef FEAT_VIRTUALEDIT
6797 /* Break tabs, etc. */
6798 if (virtual_active())
6800 if (u_save_cursor() == FAIL
)
6802 if (gchar_cursor() == NUL
)
6804 /* Add extra space and put the cursor on the first one. */
6805 coladvance_force((colnr_T
)(getviscol() + cap
->count1
));
6806 curwin
->w_cursor
.col
-= cap
->count1
;
6808 else if (gchar_cursor() == TAB
)
6809 coladvance_force(getviscol());
6813 /* Abort if not enough characters to replace. */
6814 ptr
= ml_get_cursor();
6815 if (STRLEN(ptr
) < (unsigned)cap
->count1
6817 || (has_mbyte
&& mb_charlen(ptr
) < cap
->count1
)
6821 clearopbeep(cap
->oap
);
6826 * Replacing with a TAB is done by edit() when it is complicated because
6827 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6828 * Other characters are done below to avoid problems with things like
6829 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6831 if (had_ctrl_v
!= Ctrl_V
&& cap
->nchar
== '\t' && (curbuf
->b_p_et
|| p_sta
))
6833 stuffnumReadbuff(cap
->count1
);
6834 stuffcharReadbuff('R');
6835 stuffcharReadbuff('\t');
6836 stuffcharReadbuff(ESC
);
6840 /* save line for undo */
6841 if (u_save_cursor() == FAIL
)
6844 if (had_ctrl_v
!= Ctrl_V
&& (cap
->nchar
== '\r' || cap
->nchar
== '\n'))
6847 * Replace character(s) by a single newline.
6848 * Strange vi behaviour: Only one newline is inserted.
6849 * Delete the characters here.
6850 * Insert the newline with an insert command, takes care of
6851 * autoindent. The insert command depends on being on the last
6852 * character of a line or not.
6855 (void)del_chars(cap
->count1
, FALSE
); /* delete the characters */
6857 (void)del_bytes(cap
->count1
, FALSE
, FALSE
); /* delete the characters */
6859 stuffcharReadbuff('\r');
6860 stuffcharReadbuff(ESC
);
6862 /* Give 'r' to edit(), to get the redo command right. */
6863 invoke_edit(cap
, TRUE
, 'r', FALSE
);
6867 prep_redo(cap
->oap
->regname
, cap
->count1
,
6868 NUL
, 'r', NUL
, had_ctrl_v
, cap
->nchar
);
6870 curbuf
->b_op_start
= curwin
->w_cursor
;
6874 int old_State
= State
;
6876 if (cap
->ncharC1
!= 0)
6877 AppendCharToRedobuff(cap
->ncharC1
);
6878 if (cap
->ncharC2
!= 0)
6879 AppendCharToRedobuff(cap
->ncharC2
);
6881 /* This is slow, but it handles replacing a single-byte with a
6882 * multi-byte and the other way around. Also handles adding
6883 * composing characters for utf-8. */
6884 for (n
= cap
->count1
; n
> 0; --n
)
6887 ins_char(cap
->nchar
);
6889 if (cap
->ncharC1
!= 0)
6890 ins_char(cap
->ncharC1
);
6891 if (cap
->ncharC2
!= 0)
6892 ins_char(cap
->ncharC2
);
6899 * Replace the characters within one line.
6901 for (n
= cap
->count1
; n
> 0; --n
)
6904 * Get ptr again, because u_save and/or showmatch() will have
6905 * released the line. At the same time we let know that the
6906 * line will be changed.
6908 ptr
= ml_get_buf(curbuf
, curwin
->w_cursor
.lnum
, TRUE
);
6909 ptr
[curwin
->w_cursor
.col
] = cap
->nchar
;
6910 if (p_sm
&& msg_silent
== 0)
6911 showmatch(cap
->nchar
);
6912 ++curwin
->w_cursor
.col
;
6914 #ifdef FEAT_NETBEANS_INTG
6917 colnr_T start
= (colnr_T
)(curwin
->w_cursor
.col
- cap
->count1
);
6919 netbeans_removed(curbuf
, curwin
->w_cursor
.lnum
, start
,
6921 netbeans_inserted(curbuf
, curwin
->w_cursor
.lnum
, start
,
6922 &ptr
[start
], (int)cap
->count1
);
6926 /* mark the buffer as changed and prepare for displaying */
6927 changed_bytes(curwin
->w_cursor
.lnum
,
6928 (colnr_T
)(curwin
->w_cursor
.col
- cap
->count1
));
6930 --curwin
->w_cursor
.col
; /* cursor on the last replaced char */
6932 /* if the character on the left of the current cursor is a multi-byte
6933 * character, move two characters left */
6937 curbuf
->b_op_end
= curwin
->w_cursor
;
6938 curwin
->w_set_curswant
= TRUE
;
6939 set_last_insert(cap
->nchar
);
6945 * 'o': Exchange start and end of Visual area.
6946 * 'O': same, but in block mode exchange left and right corners.
6949 v_swap_corners(cmdchar
)
6953 colnr_T left
, right
;
6955 if (cmdchar
== 'O' && VIsual_mode
== Ctrl_V
)
6957 old_cursor
= curwin
->w_cursor
;
6958 getvcols(curwin
, &old_cursor
, &VIsual
, &left
, &right
);
6959 curwin
->w_cursor
.lnum
= VIsual
.lnum
;
6961 VIsual
= curwin
->w_cursor
;
6963 curwin
->w_cursor
.lnum
= old_cursor
.lnum
;
6964 curwin
->w_curswant
= right
;
6965 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6966 * right one column */
6967 if (old_cursor
.lnum
>= VIsual
.lnum
&& *p_sel
== 'e')
6968 ++curwin
->w_curswant
;
6969 coladvance(curwin
->w_curswant
);
6970 if (curwin
->w_cursor
.col
== old_cursor
.col
6971 #ifdef FEAT_VIRTUALEDIT
6972 && (!virtual_active()
6973 || curwin
->w_cursor
.coladd
== old_cursor
.coladd
)
6977 curwin
->w_cursor
.lnum
= VIsual
.lnum
;
6978 if (old_cursor
.lnum
<= VIsual
.lnum
&& *p_sel
== 'e')
6981 VIsual
= curwin
->w_cursor
;
6983 curwin
->w_cursor
.lnum
= old_cursor
.lnum
;
6985 curwin
->w_curswant
= left
;
6990 old_cursor
= curwin
->w_cursor
;
6991 curwin
->w_cursor
= VIsual
;
6992 VIsual
= old_cursor
;
6993 curwin
->w_set_curswant
= TRUE
;
6996 #endif /* FEAT_VISUAL */
6999 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
7006 if (VIsual_active
) /* "R" is replace lines */
7015 if (!checkclearopq(cap
->oap
))
7017 if (!curbuf
->b_p_ma
)
7018 EMSG(_(e_modifiable
));
7021 #ifdef FEAT_VIRTUALEDIT
7022 if (virtual_active())
7023 coladvance(getviscol());
7025 invoke_edit(cap
, FALSE
, cap
->arg
? 'V' : 'R', FALSE
);
7030 #ifdef FEAT_VREPLACE
7042 cap
->nchar
= cap
->extra_char
;
7043 nv_replace(cap
); /* Do same as "r" in Visual mode for now */
7047 if (!checkclearopq(cap
->oap
))
7049 if (!curbuf
->b_p_ma
)
7050 EMSG(_(e_modifiable
));
7053 if (cap
->extra_char
== Ctrl_V
) /* get another character */
7054 cap
->extra_char
= get_literal();
7055 stuffcharReadbuff(cap
->extra_char
);
7056 stuffcharReadbuff(ESC
);
7057 # ifdef FEAT_VIRTUALEDIT
7058 if (virtual_active())
7059 coladvance(getviscol());
7061 invoke_edit(cap
, TRUE
, 'v', FALSE
);
7068 * Swap case for "~" command, when it does not work like an operator.
7077 #ifdef FEAT_NETBEANS_INTG
7083 if (checkclearopq(cap
->oap
))
7086 if (lineempty(curwin
->w_cursor
.lnum
) && vim_strchr(p_ww
, '~') == NULL
)
7088 clearopbeep(cap
->oap
);
7094 if (u_save_cursor() == FAIL
)
7097 startpos
= curwin
->w_cursor
;
7098 #ifdef FEAT_NETBEANS_INTG
7101 for (n
= cap
->count1
; n
> 0; --n
)
7103 did_change
|= swapchar(cap
->oap
->op_type
, &curwin
->w_cursor
);
7105 if (gchar_cursor() == NUL
)
7107 if (vim_strchr(p_ww
, '~') != NULL
7108 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
7110 #ifdef FEAT_NETBEANS_INTG
7115 ptr
= ml_get(pos
.lnum
);
7116 count
= (int)STRLEN(ptr
) - pos
.col
;
7117 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
,
7119 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
,
7120 &ptr
[pos
.col
], count
);
7126 ++curwin
->w_cursor
.lnum
;
7127 curwin
->w_cursor
.col
= 0;
7130 if (u_savesub(curwin
->w_cursor
.lnum
) == FAIL
)
7139 #ifdef FEAT_NETBEANS_INTG
7140 if (did_change
&& usingNetbeans
)
7142 ptr
= ml_get(pos
.lnum
);
7143 count
= curwin
->w_cursor
.col
- pos
.col
;
7144 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
, (long)count
);
7145 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
, &ptr
[pos
.col
], count
);
7151 curwin
->w_set_curswant
= TRUE
;
7154 changed_lines(startpos
.lnum
, startpos
.col
, curwin
->w_cursor
.lnum
+ 1,
7156 curbuf
->b_op_start
= startpos
;
7157 curbuf
->b_op_end
= curwin
->w_cursor
;
7158 if (curbuf
->b_op_end
.col
> 0)
7159 --curbuf
->b_op_end
.col
;
7164 * Move cursor to mark.
7167 nv_cursormark(cap
, flag
, pos
)
7172 if (check_mark(pos
) == FAIL
)
7176 if (cap
->cmdchar
== '\''
7177 || cap
->cmdchar
== '`'
7178 || cap
->cmdchar
== '['
7179 || cap
->cmdchar
== ']')
7181 curwin
->w_cursor
= *pos
;
7183 beginline(BL_WHITE
| BL_FIX
);
7187 cap
->oap
->motion_type
= flag
? MLINE
: MCHAR
;
7188 if (cap
->cmdchar
== '`')
7189 cap
->oap
->use_reg_one
= TRUE
;
7190 cap
->oap
->inclusive
= FALSE
; /* ignored if not MCHAR */
7191 curwin
->w_set_curswant
= TRUE
;
7196 * Handle commands that are operators in Visual mode.
7202 static char_u trans
[] = "YyDdCcxdXdAAIIrr";
7204 /* Uppercase means linewise, except in block mode, then "D" deletes till
7205 * the end of the line, and "C" replaces til EOL */
7206 if (isupper(cap
->cmdchar
))
7208 if (VIsual_mode
!= Ctrl_V
)
7210 else if (cap
->cmdchar
== 'C' || cap
->cmdchar
== 'D')
7211 curwin
->w_curswant
= MAXCOL
;
7213 cap
->cmdchar
= *(vim_strchr(trans
, cap
->cmdchar
) + 1);
7219 * "s" and "S" commands.
7226 if (VIsual_active
) /* "vs" and "vS" are the same as "vc" */
7228 if (cap
->cmdchar
== 'S')
7239 * Abbreviated commands.
7245 if (cap
->cmdchar
== K_DEL
|| cap
->cmdchar
== K_KDEL
)
7246 cap
->cmdchar
= 'x'; /* DEL key behaves like 'x' */
7249 /* in Visual mode these commands are operators */
7258 * Translate a command into another command.
7264 static char_u
*(ar
[8]) = {(char_u
*)"dl", (char_u
*)"dh",
7265 (char_u
*)"d$", (char_u
*)"c$",
7266 (char_u
*)"cl", (char_u
*)"cc",
7267 (char_u
*)"yy", (char_u
*)":s\r"};
7268 static char_u
*str
= (char_u
*)"xXDCsSY&";
7270 if (!checkclearopq(cap
->oap
))
7272 /* In Vi "2D" doesn't delete the next line. Can't translate it
7273 * either, because "2." should also not use the count. */
7274 if (cap
->cmdchar
== 'D' && vim_strchr(p_cpo
, CPO_HASH
) != NULL
)
7276 cap
->oap
->start
= curwin
->w_cursor
;
7277 cap
->oap
->op_type
= OP_DELETE
;
7279 set_op_var(OP_DELETE
);
7285 AppendCharToRedobuff('D');
7290 stuffnumReadbuff(cap
->count0
);
7291 stuffReadbuff(ar
[(int)(vim_strchr(str
, cap
->cmdchar
) - str
)]);
7298 * "'" and "`" commands. Also for "g'" and "g`".
7299 * cap->arg is TRUE for "'" and "g'".
7308 linenr_T lnum
= curwin
->w_cursor
.lnum
;
7309 int old_KeyTyped
= KeyTyped
; /* getting file may reset it */
7312 if (cap
->cmdchar
== 'g')
7313 c
= cap
->extra_char
;
7316 pos
= getmark(c
, (cap
->oap
->op_type
== OP_NOP
));
7317 if (pos
== (pos_T
*)-1) /* jumped to other file */
7321 check_cursor_lnum();
7322 beginline(BL_WHITE
| BL_FIX
);
7328 nv_cursormark(cap
, cap
->arg
, pos
);
7330 #ifdef FEAT_VIRTUALEDIT
7331 /* May need to clear the coladd that a mark includes. */
7332 if (!virtual_active())
7333 curwin
->w_cursor
.coladd
= 0;
7336 if (cap
->oap
->op_type
== OP_NOP
7337 && (pos
== (pos_T
*)-1 || lnum
!= curwin
->w_cursor
.lnum
)
7338 && (fdo_flags
& FDO_MARK
)
7345 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7351 #ifdef FEAT_JUMPLIST
7353 # ifdef FEAT_FOLDING
7354 linenr_T lnum
= curwin
->w_cursor
.lnum
;
7355 int old_KeyTyped
= KeyTyped
; /* getting file may reset it */
7358 if (!checkclearopq(cap
->oap
))
7360 if (cap
->cmdchar
== 'g')
7361 pos
= movechangelist((int)cap
->count1
);
7363 pos
= movemark((int)cap
->count1
);
7364 if (pos
== (pos_T
*)-1) /* jump to other file */
7366 curwin
->w_set_curswant
= TRUE
;
7369 else if (pos
!= NULL
) /* can jump */
7370 nv_cursormark(cap
, FALSE
, pos
);
7371 else if (cap
->cmdchar
== 'g')
7373 if (curbuf
->b_changelistlen
== 0)
7374 EMSG(_("E664: changelist is empty"));
7375 else if (cap
->count1
< 0)
7376 EMSG(_("E662: At start of changelist"));
7378 EMSG(_("E663: At end of changelist"));
7381 clearopbeep(cap
->oap
);
7382 # ifdef FEAT_FOLDING
7383 if (cap
->oap
->op_type
== OP_NOP
7384 && (pos
== (pos_T
*)-1 || lnum
!= curwin
->w_cursor
.lnum
)
7385 && (fdo_flags
& FDO_MARK
)
7391 clearopbeep(cap
->oap
);
7396 * Handle '"' command.
7402 if (checkclearop(cap
->oap
))
7405 if (cap
->nchar
== '=')
7406 cap
->nchar
= get_expr_register();
7408 if (cap
->nchar
!= NUL
&& valid_yank_reg(cap
->nchar
, FALSE
))
7410 cap
->oap
->regname
= cap
->nchar
;
7411 cap
->opcount
= cap
->count0
; /* remember count before '"' */
7413 set_reg_var(cap
->oap
->regname
);
7417 clearopbeep(cap
->oap
);
7422 * Handle "v", "V" and "CTRL-V" commands.
7423 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7425 * Handle CTRL-Q just like CTRL-V.
7431 if (cap
->cmdchar
== Ctrl_Q
)
7432 cap
->cmdchar
= Ctrl_V
;
7434 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7435 * characterwise, linewise, or blockwise. */
7436 if (cap
->oap
->op_type
!= OP_NOP
)
7438 cap
->oap
->motion_force
= cap
->cmdchar
;
7439 finish_op
= FALSE
; /* operator doesn't finish now but later */
7443 VIsual_select
= cap
->arg
;
7444 if (VIsual_active
) /* change Visual mode */
7446 if (VIsual_mode
== cap
->cmdchar
) /* stop visual mode */
7448 else /* toggle char/block mode */
7449 { /* or char/line mode */
7450 VIsual_mode
= cap
->cmdchar
;
7453 redraw_curbuf_later(INVERTED
); /* update the inversion */
7455 else /* start Visual mode */
7457 check_visual_highlight();
7458 if (cap
->count0
) /* use previously selected part */
7460 if (resel_VIsual_mode
== NUL
) /* there is none */
7465 VIsual
= curwin
->w_cursor
;
7467 VIsual_active
= TRUE
;
7468 VIsual_reselect
= TRUE
;
7470 /* start Select mode when 'selectmode' contains "cmd" */
7471 may_start_select('c');
7475 if (p_smd
&& msg_silent
== 0)
7476 redraw_cmdline
= TRUE
; /* show visual mode later */
7478 * For V and ^V, we multiply the number of lines even if there
7479 * was only one -- webb
7481 if (resel_VIsual_mode
!= 'v' || resel_VIsual_line_count
> 1)
7483 curwin
->w_cursor
.lnum
+=
7484 resel_VIsual_line_count
* cap
->count0
- 1;
7485 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
7486 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
7488 VIsual_mode
= resel_VIsual_mode
;
7489 if (VIsual_mode
== 'v')
7491 if (resel_VIsual_line_count
<= 1)
7492 curwin
->w_cursor
.col
+= resel_VIsual_col
* cap
->count0
- 1;
7494 curwin
->w_cursor
.col
= resel_VIsual_col
;
7497 if (resel_VIsual_col
== MAXCOL
)
7499 curwin
->w_curswant
= MAXCOL
;
7500 coladvance((colnr_T
)MAXCOL
);
7502 else if (VIsual_mode
== Ctrl_V
)
7505 curwin
->w_curswant
= curwin
->w_virtcol
7506 + resel_VIsual_col
* cap
->count0
- 1;
7507 coladvance(curwin
->w_curswant
);
7510 curwin
->w_set_curswant
= TRUE
;
7511 redraw_curbuf_later(INVERTED
); /* show the inversion */
7516 /* start Select mode when 'selectmode' contains "cmd" */
7517 may_start_select('c');
7518 n_start_visual_mode(cap
->cmdchar
);
7524 * Start selection for Shift-movement keys.
7529 /* if 'selectmode' contains "key", start Select mode */
7530 may_start_select('k');
7531 n_start_visual_mode('v');
7535 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7541 VIsual_select
= (stuff_empty() && typebuf_typed()
7542 && (vim_strchr(p_slm
, c
) != NULL
));
7546 * Start Visual mode "c".
7547 * Should set VIsual_select before calling this.
7550 n_start_visual_mode(c
)
7554 VIsual_active
= TRUE
;
7555 VIsual_reselect
= TRUE
;
7556 #ifdef FEAT_VIRTUALEDIT
7557 /* Corner case: the 0 position in a tab may change when going into
7558 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7560 if (c
== Ctrl_V
&& (ve_flags
& VE_BLOCK
) && gchar_cursor() == TAB
)
7561 coladvance(curwin
->w_virtcol
);
7563 VIsual
= curwin
->w_cursor
;
7572 if (p_smd
&& msg_silent
== 0)
7573 redraw_cmdline
= TRUE
; /* show visual mode later */
7574 #ifdef FEAT_CLIPBOARD
7575 /* Make sure the clipboard gets updated. Needed because start and
7576 * end may still be the same, and the selection needs to be owned */
7577 clip_star
.vmode
= NUL
;
7580 /* Only need to redraw this line, unless still need to redraw an old
7581 * Visual area (when 'lazyredraw' is set). */
7582 if (curwin
->w_redr_type
< INVERTED
)
7584 curwin
->w_old_cursor_lnum
= curwin
->w_cursor
.lnum
;
7585 curwin
->w_old_visual_lnum
= curwin
->w_cursor
.lnum
;
7589 #endif /* FEAT_VISUAL */
7592 * CTRL-W: Window commands
7599 if (!checkclearop(cap
->oap
))
7600 do_window(cap
->nchar
, cap
->count0
, NUL
); /* everything is in window.c */
7602 (void)checkclearop(cap
->oap
);
7616 end_visual_mode(); /* stop Visual mode */
7618 do_cmdline_cmd((char_u
*)"st");
7622 * Commands starting with "g".
7628 oparg_T
*oap
= cap
->oap
;
7639 * "g^A": dump log of used memory.
7642 vim_mem_profile_dump();
7646 #ifdef FEAT_VREPLACE
7648 * "gR": Enter virtual replace mode.
7661 do_cmdline_cmd((char_u
*)"%s//~/&");
7666 * "gv": Reselect the previous Visual area. If Visual already active,
7667 * exchange previous and current Visual area.
7670 if (checkclearop(oap
))
7673 if ( curbuf
->b_visual
.vi_start
.lnum
== 0
7674 || curbuf
->b_visual
.vi_start
.lnum
> curbuf
->b_ml
.ml_line_count
7675 || curbuf
->b_visual
.vi_end
.lnum
== 0)
7679 /* set w_cursor to the start of the Visual area, tpos to the end */
7683 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
7684 curbuf
->b_visual
.vi_mode
= i
;
7686 curbuf
->b_visual_mode_eval
= i
;
7688 i
= curwin
->w_curswant
;
7689 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
7690 curbuf
->b_visual
.vi_curswant
= i
;
7692 tpos
= curbuf
->b_visual
.vi_end
;
7693 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
7694 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
7695 curbuf
->b_visual
.vi_start
= VIsual
;
7699 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
7700 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
7701 tpos
= curbuf
->b_visual
.vi_end
;
7702 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
7705 VIsual_active
= TRUE
;
7706 VIsual_reselect
= TRUE
;
7708 /* Set Visual to the start and w_cursor to the end of the Visual
7709 * area. Make sure they are on an existing character. */
7711 VIsual
= curwin
->w_cursor
;
7712 curwin
->w_cursor
= tpos
;
7716 * When called from normal "g" command: start Select mode when
7717 * 'selectmode' contains "cmd". When called for K_SELECT, always
7718 * start Select mode.
7721 VIsual_select
= TRUE
;
7723 may_start_select('c');
7727 #ifdef FEAT_CLIPBOARD
7728 /* Make sure the clipboard gets updated. Needed because start and
7729 * end are still the same, and the selection needs to be owned */
7730 clip_star
.vmode
= NUL
;
7732 redraw_curbuf_later(INVERTED
);
7737 * "gV": Don't reselect the previous Visual area after a Select mode
7741 VIsual_reselect
= FALSE
;
7745 * "gh": start Select mode.
7746 * "gH": start Select line mode.
7747 * "g^H": start Select block mode.
7750 cap
->nchar
= Ctrl_H
;
7756 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7757 if (cap
->nchar
== Ctrl_H
)
7758 cap
->cmdchar
= Ctrl_V
;
7761 cap
->cmdchar
= cap
->nchar
+ ('v' - 'h');
7765 #endif /* FEAT_VISUAL */
7768 * "gj" and "gk" two new funny movement keys -- up and down
7769 * movement based on *screen* line rather than *file* line.
7773 /* with 'nowrap' it works just like the normal "j" command; also when
7774 * in a closed fold */
7775 if (!curwin
->w_p_wrap
7777 || hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
)
7781 oap
->motion_type
= MLINE
;
7782 i
= cursor_down(cap
->count1
, oap
->op_type
== OP_NOP
);
7785 i
= nv_screengo(oap
, FORWARD
, cap
->count1
);
7792 /* with 'nowrap' it works just like the normal "k" command; also when
7793 * in a closed fold */
7794 if (!curwin
->w_p_wrap
7796 || hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
)
7800 oap
->motion_type
= MLINE
;
7801 i
= cursor_up(cap
->count1
, oap
->op_type
== OP_NOP
);
7804 i
= nv_screengo(oap
, BACKWARD
, cap
->count1
);
7810 * "gJ": join two lines without inserting a space.
7817 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7818 * "gm": middle of "g0" and "g$".
7828 oap
->motion_type
= MCHAR
;
7829 oap
->inclusive
= FALSE
;
7830 if (curwin
->w_p_wrap
7831 #ifdef FEAT_VERTSPLIT
7832 && curwin
->w_width
!= 0
7836 int width1
= W_WIDTH(curwin
) - curwin_col_off();
7837 int width2
= width1
+ curwin_col_off2();
7841 if (curwin
->w_virtcol
>= (colnr_T
)width1
&& width2
> 0)
7842 i
= (curwin
->w_virtcol
- width1
) / width2
* width2
+ width1
;
7845 i
= curwin
->w_leftcol
;
7846 /* Go to the middle of the screen line. When 'number' is on and lines
7847 * are wrapping the middle can be more to the left. */
7848 if (cap
->nchar
== 'm')
7849 i
+= (W_WIDTH(curwin
) - curwin_col_off()
7850 + ((curwin
->w_p_wrap
&& i
> 0)
7851 ? curwin_col_off2() : 0)) / 2;
7852 coladvance((colnr_T
)i
);
7857 while (vim_iswhite(i
) && oneright() == OK
);
7859 curwin
->w_set_curswant
= TRUE
;
7863 /* "g_": to the last non-blank character in the line or <count> lines
7865 cap
->oap
->motion_type
= MCHAR
;
7866 cap
->oap
->inclusive
= TRUE
;
7867 curwin
->w_curswant
= MAXCOL
;
7868 if (cursor_down((long)(cap
->count1
- 1),
7869 cap
->oap
->op_type
== OP_NOP
) == FAIL
)
7870 clearopbeep(cap
->oap
);
7873 char_u
*ptr
= ml_get_curline();
7875 /* In Visual mode we may end up after the line. */
7876 if (curwin
->w_cursor
.col
> 0 && ptr
[curwin
->w_cursor
.col
] == NUL
)
7877 --curwin
->w_cursor
.col
;
7879 /* Decrease the cursor column until it's on a non-blank. */
7880 while (curwin
->w_cursor
.col
> 0
7881 && vim_iswhite(ptr
[curwin
->w_cursor
.col
]))
7882 --curwin
->w_cursor
.col
;
7883 curwin
->w_set_curswant
= TRUE
;
7885 adjust_for_sel(cap
);
7894 int col_off
= curwin_col_off();
7896 oap
->motion_type
= MCHAR
;
7897 oap
->inclusive
= TRUE
;
7898 if (curwin
->w_p_wrap
7899 #ifdef FEAT_VERTSPLIT
7900 && curwin
->w_width
!= 0
7904 curwin
->w_curswant
= MAXCOL
; /* so we stay at the end */
7905 if (cap
->count1
== 1)
7907 int width1
= W_WIDTH(curwin
) - col_off
;
7908 int width2
= width1
+ curwin_col_off2();
7912 if (curwin
->w_virtcol
>= (colnr_T
)width1
)
7913 i
+= ((curwin
->w_virtcol
- width1
) / width2
+ 1)
7915 coladvance((colnr_T
)i
);
7916 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7917 if (curwin
->w_cursor
.col
> 0 && curwin
->w_p_wrap
)
7920 * Check for landing on a character that got split at
7921 * the end of the line. We do not want to advance to
7922 * the next screen line.
7925 if (curwin
->w_virtcol
> (colnr_T
)i
)
7926 --curwin
->w_cursor
.col
;
7930 else if (nv_screengo(oap
, FORWARD
, cap
->count1
- 1) == FAIL
)
7935 i
= curwin
->w_leftcol
+ W_WIDTH(curwin
) - col_off
- 1;
7936 coladvance((colnr_T
)i
);
7938 /* Make sure we stick in this column. */
7940 curwin
->w_curswant
= curwin
->w_virtcol
;
7941 curwin
->w_set_curswant
= FALSE
;
7947 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7952 case POUND
: /* pound sign (sometimes equal to '#') */
7954 case Ctrl_RSB
: /* :tag or :tselect for current identifier */
7955 case ']': /* :tselect for current identifier */
7960 * ge and gE: go back to end of word
7964 oap
->motion_type
= MCHAR
;
7965 curwin
->w_set_curswant
= TRUE
;
7966 oap
->inclusive
= TRUE
;
7967 if (bckend_word(cap
->count1
, cap
->nchar
== 'E', FALSE
) == FAIL
)
7972 * "g CTRL-G": display info about cursor position
7979 * "gi": start Insert at the last position.
7982 if (curbuf
->b_last_insert
.lnum
!= 0)
7984 curwin
->w_cursor
= curbuf
->b_last_insert
;
7985 check_cursor_lnum();
7986 i
= (int)STRLEN(ml_get_curline());
7987 if (curwin
->w_cursor
.col
> (colnr_T
)i
)
7989 #ifdef FEAT_VIRTUALEDIT
7990 if (virtual_active())
7991 curwin
->w_cursor
.coladd
+= curwin
->w_cursor
.col
- i
;
7993 curwin
->w_cursor
.col
= i
;
8001 * "gI": Start insert in column 1.
8005 if (!checkclearopq(oap
))
8006 invoke_edit(cap
, FALSE
, 'g', FALSE
);
8009 #ifdef FEAT_SEARCHPATH
8011 * "gf": goto file, edit file under cursor
8012 * "]f" and "[f": can also be used.
8020 /* "g'm" and "g`m": jump to mark without setting pcmark */
8032 do_sleep(cap
->count1
* 1000L);
8036 * "ga": Display the ascii value of the character under the
8037 * cursor. It is displayed in decimal, hex, and octal. -- webb
8045 * "g8": Display the bytes used for the UTF-8 character under the
8046 * cursor. It is displayed in hex.
8047 * "8g8" finds illegal byte sequence.
8050 if (cap
->count0
== 8)
8062 * "gg": Goto the first line in file. With a count it goes to
8063 * that line number like for "G". -- webb
8071 * Two-character operators:
8073 * "gw" Format text and keep cursor position
8074 * "g~" Toggle the case of the text.
8075 * "gu" Change text to lower case.
8076 * "gU" Change text to upper case.
8077 * "g?" rot13 encoding
8078 * "g@" call 'operatorfunc'
8082 oap
->cursor_start
= curwin
->w_cursor
;
8093 * "gd": Find first occurrence of pattern under the cursor in the
8095 * "gD": idem, but in the current file.
8099 nv_gd(oap
, cap
->nchar
, (int)cap
->count0
);
8104 * g<*Mouse> : <C-*mouse>
8108 case K_MIDDLERELEASE
:
8114 case K_RIGHTRELEASE
:
8121 mod_mask
= MOD_MASK_CTRL
;
8122 (void)do_mouse(oap
, cap
->nchar
, BACKWARD
, cap
->count1
, 0);
8130 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8138 /* "go": goto byte count from start of buffer */
8140 goto_byte(cap
->count0
);
8144 /* "gQ": improved Ex mode */
8148 clearopbeep(cap
->oap
);
8153 if (!checkclearopq(oap
))
8157 #ifdef FEAT_JUMPLIST
8163 cap
->count1
= -cap
->count1
;
8170 goto_tabpage((int)cap
->count0
);
8173 goto_tabpage(-(int)cap
->count1
);
8178 case '-': /* "g+" and "g-": undo or redo along the timeline */
8179 if (!checkclearopq(oap
))
8180 undo_time(cap
->nchar
== '-' ? -cap
->count1
: cap
->count1
,
8191 * Handle "o" and "O" commands.
8197 if (!checkclearopq(cap
->oap
))
8200 if (cap
->cmdchar
== 'O')
8201 /* Open above the first line of a folded sequence of lines */
8202 (void)hasFolding(curwin
->w_cursor
.lnum
,
8203 &curwin
->w_cursor
.lnum
, NULL
);
8205 /* Open below the last line of a folded sequence of lines */
8206 (void)hasFolding(curwin
->w_cursor
.lnum
,
8207 NULL
, &curwin
->w_cursor
.lnum
);
8209 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
-
8210 (cap
->cmdchar
== 'O' ? 1 : 0)),
8211 (linenr_T
)(curwin
->w_cursor
.lnum
+
8212 (cap
->cmdchar
== 'o' ? 1 : 0))
8214 && open_line(cap
->cmdchar
== 'O' ? BACKWARD
: FORWARD
,
8215 #ifdef FEAT_COMMENTS
8216 has_format_option(FO_OPEN_COMS
) ? OPENLINE_DO_COM
:
8220 /* When '#' is in 'cpoptions' ignore the count. */
8221 if (vim_strchr(p_cpo
, CPO_HASH
) != NULL
)
8223 invoke_edit(cap
, FALSE
, cap
->cmdchar
, TRUE
);
8229 * "." command: redo last change.
8235 if (!checkclearopq(cap
->oap
))
8238 * If "restart_edit" is TRUE, the last but one command is repeated
8239 * instead of the last command (inserting text). This is used for
8240 * CTRL-O <.> in insert mode.
8242 if (start_redo(cap
->count0
, restart_edit
!= 0 && !arrow_used
) == FAIL
)
8243 clearopbeep(cap
->oap
);
8254 if (!checkclearopq(cap
->oap
))
8256 u_redo((int)cap
->count1
);
8257 curwin
->w_set_curswant
= TRUE
;
8262 * Handle "U" command.
8268 /* In Visual mode and typing "gUU" triggers an operator */
8269 if (cap
->oap
->op_type
== OP_UPPER
8275 /* translate "gUU" to "gUgU" */
8280 else if (!checkclearopq(cap
->oap
))
8283 curwin
->w_set_curswant
= TRUE
;
8288 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8299 && cap
->oap
->op_type
!= OP_TILDE
)
8306 * Handle an operator command.
8307 * The actual work is done by do_pending_operator().
8315 op_type
= get_op_type(cap
->cmdchar
, cap
->nchar
);
8317 if (op_type
== cap
->oap
->op_type
) /* double operator works on lines */
8319 else if (!checkclearop(cap
->oap
))
8321 cap
->oap
->start
= curwin
->w_cursor
;
8322 cap
->oap
->op_type
= op_type
;
8324 set_op_var(op_type
);
8331 * Set v:operator to the characters for "optype".
8339 if (optype
== OP_NOP
)
8340 set_vim_var_string(VV_OP
, NULL
, 0);
8343 opchars
[0] = get_op_char(optype
);
8344 opchars
[1] = get_extra_op_char(optype
);
8346 set_vim_var_string(VV_OP
, opchars
, -1);
8352 * Handle linewise operator "dd", "yy", etc.
8354 * "_" is is a strange motion command that helps make operators more logical.
8355 * It is actually implemented, but not documented in the real Vi. This motion
8356 * command actually refers to "the current line". Commands like "dd" and "yy"
8357 * are really an alternate form of "d_" and "y_". It does accept a count, so
8358 * "d3_" works to delete 3 lines.
8364 cap
->oap
->motion_type
= MLINE
;
8365 if (cursor_down(cap
->count1
- 1L, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
8366 clearopbeep(cap
->oap
);
8367 else if ( cap
->oap
->op_type
== OP_DELETE
8368 || cap
->oap
->op_type
== OP_LSHIFT
8369 || cap
->oap
->op_type
== OP_RSHIFT
)
8370 beginline(BL_SOL
| BL_FIX
);
8371 else if (cap
->oap
->op_type
!= OP_YANK
) /* 'Y' does not move cursor */
8372 beginline(BL_WHITE
| BL_FIX
);
8382 /* CTRL-HOME is like "gg" */
8383 if (mod_mask
& MOD_MASK_CTRL
)
8390 ins_at_eol
= FALSE
; /* Don't move cursor past eol (only necessary in a
8391 one-character line). */
8401 cap
->oap
->motion_type
= MCHAR
;
8402 cap
->oap
->inclusive
= FALSE
;
8404 if (cap
->count0
> 0)
8406 coladvance((colnr_T
)(cap
->count0
- 1));
8407 curwin
->w_curswant
= (colnr_T
)(cap
->count0
- 1);
8410 curwin
->w_curswant
= 0;
8411 /* keep curswant at the column where we wanted to go, not where
8412 we ended; differs if line is too short */
8413 curwin
->w_set_curswant
= FALSE
;
8417 * Handle back-word command "b" and "B".
8418 * cap->arg is 1 for "B"
8424 cap
->oap
->motion_type
= MCHAR
;
8425 cap
->oap
->inclusive
= FALSE
;
8426 curwin
->w_set_curswant
= TRUE
;
8427 if (bck_word(cap
->count1
, cap
->arg
, FALSE
) == FAIL
)
8428 clearopbeep(cap
->oap
);
8430 else if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8436 * Handle word motion commands "e", "E", "w" and "W".
8437 * cap->arg is TRUE for "E" and "W".
8446 pos_T startpos
= curwin
->w_cursor
;
8449 * Set inclusive for the "E" and "e" command.
8451 if (cap
->cmdchar
== 'e' || cap
->cmdchar
== 'E')
8455 cap
->oap
->inclusive
= word_end
;
8458 * "cw" and "cW" are a special case.
8460 if (!word_end
&& cap
->oap
->op_type
== OP_CHANGE
)
8463 if (n
!= NUL
) /* not an empty line */
8468 * Reproduce a funny Vi behaviour: "cw" on a blank only
8469 * changes one character, not all blanks until the start of
8470 * the next word. Only do this when the 'w' flag is included
8473 if (cap
->count1
== 1 && vim_strchr(p_cpo
, CPO_CW
) != NULL
)
8475 cap
->oap
->inclusive
= TRUE
;
8476 cap
->oap
->motion_type
= MCHAR
;
8483 * This is a little strange. To match what the real Vi does,
8484 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8485 * that we are not on a space or a TAB. This seems impolite
8486 * at first, but it's really more what we mean when we say
8488 * Another strangeness: When standing on the end of a word
8489 * "ce" will change until the end of the next wordt, but "cw"
8490 * will change only one character! This is done by setting
8493 cap
->oap
->inclusive
= TRUE
;
8500 cap
->oap
->motion_type
= MCHAR
;
8501 curwin
->w_set_curswant
= TRUE
;
8503 n
= end_word(cap
->count1
, cap
->arg
, flag
, FALSE
);
8505 n
= fwd_word(cap
->count1
, cap
->arg
, cap
->oap
->op_type
!= OP_NOP
);
8507 /* Don't leave the cursor on the NUL past the end of line. Unless we
8508 * didn't move it forward. */
8509 if (lt(startpos
, curwin
->w_cursor
))
8510 adjust_cursor(cap
->oap
);
8512 if (n
== FAIL
&& cap
->oap
->op_type
== OP_NOP
)
8513 clearopbeep(cap
->oap
);
8517 adjust_for_sel(cap
);
8520 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8527 * Used after a movement command: If the cursor ends up on the NUL after the
8528 * end of the line, may move it back to the last character and make the motion
8535 /* The cursor cannot remain on the NUL when:
8536 * - the column is > 0
8537 * - not in Visual mode or 'selection' is "o"
8538 * - 'virtualedit' is not "all" and not "onemore".
8540 if (curwin
->w_cursor
.col
> 0 && gchar_cursor() == NUL
8542 && (!VIsual_active
|| *p_sel
== 'o')
8544 #ifdef FEAT_VIRTUALEDIT
8545 && !virtual_active() && (ve_flags
& VE_ONEMORE
) == 0
8549 --curwin
->w_cursor
.col
;
8551 /* prevent cursor from moving on the trail byte */
8555 oap
->inclusive
= TRUE
;
8560 * "0" and "^" commands.
8561 * cap->arg is the argument for beginline().
8567 cap
->oap
->motion_type
= MCHAR
;
8568 cap
->oap
->inclusive
= FALSE
;
8569 beginline(cap
->arg
);
8571 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8574 ins_at_eol
= FALSE
; /* Don't move cursor past eol (only necessary in a
8575 one-character line). */
8580 * In exclusive Visual mode, may include the last character.
8586 if (VIsual_active
&& cap
->oap
->inclusive
&& *p_sel
== 'e'
8587 && gchar_cursor() != NUL
&& lt(VIsual
, curwin
->w_cursor
))
8594 ++curwin
->w_cursor
.col
;
8595 cap
->oap
->inclusive
= FALSE
;
8600 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8601 * Should check VIsual_mode before calling this.
8602 * Returns TRUE when backed up to the previous line.
8609 if (*p_sel
== 'e' && !equalpos(VIsual
, curwin
->w_cursor
))
8611 if (lt(VIsual
, curwin
->w_cursor
))
8612 pp
= &curwin
->w_cursor
;
8615 #ifdef FEAT_VIRTUALEDIT
8627 else if (pp
->lnum
> 1)
8630 pp
->col
= (colnr_T
)STRLEN(ml_get(pp
->lnum
));
8638 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8645 VIsual_select
= TRUE
;
8646 else if (VIsual_reselect
)
8648 cap
->nchar
= 'v'; /* fake "gv" command */
8657 * "G", "gg", CTRL-END, CTRL-HOME.
8658 * cap->arg is TRUE for "G".
8667 lnum
= curbuf
->b_ml
.ml_line_count
;
8670 cap
->oap
->motion_type
= MLINE
;
8673 /* When a count is given, use it instead of the default lnum */
8674 if (cap
->count0
!= 0)
8678 else if (lnum
> curbuf
->b_ml
.ml_line_count
)
8679 lnum
= curbuf
->b_ml
.ml_line_count
;
8680 curwin
->w_cursor
.lnum
= lnum
;
8681 beginline(BL_SOL
| BL_FIX
);
8683 if ((fdo_flags
& FDO_JUMP
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8689 * CTRL-\ in Normal mode.
8695 if (cap
->nchar
== Ctrl_N
|| cap
->nchar
== Ctrl_G
)
8698 if (restart_edit
!= 0 && mode_displayed
)
8699 clear_cmdline
= TRUE
; /* unshow mode later */
8702 if (cmdwin_type
!= 0)
8703 cmdwin_result
= Ctrl_C
;
8708 end_visual_mode(); /* stop Visual */
8709 redraw_curbuf_later(INVERTED
);
8712 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8713 if (cap
->nchar
== Ctrl_G
&& p_im
)
8717 clearopbeep(cap
->oap
);
8721 * ESC in Normal mode: beep, but don't flush buffers.
8722 * Don't even beep if we are canceling a command.
8730 no_reason
= (cap
->oap
->op_type
== OP_NOP
8731 && cap
->opcount
== 0
8733 && cap
->oap
->regname
== 0
8736 if (cap
->arg
) /* TRUE for CTRL-C */
8738 if (restart_edit
== 0
8746 MSG(_("Type :quit<Enter> to exit Vim"));
8748 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8749 * set again below when halfway a mapping. */
8753 if (cmdwin_type
!= 0)
8755 cmdwin_result
= K_IGNORE
;
8756 got_int
= FALSE
; /* don't stop executing autocommands et al. */
8765 end_visual_mode(); /* stop Visual */
8766 check_cursor_col(); /* make sure cursor is not beyond EOL */
8767 curwin
->w_set_curswant
= TRUE
;
8768 redraw_curbuf_later(INVERTED
);
8776 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8777 * set return to Insert mode afterwards. */
8778 if (restart_edit
== 0 && goto_im()
8779 #ifdef FEAT_EX_EXTRA
8780 && ex_normal_busy
== 0
8787 * Handle "A", "a", "I", "i" and <Insert> commands.
8793 /* <Insert> is equal to "i" */
8794 if (cap
->cmdchar
== K_INS
|| cap
->cmdchar
== K_KINS
)
8798 /* in Visual mode "A" and "I" are an operator */
8799 if (VIsual_active
&& (cap
->cmdchar
== 'A' || cap
->cmdchar
== 'I'))
8802 /* in Visual mode and after an operator "a" and "i" are for text objects */
8805 if ((cap
->cmdchar
== 'a' || cap
->cmdchar
== 'i')
8806 && (cap
->oap
->op_type
!= OP_NOP
8815 clearopbeep(cap
->oap
);
8818 else if (!curbuf
->b_p_ma
&& !p_im
)
8820 /* Only give this error when 'insertmode' is off. */
8821 EMSG(_(e_modifiable
));
8824 else if (!checkclearopq(cap
->oap
))
8826 switch (cap
->cmdchar
)
8828 case 'A': /* "A"ppend after the line */
8829 curwin
->w_set_curswant
= TRUE
;
8830 #ifdef FEAT_VIRTUALEDIT
8831 if (ve_flags
== VE_ALL
)
8833 int save_State
= State
;
8835 /* Pretent Insert mode here to allow the cursor on the
8836 * character past the end of the line */
8838 coladvance((colnr_T
)MAXCOL
);
8843 curwin
->w_cursor
.col
+= (colnr_T
)STRLEN(ml_get_cursor());
8846 case 'I': /* "I"nsert before the first non-blank */
8847 if (vim_strchr(p_cpo
, CPO_INSEND
) == NULL
)
8848 beginline(BL_WHITE
);
8850 beginline(BL_WHITE
|BL_FIX
);
8853 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8854 #ifdef FEAT_VIRTUALEDIT
8855 /* increment coladd when in virtual space, increment the
8856 * column otherwise, also to append after an unprintable char */
8857 if (virtual_active()
8858 && (curwin
->w_cursor
.coladd
> 0
8859 || *ml_get_cursor() == NUL
8860 || *ml_get_cursor() == TAB
))
8861 curwin
->w_cursor
.coladd
++;
8864 if (*ml_get_cursor() != NUL
)
8869 #ifdef FEAT_VIRTUALEDIT
8870 if (curwin
->w_cursor
.coladd
&& cap
->cmdchar
!= 'A')
8872 int save_State
= State
;
8874 /* Pretent Insert mode here to allow the cursor on the
8875 * character past the end of the line */
8877 coladvance(getviscol());
8882 invoke_edit(cap
, FALSE
, cap
->cmdchar
, FALSE
);
8887 * Invoke edit() and take care of "restart_edit" and the return value.
8890 invoke_edit(cap
, repl
, cmd
, startln
)
8892 int repl
; /* "r" or "gr" command */
8896 int restart_edit_save
= 0;
8898 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8899 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8901 if (repl
|| !stuff_empty())
8902 restart_edit_save
= restart_edit
;
8904 restart_edit_save
= 0;
8906 /* Always reset "restart_edit", this is not a restarted edit. */
8909 if (edit(cmd
, startln
, cap
->count1
))
8910 cap
->retval
|= CA_COMMAND_BUSY
;
8912 if (restart_edit
== 0)
8913 restart_edit
= restart_edit_save
;
8918 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8928 if (cap
->cmdchar
== 'i')
8929 include
= FALSE
; /* "ix" = inner object: exclude white space */
8931 include
= TRUE
; /* "ax" = an object: include white space */
8933 /* Make sure (), [], {} and <> are in 'matchpairs' */
8934 mps_save
= curbuf
->b_p_mps
;
8935 curbuf
->b_p_mps
= (char_u
*)"(:),{:},[:],<:>";
8939 case 'w': /* "aw" = a word */
8940 flag
= current_word(cap
->oap
, cap
->count1
, include
, FALSE
);
8942 case 'W': /* "aW" = a WORD */
8943 flag
= current_word(cap
->oap
, cap
->count1
, include
, TRUE
);
8945 case 'b': /* "ab" = a braces block */
8948 flag
= current_block(cap
->oap
, cap
->count1
, include
, '(', ')');
8950 case 'B': /* "aB" = a Brackets block */
8953 flag
= current_block(cap
->oap
, cap
->count1
, include
, '{', '}');
8955 case '[': /* "a[" = a [] block */
8957 flag
= current_block(cap
->oap
, cap
->count1
, include
, '[', ']');
8959 case '<': /* "a<" = a <> block */
8961 flag
= current_block(cap
->oap
, cap
->count1
, include
, '<', '>');
8963 case 't': /* "at" = a tag block (xml and html) */
8964 flag
= current_tagblock(cap
->oap
, cap
->count1
, include
);
8966 case 'p': /* "ap" = a paragraph */
8967 flag
= current_par(cap
->oap
, cap
->count1
, include
, 'p');
8969 case 's': /* "as" = a sentence */
8970 flag
= current_sent(cap
->oap
, cap
->count1
, include
);
8972 case '"': /* "a"" = a double quoted string */
8973 case '\'': /* "a'" = a single quoted string */
8974 case '`': /* "a`" = a backtick quoted string */
8975 flag
= current_quote(cap
->oap
, cap
->count1
, include
,
8979 case 'S': /* "aS" = a section */
8980 case 'f': /* "af" = a filename */
8981 case 'u': /* "au" = a URL */
8988 curbuf
->b_p_mps
= mps_save
;
8990 clearopbeep(cap
->oap
);
8991 adjust_cursor_col();
8992 curwin
->w_set_curswant
= TRUE
;
8997 * "q" command: Start/stop recording.
8998 * "q:", "q/", "q?": edit command-line in command-line window.
9004 if (cap
->oap
->op_type
== OP_FORMAT
)
9006 /* "gqq" is the same as "gqgq": format line */
9011 else if (!checkclearop(cap
->oap
))
9014 if (cap
->nchar
== ':' || cap
->nchar
== '/' || cap
->nchar
== '?')
9016 stuffcharReadbuff(cap
->nchar
);
9017 stuffcharReadbuff(K_CMDWIN
);
9021 /* (stop) recording into a named register, unless executing a
9023 if (!Exec_reg
&& do_record(cap
->nchar
) == FAIL
)
9024 clearopbeep(cap
->oap
);
9029 * Handle the "@r" command.
9035 if (checkclearop(cap
->oap
))
9038 if (cap
->nchar
== '=')
9040 if (get_expr_register() == NUL
)
9044 while (cap
->count1
-- && !got_int
)
9046 if (do_execreg(cap
->nchar
, FALSE
, FALSE
, FALSE
) == FAIL
)
9048 clearopbeep(cap
->oap
);
9056 * Handle the CTRL-U and CTRL-D commands.
9062 if ((cap
->cmdchar
== Ctrl_U
&& curwin
->w_cursor
.lnum
== 1)
9063 || (cap
->cmdchar
== Ctrl_D
9064 && curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
))
9065 clearopbeep(cap
->oap
);
9066 else if (!checkclearop(cap
->oap
))
9067 halfpage(cap
->cmdchar
== Ctrl_D
, cap
->count0
);
9071 * Handle "J" or "gJ" command.
9078 if (VIsual_active
) /* join the visual lines */
9082 if (!checkclearop(cap
->oap
))
9084 if (cap
->count0
<= 1)
9085 cap
->count0
= 2; /* default for join is two lines! */
9086 if (curwin
->w_cursor
.lnum
+ cap
->count0
- 1 >
9087 curbuf
->b_ml
.ml_line_count
)
9088 clearopbeep(cap
->oap
); /* beyond last line */
9091 prep_redo(cap
->oap
->regname
, cap
->count0
,
9092 NUL
, cap
->cmdchar
, NUL
, NUL
, cap
->nchar
);
9093 do_do_join(cap
->count0
, cap
->nchar
== NUL
);
9099 * "P", "gP", "p" and "gp" commands.
9107 void *reg1
= NULL
, *reg2
= NULL
;
9109 int was_visual
= FALSE
;
9114 if (cap
->oap
->op_type
!= OP_NOP
)
9117 /* "dp" is ":diffput" */
9118 if (cap
->oap
->op_type
== OP_DELETE
&& cap
->cmdchar
== 'p')
9121 nv_diffgetput(TRUE
);
9125 clearopbeep(cap
->oap
);
9129 dir
= (cap
->cmdchar
== 'P'
9130 || (cap
->cmdchar
== 'g' && cap
->nchar
== 'P'))
9131 ? BACKWARD
: FORWARD
;
9133 if (cap
->cmdchar
== 'g')
9134 flags
|= PUT_CURSEND
;
9139 /* Putting in Visual mode: The put text replaces the selected
9140 * text. First delete the selected text, then put the new text.
9141 * Need to save and restore the registers that the delete
9142 * overwrites if the old contents is being put.
9145 regname
= cap
->oap
->regname
;
9146 # ifdef FEAT_CLIPBOARD
9147 adjust_clip_reg(®name
);
9149 if (regname
== 0 || VIM_ISDIGIT(regname
)
9150 # ifdef FEAT_CLIPBOARD
9151 || (clip_unnamed
&& (regname
== '*' || regname
== '+'))
9156 /* the delete is going to overwrite the register we want to
9157 * put, save it first. */
9158 reg1
= get_register(regname
, TRUE
);
9161 /* Now delete the selected text. */
9164 cap
->oap
->regname
= NUL
;
9166 do_pending_operator(cap
, 0, FALSE
);
9167 empty
= (curbuf
->b_ml
.ml_flags
& ML_EMPTY
);
9169 /* delete PUT_LINE_BACKWARD; */
9170 cap
->oap
->regname
= regname
;
9174 /* Delete probably changed the register we want to put, save
9175 * it first. Then put back what was there before the delete. */
9176 reg2
= get_register(regname
, FALSE
);
9177 put_register(regname
, reg1
);
9180 /* When deleted a linewise Visual area, put the register as
9181 * lines to avoid it joined with the next line. When deletion was
9182 * characterwise, split a line when putting lines. */
9183 if (VIsual_mode
== 'V')
9185 else if (VIsual_mode
== 'v')
9186 flags
|= PUT_LINE_SPLIT
;
9187 if (VIsual_mode
== Ctrl_V
&& dir
== FORWARD
)
9188 flags
|= PUT_LINE_FORWARD
;
9190 if ((VIsual_mode
!= 'V'
9191 && curwin
->w_cursor
.col
< curbuf
->b_op_start
.col
)
9192 || (VIsual_mode
== 'V'
9193 && curwin
->w_cursor
.lnum
< curbuf
->b_op_start
.lnum
))
9194 /* cursor is at the end of the line or end of file, put
9199 do_put(cap
->oap
->regname
, dir
, cap
->count1
, flags
);
9202 /* If a register was saved, put it back now. */
9204 put_register(regname
, reg2
);
9206 /* What to reselect with "gv"? Selecting the just put text seems to
9207 * be the most useful, since the original text was removed. */
9210 curbuf
->b_visual
.vi_start
= curbuf
->b_op_start
;
9211 curbuf
->b_visual
.vi_end
= curbuf
->b_op_end
;
9214 /* When all lines were selected and deleted do_put() leaves an empty
9215 * line that needs to be deleted now. */
9216 if (empty
&& *ml_get(curbuf
->b_ml
.ml_line_count
) == NUL
)
9218 ml_delete(curbuf
->b_ml
.ml_line_count
, TRUE
);
9220 /* If the cursor was in that line, move it to the end of the last
9222 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
9224 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
9225 coladvance((colnr_T
)MAXCOL
);
9229 auto_format(FALSE
, TRUE
);
9234 * "o" and "O" commands.
9241 /* "do" is ":diffget" */
9242 if (cap
->oap
->op_type
== OP_DELETE
&& cap
->cmdchar
== 'o')
9245 nv_diffgetput(FALSE
);
9250 if (VIsual_active
) /* switch start and end of visual */
9251 v_swap_corners(cap
->cmdchar
);
9260 cmdarg_T
*cap UNUSED
;
9262 ProcessSniffRequests();
9266 #ifdef FEAT_NETBEANS_INTG
9271 netbeans_keycommand(cap
->nchar
);
9278 cmdarg_T
*cap UNUSED
;
9280 do_put('~', BACKWARD
, 1L, PUT_CURSEND
);
9286 * Trigger CursorHold event.
9287 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9288 * input buffer. "did_cursorhold" is set to avoid retriggering.
9294 apply_autocmds(EVENT_CURSORHOLD
, NULL
, NULL
, FALSE
, curbuf
);
9295 did_cursorhold
= TRUE
;
9296 cap
->retval
|= CA_COMMAND_BUSY
; /* don't call edit() now */