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
));
144 static void nv_lineop
__ARGS((cmdarg_T
*cap
));
145 static void nv_home
__ARGS((cmdarg_T
*cap
));
146 static void nv_pipe
__ARGS((cmdarg_T
*cap
));
147 static void nv_bck_word
__ARGS((cmdarg_T
*cap
));
148 static void nv_wordcmd
__ARGS((cmdarg_T
*cap
));
149 static void nv_beginline
__ARGS((cmdarg_T
*cap
));
151 static void adjust_for_sel
__ARGS((cmdarg_T
*cap
));
152 static int unadjust_for_sel
__ARGS((void));
153 static void nv_select
__ARGS((cmdarg_T
*cap
));
155 static void nv_goto
__ARGS((cmdarg_T
*cap
));
156 static void nv_normal
__ARGS((cmdarg_T
*cap
));
157 static void nv_esc
__ARGS((cmdarg_T
*oap
));
158 static void nv_edit
__ARGS((cmdarg_T
*cap
));
159 static void invoke_edit
__ARGS((cmdarg_T
*cap
, int repl
, int cmd
, int startln
));
161 static void nv_object
__ARGS((cmdarg_T
*cap
));
163 static void nv_record
__ARGS((cmdarg_T
*cap
));
164 static void nv_at
__ARGS((cmdarg_T
*cap
));
165 static void nv_halfpage
__ARGS((cmdarg_T
*cap
));
166 static void nv_join
__ARGS((cmdarg_T
*cap
));
167 static void nv_put
__ARGS((cmdarg_T
*cap
));
168 static void nv_open
__ARGS((cmdarg_T
*cap
));
170 static void nv_sniff
__ARGS((cmdarg_T
*cap
));
172 #ifdef FEAT_NETBEANS_INTG
173 static void nv_nbcmd
__ARGS((cmdarg_T
*cap
));
176 static void nv_drop
__ARGS((cmdarg_T
*cap
));
179 static void nv_cursorhold
__ARGS((cmdarg_T
*cap
));
183 * Function to be called for a Normal or Visual mode command.
184 * The argument is a cmdarg_T.
186 typedef void (*nv_func_T
) __ARGS((cmdarg_T
*cap
));
188 /* Values for cmd_flags. */
189 #define NV_NCH 0x01 /* may need to get a second char */
190 #define NV_NCH_NOP (0x02|NV_NCH) /* get second char when no operator pending */
191 #define NV_NCH_ALW (0x04|NV_NCH) /* always get a second char */
192 #define NV_LANG 0x08 /* second char needs language adjustment */
194 #define NV_SS 0x10 /* may start selection */
195 #define NV_SSS 0x20 /* may start selection with shift modifier */
196 #define NV_STS 0x40 /* may stop selection without shift modif. */
197 #define NV_RL 0x80 /* 'rightleft' modifies command */
198 #define NV_KEEPREG 0x100 /* don't clear regname */
199 #define NV_NCW 0x200 /* not allowed in command-line window */
202 * Generally speaking, every Normal mode command should either clear any
203 * pending operator (with *clearop*()), or set the motion type variable
206 * When a cursor motion command is made, it is marked as being a character or
207 * line oriented motion. Then, if an operator is in effect, the operation
208 * becomes character or line oriented accordingly.
212 * This table contains one entry for every Normal or Visual mode command.
213 * The order doesn't matter, init_normal_cmds() will create a sorted index.
214 * It is faster when all keys from zero to '~' are present.
216 static const struct nv_cmd
218 int cmd_char
; /* (first) command character */
219 nv_func_T cmd_func
; /* function for this command */
220 short_u cmd_flags
; /* NV_ flags */
221 short cmd_arg
; /* value for ca.arg */
224 {NUL
, nv_error
, 0, 0},
225 {Ctrl_A
, nv_addsub
, 0, 0},
226 {Ctrl_B
, nv_page
, NV_STS
, BACKWARD
},
227 {Ctrl_C
, nv_esc
, 0, TRUE
},
228 {Ctrl_D
, nv_halfpage
, 0, 0},
229 {Ctrl_E
, nv_scroll_line
, 0, TRUE
},
230 {Ctrl_F
, nv_page
, NV_STS
, FORWARD
},
231 {Ctrl_G
, nv_ctrlg
, 0, 0},
232 {Ctrl_H
, nv_ctrlh
, 0, 0},
233 {Ctrl_I
, nv_pcmark
, 0, 0},
234 {NL
, nv_down
, 0, FALSE
},
235 {Ctrl_K
, nv_error
, 0, 0},
236 {Ctrl_L
, nv_clear
, 0, 0},
237 {Ctrl_M
, nv_down
, 0, TRUE
},
238 {Ctrl_N
, nv_down
, NV_STS
, FALSE
},
239 {Ctrl_O
, nv_ctrlo
, 0, 0},
240 {Ctrl_P
, nv_up
, NV_STS
, FALSE
},
242 {Ctrl_Q
, nv_visual
, 0, FALSE
},
244 {Ctrl_Q
, nv_ignore
, 0, 0},
246 {Ctrl_R
, nv_redo
, 0, 0},
247 {Ctrl_S
, nv_ignore
, 0, 0},
248 {Ctrl_T
, nv_tagpop
, NV_NCW
, 0},
249 {Ctrl_U
, nv_halfpage
, 0, 0},
251 {Ctrl_V
, nv_visual
, 0, FALSE
},
252 {'V', nv_visual
, 0, FALSE
},
253 {'v', nv_visual
, 0, FALSE
},
255 {Ctrl_V
, nv_error
, 0, 0},
256 {'V', nv_error
, 0, 0},
257 {'v', nv_error
, 0, 0},
259 {Ctrl_W
, nv_window
, 0, 0},
260 {Ctrl_X
, nv_addsub
, 0, 0},
261 {Ctrl_Y
, nv_scroll_line
, 0, FALSE
},
262 {Ctrl_Z
, nv_suspend
, 0, 0},
263 {ESC
, nv_esc
, 0, FALSE
},
264 {Ctrl_BSL
, nv_normal
, NV_NCH_ALW
, 0},
265 {Ctrl_RSB
, nv_ident
, NV_NCW
, 0},
266 {Ctrl_HAT
, nv_hat
, NV_NCW
, 0},
267 {Ctrl__
, nv_error
, 0, 0},
268 {' ', nv_right
, 0, 0},
269 {'!', nv_operator
, 0, 0},
270 {'"', nv_regname
, NV_NCH_NOP
|NV_KEEPREG
, 0},
271 {'#', nv_ident
, 0, 0},
272 {'$', nv_dollar
, 0, 0},
273 {'%', nv_percent
, 0, 0},
274 {'&', nv_optrans
, 0, 0},
275 {'\'', nv_gomark
, NV_NCH_ALW
, TRUE
},
276 {'(', nv_brace
, 0, BACKWARD
},
277 {')', nv_brace
, 0, FORWARD
},
278 {'*', nv_ident
, 0, 0},
279 {'+', nv_down
, 0, TRUE
},
280 {',', nv_csearch
, 0, TRUE
},
281 {'-', nv_up
, 0, TRUE
},
282 {'.', nv_dot
, NV_KEEPREG
, 0},
283 {'/', nv_search
, 0, FALSE
},
284 {'0', nv_beginline
, 0, 0},
285 {'1', nv_ignore
, 0, 0},
286 {'2', nv_ignore
, 0, 0},
287 {'3', nv_ignore
, 0, 0},
288 {'4', nv_ignore
, 0, 0},
289 {'5', nv_ignore
, 0, 0},
290 {'6', nv_ignore
, 0, 0},
291 {'7', nv_ignore
, 0, 0},
292 {'8', nv_ignore
, 0, 0},
293 {'9', nv_ignore
, 0, 0},
294 {':', nv_colon
, 0, 0},
295 {';', nv_csearch
, 0, FALSE
},
296 {'<', nv_operator
, NV_RL
, 0},
297 {'=', nv_operator
, 0, 0},
298 {'>', nv_operator
, NV_RL
, 0},
299 {'?', nv_search
, 0, FALSE
},
300 {'@', nv_at
, NV_NCH_NOP
, FALSE
},
301 {'A', nv_edit
, 0, 0},
302 {'B', nv_bck_word
, 0, 1},
303 {'C', nv_abbrev
, NV_KEEPREG
, 0},
304 {'D', nv_abbrev
, NV_KEEPREG
, 0},
305 {'E', nv_wordcmd
, 0, TRUE
},
306 {'F', nv_csearch
, NV_NCH_ALW
|NV_LANG
, BACKWARD
},
307 {'G', nv_goto
, 0, TRUE
},
308 {'H', nv_scroll
, 0, 0},
309 {'I', nv_edit
, 0, 0},
310 {'J', nv_join
, 0, 0},
311 {'K', nv_ident
, 0, 0},
312 {'L', nv_scroll
, 0, 0},
313 {'M', nv_scroll
, 0, 0},
314 {'N', nv_next
, 0, SEARCH_REV
},
315 {'O', nv_open
, 0, 0},
317 {'Q', nv_exmode
, NV_NCW
, 0},
318 {'R', nv_Replace
, 0, FALSE
},
319 {'S', nv_subst
, NV_KEEPREG
, 0},
320 {'T', nv_csearch
, NV_NCH_ALW
|NV_LANG
, BACKWARD
},
321 {'U', nv_Undo
, 0, 0},
322 {'W', nv_wordcmd
, 0, TRUE
},
323 {'X', nv_abbrev
, NV_KEEPREG
, 0},
324 {'Y', nv_abbrev
, NV_KEEPREG
, 0},
325 {'Z', nv_Zet
, NV_NCH_NOP
|NV_NCW
, 0},
326 {'[', nv_brackets
, NV_NCH_ALW
, BACKWARD
},
327 {'\\', nv_error
, 0, 0},
328 {']', nv_brackets
, NV_NCH_ALW
, FORWARD
},
329 {'^', nv_beginline
, 0, BL_WHITE
| BL_FIX
},
330 {'_', nv_lineop
, 0, 0},
331 {'`', nv_gomark
, NV_NCH_ALW
, FALSE
},
332 {'a', nv_edit
, NV_NCH
, 0},
333 {'b', nv_bck_word
, 0, 0},
334 {'c', nv_operator
, 0, 0},
335 {'d', nv_operator
, 0, 0},
336 {'e', nv_wordcmd
, 0, FALSE
},
337 {'f', nv_csearch
, NV_NCH_ALW
|NV_LANG
, FORWARD
},
338 {'g', nv_g_cmd
, NV_NCH_ALW
, FALSE
},
339 {'h', nv_left
, NV_RL
, 0},
340 {'i', nv_edit
, NV_NCH
, 0},
341 {'j', nv_down
, 0, FALSE
},
342 {'k', nv_up
, 0, FALSE
},
343 {'l', nv_right
, NV_RL
, 0},
344 {'m', nv_mark
, NV_NCH_NOP
, 0},
345 {'n', nv_next
, 0, 0},
346 {'o', nv_open
, 0, 0},
348 {'q', nv_record
, NV_NCH
, 0},
349 {'r', nv_replace
, NV_NCH_NOP
|NV_LANG
, 0},
350 {'s', nv_subst
, NV_KEEPREG
, 0},
351 {'t', nv_csearch
, NV_NCH_ALW
|NV_LANG
, FORWARD
},
352 {'u', nv_undo
, 0, 0},
353 {'w', nv_wordcmd
, 0, FALSE
},
354 {'x', nv_abbrev
, NV_KEEPREG
, 0},
355 {'y', nv_operator
, 0, 0},
356 {'z', nv_zet
, NV_NCH_ALW
, 0},
357 {'{', nv_findpar
, 0, BACKWARD
},
358 {'|', nv_pipe
, 0, 0},
359 {'}', nv_findpar
, 0, FORWARD
},
360 {'~', nv_tilde
, 0, 0},
363 {POUND
, nv_ident
, 0, 0},
365 {K_MOUSEUP
, nv_mousescroll
, 0, TRUE
},
366 {K_MOUSEDOWN
, nv_mousescroll
, 0, FALSE
},
367 {K_LEFTMOUSE
, nv_mouse
, 0, 0},
368 {K_LEFTMOUSE_NM
, nv_mouse
, 0, 0},
369 {K_LEFTDRAG
, nv_mouse
, 0, 0},
370 {K_LEFTRELEASE
, nv_mouse
, 0, 0},
371 {K_LEFTRELEASE_NM
, nv_mouse
, 0, 0},
372 {K_MIDDLEMOUSE
, nv_mouse
, 0, 0},
373 {K_MIDDLEDRAG
, nv_mouse
, 0, 0},
374 {K_MIDDLERELEASE
, nv_mouse
, 0, 0},
375 {K_RIGHTMOUSE
, nv_mouse
, 0, 0},
376 {K_RIGHTDRAG
, nv_mouse
, 0, 0},
377 {K_RIGHTRELEASE
, nv_mouse
, 0, 0},
378 {K_X1MOUSE
, nv_mouse
, 0, 0},
379 {K_X1DRAG
, nv_mouse
, 0, 0},
380 {K_X1RELEASE
, nv_mouse
, 0, 0},
381 {K_X2MOUSE
, nv_mouse
, 0, 0},
382 {K_X2DRAG
, nv_mouse
, 0, 0},
383 {K_X2RELEASE
, nv_mouse
, 0, 0},
385 {K_IGNORE
, nv_ignore
, NV_KEEPREG
, 0},
386 {K_NOP
, nv_nop
, 0, 0},
387 {K_INS
, nv_edit
, 0, 0},
388 {K_KINS
, nv_edit
, 0, 0},
389 {K_BS
, nv_ctrlh
, 0, 0},
390 {K_UP
, nv_up
, NV_SSS
|NV_STS
, FALSE
},
391 {K_S_UP
, nv_page
, NV_SS
, BACKWARD
},
392 {K_DOWN
, nv_down
, NV_SSS
|NV_STS
, FALSE
},
393 {K_S_DOWN
, nv_page
, NV_SS
, FORWARD
},
394 {K_LEFT
, nv_left
, NV_SSS
|NV_STS
|NV_RL
, 0},
395 {K_S_LEFT
, nv_bck_word
, NV_SS
|NV_RL
, 0},
396 {K_C_LEFT
, nv_bck_word
, NV_SSS
|NV_RL
|NV_STS
, 1},
397 {K_RIGHT
, nv_right
, NV_SSS
|NV_STS
|NV_RL
, 0},
398 {K_S_RIGHT
, nv_wordcmd
, NV_SS
|NV_RL
, FALSE
},
399 {K_C_RIGHT
, nv_wordcmd
, NV_SSS
|NV_RL
|NV_STS
, TRUE
},
400 {K_PAGEUP
, nv_page
, NV_SSS
|NV_STS
, BACKWARD
},
401 {K_KPAGEUP
, nv_page
, NV_SSS
|NV_STS
, BACKWARD
},
402 {K_PAGEDOWN
, nv_page
, NV_SSS
|NV_STS
, FORWARD
},
403 {K_KPAGEDOWN
, nv_page
, NV_SSS
|NV_STS
, FORWARD
},
404 {K_END
, nv_end
, NV_SSS
|NV_STS
, FALSE
},
405 {K_KEND
, nv_end
, NV_SSS
|NV_STS
, FALSE
},
406 {K_S_END
, nv_end
, NV_SS
, FALSE
},
407 {K_C_END
, nv_end
, NV_SSS
|NV_STS
, TRUE
},
408 {K_HOME
, nv_home
, NV_SSS
|NV_STS
, 0},
409 {K_KHOME
, nv_home
, NV_SSS
|NV_STS
, 0},
410 {K_S_HOME
, nv_home
, NV_SS
, 0},
411 {K_C_HOME
, nv_goto
, NV_SSS
|NV_STS
, FALSE
},
412 {K_DEL
, nv_abbrev
, 0, 0},
413 {K_KDEL
, nv_abbrev
, 0, 0},
414 {K_UNDO
, nv_kundo
, 0, 0},
415 {K_HELP
, nv_help
, NV_NCW
, 0},
416 {K_F1
, nv_help
, NV_NCW
, 0},
417 {K_XF1
, nv_help
, NV_NCW
, 0},
419 {K_SELECT
, nv_select
, 0, 0},
422 {K_VER_SCROLLBAR
, nv_ver_scrollbar
, 0, 0},
423 {K_HOR_SCROLLBAR
, nv_hor_scrollbar
, 0, 0},
425 #ifdef FEAT_GUI_TABLINE
426 {K_TABLINE
, nv_tabline
, 0, 0},
427 {K_TABMENU
, nv_tabmenu
, 0, 0},
430 {K_F8
, farsi_fkey
, 0, 0},
431 {K_F9
, farsi_fkey
, 0, 0},
434 {K_SNIFF
, nv_sniff
, 0, 0},
436 #ifdef FEAT_NETBEANS_INTG
437 {K_F21
, nv_nbcmd
, NV_NCH_ALW
, 0},
440 {K_DROP
, nv_drop
, NV_STS
, 0},
443 {K_CURSORHOLD
, nv_cursorhold
, NV_KEEPREG
, 0},
447 /* Number of commands in nv_cmds[]. */
448 #define NV_CMDS_SIZE (sizeof(nv_cmds) / sizeof(struct nv_cmd))
450 /* Sorted index of commands in nv_cmds[]. */
451 static short nv_cmd_idx
[NV_CMDS_SIZE
];
453 /* The highest index for which
454 * nv_cmds[idx].cmd_char == nv_cmd_idx[nv_cmds[idx].cmd_char] */
455 static int nv_max_linear
;
458 * Compare functions for qsort() below, that checks the command character
459 * through the index in nv_cmd_idx[].
471 /* The commands are sorted on absolute value. */
472 c1
= nv_cmds
[*(const short *)s1
].cmd_char
;
473 c2
= nv_cmds
[*(const short *)s2
].cmd_char
;
482 * Initialize the nv_cmd_idx[] table.
489 /* Fill the index table with a one to one relation. */
490 for (i
= 0; i
< NV_CMDS_SIZE
; ++i
)
493 /* Sort the commands by the command character. */
494 qsort((void *)&nv_cmd_idx
, (size_t)NV_CMDS_SIZE
, sizeof(short), nv_compare
);
496 /* Find the first entry that can't be indexed by the command character. */
497 for (i
= 0; i
< NV_CMDS_SIZE
; ++i
)
498 if (i
!= nv_cmds
[nv_cmd_idx
[i
]].cmd_char
)
500 nv_max_linear
= i
- 1;
504 * Search for a command in the commands table.
505 * Returns -1 for invalid command.
508 find_command(cmdchar
)
517 /* A multi-byte character is never a command. */
518 if (cmdchar
>= 0x100)
522 /* We use the absolute value of the character. Special keys have a
523 * negative value, but are sorted on their absolute value. */
527 /* If the character is in the first part: The character is the index into
529 if (cmdchar
<= nv_max_linear
)
530 return nv_cmd_idx
[cmdchar
];
532 /* Perform a binary search. */
533 bot
= nv_max_linear
+ 1;
534 top
= NV_CMDS_SIZE
- 1;
539 c
= nv_cmds
[nv_cmd_idx
[i
]].cmd_char
;
556 * Execute a command in Normal mode.
560 normal_cmd(oap
, toplevel
)
562 int toplevel
; /* TRUE when called from main() */
564 static long opcount
= 0; /* ca.opcount saved here */
565 cmdarg_T ca
; /* command arguments */
567 int ctrl_w
= FALSE
; /* got CTRL-W command */
568 int old_col
= curwin
->w_curswant
;
569 #ifdef FEAT_CMDL_INFO
570 int need_flushbuf
; /* need to call out_flush() */
573 pos_T old_pos
; /* cursor position before command */
575 static int old_mapped_len
= 0;
579 vim_memset(&ca
, 0, sizeof(ca
)); /* also resets ca.retval */
581 ca
.opcount
= opcount
;
584 want_sniff_request
= sniff_connected
;
588 * If there is an operator pending, then the command we take this time
589 * will terminate it. Finish_op tells us to finish the operation before
590 * returning this time (unless the operation was cancelled).
595 finish_op
= (oap
->op_type
!= OP_NOP
);
599 ui_cursor_shape(); /* may show different cursor shape */
600 # ifdef FEAT_MOUSESHAPE
601 update_mouseshape(-1);
606 if (!finish_op
&& !oap
->regname
)
610 mapped_len
= typebuf_maplen();
614 #ifdef USE_ON_FLY_SCROLL
615 dont_scroll
= FALSE
; /* allow scrolling here */
619 * Get the command character from the user.
624 LANGMAP_ADJUST(c
, TRUE
);
629 * If a mapping was started in Visual or Select mode, remember the length
630 * of the mapping. This is used below to not return to Insert mode for as
631 * long as the mapping is being executed.
633 if (restart_edit
== 0)
635 else if (old_mapped_len
636 || (VIsual_active
&& mapped_len
== 0 && typebuf_maplen() > 0))
637 old_mapped_len
= typebuf_maplen();
645 * In Select mode, typed text replaces the selection.
649 && (vim_isprintc(c
) || c
== NL
|| c
== CAR
|| c
== K_KENTER
))
651 /* Fake a "c"hange command. When "restart_edit" is set (e.g., because
652 * 'insertmode' is set) fake a "d"elete command, Insert mode will
653 * restart automatically.
654 * Insert the typed character in the typeahead buffer, so that it can
655 * be mapped in Insert mode. Required for ":lmap" to work. */
657 if (restart_edit
!= 0)
661 msg_nowait
= TRUE
; /* don't delay going to insert mode */
665 #ifdef FEAT_CMDL_INFO
666 need_flushbuf
= add_to_showcmd(c
);
671 if (!(VIsual_active
&& VIsual_select
))
675 * Handle a count before a command and compute ca.count0.
676 * Note that '0' is a command and not the start of a count, but it's
677 * part of a count after other digits.
679 while ( (c
>= '1' && c
<= '9')
680 || (ca
.count0
!= 0 && (c
== K_DEL
|| c
== K_KDEL
|| c
== '0')))
682 if (c
== K_DEL
|| c
== K_KDEL
)
685 #ifdef FEAT_CMDL_INFO
686 del_from_showcmd(4); /* delete the digit and ~@% */
690 ca
.count0
= ca
.count0
* 10 + (c
- '0');
691 if (ca
.count0
< 0) /* got too large! */
692 ca
.count0
= 999999999L;
694 /* Set v:count here, when called from main() and not a stuffed
695 * command, so that v:count can be used in an expression mapping
696 * right after the count. */
697 if (toplevel
&& stuff_empty())
698 set_vcount(ca
.count0
, ca
.count0
== 0 ? 1 : ca
.count0
);
703 ++allow_keys
; /* no mapping for nchar, but keys */
705 ++no_zero_mapping
; /* don't map zero here */
708 LANGMAP_ADJUST(c
, TRUE
);
716 #ifdef FEAT_CMDL_INFO
717 need_flushbuf
|= add_to_showcmd(c
);
722 * If we got CTRL-W there may be a/another count
724 if (c
== Ctrl_W
&& !ctrl_w
&& oap
->op_type
== OP_NOP
)
727 ca
.opcount
= ca
.count0
; /* remember first count */
730 ++allow_keys
; /* no mapping for nchar, but keys */
731 c
= plain_vgetc(); /* get next character */
733 LANGMAP_ADJUST(c
, TRUE
);
737 #ifdef FEAT_CMDL_INFO
738 need_flushbuf
|= add_to_showcmd(c
);
740 goto getcount
; /* jump back */
745 * If we're in the middle of an operator (including after entering a yank
746 * buffer with '"') AND we had a count before the operator, then that
747 * count overrides the current value of ca.count0.
748 * What this means effectively, is that commands like "3dw" get turned
749 * into "d3w" which makes things fall into place pretty neatly.
750 * If you give a count before AND after the operator, they are multiplied.
755 ca
.count0
*= ca
.opcount
;
757 ca
.count0
= ca
.opcount
;
761 * Always remember the count. It will be set to zero (on the next call,
762 * above) when there is no pending operator.
763 * When called from main(), save the count for use by the "count" built-in
766 ca
.opcount
= ca
.count0
;
767 ca
.count1
= (ca
.count0
== 0 ? 1 : ca
.count0
);
771 * Only set v:count when called from main() and not a stuffed command.
773 if (toplevel
&& stuff_empty())
774 set_vcount(ca
.count0
, ca
.count1
);
778 * Find the command character in the table of commands.
779 * For CTRL-W we already got nchar when looking for a count.
788 idx
= find_command(ca
.cmdchar
);
791 /* Not a known command: beep. */
796 if (text_locked() && (nv_cmds
[idx
].cmd_flags
& NV_NCW
))
798 /* This command is not allowed wile editing a ccmdline: beep. */
804 if ((nv_cmds
[idx
].cmd_flags
& NV_NCW
) && curbuf_locked())
810 * In Visual/Select mode, a few keys are handled in a special way.
814 /* when 'keymodel' contains "stopsel" may stop Select/Visual mode */
816 && (nv_cmds
[idx
].cmd_flags
& NV_STS
)
817 && !(mod_mask
& MOD_MASK_SHIFT
))
820 redraw_curbuf_later(INVERTED
);
823 /* Keys that work different when 'keymodel' contains "startsel" */
826 if (nv_cmds
[idx
].cmd_flags
& NV_SS
)
828 unshift_special(&ca
);
829 idx
= find_command(ca
.cmdchar
);
837 else if ((nv_cmds
[idx
].cmd_flags
& NV_SSS
)
838 && (mod_mask
& MOD_MASK_SHIFT
))
840 mod_mask
&= ~MOD_MASK_SHIFT
;
846 #ifdef FEAT_RIGHTLEFT
847 if (curwin
->w_p_rl
&& KeyTyped
&& !KeyStuffed
848 && (nv_cmds
[idx
].cmd_flags
& NV_RL
))
850 /* Invert horizontal movements and operations. Only when typed by the
851 * user directly, not when the result of a mapping or "x" translated
855 case 'l': ca
.cmdchar
= 'h'; break;
856 case K_RIGHT
: ca
.cmdchar
= K_LEFT
; break;
857 case K_S_RIGHT
: ca
.cmdchar
= K_S_LEFT
; break;
858 case K_C_RIGHT
: ca
.cmdchar
= K_C_LEFT
; break;
859 case 'h': ca
.cmdchar
= 'l'; break;
860 case K_LEFT
: ca
.cmdchar
= K_RIGHT
; break;
861 case K_S_LEFT
: ca
.cmdchar
= K_S_RIGHT
; break;
862 case K_C_LEFT
: ca
.cmdchar
= K_C_RIGHT
; break;
863 case '>': ca
.cmdchar
= '<'; break;
864 case '<': ca
.cmdchar
= '>'; break;
866 idx
= find_command(ca
.cmdchar
);
871 * Get an additional character if we need one.
873 if ((nv_cmds
[idx
].cmd_flags
& NV_NCH
)
874 && (((nv_cmds
[idx
].cmd_flags
& NV_NCH_NOP
) == NV_NCH_NOP
875 && oap
->op_type
== OP_NOP
)
876 || (nv_cmds
[idx
].cmd_flags
& NV_NCH_ALW
) == NV_NCH_ALW
877 || (ca
.cmdchar
== 'q'
878 && oap
->op_type
== OP_NOP
881 || ((ca
.cmdchar
== 'a' || ca
.cmdchar
== 'i')
882 && (oap
->op_type
!= OP_NOP
889 int repl
= FALSE
; /* get character for replace mode */
890 int lit
= FALSE
; /* get extra character literally */
891 int langmap_active
= FALSE
; /* using :lmap mappings */
892 int lang
; /* getting a text character */
893 #ifdef USE_IM_CONTROL
894 int save_smd
; /* saved value of p_smd */
898 ++allow_keys
; /* no mapping for nchar, but allow key codes */
900 /* Don't generate a CursorHold event here, most commands can't handle
901 * it, e.g., nv_replace(), nv_csearch(). */
902 did_cursorhold
= TRUE
;
904 if (ca
.cmdchar
== 'g')
907 * For 'g' get the next character now, so that we can check for
908 * "gr", "g'" and "g`".
910 ca
.nchar
= plain_vgetc();
912 LANGMAP_ADJUST(ca
.nchar
, TRUE
);
914 #ifdef FEAT_CMDL_INFO
915 need_flushbuf
|= add_to_showcmd(ca
.nchar
);
917 if (ca
.nchar
== 'r' || ca
.nchar
== '\'' || ca
.nchar
== '`'
918 || ca
.nchar
== Ctrl_BSL
)
920 cp
= &ca
.extra_char
; /* need to get a third character */
922 lit
= TRUE
; /* get it literally */
924 repl
= TRUE
; /* get it in replace mode */
927 cp
= NULL
; /* no third character needed */
931 if (ca
.cmdchar
== 'r') /* get it in replace mode */
935 lang
= (repl
|| (nv_cmds
[idx
].cmd_flags
& NV_LANG
));
938 * Get a second or third character.
945 State
= REPLACE
; /* pretend Replace mode */
946 ui_cursor_shape(); /* show different cursor shape */
949 if (lang
&& curbuf
->b_p_iminsert
== B_IMODE_LMAP
)
951 /* Allow mappings defined with ":lmap". */
958 langmap_active
= TRUE
;
960 #ifdef USE_IM_CONTROL
962 p_smd
= FALSE
; /* Don't let the IM code show the mode here */
963 if (lang
&& curbuf
->b_p_iminsert
== B_IMODE_IM
)
971 /* Undo the decrement done above */
976 #ifdef USE_IM_CONTROL
979 if (curbuf
->b_p_iminsert
!= B_IMODE_LMAP
)
980 im_save_status(&curbuf
->b_p_iminsert
);
981 im_set_active(FALSE
);
988 #ifdef FEAT_CMDL_INFO
989 need_flushbuf
|= add_to_showcmd(*cp
);
995 /* Typing CTRL-K gets a digraph. */
997 && ((nv_cmds
[idx
].cmd_flags
& NV_LANG
)
998 || cp
== &ca
.extra_char
)
999 && vim_strchr(p_cpo
, CPO_DIGRAPH
) == NULL
)
1001 c
= get_digraph(FALSE
);
1005 # ifdef FEAT_CMDL_INFO
1006 /* Guessing how to update showcmd here... */
1007 del_from_showcmd(3);
1008 need_flushbuf
|= add_to_showcmd(*cp
);
1015 /* adjust chars > 127, except after "tTfFr" commands */
1016 LANGMAP_ADJUST(*cp
, !lang
);
1018 #ifdef FEAT_RIGHTLEFT
1019 /* adjust Hebrew mapped char */
1020 if (p_hkmap
&& lang
&& KeyTyped
)
1023 /* adjust Farsi mapped char */
1024 if (p_fkmap
&& lang
&& KeyTyped
)
1031 * When the next character is CTRL-\ a following CTRL-N means the
1032 * command is aborted and we go to Normal mode.
1034 if (cp
== &ca
.extra_char
1035 && ca
.nchar
== Ctrl_BSL
1036 && (ca
.extra_char
== Ctrl_N
|| ca
.extra_char
== Ctrl_G
))
1038 ca
.cmdchar
= Ctrl_BSL
;
1039 ca
.nchar
= ca
.extra_char
;
1040 idx
= find_command(ca
.cmdchar
);
1042 else if (*cp
== Ctrl_BSL
)
1044 long towait
= (p_ttm
>= 0 ? p_ttm
: p_tm
);
1046 /* There is a busy wait here when typing "f<C-\>" and then
1047 * something different from CTRL-N. Can't be avoided. */
1048 while ((c
= vpeekc()) <= 0 && towait
> 0L)
1050 do_sleep(towait
> 50L ? 50L : towait
);
1056 if (c
!= Ctrl_N
&& c
!= Ctrl_G
)
1060 ca
.cmdchar
= Ctrl_BSL
;
1062 idx
= find_command(ca
.cmdchar
);
1068 /* When getting a text character and the next character is a
1069 * multi-byte character, it could be a composing character.
1070 * However, don't wait for it to arrive. */
1071 while (enc_utf8
&& lang
&& (c
= vpeekc()) > 0
1072 && (c
>= 0x100 || MB_BYTE2LEN(vpeekc()) > 1))
1075 if (!utf_iscomposing(c
))
1077 vungetc(c
); /* it wasn't, put it back */
1080 else if (ca
.ncharC1
== 0)
1091 #ifdef FEAT_CMDL_INFO
1093 * Flush the showcmd characters onto the screen so we can see them while
1094 * the command is being executed. Only do this when the shown command was
1095 * actually displayed, otherwise this will slow down a lot when executing
1102 did_cursorhold
= FALSE
;
1107 if (ca
.nchar
== ESC
)
1110 if (restart_edit
== 0 && goto_im())
1115 if (ca
.cmdchar
!= K_IGNORE
)
1117 msg_didout
= FALSE
; /* don't scroll screen up for normal command */
1122 old_pos
= curwin
->w_cursor
; /* remember where cursor was */
1124 /* When 'keymodel' contains "startsel" some keys start Select/Visual
1126 if (!VIsual_active
&& km_startsel
)
1128 if (nv_cmds
[idx
].cmd_flags
& NV_SS
)
1131 unshift_special(&ca
);
1132 idx
= find_command(ca
.cmdchar
);
1134 else if ((nv_cmds
[idx
].cmd_flags
& NV_SSS
)
1135 && (mod_mask
& MOD_MASK_SHIFT
))
1138 mod_mask
&= ~MOD_MASK_SHIFT
;
1144 * Execute the command!
1145 * Call the command function found in the commands table.
1147 ca
.arg
= nv_cmds
[idx
].cmd_arg
;
1148 (nv_cmds
[idx
].cmd_func
)(&ca
);
1151 * If we didn't start or finish an operator, reset oap->regname, unless we
1156 && (idx
< 0 || !(nv_cmds
[idx
].cmd_flags
& NV_KEEPREG
)))
1165 /* Get the length of mapped chars again after typing a count, second
1166 * character or "z333<cr>". */
1167 if (old_mapped_len
> 0)
1168 old_mapped_len
= typebuf_maplen();
1172 * If an operation is pending, handle it...
1174 do_pending_operator(&ca
, old_col
, FALSE
);
1177 * Wait for a moment when a message is displayed that will be overwritten
1178 * by the mode message.
1179 * In Visual mode and with "^O" in Insert mode, a short message will be
1180 * overwritten by the mode message. Wait a bit, until a key is hit.
1181 * In Visual mode, it's more important to keep the Visual area updated
1182 * than keeping a message (e.g. from a /pat search).
1183 * Only do this if the command was typed, not from a mapping.
1184 * Don't wait when emsg_silent is non-zero.
1185 * Also wait a bit after an error message, e.g. for "^O:".
1186 * Don't redraw the screen, it would remove the message.
1190 && (restart_edit
!= 0
1193 && old_pos
.lnum
== curwin
->w_cursor
.lnum
1194 && old_pos
.col
== curwin
->w_cursor
.col
)
1199 && (msg_didout
|| (msg_didany
&& msg_scroll
))
1202 || (restart_edit
!= 0
1207 || emsg_on_display
)))
1208 && oap
->regname
== 0
1209 && !(ca
.retval
& CA_COMMAND_BUSY
)
1214 && oap
->op_type
== OP_NOP
)
1216 int save_State
= State
;
1218 /* Draw the cursor with the right shape here */
1219 if (restart_edit
!= 0)
1222 /* If need to redraw, and there is a "keep_msg", redraw before the
1224 if (must_redraw
&& keep_msg
!= NULL
&& !emsg_on_display
)
1230 /* showmode() will clear keep_msg, but we want to use it anyway */
1232 /* now reset it, otherwise it's put in the history again */
1234 msg_attr(kmsg
, keep_msg_attr
);
1240 if (msg_scroll
|| emsg_on_display
)
1241 ui_delay(1000L, TRUE
); /* wait at least one second */
1242 ui_delay(3000L, FALSE
); /* wait up to three seconds */
1246 emsg_on_display
= FALSE
;
1250 * Finish up after executing a Normal mode command.
1256 /* Reset finish_op, in case it was set */
1262 /* Redraw the cursor with another shape, if we were in Operator-pending
1263 * mode or did a replace command. */
1264 if (c
|| ca
.cmdchar
== 'r')
1266 ui_cursor_shape(); /* may show different cursor shape */
1267 # ifdef FEAT_MOUSESHAPE
1268 update_mouseshape(-1);
1273 #ifdef FEAT_CMDL_INFO
1274 if (oap
->op_type
== OP_NOP
&& oap
->regname
== 0)
1278 checkpcmark(); /* check if we moved since setting pcmark */
1279 vim_free(ca
.searchbuf
);
1286 #ifdef FEAT_SCROLLBIND
1287 if (curwin
->w_p_scb
&& toplevel
)
1289 validate_cursor(); /* may need to update w_leftcol */
1290 do_check_scrollbind(TRUE
);
1295 * May restart edit(), if we got here with CTRL-O in Insert mode (but not
1296 * if still inside a mapping that started in Visual mode).
1297 * May switch from Visual to Select mode after CTRL-O command.
1299 if ( oap
->op_type
== OP_NOP
1301 && ((restart_edit
!= 0 && !VIsual_active
&& old_mapped_len
== 0)
1302 || restart_VIsual_select
== 1)
1304 && restart_edit
!= 0
1306 && !(ca
.retval
& CA_COMMAND_BUSY
)
1308 && oap
->regname
== 0)
1311 if (restart_VIsual_select
== 1)
1313 VIsual_select
= TRUE
;
1315 restart_VIsual_select
= 0;
1318 if (restart_edit
!= 0
1320 && !VIsual_active
&& old_mapped_len
== 0
1323 (void)edit(restart_edit
, FALSE
, 1L);
1327 if (restart_VIsual_select
== 2)
1328 restart_VIsual_select
= 1;
1331 /* Save count before an operator for next time. */
1332 opcount
= ca
.opcount
;
1336 * Handle an operator after visual mode or when the movement is finished
1339 do_pending_operator(cap
, old_col
, gui_yank
)
1344 oparg_T
*oap
= cap
->oap
;
1346 int empty_region_error
;
1347 int restart_edit_save
;
1350 /* The visual area is remembered for redo */
1351 static int redo_VIsual_mode
= NUL
; /* 'v', 'V', or Ctrl-V */
1352 static linenr_T redo_VIsual_line_count
; /* number of lines */
1353 static colnr_T redo_VIsual_col
; /* number of cols or end column */
1354 static long redo_VIsual_count
; /* count for Visual operator */
1355 # ifdef FEAT_VIRTUALEDIT
1356 int include_line_break
= FALSE
;
1360 #if defined(FEAT_CLIPBOARD)
1362 * Yank the visual area into the GUI selection register before we operate
1363 * on it and lose it forever.
1364 * Don't do it if a specific register was specified, so that ""x"*P works.
1365 * This could call do_pending_operator() recursively, but that's OK
1366 * because gui_yank will be TRUE for the nested call.
1368 if (clip_star
.available
1369 && oap
->op_type
!= OP_NOP
1373 && !redo_VIsual_busy
1375 && oap
->regname
== 0)
1378 old_cursor
= curwin
->w_cursor
;
1381 * If an operation is pending, handle it...
1387 ) && oap
->op_type
!= OP_NOP
)
1390 oap
->is_VIsual
= VIsual_active
;
1391 if (oap
->motion_force
== 'V')
1392 oap
->motion_type
= MLINE
;
1393 else if (oap
->motion_force
== 'v')
1395 /* If the motion was linewise, "inclusive" will not have been set.
1396 * Use "exclusive" to be consistent. Makes "dvj" work nice. */
1397 if (oap
->motion_type
== MLINE
)
1398 oap
->inclusive
= FALSE
;
1399 /* If the motion already was characterwise, toggle "inclusive" */
1400 else if (oap
->motion_type
== MCHAR
)
1401 oap
->inclusive
= !oap
->inclusive
;
1402 oap
->motion_type
= MCHAR
;
1404 else if (oap
->motion_force
== Ctrl_V
)
1406 /* Change line- or characterwise motion into Visual block mode. */
1407 VIsual_active
= TRUE
;
1408 VIsual
= oap
->start
;
1409 VIsual_mode
= Ctrl_V
;
1410 VIsual_select
= FALSE
;
1411 VIsual_reselect
= FALSE
;
1415 /* only redo yank when 'y' flag is in 'cpoptions' */
1416 /* never redo "zf" (define fold) */
1417 if ((vim_strchr(p_cpo
, CPO_YANK
) != NULL
|| oap
->op_type
!= OP_YANK
)
1419 && (!VIsual_active
|| oap
->motion_force
)
1421 && cap
->cmdchar
!= 'D'
1423 && oap
->op_type
!= OP_FOLD
1424 && oap
->op_type
!= OP_FOLDOPEN
1425 && oap
->op_type
!= OP_FOLDOPENREC
1426 && oap
->op_type
!= OP_FOLDCLOSE
1427 && oap
->op_type
!= OP_FOLDCLOSEREC
1428 && oap
->op_type
!= OP_FOLDDEL
1429 && oap
->op_type
!= OP_FOLDDELREC
1433 prep_redo(oap
->regname
, cap
->count0
,
1434 get_op_char(oap
->op_type
), get_extra_op_char(oap
->op_type
),
1435 oap
->motion_force
, cap
->cmdchar
, cap
->nchar
);
1436 if (cap
->cmdchar
== '/' || cap
->cmdchar
== '?') /* was a search */
1439 * If 'cpoptions' does not contain 'r', insert the search
1440 * pattern to really repeat the same command.
1442 if (vim_strchr(p_cpo
, CPO_REDO
) == NULL
)
1443 AppendToRedobuffLit(cap
->searchbuf
, -1);
1444 AppendToRedobuff(NL_STR
);
1446 else if (cap
->cmdchar
== ':')
1448 /* do_cmdline() has stored the first typed line in
1449 * "repeat_cmdline". When several lines are typed repeating
1450 * won't be possible. */
1451 if (repeat_cmdline
== NULL
)
1455 AppendToRedobuffLit(repeat_cmdline
, -1);
1456 AppendToRedobuff(NL_STR
);
1457 vim_free(repeat_cmdline
);
1458 repeat_cmdline
= NULL
;
1464 if (redo_VIsual_busy
)
1466 oap
->start
= curwin
->w_cursor
;
1467 curwin
->w_cursor
.lnum
+= redo_VIsual_line_count
- 1;
1468 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
1469 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
1470 VIsual_mode
= redo_VIsual_mode
;
1471 if (VIsual_mode
== 'v')
1473 if (redo_VIsual_line_count
<= 1)
1474 curwin
->w_cursor
.col
+= redo_VIsual_col
- 1;
1476 curwin
->w_cursor
.col
= redo_VIsual_col
;
1478 if (redo_VIsual_col
== MAXCOL
)
1480 curwin
->w_curswant
= MAXCOL
;
1481 coladvance((colnr_T
)MAXCOL
);
1483 cap
->count0
= redo_VIsual_count
;
1484 if (redo_VIsual_count
!= 0)
1485 cap
->count1
= redo_VIsual_count
;
1489 else if (VIsual_active
)
1493 /* Save the current VIsual area for '< and '> marks, and "gv" */
1494 curbuf
->b_visual
.vi_start
= VIsual
;
1495 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
1496 curbuf
->b_visual
.vi_mode
= VIsual_mode
;
1497 curbuf
->b_visual
.vi_curswant
= curwin
->w_curswant
;
1499 curbuf
->b_visual_mode_eval
= VIsual_mode
;
1503 /* In Select mode, a linewise selection is operated upon like a
1504 * characterwise selection. */
1505 if (VIsual_select
&& VIsual_mode
== 'V')
1507 if (lt(VIsual
, curwin
->w_cursor
))
1510 curwin
->w_cursor
.col
=
1511 (colnr_T
)STRLEN(ml_get(curwin
->w_cursor
.lnum
));
1515 curwin
->w_cursor
.col
= 0;
1516 VIsual
.col
= (colnr_T
)STRLEN(ml_get(VIsual
.lnum
));
1520 /* If 'selection' is "exclusive", backup one character for
1521 * charwise selections. */
1522 else if (VIsual_mode
== 'v')
1524 # ifdef FEAT_VIRTUALEDIT
1525 include_line_break
=
1530 oap
->start
= VIsual
;
1531 if (VIsual_mode
== 'V')
1534 #endif /* FEAT_VISUAL */
1537 * Set oap->start to the first position of the operated text, oap->end
1538 * to the end of the operated text. w_cursor is equal to oap->start.
1540 if (lt(oap
->start
, curwin
->w_cursor
))
1543 /* Include folded lines completely. */
1546 if (hasFolding(oap
->start
.lnum
, &oap
->start
.lnum
, NULL
))
1548 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
,
1549 &curwin
->w_cursor
.lnum
))
1550 curwin
->w_cursor
.col
= (colnr_T
)STRLEN(ml_get_curline());
1553 oap
->end
= curwin
->w_cursor
;
1554 curwin
->w_cursor
= oap
->start
;
1556 /* w_virtcol may have been updated; if the cursor goes back to its
1557 * previous position w_virtcol becomes invalid and isn't updated
1559 curwin
->w_valid
&= ~VALID_VIRTCOL
;
1564 /* Include folded lines completely. */
1565 if (!VIsual_active
&& oap
->motion_type
== MLINE
)
1567 if (hasFolding(curwin
->w_cursor
.lnum
, &curwin
->w_cursor
.lnum
,
1569 curwin
->w_cursor
.col
= 0;
1570 if (hasFolding(oap
->start
.lnum
, NULL
, &oap
->start
.lnum
))
1571 oap
->start
.col
= (colnr_T
)STRLEN(ml_get(oap
->start
.lnum
));
1574 oap
->end
= oap
->start
;
1575 oap
->start
= curwin
->w_cursor
;
1578 oap
->line_count
= oap
->end
.lnum
- oap
->start
.lnum
+ 1;
1580 #ifdef FEAT_VIRTUALEDIT
1581 /* Set "virtual_op" before resetting VIsual_active. */
1582 virtual_op
= virtual_active();
1586 if (VIsual_active
|| redo_VIsual_busy
)
1588 if (VIsual_mode
== Ctrl_V
) /* block mode */
1592 oap
->block_mode
= TRUE
;
1594 getvvcol(curwin
, &(oap
->start
),
1595 &oap
->start_vcol
, NULL
, &oap
->end_vcol
);
1596 if (!redo_VIsual_busy
)
1598 getvvcol(curwin
, &(oap
->end
), &start
, NULL
, &end
);
1600 if (start
< oap
->start_vcol
)
1601 oap
->start_vcol
= start
;
1602 if (end
> oap
->end_vcol
)
1604 if (*p_sel
== 'e' && start
>= 1
1605 && start
- 1 >= oap
->end_vcol
)
1606 oap
->end_vcol
= start
- 1;
1608 oap
->end_vcol
= end
;
1612 /* if '$' was used, get oap->end_vcol from longest line */
1613 if (curwin
->w_curswant
== MAXCOL
)
1615 curwin
->w_cursor
.col
= MAXCOL
;
1617 for (curwin
->w_cursor
.lnum
= oap
->start
.lnum
;
1618 curwin
->w_cursor
.lnum
<= oap
->end
.lnum
;
1619 ++curwin
->w_cursor
.lnum
)
1621 getvvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &end
);
1622 if (end
> oap
->end_vcol
)
1623 oap
->end_vcol
= end
;
1626 else if (redo_VIsual_busy
)
1627 oap
->end_vcol
= oap
->start_vcol
+ redo_VIsual_col
- 1;
1629 * Correct oap->end.col and oap->start.col to be the
1630 * upper-left and lower-right corner of the block area.
1632 * (Actually, this does convert column positions into character
1635 curwin
->w_cursor
.lnum
= oap
->end
.lnum
;
1636 coladvance(oap
->end_vcol
);
1637 oap
->end
= curwin
->w_cursor
;
1639 curwin
->w_cursor
= oap
->start
;
1640 coladvance(oap
->start_vcol
);
1641 oap
->start
= curwin
->w_cursor
;
1644 if (!redo_VIsual_busy
&& !gui_yank
)
1647 * Prepare to reselect and redo Visual: this is based on the
1648 * size of the Visual text
1650 resel_VIsual_mode
= VIsual_mode
;
1651 if (curwin
->w_curswant
== MAXCOL
)
1652 resel_VIsual_col
= MAXCOL
;
1653 else if (VIsual_mode
== Ctrl_V
)
1654 resel_VIsual_col
= oap
->end_vcol
- oap
->start_vcol
+ 1;
1655 else if (oap
->line_count
> 1)
1656 resel_VIsual_col
= oap
->end
.col
;
1658 resel_VIsual_col
= oap
->end
.col
- oap
->start
.col
+ 1;
1659 resel_VIsual_line_count
= oap
->line_count
;
1662 /* can't redo yank (unless 'y' is in 'cpoptions') and ":" */
1663 if ((vim_strchr(p_cpo
, CPO_YANK
) != NULL
|| oap
->op_type
!= OP_YANK
)
1664 && oap
->op_type
!= OP_COLON
1666 && oap
->op_type
!= OP_FOLD
1667 && oap
->op_type
!= OP_FOLDOPEN
1668 && oap
->op_type
!= OP_FOLDOPENREC
1669 && oap
->op_type
!= OP_FOLDCLOSE
1670 && oap
->op_type
!= OP_FOLDCLOSEREC
1671 && oap
->op_type
!= OP_FOLDDEL
1672 && oap
->op_type
!= OP_FOLDDELREC
1674 && oap
->motion_force
== NUL
1677 /* Prepare for redoing. Only use the nchar field for "r",
1678 * otherwise it might be the second char of the operator. */
1679 prep_redo(oap
->regname
, 0L, NUL
, 'v',
1680 get_op_char(oap
->op_type
),
1681 get_extra_op_char(oap
->op_type
),
1682 oap
->op_type
== OP_REPLACE
? cap
->nchar
: NUL
);
1683 if (!redo_VIsual_busy
)
1685 redo_VIsual_mode
= resel_VIsual_mode
;
1686 redo_VIsual_col
= resel_VIsual_col
;
1687 redo_VIsual_line_count
= resel_VIsual_line_count
;
1688 redo_VIsual_count
= cap
->count0
;
1693 * oap->inclusive defaults to TRUE.
1694 * If oap->end is on a NUL (empty line) oap->inclusive becomes
1695 * FALSE. This makes "d}P" and "v}dP" work the same.
1697 if (oap
->motion_force
== NUL
|| oap
->motion_type
== MLINE
)
1698 oap
->inclusive
= TRUE
;
1699 if (VIsual_mode
== 'V')
1700 oap
->motion_type
= MLINE
;
1703 oap
->motion_type
= MCHAR
;
1704 if (VIsual_mode
!= Ctrl_V
&& *ml_get_pos(&(oap
->end
)) == NUL
1705 # ifdef FEAT_VIRTUALEDIT
1706 && (include_line_break
|| !virtual_op
)
1710 oap
->inclusive
= FALSE
;
1711 /* Try to include the newline, unless it's an operator
1712 * that works on lines only */
1714 && !op_on_lines(oap
->op_type
)
1715 && oap
->end
.lnum
< curbuf
->b_ml
.ml_line_count
)
1719 # ifdef FEAT_VIRTUALEDIT
1720 oap
->end
.coladd
= 0;
1727 redo_VIsual_busy
= FALSE
;
1730 * Switch Visual off now, so screen updating does
1731 * not show inverted text when the screen is redrawn.
1732 * With OP_YANK and sometimes with OP_COLON and OP_FILTER there is
1733 * no screen redraw, so it is done here to remove the inverted
1738 VIsual_active
= FALSE
;
1744 clear_cmdline
= TRUE
; /* unshow visual mode later */
1745 #ifdef FEAT_CMDL_INFO
1749 if ((oap
->op_type
== OP_YANK
1750 || oap
->op_type
== OP_COLON
1751 || oap
->op_type
== OP_FUNCTION
1752 || oap
->op_type
== OP_FILTER
)
1753 && oap
->motion_force
== NUL
)
1754 redraw_curbuf_later(INVERTED
);
1760 /* Include the trailing byte of a multi-byte char. */
1761 if (has_mbyte
&& oap
->inclusive
)
1765 l
= (*mb_ptr2len
)(ml_get_pos(&oap
->end
));
1767 oap
->end
.col
+= l
- 1;
1770 curwin
->w_set_curswant
= TRUE
;
1773 * oap->empty is set when start and end are the same. The inclusive
1774 * flag affects this too, unless yanking and the end is on a NUL.
1776 oap
->empty
= (oap
->motion_type
== MCHAR
1778 || (oap
->op_type
== OP_YANK
1779 && gchar_pos(&oap
->end
) == NUL
))
1780 && equalpos(oap
->start
, oap
->end
)
1781 #ifdef FEAT_VIRTUALEDIT
1782 && !(virtual_op
&& oap
->start
.coladd
!= oap
->end
.coladd
)
1786 * For delete, change and yank, it's an error to operate on an
1787 * empty region, when 'E' included in 'cpoptions' (Vi compatible).
1789 empty_region_error
= (oap
->empty
1790 && vim_strchr(p_cpo
, CPO_EMPTYREGION
) != NULL
);
1793 /* Force a redraw when operating on an empty Visual region, when
1794 * 'modifiable is off or creating a fold. */
1795 if (oap
->is_VIsual
&& (oap
->empty
|| !curbuf
->b_p_ma
1796 # ifdef FEAT_FOLDING
1797 || oap
->op_type
== OP_FOLD
1800 redraw_curbuf_later(INVERTED
);
1804 * If the end of an operator is in column one while oap->motion_type
1805 * is MCHAR and oap->inclusive is FALSE, we put op_end after the last
1806 * character in the previous line. If op_start is on or before the
1807 * first non-blank in the line, the operator becomes linewise
1808 * (strange, but that's the way vi does it).
1810 if ( oap
->motion_type
== MCHAR
1811 && oap
->inclusive
== FALSE
1812 && !(cap
->retval
& CA_NO_ADJ_OP_END
)
1813 && oap
->end
.col
== 0
1815 && (!oap
->is_VIsual
|| *p_sel
== 'o')
1818 && oap
->line_count
> 1)
1820 oap
->end_adjusted
= TRUE
; /* remember that we did this */
1824 oap
->motion_type
= MLINE
;
1827 oap
->end
.col
= (colnr_T
)STRLEN(ml_get(oap
->end
.lnum
));
1831 oap
->inclusive
= TRUE
;
1836 oap
->end_adjusted
= FALSE
;
1838 switch (oap
->op_type
)
1844 oap
->is_VIsual
? (int)cap
->count1
:
1847 auto_format(FALSE
, TRUE
);
1852 if (oap
->line_count
< 2)
1853 oap
->line_count
= 2;
1854 if (curwin
->w_cursor
.lnum
+ oap
->line_count
- 1 >
1855 curbuf
->b_ml
.ml_line_count
)
1859 do_do_join(oap
->line_count
, oap
->op_type
== OP_JOIN
);
1860 auto_format(FALSE
, TRUE
);
1866 VIsual_reselect
= FALSE
; /* don't reselect now */
1868 if (empty_region_error
)
1872 (void)op_delete(oap
);
1873 if (oap
->motion_type
== MLINE
&& has_format_option(FO_AUTO
))
1874 u_save_cursor(); /* cursor line wasn't saved yet */
1875 auto_format(FALSE
, TRUE
);
1880 if (empty_region_error
)
1886 (void)op_yank(oap
, FALSE
, !gui_yank
);
1892 VIsual_reselect
= FALSE
; /* don't reselect now */
1894 if (empty_region_error
)
1898 /* This is a new edit command, not a restart. Need to
1899 * remember it to make 'insertmode' work with mappings for
1900 * Visual mode. But do this only once and not when typed and
1901 * 'insertmode' isn't set. */
1902 if (p_im
|| !KeyTyped
)
1903 restart_edit_save
= restart_edit
;
1905 restart_edit_save
= 0;
1907 /* Reset finish_op now, don't want it set inside edit(). */
1909 if (op_change(oap
)) /* will call edit() */
1910 cap
->retval
|= CA_COMMAND_BUSY
;
1911 if (restart_edit
== 0)
1912 restart_edit
= restart_edit_save
;
1917 if (vim_strchr(p_cpo
, CPO_FILTER
) != NULL
)
1918 AppendToRedobuff((char_u
*)"!\r"); /* use any last used !cmd */
1920 bangredo
= TRUE
; /* do_bang() will put cmd in redo buffer */
1925 #if defined(FEAT_LISP) || defined(FEAT_CINDENT)
1927 * If 'equalprg' is empty, do the indenting internally.
1929 if (oap
->op_type
== OP_INDENT
&& *get_equalprg() == NUL
)
1932 if (curbuf
->b_p_lisp
)
1934 op_reindent(oap
, get_lisp_indent
);
1938 # ifdef FEAT_CINDENT
1941 *curbuf
->b_p_inde
!= NUL
? get_expr_indent
:
1956 if (empty_region_error
)
1964 #if defined(FEAT_EVAL)
1965 if (*curbuf
->b_p_fex
!= NUL
)
1966 op_formatexpr(oap
); /* use expression */
1970 op_colon(oap
); /* use external command */
1972 op_format(oap
, FALSE
); /* use internal function */
1976 op_format(oap
, TRUE
); /* use internal function */
1980 op_function(oap
); /* call 'operatorfunc' */
1986 VIsual_reselect
= FALSE
; /* don't reselect now */
1988 #ifdef FEAT_VISUALEXTRA
1989 if (empty_region_error
)
1993 /* This is a new edit command, not a restart. Need to
1994 * remember it to make 'insertmode' work with mappings for
1995 * Visual mode. But do this only once. */
1996 restart_edit_save
= restart_edit
;
1999 op_insert(oap
, cap
->count1
);
2001 /* TODO: when inserting in several lines, should format all
2003 auto_format(FALSE
, TRUE
);
2005 if (restart_edit
== 0)
2006 restart_edit
= restart_edit_save
;
2015 VIsual_reselect
= FALSE
; /* don't reselect now */
2017 #ifdef FEAT_VISUALEXTRA
2018 if (empty_region_error
)
2021 #ifdef FEAT_VISUALEXTRA
2023 op_replace(oap
, cap
->nchar
);
2029 VIsual_reselect
= FALSE
; /* don't reselect now */
2030 foldCreate(oap
->start
.lnum
, oap
->end
.lnum
);
2034 case OP_FOLDOPENREC
:
2036 case OP_FOLDCLOSEREC
:
2037 VIsual_reselect
= FALSE
; /* don't reselect now */
2038 opFoldRange(oap
->start
.lnum
, oap
->end
.lnum
,
2039 oap
->op_type
== OP_FOLDOPEN
2040 || oap
->op_type
== OP_FOLDOPENREC
,
2041 oap
->op_type
== OP_FOLDOPENREC
2042 || oap
->op_type
== OP_FOLDCLOSEREC
,
2048 VIsual_reselect
= FALSE
; /* don't reselect now */
2049 deleteFold(oap
->start
.lnum
, oap
->end
.lnum
,
2050 oap
->op_type
== OP_FOLDDELREC
, oap
->is_VIsual
);
2056 #ifdef FEAT_VIRTUALEDIT
2062 * if 'sol' not set, go back to old column for some commands
2064 if (!p_sol
&& oap
->motion_type
== MLINE
&& !oap
->end_adjusted
2065 && (oap
->op_type
== OP_LSHIFT
|| oap
->op_type
== OP_RSHIFT
2066 || oap
->op_type
== OP_DELETE
))
2067 coladvance(curwin
->w_curswant
= old_col
);
2071 curwin
->w_cursor
= old_cursor
;
2074 oap
->block_mode
= FALSE
;
2081 * Handle indent and format operators and visual mode ":".
2087 stuffcharReadbuff(':');
2090 stuffReadbuff((char_u
*)"'<,'>");
2095 * Make the range look nice, so it can be repeated.
2097 if (oap
->start
.lnum
== curwin
->w_cursor
.lnum
)
2098 stuffcharReadbuff('.');
2100 stuffnumReadbuff((long)oap
->start
.lnum
);
2101 if (oap
->end
.lnum
!= oap
->start
.lnum
)
2103 stuffcharReadbuff(',');
2104 if (oap
->end
.lnum
== curwin
->w_cursor
.lnum
)
2105 stuffcharReadbuff('.');
2106 else if (oap
->end
.lnum
== curbuf
->b_ml
.ml_line_count
)
2107 stuffcharReadbuff('$');
2108 else if (oap
->start
.lnum
== curwin
->w_cursor
.lnum
)
2110 stuffReadbuff((char_u
*)".+");
2111 stuffnumReadbuff((long)oap
->line_count
- 1);
2114 stuffnumReadbuff((long)oap
->end
.lnum
);
2117 if (oap
->op_type
!= OP_COLON
)
2118 stuffReadbuff((char_u
*)"!");
2119 if (oap
->op_type
== OP_INDENT
)
2121 #ifndef FEAT_CINDENT
2122 if (*get_equalprg() == NUL
)
2123 stuffReadbuff((char_u
*)"indent");
2126 stuffReadbuff(get_equalprg());
2127 stuffReadbuff((char_u
*)"\n");
2129 else if (oap
->op_type
== OP_FORMAT
)
2132 stuffReadbuff((char_u
*)"fmt");
2134 stuffReadbuff(p_fp
);
2135 stuffReadbuff((char_u
*)"\n']");
2139 * do_cmdline() does the rest
2144 * Handle the "g@" operator: call 'operatorfunc'.
2154 if (*p_opfunc
== NUL
)
2155 EMSG(_("E774: 'operatorfunc' is empty"));
2158 /* Set '[ and '] marks to text to be operated on. */
2159 curbuf
->b_op_start
= oap
->start
;
2160 curbuf
->b_op_end
= oap
->end
;
2161 if (oap
->motion_type
!= MLINE
&& !oap
->inclusive
)
2162 /* Exclude the end position. */
2163 decl(&curbuf
->b_op_end
);
2165 if (oap
->block_mode
)
2166 argv
[0] = (char_u
*)"block";
2167 else if (oap
->motion_type
== MLINE
)
2168 argv
[0] = (char_u
*)"line";
2170 argv
[0] = (char_u
*)"char";
2171 (void)call_func_retnr(p_opfunc
, 1, argv
, FALSE
);
2174 EMSG(_("E775: Eval feature not available"));
2178 #if defined(FEAT_MOUSE) || defined(PROTO)
2180 * Do the appropriate action for the current mouse click in the current mode.
2181 * Not used for Command-line mode.
2184 * event modi- position visual change action
2185 * fier cursor window
2186 * left press - yes end yes
2187 * left press C yes end yes "^]" (2)
2188 * left press S yes end yes "*" (2)
2189 * left drag - yes start if moved no
2190 * left relse - yes start if moved no
2191 * middle press - yes if not active no put register
2192 * middle press - yes if active no yank and put
2193 * right press - yes start or extend yes
2194 * right press S yes no change yes "#" (2)
2195 * right drag - yes extend no
2196 * right relse - yes extend no
2198 * Insert or Replace Mode:
2199 * event modi- position visual change action
2200 * fier cursor window
2201 * left press - yes (cannot be active) yes
2202 * left press C yes (cannot be active) yes "CTRL-O^]" (2)
2203 * left press S yes (cannot be active) yes "CTRL-O*" (2)
2204 * left drag - yes start or extend (1) no CTRL-O (1)
2205 * left relse - yes start or extend (1) no CTRL-O (1)
2206 * middle press - no (cannot be active) no put register
2207 * right press - yes start or extend yes CTRL-O
2208 * right press S yes (cannot be active) yes "CTRL-O#" (2)
2210 * (1) only if mouse pointer moved since press
2211 * (2) only if click is in same buffer
2213 * Return TRUE if start_arrow() should be called for edit mode.
2216 do_mouse(oap
, c
, dir
, count
, fixindent
)
2217 oparg_T
*oap
; /* operator argument, can be NULL */
2218 int c
; /* K_LEFTMOUSE, etc */
2219 int dir
; /* Direction to 'put' if necessary */
2221 int fixindent
; /* PUT_FIXINDENT if fixing indent necessary */
2223 static int do_always
= FALSE
; /* ignore 'mouse' setting next time */
2224 static int got_click
= FALSE
; /* got a click some time back */
2226 int which_button
; /* MOUSE_LEFT, _MIDDLE or _RIGHT */
2227 int is_click
; /* If FALSE it's a drag or release event */
2228 int is_drag
; /* If TRUE it's a drag event */
2229 int jump_flags
= 0; /* flags for jump_to_mouse() */
2231 int moved
; /* Has cursor moved? */
2232 int in_status_line
; /* mouse in status line */
2233 #ifdef FEAT_VERTSPLIT
2234 int in_sep_line
; /* mouse in vertical separator line */
2237 #if defined(FEAT_FOLDING)
2240 win_T
*old_curwin
= curwin
;
2242 static pos_T orig_cursor
;
2243 colnr_T leftcol
, rightcol
;
2246 int old_active
= VIsual_active
;
2247 int old_mode
= VIsual_mode
;
2251 #if defined(FEAT_FOLDING)
2252 save_cursor
= curwin
->w_cursor
;
2256 * When GUI is active, always recognize mouse events, otherwise:
2257 * - Ignore mouse event in normal mode if 'mouse' doesn't include 'n'.
2258 * - Ignore mouse event in visual mode if 'mouse' doesn't include 'v'.
2259 * - For command line and insert mode 'mouse' is checked before calling
2272 if (!mouse_has(MOUSE_VISUAL
))
2277 if (State
== NORMAL
&& !mouse_has(MOUSE_NORMAL
))
2281 which_button
= get_mouse_button(KEY2TERMCAP1(c
), &is_click
, &is_drag
);
2283 #ifdef FEAT_MOUSESHAPE
2284 /* May have stopped dragging the status or separator line. The pointer is
2285 * most likely still on the status or separator line. */
2286 if (!is_drag
&& drag_status_line
)
2288 drag_status_line
= FALSE
;
2289 update_mouseshape(SHAPE_IDX_STATUS
);
2291 # ifdef FEAT_VERTSPLIT
2292 if (!is_drag
&& drag_sep_line
)
2294 drag_sep_line
= FALSE
;
2295 update_mouseshape(SHAPE_IDX_VSEP
);
2301 * Ignore drag and release events if we didn't get a click.
2307 if (!got_click
) /* didn't get click, ignore */
2309 if (!is_drag
) /* release, reset got_click */
2315 * ALT is only used for starging/extending Visual mode.
2317 if ((mod_mask
& MOD_MASK_ALT
))
2322 * CTRL right mouse button does CTRL-T
2324 if (is_click
&& (mod_mask
& MOD_MASK_CTRL
) && which_button
== MOUSE_RIGHT
)
2327 stuffcharReadbuff(Ctrl_O
);
2329 stuffnumReadbuff(count
);
2330 stuffcharReadbuff(Ctrl_T
);
2331 got_click
= FALSE
; /* ignore drag&release now */
2336 * CTRL only works with left mouse button
2338 if ((mod_mask
& MOD_MASK_CTRL
) && which_button
!= MOUSE_LEFT
)
2342 * When a modifier is down, ignore drag and release events, as well as
2343 * multiple clicks and the middle mouse button.
2344 * Accept shift-leftmouse drags when 'mousemodel' is "popup.*".
2346 if ((mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
| MOD_MASK_ALT
2349 || (mod_mask
& MOD_MASK_MULTI_CLICK
)
2350 || which_button
== MOUSE_MIDDLE
)
2351 && !((mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_ALT
))
2352 && mouse_model_popup()
2353 && which_button
== MOUSE_LEFT
)
2354 && !((mod_mask
& MOD_MASK_ALT
)
2355 && !mouse_model_popup()
2356 && which_button
== MOUSE_RIGHT
)
2361 * If the button press was used as the movement command for an operator
2362 * (eg "d<MOUSE>"), or it is the middle button that is held down, ignore
2363 * drag/release events.
2365 if (!is_click
&& which_button
== MOUSE_MIDDLE
)
2369 regname
= oap
->regname
;
2374 * Middle mouse button does a 'put' of the selected text
2376 if (which_button
== MOUSE_MIDDLE
)
2378 if (State
== NORMAL
)
2381 * If an operator was pending, we don't know what the user wanted
2382 * to do. Go back to normal mode: Clear the operator and beep().
2384 if (oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2392 * If visual was active, yank the highlighted text and put it
2393 * before the mouse pointer position.
2394 * In Select mode replace the highlighted text with the clipboard.
2400 stuffcharReadbuff(Ctrl_G
);
2401 stuffReadbuff((char_u
*)"\"+p");
2405 stuffcharReadbuff('y');
2406 stuffcharReadbuff(K_MIDDLEMOUSE
);
2408 do_always
= TRUE
; /* ignore 'mouse' setting next time */
2413 * The rest is below jump_to_mouse()
2417 else if ((State
& INSERT
) == 0)
2421 * Middle click in insert mode doesn't move the mouse, just insert the
2422 * contents of a register. '.' register is special, can't insert that
2424 * Also paste at the cursor if the current mode isn't in 'mouse' (only
2425 * happens for the GUI).
2427 if ((State
& INSERT
) || !mouse_has(MOUSE_NORMAL
))
2430 insert_reg(regname
, TRUE
);
2433 #ifdef FEAT_CLIPBOARD
2434 if (clip_star
.available
&& regname
== 0)
2437 if ((State
& REPLACE_FLAG
) && !yank_register_mline(regname
))
2438 insert_reg(regname
, TRUE
);
2441 do_put(regname
, BACKWARD
, 1L, fixindent
| PUT_CURSEND
);
2443 /* Repeat it with CTRL-R CTRL-O r or CTRL-R CTRL-P r */
2444 AppendCharToRedobuff(Ctrl_R
);
2445 AppendCharToRedobuff(fixindent
? Ctrl_P
: Ctrl_O
);
2446 AppendCharToRedobuff(regname
== 0 ? '"' : regname
);
2453 /* When dragging or button-up stay in the same window. */
2455 jump_flags
|= MOUSE_FOCUS
| MOUSE_DID_MOVE
;
2457 start_visual
.lnum
= 0;
2460 /* Check for clicking in the tab page line. */
2461 if (mouse_row
== 0 && firstwin
->w_winrow
> 0)
2463 got_click
= FALSE
; /* ignore mouse-up and drag events */
2465 /* click in a tab selects that tab page */
2470 && mouse_col
< Columns
)
2472 c1
= TabPageIdxs
[mouse_col
];
2475 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2477 /* double click opens new page */
2480 tabpage_move(c1
== 0 ? 9999 : c1
- 1);
2484 /* Go to specified tab page, or next one if not clicking
2488 /* It's like clicking on the status line of a window. */
2489 if (curwin
!= old_curwin
)
2497 /* Close the current or specified tab page. */
2501 tp
= find_tabpage(-c1
);
2504 if (first_tabpage
->tp_next
!= NULL
)
2505 tabpage_close(FALSE
);
2507 else if (tp
!= NULL
)
2508 tabpage_close_other(tp
, FALSE
);
2516 * When 'mousemodel' is "popup" or "popup_setpos", translate mouse events:
2517 * right button up -> pop-up menu
2518 * shift-left button -> right button
2519 * alt-left button -> alt-right button
2521 if (mouse_model_popup())
2523 if (which_button
== MOUSE_RIGHT
2524 && !(mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
)))
2527 * NOTE: Ignore right button down and drag mouse events.
2528 * Windows only shows the popup menu on the button up event.
2530 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2531 || defined(FEAT_GUI_PHOTON) || defined(FEAT_GUI_MAC) \
2532 || defined(FEAT_GUI_MACVIM)
2536 #if defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN)
2537 if (is_click
|| is_drag
)
2540 #if defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_GTK) \
2541 || defined(FEAT_GUI_ATHENA) || defined(FEAT_GUI_MSWIN) \
2542 || defined(FEAT_GUI_MAC) || defined(FEAT_GUI_PHOTON) \
2543 || defined(FEAT_GUI_MACVIM)
2547 if (STRCMP(p_mousem
, "popup_setpos") == 0)
2549 /* First set the cursor position before showing the popup
2557 * set MOUSE_MAY_STOP_VIS if we are outside the
2558 * selection or the current window (might have false
2561 if (mouse_row
< W_WINROW(curwin
)
2563 > (W_WINROW(curwin
) + curwin
->w_height
))
2564 jump_flags
= MOUSE_MAY_STOP_VIS
;
2565 else if (get_fpos_of_mouse(&m_pos
) != IN_BUFFER
)
2566 jump_flags
= MOUSE_MAY_STOP_VIS
;
2569 if ((lt(curwin
->w_cursor
, VIsual
)
2570 && (lt(m_pos
, curwin
->w_cursor
)
2571 || lt(VIsual
, m_pos
)))
2572 || (lt(VIsual
, curwin
->w_cursor
)
2573 && (lt(m_pos
, VIsual
)
2574 || lt(curwin
->w_cursor
, m_pos
))))
2576 jump_flags
= MOUSE_MAY_STOP_VIS
;
2578 else if (VIsual_mode
== Ctrl_V
)
2580 getvcols(curwin
, &curwin
->w_cursor
, &VIsual
,
2581 &leftcol
, &rightcol
);
2582 getvcol(curwin
, &m_pos
, NULL
, &m_pos
.col
, NULL
);
2583 if (m_pos
.col
< leftcol
|| m_pos
.col
> rightcol
)
2584 jump_flags
= MOUSE_MAY_STOP_VIS
;
2589 jump_flags
= MOUSE_MAY_STOP_VIS
;
2594 jump_flags
= jump_to_mouse(jump_flags
, NULL
, which_button
);
2597 VIsual_active
? INVERTED
:
2601 out_flush(); /* Update before showing popup menu */
2604 gui_show_popupmenu();
2606 return (jump_flags
& CURSOR_MOVED
) != 0;
2614 if (which_button
== MOUSE_LEFT
2615 && (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_ALT
)))
2617 which_button
= MOUSE_RIGHT
;
2618 mod_mask
&= ~MOD_MASK_SHIFT
;
2623 if ((State
& (NORMAL
| INSERT
))
2624 && !(mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
)))
2626 if (which_button
== MOUSE_LEFT
)
2630 /* stop Visual mode for a left click in a window, but not when
2631 * on a status line */
2633 jump_flags
|= MOUSE_MAY_STOP_VIS
;
2635 else if (mouse_has(MOUSE_VISUAL
))
2636 jump_flags
|= MOUSE_MAY_VIS
;
2638 else if (which_button
== MOUSE_RIGHT
)
2640 if (is_click
&& VIsual_active
)
2643 * Remember the start and end of visual before moving the
2646 if (lt(curwin
->w_cursor
, VIsual
))
2648 start_visual
= curwin
->w_cursor
;
2649 end_visual
= VIsual
;
2653 start_visual
= VIsual
;
2654 end_visual
= curwin
->w_cursor
;
2657 jump_flags
|= MOUSE_FOCUS
;
2658 if (mouse_has(MOUSE_VISUAL
))
2659 jump_flags
|= MOUSE_MAY_VIS
;
2665 * If an operator is pending, ignore all drags and releases until the
2668 if (!is_drag
&& oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2671 oap
->motion_type
= MCHAR
;
2674 /* When releasing the button let jump_to_mouse() know. */
2675 if (!is_click
&& !is_drag
)
2676 jump_flags
|= MOUSE_RELEASED
;
2681 jump_flags
= jump_to_mouse(jump_flags
,
2682 oap
== NULL
? NULL
: &(oap
->inclusive
), which_button
);
2683 moved
= (jump_flags
& CURSOR_MOVED
);
2684 in_status_line
= (jump_flags
& IN_STATUS_LINE
);
2685 #ifdef FEAT_VERTSPLIT
2686 in_sep_line
= (jump_flags
& IN_SEP_LINE
);
2689 #ifdef FEAT_NETBEANS_INTG
2690 if (usingNetbeans
&& isNetbeansBuffer(curbuf
)
2691 && !(jump_flags
& (IN_STATUS_LINE
| IN_SEP_LINE
)))
2693 int key
= KEY2TERMCAP1(c
);
2695 if (key
== (int)KE_LEFTRELEASE
|| key
== (int)KE_MIDDLERELEASE
2696 || key
== (int)KE_RIGHTRELEASE
)
2697 netbeans_button_release(which_button
);
2701 /* When jumping to another window, clear a pending operator. That's a bit
2702 * friendlier than beeping and not jumping to that window. */
2703 if (curwin
!= old_curwin
&& oap
!= NULL
&& oap
->op_type
!= OP_NOP
)
2709 && (jump_flags
& (MOUSE_FOLD_CLOSE
| MOUSE_FOLD_OPEN
))
2710 && which_button
== MOUSE_LEFT
)
2712 /* open or close a fold at this line */
2713 if (jump_flags
& MOUSE_FOLD_OPEN
)
2714 openFold(curwin
->w_cursor
.lnum
, 1L);
2716 closeFold(curwin
->w_cursor
.lnum
, 1L);
2717 /* don't move the cursor if still in the same window */
2718 if (curwin
== old_curwin
)
2719 curwin
->w_cursor
= save_cursor
;
2723 #if defined(FEAT_CLIPBOARD) && defined(FEAT_CMDWIN)
2724 if ((jump_flags
& IN_OTHER_WIN
) && !VIsual_active
&& clip_star
.available
)
2726 clip_modeless(which_button
, is_click
, is_drag
);
2732 /* Set global flag that we are extending the Visual area with mouse
2733 * dragging; temporarily minimize 'scrolloff'. */
2734 if (VIsual_active
&& is_drag
&& p_so
)
2736 /* In the very first line, allow scrolling one line */
2743 /* When dragging the mouse above the window, scroll down. */
2744 if (is_drag
&& mouse_row
< 0 && !in_status_line
)
2746 scroll_redraw(FALSE
, 1L);
2750 if (start_visual
.lnum
) /* right click in visual mode */
2752 /* When ALT is pressed make Visual mode blockwise. */
2753 if (mod_mask
& MOD_MASK_ALT
)
2754 VIsual_mode
= Ctrl_V
;
2757 * In Visual-block mode, divide the area in four, pick up the corner
2758 * that is in the quarter that the cursor is in.
2760 if (VIsual_mode
== Ctrl_V
)
2762 getvcols(curwin
, &start_visual
, &end_visual
, &leftcol
, &rightcol
);
2763 if (curwin
->w_curswant
> (leftcol
+ rightcol
) / 2)
2764 end_visual
.col
= leftcol
;
2766 end_visual
.col
= rightcol
;
2767 if (curwin
->w_cursor
.lnum
<
2768 (start_visual
.lnum
+ end_visual
.lnum
) / 2)
2769 end_visual
.lnum
= end_visual
.lnum
;
2771 end_visual
.lnum
= start_visual
.lnum
;
2773 /* move VIsual to the right column */
2774 start_visual
= curwin
->w_cursor
; /* save the cursor pos */
2775 curwin
->w_cursor
= end_visual
;
2776 coladvance(end_visual
.col
);
2777 VIsual
= curwin
->w_cursor
;
2778 curwin
->w_cursor
= start_visual
; /* restore the cursor */
2783 * If the click is before the start of visual, change the start.
2784 * If the click is after the end of visual, change the end. If
2785 * the click is inside the visual, change the closest side.
2787 if (lt(curwin
->w_cursor
, start_visual
))
2788 VIsual
= end_visual
;
2789 else if (lt(end_visual
, curwin
->w_cursor
))
2790 VIsual
= start_visual
;
2793 /* In the same line, compare column number */
2794 if (end_visual
.lnum
== start_visual
.lnum
)
2796 if (curwin
->w_cursor
.col
- start_visual
.col
>
2797 end_visual
.col
- curwin
->w_cursor
.col
)
2798 VIsual
= start_visual
;
2800 VIsual
= end_visual
;
2803 /* In different lines, compare line number */
2806 diff
= (curwin
->w_cursor
.lnum
- start_visual
.lnum
) -
2807 (end_visual
.lnum
- curwin
->w_cursor
.lnum
);
2809 if (diff
> 0) /* closest to end */
2810 VIsual
= start_visual
;
2811 else if (diff
< 0) /* closest to start */
2812 VIsual
= end_visual
;
2813 else /* in the middle line */
2815 if (curwin
->w_cursor
.col
<
2816 (start_visual
.col
+ end_visual
.col
) / 2)
2817 VIsual
= end_visual
;
2819 VIsual
= start_visual
;
2826 * If Visual mode started in insert mode, execute "CTRL-O"
2828 else if ((State
& INSERT
) && VIsual_active
)
2829 stuffcharReadbuff(Ctrl_O
);
2833 * Middle mouse click: Put text before cursor.
2835 if (which_button
== MOUSE_MIDDLE
)
2837 #ifdef FEAT_CLIPBOARD
2838 if (clip_star
.available
&& regname
== 0)
2841 if (yank_register_mline(regname
))
2843 if (mouse_past_bottom
)
2846 else if (mouse_past_eol
)
2851 c1
= (dir
== BACKWARD
) ? '[' : ']';
2856 c1
= (dir
== FORWARD
) ? 'p' : 'P';
2859 prep_redo(regname
, count
, NUL
, c1
, NUL
, c2
, NUL
);
2862 * Remember where the paste started, so in edit() Insstart can be set
2865 if (restart_edit
!= 0)
2866 where_paste_started
= curwin
->w_cursor
;
2867 do_put(regname
, dir
, count
, fixindent
| PUT_CURSEND
);
2870 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2872 * Ctrl-Mouse click or double click in a quickfix window jumps to the
2873 * error under the mouse pointer.
2875 else if (((mod_mask
& MOD_MASK_CTRL
)
2876 || (mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2877 && bt_quickfix(curbuf
))
2880 stuffcharReadbuff(Ctrl_O
);
2881 if (curwin
->w_llist_ref
== NULL
) /* quickfix window */
2882 stuffReadbuff((char_u
*)":.cc\n");
2883 else /* location list window */
2884 stuffReadbuff((char_u
*)":.ll\n");
2885 got_click
= FALSE
; /* ignore drag&release now */
2890 * Ctrl-Mouse click (or double click in a help window) jumps to the tag
2891 * under the mouse pointer.
2893 else if ((mod_mask
& MOD_MASK_CTRL
) || (curbuf
->b_help
2894 && (mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
))
2897 stuffcharReadbuff(Ctrl_O
);
2898 stuffcharReadbuff(Ctrl_RSB
);
2899 got_click
= FALSE
; /* ignore drag&release now */
2903 * Shift-Mouse click searches for the next occurrence of the word under
2906 else if ((mod_mask
& MOD_MASK_SHIFT
))
2910 || (VIsual_active
&& VIsual_select
)
2913 stuffcharReadbuff(Ctrl_O
);
2914 if (which_button
== MOUSE_LEFT
)
2915 stuffcharReadbuff('*');
2916 else /* MOUSE_RIGHT */
2917 stuffcharReadbuff('#');
2920 /* Handle double clicks, unless on status line */
2921 else if (in_status_line
)
2923 #ifdef FEAT_MOUSESHAPE
2924 if ((is_drag
|| is_click
) && !drag_status_line
)
2926 drag_status_line
= TRUE
;
2927 update_mouseshape(-1);
2931 #ifdef FEAT_VERTSPLIT
2932 else if (in_sep_line
)
2934 # ifdef FEAT_MOUSESHAPE
2935 if ((is_drag
|| is_click
) && !drag_sep_line
)
2937 drag_sep_line
= TRUE
;
2938 update_mouseshape(-1);
2944 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) && (State
& (NORMAL
| INSERT
))
2945 && mouse_has(MOUSE_VISUAL
))
2947 if (is_click
|| !VIsual_active
)
2950 orig_cursor
= VIsual
;
2953 check_visual_highlight();
2954 VIsual
= curwin
->w_cursor
;
2955 orig_cursor
= VIsual
;
2956 VIsual_active
= TRUE
;
2957 VIsual_reselect
= TRUE
;
2958 /* start Select mode if 'selectmode' contains "mouse" */
2959 may_start_select('o');
2962 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2964 /* Double click with ALT pressed makes it blockwise. */
2965 if (mod_mask
& MOD_MASK_ALT
)
2966 VIsual_mode
= Ctrl_V
;
2970 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_3CLICK
)
2972 else if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_4CLICK
)
2973 VIsual_mode
= Ctrl_V
;
2974 #ifdef FEAT_CLIPBOARD
2975 /* Make sure the clipboard gets updated. Needed because start and
2976 * end may still be the same, and the selection needs to be owned */
2977 clip_star
.vmode
= NUL
;
2981 * A double click selects a word or a block.
2983 if ((mod_mask
& MOD_MASK_MULTI_CLICK
) == MOD_MASK_2CLICK
)
2990 /* If the character under the cursor (skipping white space) is
2991 * not a word character, try finding a match and select a (),
2992 * {}, [], #if/#endif, etc. block. */
2993 end_visual
= curwin
->w_cursor
;
2994 while (gc
= gchar_pos(&end_visual
), vim_iswhite(gc
))
2997 oap
->motion_type
= MCHAR
;
2999 && VIsual_mode
== 'v'
3000 && !vim_iswordc(gchar_pos(&end_visual
))
3001 && equalpos(curwin
->w_cursor
, VIsual
)
3002 && (pos
= findmatch(oap
, NUL
)) != NULL
)
3004 curwin
->w_cursor
= *pos
;
3005 if (oap
->motion_type
== MLINE
)
3007 else if (*p_sel
== 'e')
3009 if (lt(curwin
->w_cursor
, VIsual
))
3012 ++curwin
->w_cursor
.col
;
3017 if (pos
== NULL
&& (is_click
|| is_drag
))
3019 /* When not found a match or when dragging: extend to include
3021 if (lt(curwin
->w_cursor
, orig_cursor
))
3023 find_start_of_word(&curwin
->w_cursor
);
3024 find_end_of_word(&VIsual
);
3028 find_start_of_word(&VIsual
);
3029 if (*p_sel
== 'e' && *ml_get_cursor() != NUL
)
3031 curwin
->w_cursor
.col
+=
3032 (*mb_ptr2len
)(ml_get_cursor());
3034 ++curwin
->w_cursor
.col
;
3036 find_end_of_word(&curwin
->w_cursor
);
3039 curwin
->w_set_curswant
= TRUE
;
3042 redraw_curbuf_later(INVERTED
); /* update the inversion */
3044 else if (VIsual_active
&& !old_active
)
3046 if (mod_mask
& MOD_MASK_ALT
)
3047 VIsual_mode
= Ctrl_V
;
3052 /* If Visual mode changed show it later. */
3053 if ((!VIsual_active
&& old_active
&& mode_displayed
)
3054 || (VIsual_active
&& p_smd
&& msg_silent
== 0
3055 && (!old_active
|| VIsual_mode
!= old_mode
)))
3056 redraw_cmdline
= TRUE
;
3064 * Move "pos" back to the start of the word it's in.
3067 find_start_of_word(pos
)
3074 line
= ml_get(pos
->lnum
);
3075 cclass
= get_mouse_class(line
+ pos
->col
);
3077 while (pos
->col
> 0)
3081 col
-= (*mb_head_off
)(line
, line
+ col
);
3083 if (get_mouse_class(line
+ col
) != cclass
)
3090 * Move "pos" forward to the end of the word it's in.
3091 * When 'selection' is "exclusive", the position is just after the word.
3094 find_end_of_word(pos
)
3101 line
= ml_get(pos
->lnum
);
3102 if (*p_sel
== 'e' && pos
->col
> 0)
3106 pos
->col
-= (*mb_head_off
)(line
, line
+ pos
->col
);
3109 cclass
= get_mouse_class(line
+ pos
->col
);
3110 while (line
[pos
->col
] != NUL
)
3113 col
= pos
->col
+ (*mb_ptr2len
)(line
+ pos
->col
);
3117 if (get_mouse_class(line
+ col
) != cclass
)
3128 * Get class of a character for selection: same class means same word.
3130 * 1: punctuation groups
3131 * 2: normal word character
3132 * >2: multi-byte word character.
3141 if (has_mbyte
&& MB_BYTE2LEN(p
[0]) > 1)
3142 return mb_get_class(p
);
3146 if (c
== ' ' || c
== '\t')
3153 * There are a few special cases where we want certain combinations of
3154 * characters to be considered as a single word. These are things like
3155 * "->", "/ *", "*=", "+=", "&=", "<=", ">=", "!=" etc. Otherwise, each
3156 * character is in it's own class.
3158 if (c
!= NUL
&& vim_strchr((char_u
*)"-+*/%<>&|^!=", c
) != NULL
)
3162 #endif /* FEAT_VISUAL */
3163 #endif /* FEAT_MOUSE */
3165 #if defined(FEAT_VISUAL) || defined(PROTO)
3167 * Check if highlighting for visual mode is possible, give a warning message
3171 check_visual_highlight()
3173 static int did_check
= FALSE
;
3177 if (!did_check
&& hl_attr(HLF_V
) == 0)
3178 MSG(_("Warning: terminal cannot highlight"));
3185 * This function should ALWAYS be called to end Visual mode, except from
3186 * do_pending_operator().
3191 #ifdef FEAT_CLIPBOARD
3193 * If we are using the clipboard, then remember what was selected in case
3194 * we need to paste it somewhere while we still own the selection.
3195 * Only do this when the clipboard is already owned. Don't want to grab
3196 * the selection when hitting ESC.
3198 if (clip_star
.available
&& clip_star
.owned
)
3202 VIsual_active
= FALSE
;
3208 /* Save the current VIsual area for '< and '> marks, and "gv" */
3209 curbuf
->b_visual
.vi_mode
= VIsual_mode
;
3210 curbuf
->b_visual
.vi_start
= VIsual
;
3211 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
3212 curbuf
->b_visual
.vi_curswant
= curwin
->w_curswant
;
3214 curbuf
->b_visual_mode_eval
= VIsual_mode
;
3216 #ifdef FEAT_VIRTUALEDIT
3217 if (!virtual_active())
3218 curwin
->w_cursor
.coladd
= 0;
3222 clear_cmdline
= TRUE
; /* unshow visual mode later */
3223 #ifdef FEAT_CMDL_INFO
3228 adjust_cursor_eol();
3232 * Reset VIsual_active and VIsual_reselect.
3235 reset_VIsual_and_resel()
3240 redraw_curbuf_later(INVERTED
); /* delete the inversion later */
3242 VIsual_reselect
= FALSE
;
3246 * Reset VIsual_active and VIsual_reselect if it's set.
3254 redraw_curbuf_later(INVERTED
); /* delete the inversion later */
3255 VIsual_reselect
= FALSE
;
3258 #endif /* FEAT_VISUAL */
3260 #if defined(FEAT_BEVAL)
3261 static int find_is_eval_item
__ARGS((char_u
*ptr
, int *colp
, int *nbp
, int dir
));
3264 * Check for a balloon-eval special item to include when searching for an
3265 * identifier. When "dir" is BACKWARD "ptr[-1]" must be valid!
3266 * Returns TRUE if the character at "*ptr" should be included.
3267 * "dir" is FORWARD or BACKWARD, the direction of searching.
3268 * "*colp" is in/decremented if "ptr[-dir]" should also be included.
3269 * "bnp" points to a counter for square brackets.
3272 find_is_eval_item(ptr
, colp
, bnp
, dir
)
3278 /* Accept everything inside []. */
3279 if ((*ptr
== ']' && dir
== BACKWARD
) || (*ptr
== '[' && dir
== FORWARD
))
3283 if ((*ptr
== '[' && dir
== BACKWARD
) || (*ptr
== ']' && dir
== FORWARD
))
3288 /* skip over "s.var" */
3292 /* two-character item: s->var */
3293 if (ptr
[dir
== BACKWARD
? 0 : 1] == '>'
3294 && ptr
[dir
== BACKWARD
? -1 : 0] == '-')
3304 * Find the identifier under or to the right of the cursor.
3305 * "find_type" can have one of three values:
3306 * FIND_IDENT: find an identifier (keyword)
3307 * FIND_STRING: find any non-white string
3308 * FIND_IDENT + FIND_STRING: find any non-white string, identifier preferred.
3309 * FIND_EVAL: find text useful for C program debugging
3311 * There are three steps:
3312 * 1. Search forward for the start of an identifier/string. Doesn't move if
3314 * 2. Search backward for the start of this identifier/string.
3315 * This doesn't match the real Vi but I like it a little better and it
3316 * shouldn't bother anyone.
3317 * 3. Search forward to the end of this identifier/string.
3318 * When FIND_IDENT isn't defined, we backup until a blank.
3320 * Returns the length of the string, or zero if no string is found.
3321 * If a string is found, a pointer to the string is put in "*string". This
3322 * string is not always NUL terminated.
3325 find_ident_under_cursor(string
, find_type
)
3329 return find_ident_at_pos(curwin
, curwin
->w_cursor
.lnum
,
3330 curwin
->w_cursor
.col
, string
, find_type
);
3334 * Like find_ident_under_cursor(), but for any window and any position.
3335 * However: Uses 'iskeyword' from the current window!.
3338 find_ident_at_pos(wp
, lnum
, startcol
, string
, find_type
)
3346 int col
= 0; /* init to shut up GCC */
3353 #if defined(FEAT_BEVAL)
3354 int bn
= 0; /* bracket nesting */
3358 * if i == 0: try to find an identifier
3359 * if i == 1: try to find any non-white string
3361 ptr
= ml_get_buf(wp
->w_buffer
, lnum
, FALSE
);
3362 for (i
= (find_type
& FIND_IDENT
) ? 0 : 1; i
< 2; ++i
)
3365 * 1. skip to start of identifier/string
3371 while (ptr
[col
] != NUL
)
3373 # if defined(FEAT_BEVAL)
3374 /* Stop at a ']' to evaluate "a[x]". */
3375 if ((find_type
& FIND_EVAL
) && ptr
[col
] == ']')
3378 this_class
= mb_get_class(ptr
+ col
);
3379 if (this_class
!= 0 && (i
== 1 || this_class
!= 1))
3381 col
+= (*mb_ptr2len
)(ptr
+ col
);
3386 while (ptr
[col
] != NUL
3387 && (i
== 0 ? !vim_iswordc(ptr
[col
]) : vim_iswhite(ptr
[col
]))
3388 # if defined(FEAT_BEVAL)
3389 && (!(find_type
& FIND_EVAL
) || ptr
[col
] != ']')
3394 #if defined(FEAT_BEVAL)
3395 /* When starting on a ']' count it, so that we include the '['. */
3396 bn
= ptr
[col
] == ']';
3400 * 2. Back up to start of identifier/string.
3405 /* Remember class of character under cursor. */
3406 # if defined(FEAT_BEVAL)
3407 if ((find_type
& FIND_EVAL
) && ptr
[col
] == ']')
3408 this_class
= mb_get_class((char_u
*)"a");
3411 this_class
= mb_get_class(ptr
+ col
);
3412 while (col
> 0 && this_class
!= 0)
3414 prevcol
= col
- 1 - (*mb_head_off
)(ptr
, ptr
+ col
- 1);
3415 prev_class
= mb_get_class(ptr
+ prevcol
);
3416 if (this_class
!= prev_class
3419 || (find_type
& FIND_IDENT
))
3420 # if defined(FEAT_BEVAL)
3421 && (!(find_type
& FIND_EVAL
)
3423 || !find_is_eval_item(ptr
+ prevcol
, &prevcol
,
3431 /* If we don't want just any old string, or we've found an
3432 * identifier, stop searching. */
3435 if (!(find_type
& FIND_STRING
) || this_class
== 2)
3443 ? vim_iswordc(ptr
[col
- 1])
3444 : (!vim_iswhite(ptr
[col
- 1])
3445 && (!(find_type
& FIND_IDENT
)
3446 || !vim_iswordc(ptr
[col
- 1]))))
3447 #if defined(FEAT_BEVAL)
3448 || ((find_type
& FIND_EVAL
)
3450 && find_is_eval_item(ptr
+ col
- 1, &col
,
3456 /* If we don't want just any old string, or we've found an
3457 * identifier, stop searching. */
3458 if (!(find_type
& FIND_STRING
) || vim_iswordc(ptr
[col
]))
3463 if (ptr
[col
] == NUL
|| (i
== 0 && (
3465 has_mbyte
? this_class
!= 2 :
3467 !vim_iswordc(ptr
[col
]))))
3470 * didn't find an identifier or string
3472 if (find_type
& FIND_STRING
)
3473 EMSG(_("E348: No string under cursor"));
3475 EMSG(_("E349: No identifier under cursor"));
3482 * 3. Find the end if the identifier/string.
3484 #if defined(FEAT_BEVAL)
3492 /* Search for point of changing multibyte character class. */
3493 this_class
= mb_get_class(ptr
);
3494 while (ptr
[col
] != NUL
3495 && ((i
== 0 ? mb_get_class(ptr
+ col
) == this_class
3496 : mb_get_class(ptr
+ col
) != 0)
3497 # if defined(FEAT_BEVAL)
3498 || ((find_type
& FIND_EVAL
)
3499 && col
<= (int)startcol
3500 && find_is_eval_item(ptr
+ col
, &col
, &bn
, FORWARD
))
3503 col
+= (*mb_ptr2len
)(ptr
+ col
);
3507 while ((i
== 0 ? vim_iswordc(ptr
[col
])
3508 : (ptr
[col
] != NUL
&& !vim_iswhite(ptr
[col
])))
3509 # if defined(FEAT_BEVAL)
3510 || ((find_type
& FIND_EVAL
)
3511 && col
<= (int)startcol
3512 && find_is_eval_item(ptr
+ col
, &col
, &bn
, FORWARD
))
3523 * Prepare for redo of a normal command.
3529 prep_redo(cap
->oap
->regname
, cap
->count0
,
3530 NUL
, cap
->cmdchar
, NUL
, NUL
, cap
->nchar
);
3534 * Prepare for redo of any command.
3535 * Note that only the last argument can be a multi-byte char.
3538 prep_redo(regname
, num
, cmd1
, cmd2
, cmd3
, cmd4
, cmd5
)
3548 if (regname
!= 0) /* yank from specified buffer */
3550 AppendCharToRedobuff('"');
3551 AppendCharToRedobuff(regname
);
3554 AppendNumberToRedobuff(num
);
3557 AppendCharToRedobuff(cmd1
);
3559 AppendCharToRedobuff(cmd2
);
3561 AppendCharToRedobuff(cmd3
);
3563 AppendCharToRedobuff(cmd4
);
3565 AppendCharToRedobuff(cmd5
);
3569 * check for operator active and clear it
3571 * return TRUE if operator was active
3577 if (oap
->op_type
== OP_NOP
)
3584 * Check for operator or Visual active. Clear active operator.
3586 * Return TRUE if operator or Visual was active.
3592 if (oap
->op_type
== OP_NOP
3606 oap
->op_type
= OP_NOP
;
3608 oap
->motion_force
= NUL
;
3609 oap
->use_reg_one
= FALSE
;
3622 * Remove the shift modifier from a special key.
3625 unshift_special(cap
)
3628 switch (cap
->cmdchar
)
3630 case K_S_RIGHT
: cap
->cmdchar
= K_RIGHT
; break;
3631 case K_S_LEFT
: cap
->cmdchar
= K_LEFT
; break;
3632 case K_S_UP
: cap
->cmdchar
= K_UP
; break;
3633 case K_S_DOWN
: cap
->cmdchar
= K_DOWN
; break;
3634 case K_S_HOME
: cap
->cmdchar
= K_HOME
; break;
3635 case K_S_END
: cap
->cmdchar
= K_END
; break;
3637 cap
->cmdchar
= simplify_key(cap
->cmdchar
, &mod_mask
);
3641 #if defined(FEAT_CMDL_INFO) || defined(PROTO)
3643 * Routines for displaying a partly typed command
3646 #ifdef FEAT_VISUAL /* need room for size of Visual area */
3647 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1 + 30
3649 # define SHOWCMD_BUFLEN SHOWCMD_COLS + 1
3651 static char_u showcmd_buf
[SHOWCMD_BUFLEN
];
3652 static char_u old_showcmd_buf
[SHOWCMD_BUFLEN
]; /* For push_showcmd() */
3653 static int showcmd_is_clear
= TRUE
;
3654 static int showcmd_visual
= FALSE
;
3656 static void display_showcmd
__ARGS((void));
3665 if (VIsual_active
&& !char_avail())
3667 int i
= lt(VIsual
, curwin
->w_cursor
);
3669 colnr_T leftcol
, rightcol
;
3672 /* Show the size of the Visual area. */
3676 bot
= curwin
->w_cursor
.lnum
;
3680 top
= curwin
->w_cursor
.lnum
;
3683 # ifdef FEAT_FOLDING
3684 /* Include closed folds as a whole. */
3685 hasFolding(top
, &top
, NULL
);
3686 hasFolding(bot
, NULL
, &bot
);
3688 lines
= bot
- top
+ 1;
3690 if (VIsual_mode
== Ctrl_V
)
3692 getvcols(curwin
, &curwin
->w_cursor
, &VIsual
, &leftcol
, &rightcol
);
3693 sprintf((char *)showcmd_buf
, "%ldx%ld", lines
,
3694 (long)(rightcol
- leftcol
+ 1));
3696 else if (VIsual_mode
== 'V' || VIsual
.lnum
!= curwin
->w_cursor
.lnum
)
3697 sprintf((char *)showcmd_buf
, "%ld", lines
);
3699 sprintf((char *)showcmd_buf
, "%ld", (long)(i
3700 ? curwin
->w_cursor
.col
- VIsual
.col
3701 : VIsual
.col
- curwin
->w_cursor
.col
) + (*p_sel
!= 'e'));
3702 showcmd_buf
[SHOWCMD_COLS
] = NUL
; /* truncate */
3703 showcmd_visual
= TRUE
;
3708 showcmd_buf
[0] = NUL
;
3709 showcmd_visual
= FALSE
;
3711 /* Don't actually display something if there is nothing to clear. */
3712 if (showcmd_is_clear
)
3720 * Add 'c' to string of shown command chars.
3721 * Return TRUE if output has been written (and setcursor() has been called).
3731 #if defined(FEAT_MOUSE)
3733 static int ignore
[] =
3736 K_VER_SCROLLBAR
, K_HOR_SCROLLBAR
,
3737 K_LEFTMOUSE_NM
, K_LEFTRELEASE_NM
,
3740 K_LEFTMOUSE
, K_LEFTDRAG
, K_LEFTRELEASE
,
3741 K_MIDDLEMOUSE
, K_MIDDLEDRAG
, K_MIDDLERELEASE
,
3742 K_RIGHTMOUSE
, K_RIGHTDRAG
, K_RIGHTRELEASE
,
3743 K_MOUSEDOWN
, K_MOUSEUP
,
3744 K_X1MOUSE
, K_X1DRAG
, K_X1RELEASE
, K_X2MOUSE
, K_X2DRAG
, K_X2RELEASE
,
3750 if (!p_sc
|| msg_silent
!= 0)
3755 showcmd_buf
[0] = NUL
;
3756 showcmd_visual
= FALSE
;
3759 #if defined(FEAT_MOUSE)
3760 /* Ignore keys that are scrollbar updates and mouse clicks */
3762 for (i
= 0; ignore
[i
] != 0; ++i
)
3768 old_len
= (int)STRLEN(showcmd_buf
);
3769 extra_len
= (int)STRLEN(p
);
3770 overflow
= old_len
+ extra_len
- SHOWCMD_COLS
;
3772 mch_memmove(showcmd_buf
, showcmd_buf
+ overflow
,
3773 old_len
- overflow
+ 1);
3774 STRCAT(showcmd_buf
, p
);
3788 if (!add_to_showcmd(c
))
3793 * Delete 'len' characters from the end of the shown command.
3796 del_from_showcmd(len
)
3804 old_len
= (int)STRLEN(showcmd_buf
);
3807 showcmd_buf
[old_len
- len
] = NUL
;
3814 * push_showcmd() and pop_showcmd() are used when waiting for the user to type
3815 * something and there is a partial mapping.
3821 STRCPY(old_showcmd_buf
, showcmd_buf
);
3830 STRCPY(showcmd_buf
, old_showcmd_buf
);
3842 len
= (int)STRLEN(showcmd_buf
);
3844 showcmd_is_clear
= TRUE
;
3847 screen_puts(showcmd_buf
, (int)Rows
- 1, sc_col
, 0);
3848 showcmd_is_clear
= FALSE
;
3852 * clear the rest of an old message by outputting up to SHOWCMD_COLS
3855 screen_puts((char_u
*)" " + len
, (int)Rows
- 1, sc_col
+ len
, 0);
3857 setcursor(); /* put cursor back where it belongs */
3861 #ifdef FEAT_SCROLLBIND
3863 * When "check" is FALSE, prepare for commands that scroll the window.
3864 * When "check" is TRUE, take care of scroll-binding after the window has
3865 * scrolled. Called from normal_cmd() and edit().
3868 do_check_scrollbind(check
)
3871 static win_T
*old_curwin
= NULL
;
3872 static linenr_T old_topline
= 0;
3874 static int old_topfill
= 0;
3876 static buf_T
*old_buf
= NULL
;
3877 static colnr_T old_leftcol
= 0;
3879 if (check
&& curwin
->w_p_scb
)
3881 /* If a ":syncbind" command was just used, don't scroll, only reset
3884 did_syncbind
= FALSE
;
3885 else if (curwin
== old_curwin
)
3888 * Synchronize other windows, as necessary according to
3889 * 'scrollbind'. Don't do this after an ":edit" command, except
3890 * when 'diff' is set.
3892 if ((curwin
->w_buffer
== old_buf
3897 && (curwin
->w_topline
!= old_topline
3899 || curwin
->w_topfill
!= old_topfill
3901 || curwin
->w_leftcol
!= old_leftcol
))
3903 check_scrollbind(curwin
->w_topline
- old_topline
,
3904 (long)(curwin
->w_leftcol
- old_leftcol
));
3907 else if (vim_strchr(p_sbo
, 'j')) /* jump flag set in 'scrollopt' */
3910 * When switching between windows, make sure that the relative
3911 * vertical offset is valid for the new window. The relative
3912 * offset is invalid whenever another 'scrollbind' window has
3913 * scrolled to a point that would force the current window to
3914 * scroll past the beginning or end of its buffer. When the
3915 * resync is performed, some of the other 'scrollbind' windows may
3916 * need to jump so that the current window's relative position is
3917 * visible on-screen.
3919 check_scrollbind(curwin
->w_topline
- curwin
->w_scbind_pos
, 0L);
3921 curwin
->w_scbind_pos
= curwin
->w_topline
;
3924 old_curwin
= curwin
;
3925 old_topline
= curwin
->w_topline
;
3927 old_topfill
= curwin
->w_topfill
;
3929 old_buf
= curwin
->w_buffer
;
3930 old_leftcol
= curwin
->w_leftcol
;
3934 * Synchronize any windows that have "scrollbind" set, based on the
3935 * number of rows by which the current window has changed
3936 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
3939 check_scrollbind(topline_diff
, leftcol_diff
)
3940 linenr_T topline_diff
;
3945 win_T
*old_curwin
= curwin
;
3946 buf_T
*old_curbuf
= curbuf
;
3948 int old_VIsual_select
= VIsual_select
;
3949 int old_VIsual_active
= VIsual_active
;
3951 colnr_T tgt_leftcol
= curwin
->w_leftcol
;
3956 * check 'scrollopt' string for vertical and horizontal scroll options
3958 want_ver
= (vim_strchr(p_sbo
, 'v') && topline_diff
!= 0);
3960 want_ver
|= old_curwin
->w_p_diff
;
3962 want_hor
= (vim_strchr(p_sbo
, 'h') && (leftcol_diff
|| topline_diff
!= 0));
3965 * loop through the scrollbound windows and scroll accordingly
3968 VIsual_select
= VIsual_active
= 0;
3970 for (curwin
= firstwin
; curwin
; curwin
= curwin
->w_next
)
3972 curbuf
= curwin
->w_buffer
;
3973 /* skip original window and windows with 'noscrollbind' */
3974 if (curwin
!= old_curwin
&& curwin
->w_p_scb
)
3977 * do the vertical scroll
3982 if (old_curwin
->w_p_diff
&& curwin
->w_p_diff
)
3984 diff_set_topline(old_curwin
, curwin
);
3989 curwin
->w_scbind_pos
+= topline_diff
;
3990 topline
= curwin
->w_scbind_pos
;
3991 if (topline
> curbuf
->b_ml
.ml_line_count
)
3992 topline
= curbuf
->b_ml
.ml_line_count
;
3996 y
= topline
- curwin
->w_topline
;
4000 scrolldown(-y
, FALSE
);
4003 redraw_later(VALID
);
4006 curwin
->w_redr_status
= TRUE
;
4011 * do the horizontal scroll
4013 if (want_hor
&& curwin
->w_leftcol
!= tgt_leftcol
)
4015 curwin
->w_leftcol
= tgt_leftcol
;
4022 * reset current-window
4025 VIsual_select
= old_VIsual_select
;
4026 VIsual_active
= old_VIsual_active
;
4028 curwin
= old_curwin
;
4029 curbuf
= old_curbuf
;
4031 #endif /* #ifdef FEAT_SCROLLBIND */
4034 * Command character that's ignored.
4035 * Used for CTRL-Q and CTRL-S to avoid problems with terminals that use
4042 cap
->retval
|= CA_COMMAND_BUSY
; /* don't call edit() now */
4046 * Command character that doesn't do anything, but unlike nv_ignore() does
4047 * start edit(). Used for "startinsert" executed while starting up.
4057 * Command character doesn't exist.
4063 clearopbeep(cap
->oap
);
4067 * <Help> and <F1> commands.
4073 if (!checkclearopq(cap
->oap
))
4078 * CTRL-A and CTRL-X: Add or subtract from letter or number under cursor.
4084 if (!checkclearopq(cap
->oap
)
4085 && do_addsub((int)cap
->cmdchar
, cap
->count1
) == OK
)
4090 * CTRL-F, CTRL-B, etc: Scroll page up or down.
4096 if (!checkclearop(cap
->oap
))
4099 if (mod_mask
& MOD_MASK_CTRL
)
4101 /* <C-PageUp>: tab page back; <C-PageDown>: tab page forward */
4102 if (cap
->arg
== BACKWARD
)
4103 goto_tabpage(-(int)cap
->count1
);
4105 goto_tabpage((int)cap
->count0
);
4109 (void)onepage(cap
->arg
, cap
->count1
);
4114 * Implementation of "gd" and "gD" command.
4117 nv_gd(oap
, nchar
, thisblock
)
4120 int thisblock
; /* 1 for "1gd" and "1gD" */
4125 if ((len
= find_ident_under_cursor(&ptr
, FIND_IDENT
)) == 0
4126 || find_decl(ptr
, len
, nchar
== 'd', thisblock
, 0) == FAIL
)
4129 else if ((fdo_flags
& FDO_SEARCH
) && KeyTyped
&& oap
->op_type
== OP_NOP
)
4135 * Search for variable declaration of "ptr[len]".
4136 * When "locally" is TRUE in the current function ("gd"), otherwise in the
4137 * current file ("gD").
4138 * When "thisblock" is TRUE check the {} block scope.
4139 * Return FAIL when not found.
4142 find_decl(ptr
, len
, locally
, thisblock
, searchflags
)
4147 int searchflags
; /* flags passed to searchit() */
4159 if ((pat
= alloc(len
+ 7)) == NULL
)
4162 /* Put "\V" before the pattern to avoid that the special meaning of "."
4163 * and "~" causes trouble. */
4164 sprintf((char *)pat
, vim_iswordp(ptr
) ? "\\V\\<%.*s\\>" : "\\V%.*s",
4166 old_pos
= curwin
->w_cursor
;
4169 p_ws
= FALSE
; /* don't wrap around end of file now */
4170 p_scs
= FALSE
; /* don't switch ignorecase off now */
4173 * With "gD" go to line 1.
4174 * With "gd" Search back for the start of the current function, then go
4175 * back until a blank line. If this fails go to line 1.
4177 if (!locally
|| !findpar(&incll
, BACKWARD
, 1L, '{', FALSE
))
4179 setpcmark(); /* Set in findpar() otherwise */
4180 curwin
->w_cursor
.lnum
= 1;
4181 par_pos
= curwin
->w_cursor
;
4185 par_pos
= curwin
->w_cursor
;
4186 while (curwin
->w_cursor
.lnum
> 1 && *skipwhite(ml_get_curline()) != NUL
)
4187 --curwin
->w_cursor
.lnum
;
4189 curwin
->w_cursor
.col
= 0;
4191 /* Search forward for the identifier, ignore comment lines. */
4192 clearpos(&found_pos
);
4195 t
= searchit(curwin
, curbuf
, &curwin
->w_cursor
, FORWARD
,
4196 pat
, 1L, searchflags
, RE_LAST
, (linenr_T
)0);
4197 if (curwin
->w_cursor
.lnum
>= old_pos
.lnum
)
4198 t
= FAIL
; /* match after start is failure too */
4200 if (thisblock
&& t
!= FAIL
)
4204 /* Check that the block the match is in doesn't end before the
4205 * position where we started the search from. */
4206 if ((pos
= findmatchlimit(NULL
, '}', FM_FORWARD
,
4207 (int)(old_pos
.lnum
- curwin
->w_cursor
.lnum
+ 1))) != NULL
4208 && pos
->lnum
< old_pos
.lnum
)
4214 /* If we previously found a valid position, use it. */
4215 if (found_pos
.lnum
!= 0)
4217 curwin
->w_cursor
= found_pos
;
4222 #ifdef FEAT_COMMENTS
4223 if (get_leader_len(ml_get_curline(), NULL
, FALSE
) > 0)
4225 /* Ignore this line, continue at start of next line. */
4226 ++curwin
->w_cursor
.lnum
;
4227 curwin
->w_cursor
.col
= 0;
4231 if (!locally
) /* global search: use first match found */
4233 if (curwin
->w_cursor
.lnum
>= par_pos
.lnum
)
4235 /* If we previously found a valid position, use it. */
4236 if (found_pos
.lnum
!= 0)
4237 curwin
->w_cursor
= found_pos
;
4241 /* For finding a local variable and the match is before the "{" search
4242 * to find a later match. For K&R style function declarations this
4243 * skips the function header without types. */
4244 found_pos
= curwin
->w_cursor
;
4250 curwin
->w_cursor
= old_pos
;
4254 curwin
->w_set_curswant
= TRUE
;
4255 /* "n" searches forward now */
4267 * Move 'dist' lines in direction 'dir', counting lines by *screen*
4268 * lines rather than lines in the file.
4269 * 'dist' must be positive.
4271 * Return OK if able to move cursor, FAIL otherwise.
4274 nv_screengo(oap
, dir
, dist
)
4279 int linelen
= linetabsize(ml_get_curline());
4283 int col_off1
; /* margin offset for first screen line */
4284 int col_off2
; /* margin offset for wrapped screen line */
4285 int width1
; /* text width for first screen line */
4286 int width2
; /* test width for wrapped screen line */
4288 oap
->motion_type
= MCHAR
;
4289 oap
->inclusive
= FALSE
;
4291 col_off1
= curwin_col_off();
4292 col_off2
= col_off1
- curwin_col_off2();
4293 width1
= W_WIDTH(curwin
) - col_off1
;
4294 width2
= W_WIDTH(curwin
) - col_off2
;
4296 #ifdef FEAT_VERTSPLIT
4297 if (curwin
->w_width
!= 0)
4301 * Instead of sticking at the last character of the buffer line we
4302 * try to stick in the last column of the screen.
4304 if (curwin
->w_curswant
== MAXCOL
)
4309 curwin
->w_curswant
= 0;
4312 curwin
->w_curswant
= width1
- 1;
4313 if (curwin
->w_virtcol
> curwin
->w_curswant
)
4314 curwin
->w_curswant
+= ((curwin
->w_virtcol
4315 - curwin
->w_curswant
- 1) / width2
+ 1) * width2
;
4320 if (linelen
> width1
)
4321 n
= ((linelen
- width1
- 1) / width2
+ 1) * width2
+ width1
;
4324 if (curwin
->w_curswant
> (colnr_T
)n
+ 1)
4325 curwin
->w_curswant
-= ((curwin
->w_curswant
- n
) / width2
+ 1)
4331 if (dir
== BACKWARD
)
4333 if ((long)curwin
->w_curswant
>= width2
)
4334 /* move back within line */
4335 curwin
->w_curswant
-= width2
;
4338 /* to previous line */
4339 if (curwin
->w_cursor
.lnum
== 1)
4344 --curwin
->w_cursor
.lnum
;
4346 /* Move to the start of a closed fold. Don't do that when
4347 * 'foldopen' contains "all": it will open in a moment. */
4348 if (!(fdo_flags
& FDO_ALL
))
4349 (void)hasFolding(curwin
->w_cursor
.lnum
,
4350 &curwin
->w_cursor
.lnum
, NULL
);
4352 linelen
= linetabsize(ml_get_curline());
4353 if (linelen
> width1
)
4354 curwin
->w_curswant
+= (((linelen
- width1
- 1) / width2
)
4358 else /* dir == FORWARD */
4360 if (linelen
> width1
)
4361 n
= ((linelen
- width1
- 1) / width2
+ 1) * width2
+ width1
;
4364 if (curwin
->w_curswant
+ width2
< (colnr_T
)n
)
4365 /* move forward within line */
4366 curwin
->w_curswant
+= width2
;
4371 /* Move to the end of a closed fold. */
4372 (void)hasFolding(curwin
->w_cursor
.lnum
, NULL
,
4373 &curwin
->w_cursor
.lnum
);
4375 if (curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
)
4380 curwin
->w_cursor
.lnum
++;
4381 curwin
->w_curswant
%= width2
;
4385 #ifdef FEAT_VERTSPLIT
4389 coladvance(curwin
->w_curswant
);
4391 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
4392 if (curwin
->w_cursor
.col
> 0 && curwin
->w_p_wrap
)
4395 * Check for landing on a character that got split at the end of the
4396 * last line. We want to advance a screenline, not end up in the same
4397 * screenline or move two screenlines.
4400 if (curwin
->w_virtcol
> curwin
->w_curswant
4401 && (curwin
->w_curswant
< (colnr_T
)width1
4402 ? (curwin
->w_curswant
> (colnr_T
)width1
/ 2)
4403 : ((curwin
->w_curswant
- width1
) % width2
4404 > (colnr_T
)width2
/ 2)))
4405 --curwin
->w_cursor
.col
;
4410 curwin
->w_curswant
= MAXCOL
; /* stick in the last column */
4417 * Mouse scroll wheel: Default action is to scroll three lines, or one page
4418 * when Shift or Ctrl is used.
4419 * K_MOUSEUP (cap->arg == TRUE) or K_MOUSEDOWN (cap->arg == FALSE)
4425 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4426 win_T
*old_curwin
= curwin
;
4428 /* Currently we only get the mouse coordinates in the GUI. */
4429 if (gui
.in_use
&& mouse_row
>= 0 && mouse_col
>= 0)
4436 /* find the window at the pointer coordinates */
4437 curwin
= mouse_find_win(&row
, &col
);
4438 curbuf
= curwin
->w_buffer
;
4442 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
4444 (void)onepage(cap
->arg
? FORWARD
: BACKWARD
, 1L);
4450 nv_scroll_line(cap
);
4453 # if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
4454 curwin
->w_redr_status
= TRUE
;
4456 curwin
= old_curwin
;
4457 curbuf
= curwin
->w_buffer
;
4462 * Mouse clicks and drags.
4468 (void)do_mouse(cap
->oap
, cap
->cmdchar
, BACKWARD
, cap
->count1
, 0);
4473 * Handle CTRL-E and CTRL-Y commands: scroll a line up or down.
4474 * cap->arg must be TRUE for CTRL-E.
4480 if (!checkclearop(cap
->oap
))
4481 scroll_redraw(cap
->arg
, cap
->count1
);
4485 * Scroll "count" lines up or down, and redraw.
4488 scroll_redraw(up
, count
)
4492 linenr_T prev_topline
= curwin
->w_topline
;
4494 int prev_topfill
= curwin
->w_topfill
;
4496 linenr_T prev_lnum
= curwin
->w_cursor
.lnum
;
4499 scrollup(count
, TRUE
);
4501 scrolldown(count
, TRUE
);
4504 /* Adjust the cursor position for 'scrolloff'. Mark w_topline as
4505 * valid, otherwise the screen jumps back at the end of the file. */
4507 check_cursor_moved(curwin
);
4508 curwin
->w_valid
|= VALID_TOPLINE
;
4510 /* If moved back to where we were, at least move the cursor, otherwise
4511 * we get stuck at one position. Don't move the cursor up if the
4512 * first line of the buffer is already on the screen */
4513 while (curwin
->w_topline
== prev_topline
4515 && curwin
->w_topfill
== prev_topfill
4521 if (curwin
->w_cursor
.lnum
> prev_lnum
4522 || cursor_down(1L, FALSE
) == FAIL
)
4527 if (curwin
->w_cursor
.lnum
< prev_lnum
4528 || prev_topline
== 1L
4529 || cursor_up(1L, FALSE
) == FAIL
)
4532 /* Mark w_topline as valid, otherwise the screen jumps back at the
4533 * end of the file. */
4534 check_cursor_moved(curwin
);
4535 curwin
->w_valid
|= VALID_TOPLINE
;
4538 if (curwin
->w_cursor
.lnum
!= prev_lnum
)
4539 coladvance(curwin
->w_curswant
);
4540 redraw_later(VALID
);
4544 * Commands that start with "z".
4552 int nchar
= cap
->nchar
;
4554 long old_fdl
= curwin
->w_p_fdl
;
4555 int old_fen
= curwin
->w_p_fen
;
4561 if (VIM_ISDIGIT(nchar
))
4564 * "z123{nchar}": edit the count before obtaining {nchar}
4566 if (checkclearop(cap
->oap
))
4571 #ifdef USE_ON_FLY_SCROLL
4572 dont_scroll
= TRUE
; /* disallow scrolling here */
4575 ++allow_keys
; /* no mapping for nchar, but allow key codes */
4576 nchar
= plain_vgetc();
4578 LANGMAP_ADJUST(nchar
, TRUE
);
4582 #ifdef FEAT_CMDL_INFO
4583 (void)add_to_showcmd(nchar
);
4585 if (nchar
== K_DEL
|| nchar
== K_KDEL
)
4587 else if (VIM_ISDIGIT(nchar
))
4588 n
= n
* 10 + (nchar
- '0');
4589 else if (nchar
== CAR
)
4592 need_mouse_correct
= TRUE
;
4594 win_setheight((int)n
);
4597 else if (nchar
== 'l'
4600 || nchar
== K_RIGHT
)
4602 cap
->count1
= n
? n
* cap
->count1
: cap
->count1
;
4607 clearopbeep(cap
->oap
);
4611 cap
->oap
->op_type
= OP_NOP
;
4618 /* "zf" and "zF" are always an operator, "zd", "zo", "zO", "zc"
4619 * and "zC" only in Visual mode. "zj" and "zk" are motion
4621 cap
->nchar
!= 'f' && cap
->nchar
!= 'F'
4622 && !(VIsual_active
&& vim_strchr((char_u
*)"dcCoO", cap
->nchar
))
4623 && cap
->nchar
!= 'j' && cap
->nchar
!= 'k'
4626 checkclearop(cap
->oap
))
4630 * For "z+", "z<CR>", "zt", "z.", "zz", "z^", "z-", "zb":
4631 * If line number given, set cursor.
4633 if ((vim_strchr((char_u
*)"+\r\nt.z^-b", nchar
) != NULL
)
4635 && cap
->count0
!= curwin
->w_cursor
.lnum
)
4638 if (cap
->count0
> curbuf
->b_ml
.ml_line_count
)
4639 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
4641 curwin
->w_cursor
.lnum
= cap
->count0
;
4647 /* "z+", "z<CR>" and "zt": put cursor at top of screen */
4649 if (cap
->count0
== 0)
4651 /* No count given: put cursor at the line below screen */
4652 validate_botline(); /* make sure w_botline is valid */
4653 if (curwin
->w_botline
> curbuf
->b_ml
.ml_line_count
)
4654 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
4656 curwin
->w_cursor
.lnum
= curwin
->w_botline
;
4662 beginline(BL_WHITE
| BL_FIX
);
4665 case 't': scroll_cursor_top(0, TRUE
);
4666 redraw_later(VALID
);
4669 /* "z." and "zz": put cursor in middle of screen */
4670 case '.': beginline(BL_WHITE
| BL_FIX
);
4673 case 'z': scroll_cursor_halfway(TRUE
);
4674 redraw_later(VALID
);
4677 /* "z^", "z-" and "zb": put cursor at bottom of screen */
4678 case '^': /* Strange Vi behavior: <count>z^ finds line at top of window
4679 * when <count> is at bottom of window, and puts that one at
4680 * bottom of window. */
4681 if (cap
->count0
!= 0)
4683 scroll_cursor_bot(0, TRUE
);
4684 curwin
->w_cursor
.lnum
= curwin
->w_topline
;
4686 else if (curwin
->w_topline
== 1)
4687 curwin
->w_cursor
.lnum
= 1;
4689 curwin
->w_cursor
.lnum
= curwin
->w_topline
- 1;
4692 beginline(BL_WHITE
| BL_FIX
);
4695 case 'b': scroll_cursor_bot(0, TRUE
);
4696 redraw_later(VALID
);
4699 /* "zH" - scroll screen right half-page */
4701 cap
->count1
*= W_WIDTH(curwin
) / 2;
4704 /* "zh" - scroll screen to the right */
4707 if (!curwin
->w_p_wrap
)
4709 if ((colnr_T
)cap
->count1
> curwin
->w_leftcol
)
4710 curwin
->w_leftcol
= 0;
4712 curwin
->w_leftcol
-= (colnr_T
)cap
->count1
;
4717 /* "zL" - scroll screen left half-page */
4718 case 'L': cap
->count1
*= W_WIDTH(curwin
) / 2;
4721 /* "zl" - scroll screen to the left */
4724 if (!curwin
->w_p_wrap
)
4726 /* scroll the window left */
4727 curwin
->w_leftcol
+= (colnr_T
)cap
->count1
;
4732 /* "zs" - scroll screen, cursor at the start */
4733 case 's': if (!curwin
->w_p_wrap
)
4736 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4737 col
= 0; /* like the cursor is in col 0 */
4740 getvcol(curwin
, &curwin
->w_cursor
, &col
, NULL
, NULL
);
4741 if ((long)col
> p_siso
)
4745 if (curwin
->w_leftcol
!= col
)
4747 curwin
->w_leftcol
= col
;
4748 redraw_later(NOT_VALID
);
4753 /* "ze" - scroll screen, cursor at the end */
4754 case 'e': if (!curwin
->w_p_wrap
)
4757 if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4758 col
= 0; /* like the cursor is in col 0 */
4761 getvcol(curwin
, &curwin
->w_cursor
, NULL
, NULL
, &col
);
4762 n
= W_WIDTH(curwin
) - curwin_col_off();
4763 if ((long)col
+ p_siso
< n
)
4766 col
= col
+ p_siso
- n
+ 1;
4767 if (curwin
->w_leftcol
!= col
)
4769 curwin
->w_leftcol
= col
;
4770 redraw_later(NOT_VALID
);
4776 /* "zF": create fold command */
4777 /* "zf": create fold operator */
4779 case 'f': if (foldManualAllowed(TRUE
))
4783 curwin
->w_p_fen
= TRUE
;
4785 /* "zF" is like "zfzf" */
4786 if (nchar
== 'F' && cap
->oap
->op_type
== OP_FOLD
)
4793 clearopbeep(cap
->oap
);
4796 /* "zd": delete fold at cursor */
4797 /* "zD": delete fold at cursor recursively */
4799 case 'D': if (foldManualAllowed(FALSE
))
4804 deleteFold(curwin
->w_cursor
.lnum
,
4805 curwin
->w_cursor
.lnum
, nchar
== 'D', FALSE
);
4809 /* "zE": erease all folds */
4810 case 'E': if (foldmethodIsManual(curwin
))
4812 clearFolding(curwin
);
4813 changed_window_setting();
4815 else if (foldmethodIsMarker(curwin
))
4816 deleteFold((linenr_T
)1, curbuf
->b_ml
.ml_line_count
,
4819 EMSG(_("E352: Cannot erase folds with current 'foldmethod'"));
4822 /* "zn": fold none: reset 'foldenable' */
4823 case 'n': curwin
->w_p_fen
= FALSE
;
4826 /* "zN": fold Normal: set 'foldenable' */
4827 case 'N': curwin
->w_p_fen
= TRUE
;
4830 /* "zi": invert folding: toggle 'foldenable' */
4831 case 'i': curwin
->w_p_fen
= !curwin
->w_p_fen
;
4834 /* "za": open closed fold or close open fold at cursor */
4835 case 'a': if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4836 openFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4839 closeFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4840 curwin
->w_p_fen
= TRUE
;
4844 /* "zA": open fold at cursor recursively */
4845 case 'A': if (hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
))
4846 openFoldRecurse(curwin
->w_cursor
.lnum
);
4849 closeFoldRecurse(curwin
->w_cursor
.lnum
);
4850 curwin
->w_p_fen
= TRUE
;
4854 /* "zo": open fold at cursor or Visual area */
4855 case 'o': if (VIsual_active
)
4858 openFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4861 /* "zO": open fold recursively */
4862 case 'O': if (VIsual_active
)
4865 openFoldRecurse(curwin
->w_cursor
.lnum
);
4868 /* "zc": close fold at cursor or Visual area */
4869 case 'c': if (VIsual_active
)
4872 closeFold(curwin
->w_cursor
.lnum
, cap
->count1
);
4873 curwin
->w_p_fen
= TRUE
;
4876 /* "zC": close fold recursively */
4877 case 'C': if (VIsual_active
)
4880 closeFoldRecurse(curwin
->w_cursor
.lnum
);
4881 curwin
->w_p_fen
= TRUE
;
4884 /* "zv": open folds at the cursor */
4885 case 'v': foldOpenCursor();
4888 /* "zx": re-apply 'foldlevel' and open folds at the cursor */
4889 case 'x': curwin
->w_p_fen
= TRUE
;
4890 newFoldLevel(); /* update right now */
4894 /* "zX": undo manual opens/closes, re-apply 'foldlevel' */
4895 case 'X': curwin
->w_p_fen
= TRUE
;
4896 old_fdl
= -1; /* force an update */
4899 /* "zm": fold more */
4900 case 'm': if (curwin
->w_p_fdl
> 0)
4902 old_fdl
= -1; /* force an update */
4903 curwin
->w_p_fen
= TRUE
;
4906 /* "zM": close all folds */
4907 case 'M': curwin
->w_p_fdl
= 0;
4908 old_fdl
= -1; /* force an update */
4909 curwin
->w_p_fen
= TRUE
;
4912 /* "zr": reduce folding */
4913 case 'r': ++curwin
->w_p_fdl
;
4916 /* "zR": open all folds */
4917 case 'R': curwin
->w_p_fdl
= getDeepestNesting();
4918 old_fdl
= -1; /* force an update */
4921 case 'j': /* "zj" move to next fold downwards */
4922 case 'k': /* "zk" move to next fold upwards */
4923 if (foldMoveTo(TRUE
, nchar
== 'j' ? FORWARD
: BACKWARD
,
4924 cap
->count1
) == FAIL
)
4925 clearopbeep(cap
->oap
);
4928 #endif /* FEAT_FOLDING */
4931 case 'u': /* "zug" and "zuw": undo "zg" and "zw" */
4933 ++allow_keys
; /* no mapping for nchar, but allow key codes */
4934 nchar
= plain_vgetc();
4936 LANGMAP_ADJUST(nchar
, TRUE
);
4940 #ifdef FEAT_CMDL_INFO
4941 (void)add_to_showcmd(nchar
);
4943 if (vim_strchr((char_u
*)"gGwW", nchar
) == NULL
)
4945 clearopbeep(cap
->oap
);
4951 case 'g': /* "zg": add good word to word list */
4952 case 'w': /* "zw": add wrong word to word list */
4953 case 'G': /* "zG": add good word to temp word list */
4954 case 'W': /* "zW": add wrong word to temp word list */
4959 if (checkclearop(cap
->oap
))
4962 if (VIsual_active
&& get_visual_text(cap
, &ptr
, &len
)
4968 pos_T pos
= curwin
->w_cursor
;
4970 /* Find bad word under the cursor. */
4971 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, NULL
);
4972 if (len
!= 0 && curwin
->w_cursor
.col
<= pos
.col
)
4973 ptr
= ml_get_pos(&curwin
->w_cursor
);
4974 curwin
->w_cursor
= pos
;
4977 if (ptr
== NULL
&& (len
= find_ident_under_cursor(&ptr
,
4980 spell_add_word(ptr
, len
, nchar
== 'w' || nchar
== 'W',
4981 (nchar
== 'G' || nchar
== 'W')
4982 ? 0 : (int)cap
->count1
,
4987 case '=': /* "z=": suggestions for a badly spelled word */
4988 if (!checkclearop(cap
->oap
))
4989 spell_suggest((int)cap
->count0
);
4993 default: clearopbeep(cap
->oap
);
4997 /* Redraw when 'foldenable' changed */
4998 if (old_fen
!= curwin
->w_p_fen
)
5003 if (foldmethodIsDiff(curwin
) && curwin
->w_p_scb
)
5005 /* Adjust 'foldenable' in diff-synced windows. */
5008 if (wp
!= curwin
&& foldmethodIsDiff(wp
) && wp
->w_p_scb
)
5010 wp
->w_p_fen
= curwin
->w_p_fen
;
5011 changed_window_setting_win(wp
);
5016 changed_window_setting();
5019 /* Redraw when 'foldlevel' changed. */
5020 if (old_fdl
!= curwin
->w_p_fdl
)
5027 * Vertical scrollbar movement.
5030 nv_ver_scrollbar(cap
)
5033 if (cap
->oap
->op_type
!= OP_NOP
)
5034 clearopbeep(cap
->oap
);
5036 /* Even if an operator was pending, we still want to scroll */
5041 * Horizontal scrollbar movement.
5044 nv_hor_scrollbar(cap
)
5047 if (cap
->oap
->op_type
!= OP_NOP
)
5048 clearopbeep(cap
->oap
);
5050 /* Even if an operator was pending, we still want to scroll */
5051 gui_do_horiz_scroll();
5055 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
5063 if (cap
->oap
->op_type
!= OP_NOP
)
5064 clearopbeep(cap
->oap
);
5066 /* Even if an operator was pending, we still want to jump tabs. */
5067 goto_tabpage(current_tab
);
5071 * Selected item in tab line menu.
5077 if (cap
->oap
->op_type
!= OP_NOP
)
5078 clearopbeep(cap
->oap
);
5080 /* Even if an operator was pending, we still want to jump tabs. */
5085 * Handle selecting an item of the GUI tab line menu.
5086 * Used in Normal and Insert mode.
5091 switch (current_tabmenu
)
5093 case TABLINE_MENU_CLOSE
:
5094 if (current_tab
== 0)
5095 do_cmdline_cmd((char_u
*)"tabclose");
5098 vim_snprintf((char *)IObuff
, IOSIZE
, "tabclose %d",
5100 do_cmdline_cmd(IObuff
);
5104 case TABLINE_MENU_NEW
:
5105 vim_snprintf((char *)IObuff
, IOSIZE
, "%dtabnew",
5106 current_tab
> 0 ? current_tab
- 1 : 999);
5107 do_cmdline_cmd(IObuff
);
5110 case TABLINE_MENU_OPEN
:
5111 vim_snprintf((char *)IObuff
, IOSIZE
, "browse %dtabnew",
5112 current_tab
> 0 ? current_tab
- 1 : 999);
5113 do_cmdline_cmd(IObuff
);
5127 * Ignore 'Q' in Visual mode, just give a beep.
5134 if (!checkclearop(cap
->oap
))
5139 * Handle a ":" command.
5153 if (cap
->oap
->op_type
!= OP_NOP
)
5155 /* Using ":" as a movement is characterwise exclusive. */
5156 cap
->oap
->motion_type
= MCHAR
;
5157 cap
->oap
->inclusive
= FALSE
;
5159 else if (cap
->count0
)
5161 /* translate "count:" into ":.,.+(count - 1)" */
5162 stuffcharReadbuff('.');
5163 if (cap
->count0
> 1)
5165 stuffReadbuff((char_u
*)",.+");
5166 stuffnumReadbuff((long)cap
->count0
- 1L);
5170 /* When typing, don't type below an old message */
5176 /* get a command line and execute it */
5177 do_cmdline(NULL
, getexline
, NULL
,
5178 cap
->oap
->op_type
!= OP_NOP
? DOCMD_KEEPLINE
: 0);
5180 /* If 'insertmode' changed, enter or exit Insert mode */
5181 if (p_im
!= old_p_im
)
5189 /* The start of the operator may have become invalid by the Ex
5191 if (cap
->oap
->op_type
!= OP_NOP
5192 && (cap
->oap
->start
.lnum
> curbuf
->b_ml
.ml_line_count
5193 || cap
->oap
->start
.col
>
5194 STRLEN(ml_get(cap
->oap
->start
.lnum
))))
5195 clearopbeep(cap
->oap
);
5200 * Handle CTRL-G command.
5207 if (VIsual_active
) /* toggle Selection/Visual mode */
5209 VIsual_select
= !VIsual_select
;
5214 if (!checkclearop(cap
->oap
))
5215 /* print full name if count given or :cd used */
5216 fileinfo((int)cap
->count0
, FALSE
, TRUE
);
5220 * Handle CTRL-H <Backspace> command.
5227 if (VIsual_active
&& VIsual_select
)
5229 cap
->cmdchar
= 'x'; /* BS key behaves like 'x' in Select mode */
5238 * CTRL-L: clear screen and redraw.
5244 if (!checkclearop(cap
->oap
))
5246 #if defined(__BEOS__) && !USE_THREAD_FOR_INPUT_WITH_TIMEOUT
5248 * Right now, the BeBox doesn't seem to have an easy way to detect
5249 * window resizing, so we cheat and make the user detect it
5250 * manually with CTRL-L instead
5255 /* Clear all syntax states to force resyncing. */
5256 syn_stack_free_all(curbuf
);
5258 redraw_later(CLEAR
);
5263 * CTRL-O: In Select mode: switch to Visual mode for one command.
5264 * Otherwise: Go to older pcmark.
5271 if (VIsual_active
&& VIsual_select
)
5273 VIsual_select
= FALSE
;
5275 restart_VIsual_select
= 2; /* restart Select mode later */
5280 cap
->count1
= -cap
->count1
;
5286 * CTRL-^ command, short for ":e #"
5292 if (!checkclearopq(cap
->oap
))
5293 (void)buflist_getfile((int)cap
->count0
, (linenr_T
)0,
5294 GETF_SETMARK
|GETF_ALT
, FALSE
);
5304 if (!checkclearopq(cap
->oap
))
5308 /* "ZZ": equivalent to ":x". */
5309 case 'Z': do_cmdline_cmd((char_u
*)"x");
5312 /* "ZQ": equivalent to ":q!" (Elvis compatible). */
5313 case 'Q': do_cmdline_cmd((char_u
*)"q!");
5316 default: clearopbeep(cap
->oap
);
5321 #if defined(FEAT_WINDOWS) || defined(PROTO)
5323 * Call nv_ident() as if "c1" was used, with "c2" as next character.
5334 vim_memset(&ca
, 0, sizeof(ca
));
5343 * Handle the commands that use the word under the cursor.
5344 * [g] CTRL-] :ta to current identifier
5345 * [g] 'K' run program for current identifier
5346 * [g] '*' / to current identifier or string
5347 * [g] '#' ? to current identifier or string
5348 * g ']' :tselect for current identifier
5357 char_u
*kp
; /* value of 'keywordprg' */
5358 int kp_help
; /* 'keywordprg' is ":help" */
5359 int n
= 0; /* init for GCC */
5361 int g_cmd
; /* "g" command */
5366 if (cap
->cmdchar
== 'g') /* "g*", "g#", "g]" and "gCTRL-]" */
5368 cmdchar
= cap
->nchar
;
5373 cmdchar
= cap
->cmdchar
;
5377 if (cmdchar
== POUND
) /* the pound sign, '#' for English keyboards */
5381 * The "]", "CTRL-]" and "K" commands accept an argument in Visual mode.
5383 if (cmdchar
== ']' || cmdchar
== Ctrl_RSB
|| cmdchar
== 'K')
5386 if (VIsual_active
&& get_visual_text(cap
, &ptr
, &n
) == FAIL
)
5389 if (checkclearopq(cap
->oap
))
5393 if (ptr
== NULL
&& (n
= find_ident_under_cursor(&ptr
,
5394 (cmdchar
== '*' || cmdchar
== '#')
5395 ? FIND_IDENT
|FIND_STRING
: FIND_IDENT
)) == 0)
5401 /* Allocate buffer to put the command in. Inserting backslashes can
5402 * double the length of the word. p_kp / curbuf->b_p_kp could be added
5403 * and some numbers. */
5404 kp
= (*curbuf
->b_p_kp
== NUL
? p_kp
: curbuf
->b_p_kp
);
5405 kp_help
= (*kp
== NUL
|| STRCMP(kp
, ":he") == 0
5406 || STRCMP(kp
, ":help") == 0);
5407 buf
= alloc((unsigned)(n
* 2 + 30 + STRLEN(kp
)));
5417 * Put cursor at start of word, makes search skip the word
5419 * Call setpcmark() first, so "*``" puts the cursor back where
5423 curwin
->w_cursor
.col
= (colnr_T
) (ptr
- ml_get_curline());
5425 if (!g_cmd
&& vim_iswordp(ptr
))
5427 no_smartcase
= TRUE
; /* don't use 'smartcase' now */
5432 STRCPY(buf
, "he! ");
5435 /* When a count is given, turn it into a range. Is this
5436 * really what we want? */
5437 isman
= (STRCMP(kp
, "man") == 0);
5438 isman_s
= (STRCMP(kp
, "man -s") == 0);
5439 if (cap
->count0
!= 0 && !(isman
|| isman_s
))
5440 sprintf((char *)buf
, ".,.+%ld", cap
->count0
- 1);
5443 if (cap
->count0
== 0 && isman_s
)
5448 if (cap
->count0
!= 0 && (isman
|| isman_s
))
5450 sprintf((char *)buf
+ STRLEN(buf
), "%ld", cap
->count0
);
5459 STRCPY(buf
, "cstag ");
5467 STRCPY(buf
, "he! ");
5471 sprintf((char *)buf
, "%ldta ", cap
->count0
);
5475 * Now grab the chars in the identifier
5478 aux_ptr
= (char_u
*)(p_magic
? "/.*~[^$\\" : "/^$\\");
5479 else if (cmdchar
== '#')
5480 aux_ptr
= (char_u
*)(p_magic
? "/?.*~[^$\\" : "/?^$\\");
5481 else if (cmdchar
== 'K' && !kp_help
)
5482 aux_ptr
= (char_u
*)" \t\\\"|!";
5484 /* Don't escape spaces and Tabs in a tag with a backslash */
5485 aux_ptr
= (char_u
*)"\\|\"";
5487 p
= buf
+ STRLEN(buf
);
5490 /* put a backslash before \ and some others */
5491 if (vim_strchr(aux_ptr
, *ptr
) != NULL
)
5494 /* When current byte is a part of multibyte character, copy all bytes
5495 * of that character. */
5499 int len
= (*mb_ptr2len
)(ptr
) - 1;
5501 for (i
= 0; i
< len
&& n
>= 1; ++i
, --n
)
5510 * Execute the command.
5512 if (cmdchar
== '*' || cmdchar
== '#')
5516 has_mbyte
? vim_iswordp(mb_prevptr(ml_get_curline(), ptr
)) :
5518 vim_iswordc(ptr
[-1])))
5521 /* put pattern in search history */
5522 add_to_history(HIST_SEARCH
, buf
, TRUE
, NUL
);
5524 normal_search(cap
, cmdchar
== '*' ? '/' : '?', buf
, 0);
5527 do_cmdline_cmd(buf
);
5532 #if defined(FEAT_VISUAL) || defined(PROTO)
5534 * Get visually selected text, within one line only.
5535 * Returns FAIL if more than one line selected.
5538 get_visual_text(cap
, pp
, lenp
)
5540 char_u
**pp
; /* return: start of selected text */
5541 int *lenp
; /* return: length of selected text */
5543 if (VIsual_mode
!= 'V')
5545 if (VIsual
.lnum
!= curwin
->w_cursor
.lnum
)
5548 clearopbeep(cap
->oap
);
5551 if (VIsual_mode
== 'V')
5553 *pp
= ml_get_curline();
5554 *lenp
= (int)STRLEN(*pp
);
5558 if (lt(curwin
->w_cursor
, VIsual
))
5560 *pp
= ml_get_pos(&curwin
->w_cursor
);
5561 *lenp
= VIsual
.col
- curwin
->w_cursor
.col
+ 1;
5565 *pp
= ml_get_pos(&VIsual
);
5566 *lenp
= curwin
->w_cursor
.col
- VIsual
.col
+ 1;
5570 /* Correct the length to include the whole last character. */
5571 *lenp
+= (*mb_ptr2len
)(*pp
+ (*lenp
- 1)) - 1;
5574 reset_VIsual_and_resel();
5580 * CTRL-T: backwards in tag stack
5586 if (!checkclearopq(cap
->oap
))
5587 do_tag((char_u
*)"", DT_POP
, (int)cap
->count1
, FALSE
, TRUE
);
5591 * Handle scrolling command 'H', 'L' and 'M'.
5604 cap
->oap
->motion_type
= MLINE
;
5607 if (cap
->cmdchar
== 'L')
5609 validate_botline(); /* make sure curwin->w_botline is valid */
5610 curwin
->w_cursor
.lnum
= curwin
->w_botline
- 1;
5611 if (cap
->count1
- 1 >= curwin
->w_cursor
.lnum
)
5612 curwin
->w_cursor
.lnum
= 1;
5616 if (hasAnyFolding(curwin
))
5618 /* Count a fold for one screen line. */
5619 for (n
= cap
->count1
- 1; n
> 0
5620 && curwin
->w_cursor
.lnum
> curwin
->w_topline
; --n
)
5622 (void)hasFolding(curwin
->w_cursor
.lnum
,
5623 &curwin
->w_cursor
.lnum
, NULL
);
5624 --curwin
->w_cursor
.lnum
;
5629 curwin
->w_cursor
.lnum
-= cap
->count1
- 1;
5634 if (cap
->cmdchar
== 'M')
5637 /* Don't count filler lines above the window. */
5638 used
-= diff_check_fill(curwin
, curwin
->w_topline
)
5639 - curwin
->w_topfill
;
5641 validate_botline(); /* make sure w_empty_rows is valid */
5642 half
= (curwin
->w_height
- curwin
->w_empty_rows
+ 1) / 2;
5643 for (n
= 0; curwin
->w_topline
+ n
< curbuf
->b_ml
.ml_line_count
; ++n
)
5646 /* Count half he number of filler lines to be "below this
5647 * line" and half to be "above the next line". */
5648 if (n
> 0 && used
+ diff_check_fill(curwin
, curwin
->w_topline
5655 used
+= plines(curwin
->w_topline
+ n
);
5659 if (hasFolding(curwin
->w_topline
+ n
, NULL
, &lnum
))
5660 n
= lnum
- curwin
->w_topline
;
5663 if (n
> 0 && used
> curwin
->w_height
)
5666 else /* (cap->cmdchar == 'H') */
5668 n
= cap
->count1
- 1;
5670 if (hasAnyFolding(curwin
))
5672 /* Count a fold for one screen line. */
5673 lnum
= curwin
->w_topline
;
5674 while (n
-- > 0 && lnum
< curwin
->w_botline
- 1)
5676 hasFolding(lnum
, NULL
, &lnum
);
5679 n
= lnum
- curwin
->w_topline
;
5683 curwin
->w_cursor
.lnum
= curwin
->w_topline
+ n
;
5684 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
5685 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
5688 cursor_correct(); /* correct for 'so' */
5689 beginline(BL_SOL
| BL_FIX
);
5693 * Cursor right commands.
5703 # define PAST_LINE 0
5706 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
5708 /* <C-Right> and <S-Right> move a word or WORD right */
5709 if (mod_mask
& MOD_MASK_CTRL
)
5715 cap
->oap
->motion_type
= MCHAR
;
5716 cap
->oap
->inclusive
= FALSE
;
5718 PAST_LINE
= (VIsual_active
&& *p_sel
!= 'o');
5720 # ifdef FEAT_VIRTUALEDIT
5722 * In virtual mode, there's no such thing as "PAST_LINE", as lines are
5723 * (theoretically) infinitely long.
5725 if (virtual_active())
5730 for (n
= cap
->count1
; n
> 0; --n
)
5732 if ((!PAST_LINE
&& oneright() == FAIL
)
5733 || (PAST_LINE
&& *ml_get_cursor() == NUL
))
5736 * <Space> wraps to next line if 'whichwrap' has 's'.
5737 * 'l' wraps to next line if 'whichwrap' has 'l'.
5738 * CURS_RIGHT wraps to next line if 'whichwrap' has '>'.
5740 if ( ((cap
->cmdchar
== ' '
5741 && vim_strchr(p_ww
, 's') != NULL
)
5742 || (cap
->cmdchar
== 'l'
5743 && vim_strchr(p_ww
, 'l') != NULL
)
5744 || (cap
->cmdchar
== K_RIGHT
5745 && vim_strchr(p_ww
, '>') != NULL
))
5746 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
5748 /* When deleting we also count the NL as a character.
5749 * Set cap->oap->inclusive when last char in the line is
5750 * included, move to next line after that */
5751 if ( cap
->oap
->op_type
!= OP_NOP
5752 && !cap
->oap
->inclusive
5753 && !lineempty(curwin
->w_cursor
.lnum
))
5754 cap
->oap
->inclusive
= TRUE
;
5757 ++curwin
->w_cursor
.lnum
;
5758 curwin
->w_cursor
.col
= 0;
5759 #ifdef FEAT_VIRTUALEDIT
5760 curwin
->w_cursor
.coladd
= 0;
5762 curwin
->w_set_curswant
= TRUE
;
5763 cap
->oap
->inclusive
= FALSE
;
5767 if (cap
->oap
->op_type
== OP_NOP
)
5769 /* Only beep and flush if not moved at all */
5770 if (n
== cap
->count1
)
5775 if (!lineempty(curwin
->w_cursor
.lnum
))
5776 cap
->oap
->inclusive
= TRUE
;
5783 curwin
->w_set_curswant
= TRUE
;
5784 # ifdef FEAT_VIRTUALEDIT
5785 if (virtual_active())
5792 curwin
->w_cursor
.col
+=
5793 (*mb_ptr2len
)(ml_get_cursor());
5796 ++curwin
->w_cursor
.col
;
5802 if (n
!= cap
->count1
&& (fdo_flags
& FDO_HOR
) && KeyTyped
5803 && cap
->oap
->op_type
== OP_NOP
)
5809 * Cursor left commands.
5811 * Returns TRUE when operator end should not be adjusted.
5819 if (mod_mask
& (MOD_MASK_SHIFT
| MOD_MASK_CTRL
))
5821 /* <C-Left> and <S-Left> move a word or WORD left */
5822 if (mod_mask
& MOD_MASK_CTRL
)
5828 cap
->oap
->motion_type
= MCHAR
;
5829 cap
->oap
->inclusive
= FALSE
;
5830 for (n
= cap
->count1
; n
> 0; --n
)
5832 if (oneleft() == FAIL
)
5834 /* <BS> and <Del> wrap to previous line if 'whichwrap' has 'b'.
5835 * 'h' wraps to previous line if 'whichwrap' has 'h'.
5836 * CURS_LEFT wraps to previous line if 'whichwrap' has '<'.
5838 if ( (((cap
->cmdchar
== K_BS
5839 || cap
->cmdchar
== Ctrl_H
)
5840 && vim_strchr(p_ww
, 'b') != NULL
)
5841 || (cap
->cmdchar
== 'h'
5842 && vim_strchr(p_ww
, 'h') != NULL
)
5843 || (cap
->cmdchar
== K_LEFT
5844 && vim_strchr(p_ww
, '<') != NULL
))
5845 && curwin
->w_cursor
.lnum
> 1)
5847 --(curwin
->w_cursor
.lnum
);
5848 coladvance((colnr_T
)MAXCOL
);
5849 curwin
->w_set_curswant
= TRUE
;
5851 /* When the NL before the first char has to be deleted we
5852 * put the cursor on the NUL after the previous line.
5853 * This is a very special case, be careful!
5854 * don't adjust op_end now, otherwise it won't work */
5855 if ( (cap
->oap
->op_type
== OP_DELETE
5856 || cap
->oap
->op_type
== OP_CHANGE
)
5857 && !lineempty(curwin
->w_cursor
.lnum
))
5859 ++curwin
->w_cursor
.col
;
5860 cap
->retval
|= CA_NO_ADJ_OP_END
;
5864 /* Only beep and flush if not moved at all */
5865 else if (cap
->oap
->op_type
== OP_NOP
&& n
== cap
->count1
)
5871 if (n
!= cap
->count1
&& (fdo_flags
& FDO_HOR
) && KeyTyped
5872 && cap
->oap
->op_type
== OP_NOP
)
5878 * Cursor up commands.
5879 * cap->arg is TRUE for "-": Move cursor to first non-blank.
5885 if (mod_mask
& MOD_MASK_SHIFT
)
5887 /* <S-Up> is page up */
5888 cap
->arg
= BACKWARD
;
5893 cap
->oap
->motion_type
= MLINE
;
5894 if (cursor_up(cap
->count1
, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
5895 clearopbeep(cap
->oap
);
5897 beginline(BL_WHITE
| BL_FIX
);
5902 * Cursor down commands.
5903 * cap->arg is TRUE for CR and "+": Move cursor to first non-blank.
5909 if (mod_mask
& MOD_MASK_SHIFT
)
5911 /* <S-Down> is page down */
5916 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5917 /* In a quickfix window a <CR> jumps to the error under the cursor. */
5918 if (bt_quickfix(curbuf
) && cap
->cmdchar
== CAR
)
5919 if (curwin
->w_llist_ref
== NULL
)
5920 do_cmdline_cmd((char_u
*)".cc"); /* quickfix window */
5922 do_cmdline_cmd((char_u
*)".ll"); /* location list window */
5927 /* In the cmdline window a <CR> executes the command. */
5928 if (cmdwin_type
!= 0 && cap
->cmdchar
== CAR
)
5929 cmdwin_result
= CAR
;
5933 cap
->oap
->motion_type
= MLINE
;
5934 if (cursor_down(cap
->count1
, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
5935 clearopbeep(cap
->oap
);
5937 beginline(BL_WHITE
| BL_FIX
);
5942 #ifdef FEAT_SEARCHPATH
5944 * Grab the file name under the cursor and edit it.
5955 clearopbeep(cap
->oap
);
5960 if (curbuf_locked())
5967 ptr
= grab_file_name(cap
->count1
, &lnum
);
5971 /* do autowrite if necessary */
5972 if (curbufIsChanged() && curbuf
->b_nwindows
<= 1 && !P_HID(curbuf
))
5973 autowrite(curbuf
, FALSE
);
5975 (void)do_ecmd(0, ptr
, NULL
, NULL
, ECMD_LAST
,
5976 P_HID(curbuf
) ? ECMD_HIDE
: 0);
5977 if (cap
->nchar
== 'F' && lnum
>= 0)
5979 curwin
->w_cursor
.lnum
= lnum
;
5980 check_cursor_lnum();
5981 beginline(BL_SOL
| BL_FIX
);
5991 * <End> command: to end of current line or last line.
5997 if (cap
->arg
|| (mod_mask
& MOD_MASK_CTRL
)) /* CTRL-END = goto last line */
6001 cap
->count1
= 1; /* to end of current line */
6007 * Handle the "$" command.
6013 cap
->oap
->motion_type
= MCHAR
;
6014 cap
->oap
->inclusive
= TRUE
;
6015 #ifdef FEAT_VIRTUALEDIT
6016 /* In virtual mode when off the edge of a line and an operator
6017 * is pending (whew!) keep the cursor where it is.
6018 * Otherwise, send it to the end of the line. */
6019 if (!virtual_active() || gchar_cursor() != NUL
6020 || cap
->oap
->op_type
== OP_NOP
)
6022 curwin
->w_curswant
= MAXCOL
; /* so we stay at the end */
6023 if (cursor_down((long)(cap
->count1
- 1),
6024 cap
->oap
->op_type
== OP_NOP
) == FAIL
)
6025 clearopbeep(cap
->oap
);
6027 else if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6033 * Implementation of '?' and '/' commands.
6034 * If cap->arg is TRUE don't set PC mark.
6040 oparg_T
*oap
= cap
->oap
;
6042 if (cap
->cmdchar
== '?' && cap
->oap
->op_type
== OP_ROT13
)
6044 /* Translate "g??" to "g?g?" */
6051 cap
->searchbuf
= getcmdline(cap
->cmdchar
, cap
->count1
, 0);
6053 if (cap
->searchbuf
== NULL
)
6059 normal_search(cap
, cap
->cmdchar
, cap
->searchbuf
,
6060 (cap
->arg
? 0 : SEARCH_MARK
));
6064 * Handle "N" and "n" commands.
6065 * cap->arg is SEARCH_REV for "N", 0 for "n".
6071 normal_search(cap
, 0, NULL
, SEARCH_MARK
| cap
->arg
);
6075 * Search for "pat" in direction "dir" ('/' or '?', 0 for repeat).
6076 * Uses only cap->count1 and cap->oap from "cap".
6079 normal_search(cap
, dir
, pat
, opt
)
6083 int opt
; /* extra flags for do_search() */
6087 cap
->oap
->motion_type
= MCHAR
;
6088 cap
->oap
->inclusive
= FALSE
;
6089 cap
->oap
->use_reg_one
= TRUE
;
6090 curwin
->w_set_curswant
= TRUE
;
6092 i
= do_search(cap
->oap
, dir
, pat
, cap
->count1
,
6093 opt
| SEARCH_OPT
| SEARCH_ECHO
| SEARCH_MSG
);
6099 cap
->oap
->motion_type
= MLINE
;
6100 #ifdef FEAT_VIRTUALEDIT
6101 curwin
->w_cursor
.coladd
= 0;
6104 if (cap
->oap
->op_type
== OP_NOP
&& (fdo_flags
& FDO_SEARCH
) && KeyTyped
)
6109 /* "/$" will put the cursor after the end of the line, may need to
6110 * correct that here */
6115 * Character search commands.
6116 * cap->arg is BACKWARD for 'F' and 'T', FORWARD for 'f' and 't', TRUE for
6117 * ',' and FALSE for ';'.
6118 * cap->nchar is NUL for ',' and ';' (repeat the search)
6126 if (cap
->cmdchar
== 't' || cap
->cmdchar
== 'T')
6131 cap
->oap
->motion_type
= MCHAR
;
6132 if (IS_SPECIAL(cap
->nchar
) || searchc(cap
, t_cmd
) == FAIL
)
6133 clearopbeep(cap
->oap
);
6136 curwin
->w_set_curswant
= TRUE
;
6137 #ifdef FEAT_VIRTUALEDIT
6138 /* Include a Tab for "tx" and for "dfx". */
6139 if (gchar_cursor() == TAB
&& virtual_active() && cap
->arg
== FORWARD
6140 && (t_cmd
|| cap
->oap
->op_type
!= OP_NOP
))
6144 getvcol(curwin
, &curwin
->w_cursor
, &scol
, NULL
, &ecol
);
6145 curwin
->w_cursor
.coladd
= ecol
- scol
;
6148 curwin
->w_cursor
.coladd
= 0;
6151 adjust_for_sel(cap
);
6154 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6161 * "[" and "]" commands.
6162 * cap->arg is BACKWARD for "[" and FORWARD for "]".
6170 pos_T
*pos
= NULL
; /* init for GCC */
6171 pos_T old_pos
; /* cursor position before command */
6177 cap
->oap
->motion_type
= MCHAR
;
6178 cap
->oap
->inclusive
= FALSE
;
6179 old_pos
= curwin
->w_cursor
;
6180 #ifdef FEAT_VIRTUALEDIT
6181 curwin
->w_cursor
.coladd
= 0; /* TODO: don't do this for an error. */
6184 #ifdef FEAT_SEARCHPATH
6186 * "[f" or "]f" : Edit file under the cursor (same as "gf")
6188 if (cap
->nchar
== 'f')
6195 * Find the occurrence(s) of the identifier or define under cursor
6196 * in current and included files or jump to the first occurrence.
6199 * fwd bwd fwd bwd fwd bwd
6200 * identifier "]i" "[i" "]I" "[I" "]^I" "[^I"
6201 * define "]d" "[d" "]D" "[D" "]^D" "[^D"
6203 if (vim_strchr((char_u
*)
6209 cap
->nchar
) != NULL
)
6214 if ((len
= find_ident_under_cursor(&ptr
, FIND_IDENT
)) == 0)
6218 find_pattern_in_path(ptr
, 0, len
, TRUE
,
6219 cap
->count0
== 0 ? !isupper(cap
->nchar
) : FALSE
,
6220 ((cap
->nchar
& 0xf) == ('d' & 0xf)) ? FIND_DEFINE
: FIND_ANY
,
6222 isupper(cap
->nchar
) ? ACTION_SHOW_ALL
:
6223 islower(cap
->nchar
) ? ACTION_SHOW
: ACTION_GOTO
,
6224 cap
->cmdchar
== ']' ? curwin
->w_cursor
.lnum
+ 1 : (linenr_T
)1,
6226 curwin
->w_set_curswant
= TRUE
;
6233 * "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
6234 * "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
6235 * "[/", "[*", "]/", "]*": go to Nth comment start/end.
6236 * "[m" or "]m" search for prev/next start of (Java) method.
6237 * "[M" or "]M" search for prev/next end of (Java) method.
6239 if ( (cap
->cmdchar
== '['
6240 && vim_strchr((char_u
*)"{(*/#mM", cap
->nchar
) != NULL
)
6241 || (cap
->cmdchar
== ']'
6242 && vim_strchr((char_u
*)"})*/#mM", cap
->nchar
) != NULL
))
6244 if (cap
->nchar
== '*')
6248 if (cap
->nchar
== 'm' || cap
->nchar
== 'M')
6250 if (cap
->cmdchar
== '[')
6263 if ((pos
= findmatchlimit(cap
->oap
, findc
,
6264 (cap
->cmdchar
== '[') ? FM_BACKWARD
: FM_FORWARD
, 0)) == NULL
)
6266 if (new_pos
.lnum
== 0) /* nothing found */
6268 if (cap
->nchar
!= 'm' && cap
->nchar
!= 'M')
6269 clearopbeep(cap
->oap
);
6272 pos
= &new_pos
; /* use last one found */
6276 curwin
->w_cursor
= *pos
;
6279 curwin
->w_cursor
= old_pos
;
6282 * Handle "[m", "]m", "[M" and "[M". The findmatchlimit() only
6283 * brought us to the match for "[m" and "]M" when inside a method.
6284 * Try finding the '{' or '}' we want to be at.
6285 * Also repeat for the given count.
6287 if (cap
->nchar
== 'm' || cap
->nchar
== 'M')
6289 /* norm is TRUE for "]M" and "[m" */
6290 int norm
= ((findc
== '{') == (cap
->nchar
== 'm'));
6293 /* found a match: we were inside a method */
6294 if (prev_pos
.lnum
!= 0)
6297 curwin
->w_cursor
= prev_pos
;
6307 if ((findc
== '{' ? dec_cursor() : inc_cursor()) < 0)
6309 /* if not found anything, that's an error */
6311 clearopbeep(cap
->oap
);
6316 if (c
== '{' || c
== '}')
6318 /* Must have found end/start of class: use it.
6319 * Or found the place to be at. */
6320 if ((c
== findc
&& norm
) || (n
== 1 && !norm
))
6322 new_pos
= curwin
->w_cursor
;
6326 /* if no match found at all, we started outside of the
6327 * class and we're inside now. Just go on. */
6328 else if (new_pos
.lnum
== 0)
6330 new_pos
= curwin
->w_cursor
;
6333 /* found start/end of other method: go to match */
6334 else if ((pos
= findmatchlimit(cap
->oap
, findc
,
6335 (cap
->cmdchar
== '[') ? FM_BACKWARD
: FM_FORWARD
,
6339 curwin
->w_cursor
= *pos
;
6345 curwin
->w_cursor
= old_pos
;
6346 if (pos
== NULL
&& new_pos
.lnum
!= 0)
6347 clearopbeep(cap
->oap
);
6352 curwin
->w_cursor
= *pos
;
6353 curwin
->w_set_curswant
= TRUE
;
6355 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
6356 && cap
->oap
->op_type
== OP_NOP
)
6363 * "[[", "[]", "]]" and "][": move to start or end of function
6365 else if (cap
->nchar
== '[' || cap
->nchar
== ']')
6367 if (cap
->nchar
== cap
->cmdchar
) /* "]]" or "[[" */
6370 flag
= '}'; /* "][" or "[]" */
6372 curwin
->w_set_curswant
= TRUE
;
6374 * Imitate strange Vi behaviour: When using "]]" with an operator
6375 * we also stop at '}'.
6377 if (!findpar(&cap
->oap
->inclusive
, cap
->arg
, cap
->count1
, flag
,
6378 (cap
->oap
->op_type
!= OP_NOP
6379 && cap
->arg
== FORWARD
&& flag
== '{')))
6380 clearopbeep(cap
->oap
);
6383 if (cap
->oap
->op_type
== OP_NOP
)
6384 beginline(BL_WHITE
| BL_FIX
);
6386 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6393 * "[p", "[P", "]P" and "]p": put with indent adjustment
6395 else if (cap
->nchar
== 'p' || cap
->nchar
== 'P')
6397 if (!checkclearop(cap
->oap
))
6400 do_put(cap
->oap
->regname
,
6401 (cap
->cmdchar
== ']' && cap
->nchar
== 'p') ? FORWARD
: BACKWARD
,
6402 cap
->count1
, PUT_FIXINDENT
);
6407 * "['", "[`", "]'" and "]`": jump to next mark
6409 else if (cap
->nchar
== '\'' || cap
->nchar
== '`')
6411 pos
= &curwin
->w_cursor
;
6412 for (n
= cap
->count1
; n
> 0; --n
)
6415 pos
= getnextmark(pos
, cap
->cmdchar
== '[' ? BACKWARD
: FORWARD
,
6416 cap
->nchar
== '\'');
6422 nv_cursormark(cap
, cap
->nchar
== '\'', pos
);
6427 * [ or ] followed by a middle mouse click: put selected text with
6428 * indent adjustment. Any other button just does as usual.
6430 else if (cap
->nchar
>= K_LEFTMOUSE
&& cap
->nchar
<= K_RIGHTRELEASE
)
6432 (void)do_mouse(cap
->oap
, cap
->nchar
,
6433 (cap
->cmdchar
== ']') ? FORWARD
: BACKWARD
,
6434 cap
->count1
, PUT_FIXINDENT
);
6436 #endif /* FEAT_MOUSE */
6440 * "[z" and "]z": move to start or end of open fold.
6442 else if (cap
->nchar
== 'z')
6444 if (foldMoveTo(FALSE
, cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6445 cap
->count1
) == FAIL
)
6446 clearopbeep(cap
->oap
);
6452 * "[c" and "]c": move to next or previous diff-change.
6454 else if (cap
->nchar
== 'c')
6456 if (diff_move_to(cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6457 cap
->count1
) == FAIL
)
6458 clearopbeep(cap
->oap
);
6464 * "[s", "[S", "]s" and "]S": move to next spell error.
6466 else if (cap
->nchar
== 's' || cap
->nchar
== 'S')
6469 for (n
= 0; n
< cap
->count1
; ++n
)
6470 if (spell_move_to(curwin
, cap
->cmdchar
== ']' ? FORWARD
: BACKWARD
,
6471 cap
->nchar
== 's' ? TRUE
: FALSE
, FALSE
, NULL
) == 0)
6473 clearopbeep(cap
->oap
);
6476 # ifdef FEAT_FOLDING
6477 if (cap
->oap
->op_type
== OP_NOP
&& (fdo_flags
& FDO_SEARCH
) && KeyTyped
)
6483 /* Not a valid cap->nchar. */
6485 clearopbeep(cap
->oap
);
6489 * Handle Normal mode "%" command.
6497 linenr_T lnum
= curwin
->w_cursor
.lnum
;
6500 cap
->oap
->inclusive
= TRUE
;
6501 if (cap
->count0
) /* {cnt}% : goto {cnt} percentage in file */
6503 if (cap
->count0
> 100)
6504 clearopbeep(cap
->oap
);
6507 cap
->oap
->motion_type
= MLINE
;
6509 /* Round up, so CTRL-G will give same value. Watch out for a
6510 * large line count, the line number must not go negative! */
6511 if (curbuf
->b_ml
.ml_line_count
> 1000000)
6512 curwin
->w_cursor
.lnum
= (curbuf
->b_ml
.ml_line_count
+ 99L)
6513 / 100L * cap
->count0
;
6515 curwin
->w_cursor
.lnum
= (curbuf
->b_ml
.ml_line_count
*
6516 cap
->count0
+ 99L) / 100L;
6517 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
6518 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6519 beginline(BL_SOL
| BL_FIX
);
6522 else /* "%" : go to matching paren */
6524 cap
->oap
->motion_type
= MCHAR
;
6525 cap
->oap
->use_reg_one
= TRUE
;
6526 if ((pos
= findmatch(cap
->oap
, NUL
)) == NULL
)
6527 clearopbeep(cap
->oap
);
6531 curwin
->w_cursor
= *pos
;
6532 curwin
->w_set_curswant
= TRUE
;
6533 #ifdef FEAT_VIRTUALEDIT
6534 curwin
->w_cursor
.coladd
= 0;
6537 adjust_for_sel(cap
);
6542 if (cap
->oap
->op_type
== OP_NOP
6543 && lnum
!= curwin
->w_cursor
.lnum
6544 && (fdo_flags
& FDO_PERCENT
)
6551 * Handle "(" and ")" commands.
6552 * cap->arg is BACKWARD for "(" and FORWARD for ")".
6558 cap
->oap
->motion_type
= MCHAR
;
6559 cap
->oap
->use_reg_one
= TRUE
;
6560 /* The motion used to be inclusive for "(", but that is not what Vi does. */
6561 cap
->oap
->inclusive
= FALSE
;
6562 curwin
->w_set_curswant
= TRUE
;
6564 if (findsent(cap
->arg
, cap
->count1
) == FAIL
)
6565 clearopbeep(cap
->oap
);
6568 #ifdef FEAT_VIRTUALEDIT
6569 curwin
->w_cursor
.coladd
= 0;
6572 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6579 * "m" command: Mark a position.
6585 if (!checkclearop(cap
->oap
))
6587 if (setmark(cap
->nchar
) == FAIL
)
6588 clearopbeep(cap
->oap
);
6593 * "{" and "}" commands.
6594 * cmd->arg is BACKWARD for "{" and FORWARD for "}".
6600 cap
->oap
->motion_type
= MCHAR
;
6601 cap
->oap
->inclusive
= FALSE
;
6602 cap
->oap
->use_reg_one
= TRUE
;
6603 curwin
->w_set_curswant
= TRUE
;
6604 if (!findpar(&cap
->oap
->inclusive
, cap
->arg
, cap
->count1
, NUL
, FALSE
))
6605 clearopbeep(cap
->oap
);
6608 #ifdef FEAT_VIRTUALEDIT
6609 curwin
->w_cursor
.coladd
= 0;
6612 if ((fdo_flags
& FDO_BLOCK
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
6619 * "u" command: Undo or make lower case.
6625 if (cap
->oap
->op_type
== OP_LOWER
6631 /* translate "<Visual>u" to "<Visual>gu" and "guu" to "gugu" */
6647 if (!checkclearopq(cap
->oap
))
6649 u_undo((int)cap
->count1
);
6650 curwin
->w_set_curswant
= TRUE
;
6655 * Handle the "r" command.
6665 if (checkclearop(cap
->oap
))
6668 /* get another character */
6669 if (cap
->nchar
== Ctrl_V
)
6671 had_ctrl_v
= Ctrl_V
;
6672 cap
->nchar
= get_literal();
6673 /* Don't redo a multibyte character with CTRL-V. */
6674 if (cap
->nchar
> DEL
)
6680 /* Abort if the character is a special key. */
6681 if (IS_SPECIAL(cap
->nchar
))
6683 clearopbeep(cap
->oap
);
6688 /* Visual mode "r" */
6696 #ifdef FEAT_VIRTUALEDIT
6697 /* Break tabs, etc. */
6698 if (virtual_active())
6700 if (u_save_cursor() == FAIL
)
6702 if (gchar_cursor() == NUL
)
6704 /* Add extra space and put the cursor on the first one. */
6705 coladvance_force((colnr_T
)(getviscol() + cap
->count1
));
6706 curwin
->w_cursor
.col
-= cap
->count1
;
6708 else if (gchar_cursor() == TAB
)
6709 coladvance_force(getviscol());
6713 /* Abort if not enough characters to replace. */
6714 ptr
= ml_get_cursor();
6715 if (STRLEN(ptr
) < (unsigned)cap
->count1
6717 || (has_mbyte
&& mb_charlen(ptr
) < cap
->count1
)
6721 clearopbeep(cap
->oap
);
6726 * Replacing with a TAB is done by edit() when it is complicated because
6727 * 'expandtab' or 'smarttab' is set. CTRL-V TAB inserts a literal TAB.
6728 * Other characters are done below to avoid problems with things like
6729 * CTRL-V 048 (for edit() this would be R CTRL-V 0 ESC).
6731 if (had_ctrl_v
!= Ctrl_V
&& cap
->nchar
== '\t' && (curbuf
->b_p_et
|| p_sta
))
6733 stuffnumReadbuff(cap
->count1
);
6734 stuffcharReadbuff('R');
6735 stuffcharReadbuff('\t');
6736 stuffcharReadbuff(ESC
);
6740 /* save line for undo */
6741 if (u_save_cursor() == FAIL
)
6744 if (had_ctrl_v
!= Ctrl_V
&& (cap
->nchar
== '\r' || cap
->nchar
== '\n'))
6747 * Replace character(s) by a single newline.
6748 * Strange vi behaviour: Only one newline is inserted.
6749 * Delete the characters here.
6750 * Insert the newline with an insert command, takes care of
6751 * autoindent. The insert command depends on being on the last
6752 * character of a line or not.
6755 (void)del_chars(cap
->count1
, FALSE
); /* delete the characters */
6757 (void)del_bytes(cap
->count1
, FALSE
, FALSE
); /* delete the characters */
6759 stuffcharReadbuff('\r');
6760 stuffcharReadbuff(ESC
);
6762 /* Give 'r' to edit(), to get the redo command right. */
6763 invoke_edit(cap
, TRUE
, 'r', FALSE
);
6767 prep_redo(cap
->oap
->regname
, cap
->count1
,
6768 NUL
, 'r', NUL
, had_ctrl_v
, cap
->nchar
);
6770 curbuf
->b_op_start
= curwin
->w_cursor
;
6774 int old_State
= State
;
6776 if (cap
->ncharC1
!= 0)
6777 AppendCharToRedobuff(cap
->ncharC1
);
6778 if (cap
->ncharC2
!= 0)
6779 AppendCharToRedobuff(cap
->ncharC2
);
6781 /* This is slow, but it handles replacing a single-byte with a
6782 * multi-byte and the other way around. Also handles adding
6783 * composing characters for utf-8. */
6784 for (n
= cap
->count1
; n
> 0; --n
)
6787 ins_char(cap
->nchar
);
6789 if (cap
->ncharC1
!= 0)
6790 ins_char(cap
->ncharC1
);
6791 if (cap
->ncharC2
!= 0)
6792 ins_char(cap
->ncharC2
);
6799 * Replace the characters within one line.
6801 for (n
= cap
->count1
; n
> 0; --n
)
6804 * Get ptr again, because u_save and/or showmatch() will have
6805 * released the line. At the same time we let know that the
6806 * line will be changed.
6808 ptr
= ml_get_buf(curbuf
, curwin
->w_cursor
.lnum
, TRUE
);
6809 ptr
[curwin
->w_cursor
.col
] = cap
->nchar
;
6810 if (p_sm
&& msg_silent
== 0)
6811 showmatch(cap
->nchar
);
6812 ++curwin
->w_cursor
.col
;
6814 #ifdef FEAT_NETBEANS_INTG
6817 colnr_T start
= (colnr_T
)(curwin
->w_cursor
.col
- cap
->count1
);
6819 netbeans_removed(curbuf
, curwin
->w_cursor
.lnum
, start
,
6821 netbeans_inserted(curbuf
, curwin
->w_cursor
.lnum
, start
,
6822 &ptr
[start
], (int)cap
->count1
);
6826 /* mark the buffer as changed and prepare for displaying */
6827 changed_bytes(curwin
->w_cursor
.lnum
,
6828 (colnr_T
)(curwin
->w_cursor
.col
- cap
->count1
));
6830 --curwin
->w_cursor
.col
; /* cursor on the last replaced char */
6832 /* if the character on the left of the current cursor is a multi-byte
6833 * character, move two characters left */
6837 curbuf
->b_op_end
= curwin
->w_cursor
;
6838 curwin
->w_set_curswant
= TRUE
;
6839 set_last_insert(cap
->nchar
);
6845 * 'o': Exchange start and end of Visual area.
6846 * 'O': same, but in block mode exchange left and right corners.
6849 v_swap_corners(cmdchar
)
6853 colnr_T left
, right
;
6855 if (cmdchar
== 'O' && VIsual_mode
== Ctrl_V
)
6857 old_cursor
= curwin
->w_cursor
;
6858 getvcols(curwin
, &old_cursor
, &VIsual
, &left
, &right
);
6859 curwin
->w_cursor
.lnum
= VIsual
.lnum
;
6861 VIsual
= curwin
->w_cursor
;
6863 curwin
->w_cursor
.lnum
= old_cursor
.lnum
;
6864 curwin
->w_curswant
= right
;
6865 /* 'selection "exclusive" and cursor at right-bottom corner: move it
6866 * right one column */
6867 if (old_cursor
.lnum
>= VIsual
.lnum
&& *p_sel
== 'e')
6868 ++curwin
->w_curswant
;
6869 coladvance(curwin
->w_curswant
);
6870 if (curwin
->w_cursor
.col
== old_cursor
.col
6871 #ifdef FEAT_VIRTUALEDIT
6872 && (!virtual_active()
6873 || curwin
->w_cursor
.coladd
== old_cursor
.coladd
)
6877 curwin
->w_cursor
.lnum
= VIsual
.lnum
;
6878 if (old_cursor
.lnum
<= VIsual
.lnum
&& *p_sel
== 'e')
6881 VIsual
= curwin
->w_cursor
;
6883 curwin
->w_cursor
.lnum
= old_cursor
.lnum
;
6885 curwin
->w_curswant
= left
;
6890 old_cursor
= curwin
->w_cursor
;
6891 curwin
->w_cursor
= VIsual
;
6892 VIsual
= old_cursor
;
6893 curwin
->w_set_curswant
= TRUE
;
6896 #endif /* FEAT_VISUAL */
6899 * "R" (cap->arg is FALSE) and "gR" (cap->arg is TRUE).
6906 if (VIsual_active
) /* "R" is replace lines */
6915 if (!checkclearopq(cap
->oap
))
6917 if (!curbuf
->b_p_ma
)
6918 EMSG(_(e_modifiable
));
6921 #ifdef FEAT_VIRTUALEDIT
6922 if (virtual_active())
6923 coladvance(getviscol());
6925 invoke_edit(cap
, FALSE
, cap
->arg
? 'V' : 'R', FALSE
);
6930 #ifdef FEAT_VREPLACE
6942 cap
->nchar
= cap
->extra_char
;
6943 nv_replace(cap
); /* Do same as "r" in Visual mode for now */
6947 if (!checkclearopq(cap
->oap
))
6949 if (!curbuf
->b_p_ma
)
6950 EMSG(_(e_modifiable
));
6953 if (cap
->extra_char
== Ctrl_V
) /* get another character */
6954 cap
->extra_char
= get_literal();
6955 stuffcharReadbuff(cap
->extra_char
);
6956 stuffcharReadbuff(ESC
);
6957 # ifdef FEAT_VIRTUALEDIT
6958 if (virtual_active())
6959 coladvance(getviscol());
6961 invoke_edit(cap
, TRUE
, 'v', FALSE
);
6968 * Swap case for "~" command, when it does not work like an operator.
6977 #ifdef FEAT_NETBEANS_INTG
6983 if (checkclearopq(cap
->oap
))
6986 if (lineempty(curwin
->w_cursor
.lnum
) && vim_strchr(p_ww
, '~') == NULL
)
6988 clearopbeep(cap
->oap
);
6994 if (u_save_cursor() == FAIL
)
6997 startpos
= curwin
->w_cursor
;
6998 #ifdef FEAT_NETBEANS_INTG
7001 for (n
= cap
->count1
; n
> 0; --n
)
7003 did_change
|= swapchar(cap
->oap
->op_type
, &curwin
->w_cursor
);
7005 if (gchar_cursor() == NUL
)
7007 if (vim_strchr(p_ww
, '~') != NULL
7008 && curwin
->w_cursor
.lnum
< curbuf
->b_ml
.ml_line_count
)
7010 #ifdef FEAT_NETBEANS_INTG
7015 ptr
= ml_get(pos
.lnum
);
7016 count
= (int)STRLEN(ptr
) - pos
.col
;
7017 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
,
7019 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
,
7020 &ptr
[pos
.col
], count
);
7026 ++curwin
->w_cursor
.lnum
;
7027 curwin
->w_cursor
.col
= 0;
7030 if (u_savesub(curwin
->w_cursor
.lnum
) == FAIL
)
7039 #ifdef FEAT_NETBEANS_INTG
7040 if (did_change
&& usingNetbeans
)
7042 ptr
= ml_get(pos
.lnum
);
7043 count
= curwin
->w_cursor
.col
- pos
.col
;
7044 netbeans_removed(curbuf
, pos
.lnum
, pos
.col
, (long)count
);
7045 netbeans_inserted(curbuf
, pos
.lnum
, pos
.col
, &ptr
[pos
.col
], count
);
7051 curwin
->w_set_curswant
= TRUE
;
7054 changed_lines(startpos
.lnum
, startpos
.col
, curwin
->w_cursor
.lnum
+ 1,
7056 curbuf
->b_op_start
= startpos
;
7057 curbuf
->b_op_end
= curwin
->w_cursor
;
7058 if (curbuf
->b_op_end
.col
> 0)
7059 --curbuf
->b_op_end
.col
;
7064 * Move cursor to mark.
7067 nv_cursormark(cap
, flag
, pos
)
7072 if (check_mark(pos
) == FAIL
)
7076 if (cap
->cmdchar
== '\''
7077 || cap
->cmdchar
== '`'
7078 || cap
->cmdchar
== '['
7079 || cap
->cmdchar
== ']')
7081 curwin
->w_cursor
= *pos
;
7083 beginline(BL_WHITE
| BL_FIX
);
7087 cap
->oap
->motion_type
= flag
? MLINE
: MCHAR
;
7088 if (cap
->cmdchar
== '`')
7089 cap
->oap
->use_reg_one
= TRUE
;
7090 cap
->oap
->inclusive
= FALSE
; /* ignored if not MCHAR */
7091 curwin
->w_set_curswant
= TRUE
;
7096 * Handle commands that are operators in Visual mode.
7102 static char_u trans
[] = "YyDdCcxdXdAAIIrr";
7104 /* Uppercase means linewise, except in block mode, then "D" deletes till
7105 * the end of the line, and "C" replaces til EOL */
7106 if (isupper(cap
->cmdchar
))
7108 if (VIsual_mode
!= Ctrl_V
)
7110 else if (cap
->cmdchar
== 'C' || cap
->cmdchar
== 'D')
7111 curwin
->w_curswant
= MAXCOL
;
7113 cap
->cmdchar
= *(vim_strchr(trans
, cap
->cmdchar
) + 1);
7119 * "s" and "S" commands.
7126 if (VIsual_active
) /* "vs" and "vS" are the same as "vc" */
7128 if (cap
->cmdchar
== 'S')
7139 * Abbreviated commands.
7145 if (cap
->cmdchar
== K_DEL
|| cap
->cmdchar
== K_KDEL
)
7146 cap
->cmdchar
= 'x'; /* DEL key behaves like 'x' */
7149 /* in Visual mode these commands are operators */
7158 * Translate a command into another command.
7164 static char_u
*(ar
[8]) = {(char_u
*)"dl", (char_u
*)"dh",
7165 (char_u
*)"d$", (char_u
*)"c$",
7166 (char_u
*)"cl", (char_u
*)"cc",
7167 (char_u
*)"yy", (char_u
*)":s\r"};
7168 static char_u
*str
= (char_u
*)"xXDCsSY&";
7170 if (!checkclearopq(cap
->oap
))
7172 /* In Vi "2D" doesn't delete the next line. Can't translate it
7173 * either, because "2." should also not use the count. */
7174 if (cap
->cmdchar
== 'D' && vim_strchr(p_cpo
, CPO_HASH
) != NULL
)
7176 cap
->oap
->start
= curwin
->w_cursor
;
7177 cap
->oap
->op_type
= OP_DELETE
;
7182 AppendCharToRedobuff('D');
7187 stuffnumReadbuff(cap
->count0
);
7188 stuffReadbuff(ar
[(int)(vim_strchr(str
, cap
->cmdchar
) - str
)]);
7195 * "'" and "`" commands. Also for "g'" and "g`".
7196 * cap->arg is TRUE for "'" and "g'".
7205 linenr_T lnum
= curwin
->w_cursor
.lnum
;
7206 int old_KeyTyped
= KeyTyped
; /* getting file may reset it */
7209 if (cap
->cmdchar
== 'g')
7210 c
= cap
->extra_char
;
7213 pos
= getmark(c
, (cap
->oap
->op_type
== OP_NOP
));
7214 if (pos
== (pos_T
*)-1) /* jumped to other file */
7218 check_cursor_lnum();
7219 beginline(BL_WHITE
| BL_FIX
);
7225 nv_cursormark(cap
, cap
->arg
, pos
);
7227 #ifdef FEAT_VIRTUALEDIT
7228 /* May need to clear the coladd that a mark includes. */
7229 if (!virtual_active())
7230 curwin
->w_cursor
.coladd
= 0;
7233 if (cap
->oap
->op_type
== OP_NOP
7234 && (pos
== (pos_T
*)-1 || lnum
!= curwin
->w_cursor
.lnum
)
7235 && (fdo_flags
& FDO_MARK
)
7242 * Handle CTRL-O, CTRL-I, "g;" and "g," commands.
7248 #ifdef FEAT_JUMPLIST
7250 # ifdef FEAT_FOLDING
7251 linenr_T lnum
= curwin
->w_cursor
.lnum
;
7252 int old_KeyTyped
= KeyTyped
; /* getting file may reset it */
7255 if (!checkclearopq(cap
->oap
))
7257 if (cap
->cmdchar
== 'g')
7258 pos
= movechangelist((int)cap
->count1
);
7260 pos
= movemark((int)cap
->count1
);
7261 if (pos
== (pos_T
*)-1) /* jump to other file */
7263 curwin
->w_set_curswant
= TRUE
;
7266 else if (pos
!= NULL
) /* can jump */
7267 nv_cursormark(cap
, FALSE
, pos
);
7268 else if (cap
->cmdchar
== 'g')
7270 if (curbuf
->b_changelistlen
== 0)
7271 EMSG(_("E664: changelist is empty"));
7272 else if (cap
->count1
< 0)
7273 EMSG(_("E662: At start of changelist"));
7275 EMSG(_("E663: At end of changelist"));
7278 clearopbeep(cap
->oap
);
7279 # ifdef FEAT_FOLDING
7280 if (cap
->oap
->op_type
== OP_NOP
7281 && (pos
== (pos_T
*)-1 || lnum
!= curwin
->w_cursor
.lnum
)
7282 && (fdo_flags
& FDO_MARK
)
7288 clearopbeep(cap
->oap
);
7293 * Handle '"' command.
7299 if (checkclearop(cap
->oap
))
7302 if (cap
->nchar
== '=')
7303 cap
->nchar
= get_expr_register();
7305 if (cap
->nchar
!= NUL
&& valid_yank_reg(cap
->nchar
, FALSE
))
7307 cap
->oap
->regname
= cap
->nchar
;
7308 cap
->opcount
= cap
->count0
; /* remember count before '"' */
7310 set_reg_var(cap
->oap
->regname
);
7314 clearopbeep(cap
->oap
);
7319 * Handle "v", "V" and "CTRL-V" commands.
7320 * Also for "gh", "gH" and "g^H" commands: Always start Select mode, cap->arg
7322 * Handle CTRL-Q just like CTRL-V.
7328 if (cap
->cmdchar
== Ctrl_Q
)
7329 cap
->cmdchar
= Ctrl_V
;
7331 /* 'v', 'V' and CTRL-V can be used while an operator is pending to make it
7332 * characterwise, linewise, or blockwise. */
7333 if (cap
->oap
->op_type
!= OP_NOP
)
7335 cap
->oap
->motion_force
= cap
->cmdchar
;
7336 finish_op
= FALSE
; /* operator doesn't finish now but later */
7340 VIsual_select
= cap
->arg
;
7341 if (VIsual_active
) /* change Visual mode */
7343 if (VIsual_mode
== cap
->cmdchar
) /* stop visual mode */
7345 else /* toggle char/block mode */
7346 { /* or char/line mode */
7347 VIsual_mode
= cap
->cmdchar
;
7350 redraw_curbuf_later(INVERTED
); /* update the inversion */
7352 else /* start Visual mode */
7354 check_visual_highlight();
7355 if (cap
->count0
) /* use previously selected part */
7357 if (resel_VIsual_mode
== NUL
) /* there is none */
7362 VIsual
= curwin
->w_cursor
;
7364 VIsual_active
= TRUE
;
7365 VIsual_reselect
= TRUE
;
7367 /* start Select mode when 'selectmode' contains "cmd" */
7368 may_start_select('c');
7372 if (p_smd
&& msg_silent
== 0)
7373 redraw_cmdline
= TRUE
; /* show visual mode later */
7375 * For V and ^V, we multiply the number of lines even if there
7376 * was only one -- webb
7378 if (resel_VIsual_mode
!= 'v' || resel_VIsual_line_count
> 1)
7380 curwin
->w_cursor
.lnum
+=
7381 resel_VIsual_line_count
* cap
->count0
- 1;
7382 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
7383 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
7385 VIsual_mode
= resel_VIsual_mode
;
7386 if (VIsual_mode
== 'v')
7388 if (resel_VIsual_line_count
<= 1)
7389 curwin
->w_cursor
.col
+= resel_VIsual_col
* cap
->count0
- 1;
7391 curwin
->w_cursor
.col
= resel_VIsual_col
;
7394 if (resel_VIsual_col
== MAXCOL
)
7396 curwin
->w_curswant
= MAXCOL
;
7397 coladvance((colnr_T
)MAXCOL
);
7399 else if (VIsual_mode
== Ctrl_V
)
7402 curwin
->w_curswant
= curwin
->w_virtcol
7403 + resel_VIsual_col
* cap
->count0
- 1;
7404 coladvance(curwin
->w_curswant
);
7407 curwin
->w_set_curswant
= TRUE
;
7408 redraw_curbuf_later(INVERTED
); /* show the inversion */
7413 /* start Select mode when 'selectmode' contains "cmd" */
7414 may_start_select('c');
7415 n_start_visual_mode(cap
->cmdchar
);
7421 * Start selection for Shift-movement keys.
7426 /* if 'selectmode' contains "key", start Select mode */
7427 may_start_select('k');
7428 n_start_visual_mode('v');
7432 * Start Select mode, if "c" is in 'selectmode' and not in a mapping or menu.
7438 VIsual_select
= (stuff_empty() && typebuf_typed()
7439 && (vim_strchr(p_slm
, c
) != NULL
));
7443 * Start Visual mode "c".
7444 * Should set VIsual_select before calling this.
7447 n_start_visual_mode(c
)
7451 VIsual_active
= TRUE
;
7452 VIsual_reselect
= TRUE
;
7453 #ifdef FEAT_VIRTUALEDIT
7454 /* Corner case: the 0 position in a tab may change when going into
7455 * virtualedit. Recalculate curwin->w_cursor to avoid bad hilighting.
7457 if (c
== Ctrl_V
&& (ve_flags
& VE_BLOCK
) && gchar_cursor() == TAB
)
7458 coladvance(curwin
->w_virtcol
);
7460 VIsual
= curwin
->w_cursor
;
7469 if (p_smd
&& msg_silent
== 0)
7470 redraw_cmdline
= TRUE
; /* show visual mode later */
7471 #ifdef FEAT_CLIPBOARD
7472 /* Make sure the clipboard gets updated. Needed because start and
7473 * end may still be the same, and the selection needs to be owned */
7474 clip_star
.vmode
= NUL
;
7477 /* Only need to redraw this line, unless still need to redraw an old
7478 * Visual area (when 'lazyredraw' is set). */
7479 if (curwin
->w_redr_type
< INVERTED
)
7481 curwin
->w_old_cursor_lnum
= curwin
->w_cursor
.lnum
;
7482 curwin
->w_old_visual_lnum
= curwin
->w_cursor
.lnum
;
7486 #endif /* FEAT_VISUAL */
7489 * CTRL-W: Window commands
7496 if (!checkclearop(cap
->oap
))
7497 do_window(cap
->nchar
, cap
->count0
, NUL
); /* everything is in window.c */
7499 (void)checkclearop(cap
->oap
);
7513 end_visual_mode(); /* stop Visual mode */
7515 do_cmdline_cmd((char_u
*)"st");
7519 * Commands starting with "g".
7525 oparg_T
*oap
= cap
->oap
;
7536 * "g^A": dump log of used memory.
7539 vim_mem_profile_dump();
7543 #ifdef FEAT_VREPLACE
7545 * "gR": Enter virtual replace mode.
7558 do_cmdline_cmd((char_u
*)"%s//~/&");
7563 * "gv": Reselect the previous Visual area. If Visual already active,
7564 * exchange previous and current Visual area.
7567 if (checkclearop(oap
))
7570 if ( curbuf
->b_visual
.vi_start
.lnum
== 0
7571 || curbuf
->b_visual
.vi_start
.lnum
> curbuf
->b_ml
.ml_line_count
7572 || curbuf
->b_visual
.vi_end
.lnum
== 0)
7576 /* set w_cursor to the start of the Visual area, tpos to the end */
7580 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
7581 curbuf
->b_visual
.vi_mode
= i
;
7583 curbuf
->b_visual_mode_eval
= i
;
7585 i
= curwin
->w_curswant
;
7586 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
7587 curbuf
->b_visual
.vi_curswant
= i
;
7589 tpos
= curbuf
->b_visual
.vi_end
;
7590 curbuf
->b_visual
.vi_end
= curwin
->w_cursor
;
7591 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
7592 curbuf
->b_visual
.vi_start
= VIsual
;
7596 VIsual_mode
= curbuf
->b_visual
.vi_mode
;
7597 curwin
->w_curswant
= curbuf
->b_visual
.vi_curswant
;
7598 tpos
= curbuf
->b_visual
.vi_end
;
7599 curwin
->w_cursor
= curbuf
->b_visual
.vi_start
;
7602 VIsual_active
= TRUE
;
7603 VIsual_reselect
= TRUE
;
7605 /* Set Visual to the start and w_cursor to the end of the Visual
7606 * area. Make sure they are on an existing character. */
7608 VIsual
= curwin
->w_cursor
;
7609 curwin
->w_cursor
= tpos
;
7613 * When called from normal "g" command: start Select mode when
7614 * 'selectmode' contains "cmd". When called for K_SELECT, always
7615 * start Select mode.
7618 VIsual_select
= TRUE
;
7620 may_start_select('c');
7624 #ifdef FEAT_CLIPBOARD
7625 /* Make sure the clipboard gets updated. Needed because start and
7626 * end are still the same, and the selection needs to be owned */
7627 clip_star
.vmode
= NUL
;
7629 redraw_curbuf_later(INVERTED
);
7634 * "gV": Don't reselect the previous Visual area after a Select mode
7638 VIsual_reselect
= FALSE
;
7642 * "gh": start Select mode.
7643 * "gH": start Select line mode.
7644 * "g^H": start Select block mode.
7647 cap
->nchar
= Ctrl_H
;
7653 /* EBCDIC: 'v'-'h' != '^v'-'^h' */
7654 if (cap
->nchar
== Ctrl_H
)
7655 cap
->cmdchar
= Ctrl_V
;
7658 cap
->cmdchar
= cap
->nchar
+ ('v' - 'h');
7662 #endif /* FEAT_VISUAL */
7665 * "gj" and "gk" two new funny movement keys -- up and down
7666 * movement based on *screen* line rather than *file* line.
7670 /* with 'nowrap' it works just like the normal "j" command; also when
7671 * in a closed fold */
7672 if (!curwin
->w_p_wrap
7674 || hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
)
7678 oap
->motion_type
= MLINE
;
7679 i
= cursor_down(cap
->count1
, oap
->op_type
== OP_NOP
);
7682 i
= nv_screengo(oap
, FORWARD
, cap
->count1
);
7689 /* with 'nowrap' it works just like the normal "k" command; also when
7690 * in a closed fold */
7691 if (!curwin
->w_p_wrap
7693 || hasFolding(curwin
->w_cursor
.lnum
, NULL
, NULL
)
7697 oap
->motion_type
= MLINE
;
7698 i
= cursor_up(cap
->count1
, oap
->op_type
== OP_NOP
);
7701 i
= nv_screengo(oap
, BACKWARD
, cap
->count1
);
7707 * "gJ": join two lines without inserting a space.
7714 * "g0", "g^" and "g$": Like "0", "^" and "$" but for screen lines.
7715 * "gm": middle of "g0" and "g$".
7725 oap
->motion_type
= MCHAR
;
7726 oap
->inclusive
= FALSE
;
7727 if (curwin
->w_p_wrap
7728 #ifdef FEAT_VERTSPLIT
7729 && curwin
->w_width
!= 0
7733 int width1
= W_WIDTH(curwin
) - curwin_col_off();
7734 int width2
= width1
+ curwin_col_off2();
7738 if (curwin
->w_virtcol
>= (colnr_T
)width1
&& width2
> 0)
7739 i
= (curwin
->w_virtcol
- width1
) / width2
* width2
+ width1
;
7742 i
= curwin
->w_leftcol
;
7743 /* Go to the middle of the screen line. When 'number' is on and lines
7744 * are wrapping the middle can be more to the left.*/
7745 if (cap
->nchar
== 'm')
7746 i
+= (W_WIDTH(curwin
) - curwin_col_off()
7747 + ((curwin
->w_p_wrap
&& i
> 0)
7748 ? curwin_col_off2() : 0)) / 2;
7749 coladvance((colnr_T
)i
);
7754 while (vim_iswhite(i
) && oneright() == OK
);
7756 curwin
->w_set_curswant
= TRUE
;
7760 /* "g_": to the last non-blank character in the line or <count> lines
7762 cap
->oap
->motion_type
= MCHAR
;
7763 cap
->oap
->inclusive
= TRUE
;
7764 curwin
->w_curswant
= MAXCOL
;
7765 if (cursor_down((long)(cap
->count1
- 1),
7766 cap
->oap
->op_type
== OP_NOP
) == FAIL
)
7767 clearopbeep(cap
->oap
);
7770 char_u
*ptr
= ml_get_curline();
7772 /* In Visual mode we may end up after the line. */
7773 if (curwin
->w_cursor
.col
> 0 && ptr
[curwin
->w_cursor
.col
] == NUL
)
7774 --curwin
->w_cursor
.col
;
7776 /* Decrease the cursor column until it's on a non-blank. */
7777 while (curwin
->w_cursor
.col
> 0
7778 && vim_iswhite(ptr
[curwin
->w_cursor
.col
]))
7779 --curwin
->w_cursor
.col
;
7780 curwin
->w_set_curswant
= TRUE
;
7788 int col_off
= curwin_col_off();
7790 oap
->motion_type
= MCHAR
;
7791 oap
->inclusive
= TRUE
;
7792 if (curwin
->w_p_wrap
7793 #ifdef FEAT_VERTSPLIT
7794 && curwin
->w_width
!= 0
7798 curwin
->w_curswant
= MAXCOL
; /* so we stay at the end */
7799 if (cap
->count1
== 1)
7801 int width1
= W_WIDTH(curwin
) - col_off
;
7802 int width2
= width1
+ curwin_col_off2();
7806 if (curwin
->w_virtcol
>= (colnr_T
)width1
)
7807 i
+= ((curwin
->w_virtcol
- width1
) / width2
+ 1)
7809 coladvance((colnr_T
)i
);
7810 #if defined(FEAT_LINEBREAK) || defined(FEAT_MBYTE)
7811 if (curwin
->w_cursor
.col
> 0 && curwin
->w_p_wrap
)
7814 * Check for landing on a character that got split at
7815 * the end of the line. We do not want to advance to
7816 * the next screen line.
7819 if (curwin
->w_virtcol
> (colnr_T
)i
)
7820 --curwin
->w_cursor
.col
;
7824 else if (nv_screengo(oap
, FORWARD
, cap
->count1
- 1) == FAIL
)
7829 i
= curwin
->w_leftcol
+ W_WIDTH(curwin
) - col_off
- 1;
7830 coladvance((colnr_T
)i
);
7832 /* Make sure we stick in this column. */
7834 curwin
->w_curswant
= curwin
->w_virtcol
;
7835 curwin
->w_set_curswant
= FALSE
;
7841 * "g*" and "g#", like "*" and "#" but without using "\<" and "\>"
7846 case POUND
: /* pound sign (sometimes equal to '#') */
7848 case Ctrl_RSB
: /* :tag or :tselect for current identifier */
7849 case ']': /* :tselect for current identifier */
7854 * ge and gE: go back to end of word
7858 oap
->motion_type
= MCHAR
;
7859 curwin
->w_set_curswant
= TRUE
;
7860 oap
->inclusive
= TRUE
;
7861 if (bckend_word(cap
->count1
, cap
->nchar
== 'E', FALSE
) == FAIL
)
7866 * "g CTRL-G": display info about cursor position
7873 * "gi": start Insert at the last position.
7876 if (curbuf
->b_last_insert
.lnum
!= 0)
7878 curwin
->w_cursor
= curbuf
->b_last_insert
;
7879 check_cursor_lnum();
7880 i
= (int)STRLEN(ml_get_curline());
7881 if (curwin
->w_cursor
.col
> (colnr_T
)i
)
7883 #ifdef FEAT_VIRTUALEDIT
7884 if (virtual_active())
7885 curwin
->w_cursor
.coladd
+= curwin
->w_cursor
.col
- i
;
7887 curwin
->w_cursor
.col
= i
;
7895 * "gI": Start insert in column 1.
7899 if (!checkclearopq(oap
))
7900 invoke_edit(cap
, FALSE
, 'g', FALSE
);
7903 #ifdef FEAT_SEARCHPATH
7905 * "gf": goto file, edit file under cursor
7906 * "]f" and "[f": can also be used.
7914 /* "g'm" and "g`m": jump to mark without setting pcmark */
7926 do_sleep(cap
->count1
* 1000L);
7930 * "ga": Display the ascii value of the character under the
7931 * cursor. It is displayed in decimal, hex, and octal. -- webb
7939 * "g8": Display the bytes used for the UTF-8 character under the
7940 * cursor. It is displayed in hex.
7941 * "8g8" finds illegal byte sequence.
7944 if (cap
->count0
== 8)
7956 * "gg": Goto the first line in file. With a count it goes to
7957 * that line number like for "G". -- webb
7965 * Two-character operators:
7967 * "gw" Format text and keep cursor position
7968 * "g~" Toggle the case of the text.
7969 * "gu" Change text to lower case.
7970 * "gU" Change text to upper case.
7971 * "g?" rot13 encoding
7972 * "g@" call 'operatorfunc'
7976 oap
->cursor_start
= curwin
->w_cursor
;
7987 * "gd": Find first occurrence of pattern under the cursor in the
7989 * "gD": idem, but in the current file.
7993 nv_gd(oap
, cap
->nchar
, (int)cap
->count0
);
7998 * g<*Mouse> : <C-*mouse>
8002 case K_MIDDLERELEASE
:
8008 case K_RIGHTRELEASE
:
8015 mod_mask
= MOD_MASK_CTRL
;
8016 (void)do_mouse(oap
, cap
->nchar
, BACKWARD
, cap
->count1
, 0);
8024 * "gP" and "gp": same as "P" and "p" but leave cursor just after new text
8032 /* "go": goto byte count from start of buffer */
8034 goto_byte(cap
->count0
);
8038 /* "gQ": improved Ex mode */
8042 clearopbeep(cap
->oap
);
8047 if (!checkclearopq(oap
))
8051 #ifdef FEAT_JUMPLIST
8057 cap
->count1
= -cap
->count1
;
8064 goto_tabpage((int)cap
->count0
);
8067 goto_tabpage(-(int)cap
->count1
);
8072 case '-': /* "g+" and "g-": undo or redo along the timeline */
8073 if (!checkclearopq(oap
))
8074 undo_time(cap
->nchar
== '-' ? -cap
->count1
: cap
->count1
,
8085 * Handle "o" and "O" commands.
8091 if (!checkclearopq(cap
->oap
))
8094 if (cap
->cmdchar
== 'O')
8095 /* Open above the first line of a folded sequence of lines */
8096 (void)hasFolding(curwin
->w_cursor
.lnum
,
8097 &curwin
->w_cursor
.lnum
, NULL
);
8099 /* Open below the last line of a folded sequence of lines */
8100 (void)hasFolding(curwin
->w_cursor
.lnum
,
8101 NULL
, &curwin
->w_cursor
.lnum
);
8103 if (u_save((linenr_T
)(curwin
->w_cursor
.lnum
-
8104 (cap
->cmdchar
== 'O' ? 1 : 0)),
8105 (linenr_T
)(curwin
->w_cursor
.lnum
+
8106 (cap
->cmdchar
== 'o' ? 1 : 0))
8108 && open_line(cap
->cmdchar
== 'O' ? BACKWARD
: FORWARD
,
8109 #ifdef FEAT_COMMENTS
8110 has_format_option(FO_OPEN_COMS
) ? OPENLINE_DO_COM
:
8114 /* When '#' is in 'cpoptions' ignore the count. */
8115 if (vim_strchr(p_cpo
, CPO_HASH
) != NULL
)
8117 invoke_edit(cap
, FALSE
, cap
->cmdchar
, TRUE
);
8123 * "." command: redo last change.
8129 if (!checkclearopq(cap
->oap
))
8132 * If "restart_edit" is TRUE, the last but one command is repeated
8133 * instead of the last command (inserting text). This is used for
8134 * CTRL-O <.> in insert mode.
8136 if (start_redo(cap
->count0
, restart_edit
!= 0 && !arrow_used
) == FAIL
)
8137 clearopbeep(cap
->oap
);
8148 if (!checkclearopq(cap
->oap
))
8150 u_redo((int)cap
->count1
);
8151 curwin
->w_set_curswant
= TRUE
;
8156 * Handle "U" command.
8162 /* In Visual mode and typing "gUU" triggers an operator */
8163 if (cap
->oap
->op_type
== OP_UPPER
8169 /* translate "gUU" to "gUgU" */
8174 else if (!checkclearopq(cap
->oap
))
8177 curwin
->w_set_curswant
= TRUE
;
8182 * '~' command: If tilde is not an operator and Visual is off: swap case of a
8193 && cap
->oap
->op_type
!= OP_TILDE
)
8200 * Handle an operator command.
8201 * The actual work is done by do_pending_operator().
8209 op_type
= get_op_type(cap
->cmdchar
, cap
->nchar
);
8211 if (op_type
== cap
->oap
->op_type
) /* double operator works on lines */
8213 else if (!checkclearop(cap
->oap
))
8215 cap
->oap
->start
= curwin
->w_cursor
;
8216 cap
->oap
->op_type
= op_type
;
8221 * Handle linewise operator "dd", "yy", etc.
8223 * "_" is is a strange motion command that helps make operators more logical.
8224 * It is actually implemented, but not documented in the real Vi. This motion
8225 * command actually refers to "the current line". Commands like "dd" and "yy"
8226 * are really an alternate form of "d_" and "y_". It does accept a count, so
8227 * "d3_" works to delete 3 lines.
8233 cap
->oap
->motion_type
= MLINE
;
8234 if (cursor_down(cap
->count1
- 1L, cap
->oap
->op_type
== OP_NOP
) == FAIL
)
8235 clearopbeep(cap
->oap
);
8236 else if ( cap
->oap
->op_type
== OP_DELETE
8237 || cap
->oap
->op_type
== OP_LSHIFT
8238 || cap
->oap
->op_type
== OP_RSHIFT
)
8239 beginline(BL_SOL
| BL_FIX
);
8240 else if (cap
->oap
->op_type
!= OP_YANK
) /* 'Y' does not move cursor */
8241 beginline(BL_WHITE
| BL_FIX
);
8251 /* CTRL-HOME is like "gg" */
8252 if (mod_mask
& MOD_MASK_CTRL
)
8259 ins_at_eol
= FALSE
; /* Don't move cursor past eol (only necessary in a
8260 one-character line). */
8270 cap
->oap
->motion_type
= MCHAR
;
8271 cap
->oap
->inclusive
= FALSE
;
8273 if (cap
->count0
> 0)
8275 coladvance((colnr_T
)(cap
->count0
- 1));
8276 curwin
->w_curswant
= (colnr_T
)(cap
->count0
- 1);
8279 curwin
->w_curswant
= 0;
8280 /* keep curswant at the column where we wanted to go, not where
8281 we ended; differs if line is too short */
8282 curwin
->w_set_curswant
= FALSE
;
8286 * Handle back-word command "b" and "B".
8287 * cap->arg is 1 for "B"
8293 cap
->oap
->motion_type
= MCHAR
;
8294 cap
->oap
->inclusive
= FALSE
;
8295 curwin
->w_set_curswant
= TRUE
;
8296 if (bck_word(cap
->count1
, cap
->arg
, FALSE
) == FAIL
)
8297 clearopbeep(cap
->oap
);
8299 else if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8305 * Handle word motion commands "e", "E", "w" and "W".
8306 * cap->arg is TRUE for "E" and "W".
8317 * Set inclusive for the "E" and "e" command.
8319 if (cap
->cmdchar
== 'e' || cap
->cmdchar
== 'E')
8323 cap
->oap
->inclusive
= word_end
;
8326 * "cw" and "cW" are a special case.
8328 if (!word_end
&& cap
->oap
->op_type
== OP_CHANGE
)
8331 if (n
!= NUL
) /* not an empty line */
8336 * Reproduce a funny Vi behaviour: "cw" on a blank only
8337 * changes one character, not all blanks until the start of
8338 * the next word. Only do this when the 'w' flag is included
8341 if (cap
->count1
== 1 && vim_strchr(p_cpo
, CPO_CW
) != NULL
)
8343 cap
->oap
->inclusive
= TRUE
;
8344 cap
->oap
->motion_type
= MCHAR
;
8351 * This is a little strange. To match what the real Vi does,
8352 * we effectively map 'cw' to 'ce', and 'cW' to 'cE', provided
8353 * that we are not on a space or a TAB. This seems impolite
8354 * at first, but it's really more what we mean when we say
8356 * Another strangeness: When standing on the end of a word
8357 * "ce" will change until the end of the next wordt, but "cw"
8358 * will change only one character! This is done by setting
8361 cap
->oap
->inclusive
= TRUE
;
8368 cap
->oap
->motion_type
= MCHAR
;
8369 curwin
->w_set_curswant
= TRUE
;
8371 n
= end_word(cap
->count1
, cap
->arg
, flag
, FALSE
);
8373 n
= fwd_word(cap
->count1
, cap
->arg
, cap
->oap
->op_type
!= OP_NOP
);
8375 /* Don't leave the cursor on the NUL past a line */
8376 if (n
!= FAIL
&& curwin
->w_cursor
.col
> 0 && gchar_cursor() == NUL
)
8378 --curwin
->w_cursor
.col
;
8379 cap
->oap
->inclusive
= TRUE
;
8382 if (n
== FAIL
&& cap
->oap
->op_type
== OP_NOP
)
8383 clearopbeep(cap
->oap
);
8387 adjust_for_sel(cap
);
8390 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8397 * "0" and "^" commands.
8398 * cap->arg is the argument for beginline().
8404 cap
->oap
->motion_type
= MCHAR
;
8405 cap
->oap
->inclusive
= FALSE
;
8406 beginline(cap
->arg
);
8408 if ((fdo_flags
& FDO_HOR
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8411 ins_at_eol
= FALSE
; /* Don't move cursor past eol (only necessary in a
8412 one-character line). */
8417 * In exclusive Visual mode, may include the last character.
8423 if (VIsual_active
&& cap
->oap
->inclusive
&& *p_sel
== 'e'
8424 && gchar_cursor() != NUL
&& lt(VIsual
, curwin
->w_cursor
))
8431 ++curwin
->w_cursor
.col
;
8432 cap
->oap
->inclusive
= FALSE
;
8437 * Exclude last character at end of Visual area for 'selection' == "exclusive".
8438 * Should check VIsual_mode before calling this.
8439 * Returns TRUE when backed up to the previous line.
8446 if (*p_sel
== 'e' && !equalpos(VIsual
, curwin
->w_cursor
))
8448 if (lt(VIsual
, curwin
->w_cursor
))
8449 pp
= &curwin
->w_cursor
;
8452 #ifdef FEAT_VIRTUALEDIT
8464 else if (pp
->lnum
> 1)
8467 pp
->col
= (colnr_T
)STRLEN(ml_get(pp
->lnum
));
8475 * SELECT key in Normal or Visual mode: end of Select mode mapping.
8482 VIsual_select
= TRUE
;
8483 else if (VIsual_reselect
)
8485 cap
->nchar
= 'v'; /* fake "gv" command */
8494 * "G", "gg", CTRL-END, CTRL-HOME.
8495 * cap->arg is TRUE for "G".
8504 lnum
= curbuf
->b_ml
.ml_line_count
;
8507 cap
->oap
->motion_type
= MLINE
;
8510 /* When a count is given, use it instead of the default lnum */
8511 if (cap
->count0
!= 0)
8515 else if (lnum
> curbuf
->b_ml
.ml_line_count
)
8516 lnum
= curbuf
->b_ml
.ml_line_count
;
8517 curwin
->w_cursor
.lnum
= lnum
;
8518 beginline(BL_SOL
| BL_FIX
);
8520 if ((fdo_flags
& FDO_JUMP
) && KeyTyped
&& cap
->oap
->op_type
== OP_NOP
)
8526 * CTRL-\ in Normal mode.
8532 if (cap
->nchar
== Ctrl_N
|| cap
->nchar
== Ctrl_G
)
8535 if (restart_edit
!= 0 && mode_displayed
)
8536 clear_cmdline
= TRUE
; /* unshow mode later */
8539 if (cmdwin_type
!= 0)
8540 cmdwin_result
= Ctrl_C
;
8545 end_visual_mode(); /* stop Visual */
8546 redraw_curbuf_later(INVERTED
);
8549 /* CTRL-\ CTRL-G restarts Insert mode when 'insertmode' is set. */
8550 if (cap
->nchar
== Ctrl_G
&& p_im
)
8554 clearopbeep(cap
->oap
);
8558 * ESC in Normal mode: beep, but don't flush buffers.
8559 * Don't even beep if we are canceling a command.
8567 no_reason
= (cap
->oap
->op_type
== OP_NOP
8568 && cap
->opcount
== 0
8570 && cap
->oap
->regname
== 0
8573 if (cap
->arg
) /* TRUE for CTRL-C */
8575 if (restart_edit
== 0
8583 MSG(_("Type :quit<Enter> to exit Vim"));
8585 /* Don't reset "restart_edit" when 'insertmode' is set, it won't be
8586 * set again below when halfway a mapping. */
8590 if (cmdwin_type
!= 0)
8592 cmdwin_result
= K_IGNORE
;
8593 got_int
= FALSE
; /* don't stop executing autocommands et al. */
8602 end_visual_mode(); /* stop Visual */
8603 check_cursor_col(); /* make sure cursor is not beyond EOL */
8604 curwin
->w_set_curswant
= TRUE
;
8605 redraw_curbuf_later(INVERTED
);
8613 /* A CTRL-C is often used at the start of a menu. When 'insertmode' is
8614 * set return to Insert mode afterwards. */
8615 if (restart_edit
== 0 && goto_im()
8616 #ifdef FEAT_EX_EXTRA
8617 && ex_normal_busy
== 0
8624 * Handle "A", "a", "I", "i" and <Insert> commands.
8630 /* <Insert> is equal to "i" */
8631 if (cap
->cmdchar
== K_INS
|| cap
->cmdchar
== K_KINS
)
8635 /* in Visual mode "A" and "I" are an operator */
8636 if (VIsual_active
&& (cap
->cmdchar
== 'A' || cap
->cmdchar
== 'I'))
8639 /* in Visual mode and after an operator "a" and "i" are for text objects */
8642 if ((cap
->cmdchar
== 'a' || cap
->cmdchar
== 'i')
8643 && (cap
->oap
->op_type
!= OP_NOP
8652 clearopbeep(cap
->oap
);
8655 else if (!curbuf
->b_p_ma
&& !p_im
)
8657 /* Only give this error when 'insertmode' is off. */
8658 EMSG(_(e_modifiable
));
8661 else if (!checkclearopq(cap
->oap
))
8663 switch (cap
->cmdchar
)
8665 case 'A': /* "A"ppend after the line */
8666 curwin
->w_set_curswant
= TRUE
;
8667 #ifdef FEAT_VIRTUALEDIT
8668 if (ve_flags
== VE_ALL
)
8670 int save_State
= State
;
8672 /* Pretent Insert mode here to allow the cursor on the
8673 * character past the end of the line */
8675 coladvance((colnr_T
)MAXCOL
);
8680 curwin
->w_cursor
.col
+= (colnr_T
)STRLEN(ml_get_cursor());
8683 case 'I': /* "I"nsert before the first non-blank */
8684 if (vim_strchr(p_cpo
, CPO_INSEND
) == NULL
)
8685 beginline(BL_WHITE
);
8687 beginline(BL_WHITE
|BL_FIX
);
8690 case 'a': /* "a"ppend is like "i"nsert on the next character. */
8691 #ifdef FEAT_VIRTUALEDIT
8692 /* increment coladd when in virtual space, increment the
8693 * column otherwise, also to append after an unprintable char */
8694 if (virtual_active()
8695 && (curwin
->w_cursor
.coladd
> 0
8696 || *ml_get_cursor() == NUL
8697 || *ml_get_cursor() == TAB
))
8698 curwin
->w_cursor
.coladd
++;
8701 if (*ml_get_cursor() != NUL
)
8706 #ifdef FEAT_VIRTUALEDIT
8707 if (curwin
->w_cursor
.coladd
&& cap
->cmdchar
!= 'A')
8709 int save_State
= State
;
8711 /* Pretent Insert mode here to allow the cursor on the
8712 * character past the end of the line */
8714 coladvance(getviscol());
8719 invoke_edit(cap
, FALSE
, cap
->cmdchar
, FALSE
);
8724 * Invoke edit() and take care of "restart_edit" and the return value.
8727 invoke_edit(cap
, repl
, cmd
, startln
)
8729 int repl
; /* "r" or "gr" command */
8733 int restart_edit_save
= 0;
8735 /* Complicated: When the user types "a<C-O>a" we don't want to do Insert
8736 * mode recursively. But when doing "a<C-O>." or "a<C-O>rx" we do allow
8738 if (repl
|| !stuff_empty())
8739 restart_edit_save
= restart_edit
;
8741 restart_edit_save
= 0;
8743 /* Always reset "restart_edit", this is not a restarted edit. */
8746 if (edit(cmd
, startln
, cap
->count1
))
8747 cap
->retval
|= CA_COMMAND_BUSY
;
8749 if (restart_edit
== 0)
8750 restart_edit
= restart_edit_save
;
8755 * "a" or "i" while an operator is pending or in Visual mode: object motion.
8765 if (cap
->cmdchar
== 'i')
8766 include
= FALSE
; /* "ix" = inner object: exclude white space */
8768 include
= TRUE
; /* "ax" = an object: include white space */
8770 /* Make sure (), [], {} and <> are in 'matchpairs' */
8771 mps_save
= curbuf
->b_p_mps
;
8772 curbuf
->b_p_mps
= (char_u
*)"(:),{:},[:],<:>";
8776 case 'w': /* "aw" = a word */
8777 flag
= current_word(cap
->oap
, cap
->count1
, include
, FALSE
);
8779 case 'W': /* "aW" = a WORD */
8780 flag
= current_word(cap
->oap
, cap
->count1
, include
, TRUE
);
8782 case 'b': /* "ab" = a braces block */
8785 flag
= current_block(cap
->oap
, cap
->count1
, include
, '(', ')');
8787 case 'B': /* "aB" = a Brackets block */
8790 flag
= current_block(cap
->oap
, cap
->count1
, include
, '{', '}');
8792 case '[': /* "a[" = a [] block */
8794 flag
= current_block(cap
->oap
, cap
->count1
, include
, '[', ']');
8796 case '<': /* "a<" = a <> block */
8798 flag
= current_block(cap
->oap
, cap
->count1
, include
, '<', '>');
8800 case 't': /* "at" = a tag block (xml and html) */
8801 flag
= current_tagblock(cap
->oap
, cap
->count1
, include
);
8803 case 'p': /* "ap" = a paragraph */
8804 flag
= current_par(cap
->oap
, cap
->count1
, include
, 'p');
8806 case 's': /* "as" = a sentence */
8807 flag
= current_sent(cap
->oap
, cap
->count1
, include
);
8809 case '"': /* "a"" = a double quoted string */
8810 case '\'': /* "a'" = a single quoted string */
8811 case '`': /* "a`" = a backtick quoted string */
8812 flag
= current_quote(cap
->oap
, cap
->count1
, include
,
8816 case 'S': /* "aS" = a section */
8817 case 'f': /* "af" = a filename */
8818 case 'u': /* "au" = a URL */
8825 curbuf
->b_p_mps
= mps_save
;
8827 clearopbeep(cap
->oap
);
8828 adjust_cursor_col();
8829 curwin
->w_set_curswant
= TRUE
;
8834 * "q" command: Start/stop recording.
8835 * "q:", "q/", "q?": edit command-line in command-line window.
8841 if (cap
->oap
->op_type
== OP_FORMAT
)
8843 /* "gqq" is the same as "gqgq": format line */
8848 else if (!checkclearop(cap
->oap
))
8851 if (cap
->nchar
== ':' || cap
->nchar
== '/' || cap
->nchar
== '?')
8853 stuffcharReadbuff(cap
->nchar
);
8854 stuffcharReadbuff(K_CMDWIN
);
8858 /* (stop) recording into a named register, unless executing a
8860 if (!Exec_reg
&& do_record(cap
->nchar
) == FAIL
)
8861 clearopbeep(cap
->oap
);
8866 * Handle the "@r" command.
8872 if (checkclearop(cap
->oap
))
8875 if (cap
->nchar
== '=')
8877 if (get_expr_register() == NUL
)
8881 while (cap
->count1
-- && !got_int
)
8883 if (do_execreg(cap
->nchar
, FALSE
, FALSE
, FALSE
) == FAIL
)
8885 clearopbeep(cap
->oap
);
8893 * Handle the CTRL-U and CTRL-D commands.
8899 if ((cap
->cmdchar
== Ctrl_U
&& curwin
->w_cursor
.lnum
== 1)
8900 || (cap
->cmdchar
== Ctrl_D
8901 && curwin
->w_cursor
.lnum
== curbuf
->b_ml
.ml_line_count
))
8902 clearopbeep(cap
->oap
);
8903 else if (!checkclearop(cap
->oap
))
8904 halfpage(cap
->cmdchar
== Ctrl_D
, cap
->count0
);
8908 * Handle "J" or "gJ" command.
8915 if (VIsual_active
) /* join the visual lines */
8919 if (!checkclearop(cap
->oap
))
8921 if (cap
->count0
<= 1)
8922 cap
->count0
= 2; /* default for join is two lines! */
8923 if (curwin
->w_cursor
.lnum
+ cap
->count0
- 1 >
8924 curbuf
->b_ml
.ml_line_count
)
8925 clearopbeep(cap
->oap
); /* beyond last line */
8928 prep_redo(cap
->oap
->regname
, cap
->count0
,
8929 NUL
, cap
->cmdchar
, NUL
, NUL
, cap
->nchar
);
8930 do_do_join(cap
->count0
, cap
->nchar
== NUL
);
8936 * "P", "gP", "p" and "gp" commands.
8944 void *reg1
= NULL
, *reg2
= NULL
;
8946 int was_visual
= FALSE
;
8951 if (cap
->oap
->op_type
!= OP_NOP
)
8954 /* "dp" is ":diffput" */
8955 if (cap
->oap
->op_type
== OP_DELETE
&& cap
->cmdchar
== 'p')
8958 nv_diffgetput(TRUE
);
8962 clearopbeep(cap
->oap
);
8966 dir
= (cap
->cmdchar
== 'P'
8967 || (cap
->cmdchar
== 'g' && cap
->nchar
== 'P'))
8968 ? BACKWARD
: FORWARD
;
8970 if (cap
->cmdchar
== 'g')
8971 flags
|= PUT_CURSEND
;
8976 /* Putting in Visual mode: The put text replaces the selected
8977 * text. First delete the selected text, then put the new text.
8978 * Need to save and restore the registers that the delete
8979 * overwrites if the old contents is being put.
8982 regname
= cap
->oap
->regname
;
8983 # ifdef FEAT_CLIPBOARD
8984 adjust_clip_reg(®name
);
8986 if (regname
== 0 || VIM_ISDIGIT(regname
)
8987 # ifdef FEAT_CLIPBOARD
8988 || (clip_unnamed
&& (regname
== '*' || regname
== '+'))
8993 /* the delete is going to overwrite the register we want to
8994 * put, save it first. */
8995 reg1
= get_register(regname
, TRUE
);
8998 /* Now delete the selected text. */
9001 cap
->oap
->regname
= NUL
;
9003 do_pending_operator(cap
, 0, FALSE
);
9004 empty
= (curbuf
->b_ml
.ml_flags
& ML_EMPTY
);
9006 /* delete PUT_LINE_BACKWARD; */
9007 cap
->oap
->regname
= regname
;
9011 /* Delete probably changed the register we want to put, save
9012 * it first. Then put back what was there before the delete. */
9013 reg2
= get_register(regname
, FALSE
);
9014 put_register(regname
, reg1
);
9017 /* When deleted a linewise Visual area, put the register as
9018 * lines to avoid it joined with the next line. When deletion was
9019 * characterwise, split a line when putting lines. */
9020 if (VIsual_mode
== 'V')
9022 else if (VIsual_mode
== 'v')
9023 flags
|= PUT_LINE_SPLIT
;
9024 if (VIsual_mode
== Ctrl_V
&& dir
== FORWARD
)
9025 flags
|= PUT_LINE_FORWARD
;
9027 if ((VIsual_mode
!= 'V'
9028 && curwin
->w_cursor
.col
< curbuf
->b_op_start
.col
)
9029 || (VIsual_mode
== 'V'
9030 && curwin
->w_cursor
.lnum
< curbuf
->b_op_start
.lnum
))
9031 /* cursor is at the end of the line or end of file, put
9036 do_put(cap
->oap
->regname
, dir
, cap
->count1
, flags
);
9039 /* If a register was saved, put it back now. */
9041 put_register(regname
, reg2
);
9043 /* What to reselect with "gv"? Selecting the just put text seems to
9044 * be the most useful, since the original text was removed. */
9047 curbuf
->b_visual
.vi_start
= curbuf
->b_op_start
;
9048 curbuf
->b_visual
.vi_end
= curbuf
->b_op_end
;
9051 /* When all lines were selected and deleted do_put() leaves an empty
9052 * line that needs to be deleted now. */
9053 if (empty
&& *ml_get(curbuf
->b_ml
.ml_line_count
) == NUL
)
9055 ml_delete(curbuf
->b_ml
.ml_line_count
, TRUE
);
9057 /* If the cursor was in that line, move it to the end of the last
9059 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
9061 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
9062 coladvance((colnr_T
)MAXCOL
);
9066 auto_format(FALSE
, TRUE
);
9071 * "o" and "O" commands.
9078 /* "do" is ":diffget" */
9079 if (cap
->oap
->op_type
== OP_DELETE
&& cap
->cmdchar
== 'o')
9082 nv_diffgetput(FALSE
);
9087 if (VIsual_active
) /* switch start and end of visual */
9088 v_swap_corners(cap
->cmdchar
);
9100 ProcessSniffRequests();
9104 #ifdef FEAT_NETBEANS_INTG
9109 netbeans_keycommand(cap
->nchar
);
9119 do_put('~', BACKWARD
, 1L, PUT_CURSEND
);
9125 * Trigger CursorHold event.
9126 * When waiting for a character for 'updatetime' K_CURSORHOLD is put in the
9127 * input buffer. "did_cursorhold" is set to avoid retriggering.
9134 apply_autocmds(EVENT_CURSORHOLD
, NULL
, NULL
, FALSE
, curbuf
);
9135 did_cursorhold
= TRUE
;
9136 cap
->retval
|= CA_COMMAND_BUSY
; /* don't call edit() now */