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.
11 * ex_getln.c: Functions for entering and editing an Ex command line.
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
23 char_u
*cmdbuff
; /* pointer to command line buffer */
24 int cmdbufflen
; /* length of cmdbuff */
25 int cmdlen
; /* number of chars in command line */
26 int cmdpos
; /* current cursor position */
27 int cmdspos
; /* cursor column on screen */
28 int cmdfirstc
; /* ':', '/', '?', '=' or NUL */
29 int cmdindent
; /* number of spaces before cmdline */
30 char_u
*cmdprompt
; /* message in front of cmdline */
31 int cmdattr
; /* attributes for prompt */
32 int overstrike
; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
34 expand_T
*xpc
; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
36 int xp_context
; /* type of expansion */
38 char_u
*xp_arg
; /* user-defined expansion arg */
39 int input_fn
; /* when TRUE Invoked for input() function */
43 /* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47 static struct cmdline_info ccline
;
49 static int cmd_showtail
; /* Only show path tail in lists ? */
52 static int new_cmdpos
; /* position set by set_cmdline_pos() */
56 typedef struct hist_entry
58 int hisnum
; /* identifying number */
59 char_u
*hisstr
; /* actual entry, separator char after the NUL */
62 static histentry_T
*(history
[HIST_COUNT
]) = {NULL
, NULL
, NULL
, NULL
, NULL
};
63 static int hisidx
[HIST_COUNT
] = {-1, -1, -1, -1, -1}; /* lastused entry */
64 static int hisnum
[HIST_COUNT
] = {0, 0, 0, 0, 0};
65 /* identifying (unique) number of newest history entry */
66 static int hislen
= 0; /* actual length of history tables */
68 static int hist_char2type
__ARGS((int c
));
70 static int in_history
__ARGS((int, char_u
*, int));
72 static int calc_hist_idx
__ARGS((int histype
, int num
));
77 static int cmd_hkmap
= 0; /* Hebrew mapping during command line */
81 static int cmd_fkmap
= 0; /* Farsi mapping during command line */
84 static int cmdline_charsize
__ARGS((int idx
));
85 static void set_cmdspos
__ARGS((void));
86 static void set_cmdspos_cursor
__ARGS((void));
88 static void correct_cmdspos
__ARGS((int idx
, int cells
));
90 static void alloc_cmdbuff
__ARGS((int len
));
91 static int realloc_cmdbuff
__ARGS((int len
));
92 static void draw_cmdline
__ARGS((int start
, int len
));
93 static void save_cmdline
__ARGS((struct cmdline_info
*ccp
));
94 static void restore_cmdline
__ARGS((struct cmdline_info
*ccp
));
95 static int cmdline_paste
__ARGS((int regname
, int literally
, int remcr
));
96 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
97 static void redrawcmd_preedit
__ARGS((void));
100 static void cmdline_del
__ARGS((int from
));
102 static void redrawcmdprompt
__ARGS((void));
103 static void cursorcmd
__ARGS((void));
104 static int ccheck_abbr
__ARGS((int));
105 static int nextwild
__ARGS((expand_T
*xp
, int type
, int options
));
106 static void escape_fname
__ARGS((char_u
**pp
));
107 static int showmatches
__ARGS((expand_T
*xp
, int wildmenu
));
108 static void set_expand_context
__ARGS((expand_T
*xp
));
109 static int ExpandFromContext
__ARGS((expand_T
*xp
, char_u
*, int *, char_u
***, int));
110 static int expand_showtail
__ARGS((expand_T
*xp
));
111 #ifdef FEAT_CMDL_COMPL
112 static int expand_shellcmd
__ARGS((char_u
*filepat
, int *num_file
, char_u
***file
, int flagsarg
));
113 static int ExpandRTDir
__ARGS((char_u
*pat
, int *num_file
, char_u
***file
, char *dirname
));
114 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
115 static int ExpandUserDefined
__ARGS((expand_T
*xp
, regmatch_T
*regmatch
, int *num_file
, char_u
***file
));
116 static int ExpandUserList
__ARGS((expand_T
*xp
, int *num_file
, char_u
***file
));
121 static int ex_window
__ARGS((void));
125 * getcmdline() - accept a command line starting with firstc.
127 * firstc == ':' get ":" command line.
128 * firstc == '/' or '?' get search pattern
129 * firstc == '=' get expression
130 * firstc == '@' get text for input() function
131 * firstc == '>' get text for debug mode
132 * firstc == NUL get text for :insert command
133 * firstc == -1 like NUL, and break on CTRL-C
135 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
138 * Careful: getcmdline() can be called recursively!
140 * Return pointer to allocated string if there is a commandline, NULL
145 getcmdline(firstc
, count
, indent
)
147 long count
; /* only used for incremental search */
148 int indent
; /* indent for inside conditionals */
153 int gotesc
= FALSE
; /* TRUE when <ESC> just typed */
154 int do_abbr
; /* when TRUE check for abbr. */
156 char_u
*lookfor
= NULL
; /* string to match */
157 int hiscnt
; /* current history line in use */
158 int histype
; /* history type to be used */
160 #ifdef FEAT_SEARCH_EXTRA
162 colnr_T old_curswant
;
164 linenr_T old_topline
;
168 linenr_T old_botline
;
169 int did_incsearch
= FALSE
;
170 int incsearch_postponed
= FALSE
;
172 int did_wild_list
= FALSE
; /* did wild_list() recently */
173 int wim_index
= 0; /* index in wim_flags[] */
175 int save_msg_scroll
= msg_scroll
;
176 int save_State
= State
; /* remember State when called */
177 int some_key_typed
= FALSE
; /* one of the keys was typed */
179 /* mouse drag and release events are ignored, unless they are
180 * preceded with a mouse down event */
181 int ignore_drag_release
= TRUE
;
184 int break_ctrl_c
= FALSE
;
187 long *b_im_ptr
= NULL
;
188 #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
189 /* Everything that may work recursively should save and restore the
190 * current command line in save_ccline. That includes update_screen(), a
191 * custom status line may invoke ":normal". */
192 struct cmdline_info save_ccline
;
196 want_sniff_request
= 0;
205 #ifdef FEAT_RIGHTLEFT
206 /* start without Hebrew mapping for a command line */
207 if (firstc
== ':' || firstc
== '=' || firstc
== '>')
211 ccline
.overstrike
= FALSE
; /* always start in insert mode */
212 #ifdef FEAT_SEARCH_EXTRA
213 old_cursor
= curwin
->w_cursor
; /* needs to be restored later */
214 old_curswant
= curwin
->w_curswant
;
215 old_leftcol
= curwin
->w_leftcol
;
216 old_topline
= curwin
->w_topline
;
218 old_topfill
= curwin
->w_topfill
;
220 old_botline
= curwin
->w_botline
;
224 * set some variables for redrawcmd()
226 ccline
.cmdfirstc
= (firstc
== '@' ? 0 : firstc
);
227 ccline
.cmdindent
= (firstc
> 0 ? indent
: 0);
229 /* alloc initial ccline.cmdbuff */
230 alloc_cmdbuff(exmode_active
? 250 : indent
+ 1);
231 if (ccline
.cmdbuff
== NULL
)
232 return NULL
; /* out of memory */
233 ccline
.cmdlen
= ccline
.cmdpos
= 0;
234 ccline
.cmdbuff
[0] = NUL
;
236 /* autoindent for :insert and :append */
239 copy_spaces(ccline
.cmdbuff
, indent
);
240 ccline
.cmdbuff
[indent
] = NUL
;
241 ccline
.cmdpos
= indent
;
242 ccline
.cmdspos
= indent
;
243 ccline
.cmdlen
= indent
;
249 #ifdef FEAT_RIGHTLEFT
250 if (curwin
->w_p_rl
&& *curwin
->w_p_rlc
== 's'
251 && (firstc
== '/' || firstc
== '?'))
257 redir_off
= TRUE
; /* don't redirect the typed command */
261 msg_scrolled
= 0; /* avoid wait_return message */
264 redrawcmdprompt(); /* draw prompt or indent */
267 xpc
.xp_context
= EXPAND_NOTHING
;
268 xpc
.xp_backslash
= XP_BS_NONE
;
269 #ifndef BACKSLASH_IN_FILENAME
270 xpc
.xp_shell
= FALSE
;
273 #if defined(FEAT_EVAL)
276 xpc
.xp_context
= ccline
.xp_context
;
277 xpc
.xp_pattern
= ccline
.cmdbuff
;
278 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
279 xpc
.xp_arg
= ccline
.xp_arg
;
285 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
286 * doing ":@0" when register 0 doesn't contain a CR.
292 if (firstc
== '/' || firstc
== '?' || firstc
== '@')
294 /* Use ":lmap" mappings for search pattern and input(). */
295 if (curbuf
->b_p_imsearch
== B_IMODE_USE_INSERT
)
296 b_im_ptr
= &curbuf
->b_p_iminsert
;
298 b_im_ptr
= &curbuf
->b_p_imsearch
;
299 if (*b_im_ptr
== B_IMODE_LMAP
)
301 #ifdef USE_IM_CONTROL
302 im_set_active(*b_im_ptr
== B_IMODE_IM
);
305 #ifdef USE_IM_CONTROL
306 else if (p_imcmdline
)
314 ui_cursor_shape(); /* may show different cursor shape */
317 /* When inside an autocommand for writing "exiting" may be set and
318 * terminal mode set to cooked. Need to set raw mode here then. */
323 hiscnt
= hislen
; /* set hiscnt to impossible history value */
324 histype
= hist_char2type(firstc
);
328 do_digraph(-1); /* init digraph typahead */
332 * Collect the command string, handling editing keys.
336 redir_off
= TRUE
; /* Don't redirect the typed command.
337 Repeated, because a ":redir" inside
338 completion may switch it on. */
339 #ifdef USE_ON_FLY_SCROLL
340 dont_scroll
= FALSE
; /* allow scrolling here */
342 quit_more
= FALSE
; /* reset after CTRL-D which had a more-prompt */
344 cursorcmd(); /* set the cursor on the right spot */
346 /* Get a character. Ignore K_IGNORE, it should not do anything, such
347 * as stop completion. */
351 } while (c
== K_IGNORE
);
355 some_key_typed
= TRUE
;
356 #ifdef FEAT_RIGHTLEFT
363 if (cmdmsg_rl
&& !KeyStuffed
)
365 /* Invert horizontal movements and operations. Only when
366 * typed by the user directly, not when the result of a
370 case K_RIGHT
: c
= K_LEFT
; break;
371 case K_S_RIGHT
: c
= K_S_LEFT
; break;
372 case K_C_RIGHT
: c
= K_C_LEFT
; break;
373 case K_LEFT
: c
= K_RIGHT
; break;
374 case K_S_LEFT
: c
= K_S_RIGHT
; break;
375 case K_C_LEFT
: c
= K_C_RIGHT
; break;
382 * Ignore got_int when CTRL-C was typed here.
383 * Don't ignore it in :global, we really need to break then, e.g., for
384 * ":g/pat/normal /pat" (without the <CR>).
385 * Don't ignore it for the input() function.
392 #if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
402 /* free old command line when finished moving around in the history
405 && c
!= K_S_DOWN
&& c
!= K_S_UP
406 && c
!= K_DOWN
&& c
!= K_UP
407 && c
!= K_PAGEDOWN
&& c
!= K_PAGEUP
408 && c
!= K_KPAGEDOWN
&& c
!= K_KPAGEUP
409 && c
!= K_LEFT
&& c
!= K_RIGHT
410 && (xpc
.xp_numfiles
> 0 || (c
!= Ctrl_P
&& c
!= Ctrl_N
)))
418 * When there are matching completions to select <S-Tab> works like
419 * CTRL-P (unless 'wc' is <S-Tab>).
421 if (c
!= p_wc
&& c
== K_S_TAB
&& xpc
.xp_numfiles
> 0)
425 /* Special translations for 'wildmenu' */
426 if (did_wild_list
&& p_wmnu
)
430 else if (c
== K_RIGHT
)
433 /* Hitting CR after "emenu Name.": complete submenu */
434 if (xpc
.xp_context
== EXPAND_MENUNAMES
&& p_wmnu
436 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '.'
437 && ccline
.cmdbuff
[ccline
.cmdpos
- 2] != '\\'
438 && (c
== '\n' || c
== '\r' || c
== K_KENTER
))
442 /* free expanded names when finished walking through matches */
443 if (xpc
.xp_numfiles
!= -1
444 && !(c
== p_wc
&& KeyTyped
) && c
!= p_wcm
445 && c
!= Ctrl_N
&& c
!= Ctrl_P
&& c
!= Ctrl_A
448 (void)ExpandOne(&xpc
, NULL
, NULL
, 0, WILD_FREE
);
449 did_wild_list
= FALSE
;
451 if (!p_wmnu
|| (c
!= K_UP
&& c
!= K_DOWN
))
453 xpc
.xp_context
= EXPAND_NOTHING
;
456 if (p_wmnu
&& wild_menu_showing
!= 0)
459 int old_RedrawingDisabled
= RedrawingDisabled
;
462 RedrawingDisabled
= 0;
464 if (wild_menu_showing
== WM_SCROLLED
)
466 /* Entered command line, move it up */
470 else if (save_p_ls
!= -1)
472 /* restore 'laststatus' and 'winminheight' */
476 save_cmdline(&save_ccline
);
477 update_screen(VALID
); /* redraw the screen NOW */
478 restore_cmdline(&save_ccline
);
484 # ifdef FEAT_VERTSPLIT
485 win_redraw_last_status(topframe
);
487 lastwin
->w_redr_status
= TRUE
;
489 redraw_statuslines();
492 wild_menu_showing
= 0;
494 RedrawingDisabled
= old_RedrawingDisabled
;
500 /* Special translations for 'wildmenu' */
501 if (xpc
.xp_context
== EXPAND_MENUNAMES
&& p_wmnu
)
503 /* Hitting <Down> after "emenu Name.": complete submenu */
504 if (c
== K_DOWN
&& ccline
.cmdpos
> 0
505 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '.')
509 /* Hitting <Up>: Remove one submenu name in front of the
513 j
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
517 /* check for start of menu name */
518 if (ccline
.cmdbuff
[j
] == ' '
519 && ccline
.cmdbuff
[j
- 1] != '\\')
524 /* check for start of submenu name */
525 if (ccline
.cmdbuff
[j
] == '.'
526 && ccline
.cmdbuff
[j
- 1] != '\\')
540 xpc
.xp_context
= EXPAND_NOTHING
;
543 if ((xpc
.xp_context
== EXPAND_FILES
544 || xpc
.xp_context
== EXPAND_DIRECTORIES
545 || xpc
.xp_context
== EXPAND_SHELLCMD
) && p_wmnu
)
557 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == PATHSEP
558 && (ccline
.cmdpos
< 3
559 || ccline
.cmdbuff
[ccline
.cmdpos
- 2] != '.'
560 || ccline
.cmdbuff
[ccline
.cmdpos
- 3] != '.'))
562 /* go down a directory */
565 else if (STRNCMP(xpc
.xp_pattern
, upseg
+ 1, 3) == 0 && c
== K_DOWN
)
567 /* If in a direct ancestor, strip off one ../ to go down */
571 i
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
576 j
-= (*mb_head_off
)(ccline
.cmdbuff
, ccline
.cmdbuff
+ j
);
578 if (vim_ispathsep(ccline
.cmdbuff
[j
]))
585 && ccline
.cmdbuff
[j
- 1] == '.'
586 && ccline
.cmdbuff
[j
- 2] == '.'
587 && (vim_ispathsep(ccline
.cmdbuff
[j
- 3]) || j
== i
+ 2))
595 /* go up a directory */
598 j
= ccline
.cmdpos
- 1;
599 i
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
604 j
-= (*mb_head_off
)(ccline
.cmdbuff
, ccline
.cmdbuff
+ j
);
606 if (vim_ispathsep(ccline
.cmdbuff
[j
])
607 #ifdef BACKSLASH_IN_FILENAME
608 && vim_strchr(" *?[{`$%#", ccline
.cmdbuff
[j
+ 1])
625 else if (STRNCMP(ccline
.cmdbuff
+ j
, upseg
, 4) == 0)
627 else if (STRNCMP(ccline
.cmdbuff
+ j
, upseg
+ 1, 3) == 0
634 /* TODO this is only for DOS/UNIX systems - need to put in
635 * machine-specific stuff here and in upseg init */
637 put_on_cmdline(upseg
+ 1, 3, FALSE
);
639 else if (ccline
.cmdpos
> i
)
644 #if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
646 && (xpc
.xp_context
== EXPAND_FILES
647 || xpc
.xp_context
== EXPAND_MENUNAMES
)
648 && (c
== K_UP
|| c
== K_DOWN
))
649 xpc
.xp_context
= EXPAND_NOTHING
;
652 #endif /* FEAT_WILDMENU */
654 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
655 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
663 /* CTRL-\ e doesn't work when obtaining an expression. */
664 if (c
!= Ctrl_N
&& c
!= Ctrl_G
665 && (c
!= 'e' || ccline
.cmdfirstc
== '='))
676 * Replace the command line with the result of an expression.
677 * Need to save and restore the current command line, to be
678 * able to enter a new one...
680 if (ccline
.cmdpos
== ccline
.cmdlen
)
681 new_cmdpos
= 99999; /* keep it at the end */
683 new_cmdpos
= ccline
.cmdpos
;
685 save_cmdline(&save_ccline
);
686 c
= get_expr_register();
687 restore_cmdline(&save_ccline
);
690 /* Need to save and restore ccline. And set "textlock"
691 * to avoid nasty things like going to another buffer when
692 * evaluating an expression. */
693 save_cmdline(&save_ccline
);
697 restore_cmdline(&save_ccline
);
699 if (p
!= NULL
&& realloc_cmdbuff((int)STRLEN(p
) + 1) == OK
)
701 ccline
.cmdlen
= (int)STRLEN(p
);
702 STRCPY(ccline
.cmdbuff
, p
);
705 /* Restore the cursor or use the position set with
706 * set_cmdline_pos(). */
707 if (new_cmdpos
> ccline
.cmdlen
)
708 ccline
.cmdpos
= ccline
.cmdlen
;
710 ccline
.cmdpos
= new_cmdpos
;
712 KeyTyped
= FALSE
; /* Don't do p_wc completion. */
714 goto cmdline_changed
;
723 if (c
== Ctrl_G
&& p_im
&& restart_edit
== 0)
725 gotesc
= TRUE
; /* will free ccline.cmdbuff after putting it
727 goto returncmd
; /* back to Normal mode */
732 if (c
== cedit_key
|| c
== K_CMDWIN
)
735 * Open a window to edit the command line (and history).
738 some_key_typed
= TRUE
;
740 # ifdef FEAT_DIGRAPHS
748 if (c
== '\n' || c
== '\r' || c
== K_KENTER
|| (c
== ESC
749 && (!KeyTyped
|| vim_strchr(p_cpo
, CPO_ESC
) != NULL
)))
751 /* In Ex mode a backslash escapes a newline. */
754 && ccline
.cmdpos
== ccline
.cmdlen
756 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '\\')
763 gotesc
= FALSE
; /* Might have typed ESC previously, don't
764 truncate the cmdline now. */
765 if (ccheck_abbr(c
+ ABBR_OFF
))
766 goto cmdline_changed
;
769 windgoto(msg_row
, 0);
777 * Completion for 'wildchar' or 'wildcharm' key.
778 * - hitting <ESC> twice means: abandon command line.
779 * - wildcard expansion is only done when the 'wildchar' key is really
780 * typed, not when it comes from a macro
782 if ((c
== p_wc
&& !gotesc
&& KeyTyped
) || c
== p_wcm
)
784 if (xpc
.xp_numfiles
> 0) /* typed p_wc at least twice */
786 /* if 'wildmode' contains "list" may still need to list */
787 if (xpc
.xp_numfiles
> 1
789 && (wim_flags
[wim_index
] & WIM_LIST
))
791 (void)showmatches(&xpc
, FALSE
);
793 did_wild_list
= TRUE
;
795 if (wim_flags
[wim_index
] & WIM_LONGEST
)
796 res
= nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
797 else if (wim_flags
[wim_index
] & WIM_FULL
)
798 res
= nextwild(&xpc
, WILD_NEXT
, WILD_NO_BEEP
);
800 res
= OK
; /* don't insert 'wildchar' now */
802 else /* typed p_wc first time */
806 /* if 'wildmode' first contains "longest", get longest
808 if (wim_flags
[0] & WIM_LONGEST
)
809 res
= nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
811 res
= nextwild(&xpc
, WILD_EXPAND_KEEP
, WILD_NO_BEEP
);
813 /* if interrupted while completing, behave like it failed */
816 (void)vpeekc(); /* remove <C-C> from input stream */
817 got_int
= FALSE
; /* don't abandon the command line */
818 (void)ExpandOne(&xpc
, NULL
, NULL
, 0, WILD_FREE
);
820 xpc
.xp_context
= EXPAND_NOTHING
;
822 goto cmdline_changed
;
825 /* when more than one match, and 'wildmode' first contains
826 * "list", or no change and 'wildmode' contains "longest,list",
827 * list all matches */
828 if (res
== OK
&& xpc
.xp_numfiles
> 1)
830 /* a "longest" that didn't do anything is skipped (but not
832 if (wim_flags
[0] == WIM_LONGEST
&& ccline
.cmdpos
== j
)
834 if ((wim_flags
[wim_index
] & WIM_LIST
)
836 || (p_wmnu
&& (wim_flags
[wim_index
] & WIM_FULL
) != 0)
840 if (!(wim_flags
[0] & WIM_LONGEST
))
843 int p_wmnu_save
= p_wmnu
;
846 nextwild(&xpc
, WILD_PREV
, 0); /* remove match */
848 p_wmnu
= p_wmnu_save
;
852 (void)showmatches(&xpc
, p_wmnu
853 && ((wim_flags
[wim_index
] & WIM_LIST
) == 0));
855 (void)showmatches(&xpc
, FALSE
);
858 did_wild_list
= TRUE
;
859 if (wim_flags
[wim_index
] & WIM_LONGEST
)
860 nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
861 else if (wim_flags
[wim_index
] & WIM_FULL
)
862 nextwild(&xpc
, WILD_NEXT
, WILD_NO_BEEP
);
868 else if (xpc
.xp_numfiles
== -1)
869 xpc
.xp_context
= EXPAND_NOTHING
;
877 goto cmdline_changed
;
882 /* <S-Tab> goes to last match, in a clumsy way */
883 if (c
== K_S_TAB
&& KeyTyped
)
885 if (nextwild(&xpc
, WILD_EXPAND_KEEP
, 0) == OK
886 && nextwild(&xpc
, WILD_PREV
, 0) == OK
887 && nextwild(&xpc
, WILD_PREV
, 0) == OK
)
888 goto cmdline_changed
;
891 if (c
== NUL
|| c
== K_ZERO
) /* NUL is stored as NL */
894 do_abbr
= TRUE
; /* default: check for abbreviation */
897 * Big switch for a typed command line character.
907 if (cmd_fkmap
&& c
== K_BS
)
914 * delete current character is the same as backspace on next
915 * character, except at end of line
917 if (c
== K_DEL
&& ccline
.cmdpos
!= ccline
.cmdlen
)
920 if (has_mbyte
&& c
== K_DEL
)
921 ccline
.cmdpos
+= mb_off_next(ccline
.cmdbuff
,
922 ccline
.cmdbuff
+ ccline
.cmdpos
);
924 if (ccline
.cmdpos
> 0)
929 p
= ccline
.cmdbuff
+ j
;
933 p
= mb_prevptr(ccline
.cmdbuff
, p
);
936 while (p
> ccline
.cmdbuff
&& vim_isspace(*p
))
937 p
= mb_prevptr(ccline
.cmdbuff
, p
);
939 while (p
> ccline
.cmdbuff
&& mb_get_class(p
) == i
)
940 p
= mb_prevptr(ccline
.cmdbuff
, p
);
941 if (mb_get_class(p
) != i
)
942 p
+= (*mb_ptr2len
)(p
);
949 while (p
> ccline
.cmdbuff
&& vim_isspace(p
[-1]))
951 i
= vim_iswordc(p
[-1]);
952 while (p
> ccline
.cmdbuff
&& !vim_isspace(p
[-1])
953 && vim_iswordc(p
[-1]) == i
)
958 ccline
.cmdpos
= (int)(p
- ccline
.cmdbuff
);
959 ccline
.cmdlen
-= j
- ccline
.cmdpos
;
961 while (i
< ccline
.cmdlen
)
962 ccline
.cmdbuff
[i
++] = ccline
.cmdbuff
[j
++];
964 /* Truncate at the end, required for multi-byte chars. */
965 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
968 else if (ccline
.cmdlen
== 0 && c
!= Ctrl_W
969 && ccline
.cmdprompt
== NULL
&& indent
== 0)
971 /* In ex and debug mode it doesn't make sense to return. */
974 || ccline
.cmdfirstc
== '>'
977 goto cmdline_not_changed
;
979 vim_free(ccline
.cmdbuff
); /* no commandline to return */
980 ccline
.cmdbuff
= NULL
;
983 #ifdef FEAT_RIGHTLEFT
989 msg_putchar(' '); /* delete ':' */
991 redraw_cmdline
= TRUE
;
992 goto returncmd
; /* back to cmd mode */
994 goto cmdline_changed
;
999 /* if Farsi mode set, we are in reverse insert mode -
1000 Do not change the mode */
1005 ccline
.overstrike
= !ccline
.overstrike
;
1007 ui_cursor_shape(); /* may show different cursor shape */
1009 goto cmdline_not_changed
;
1012 if (map_to_exists_mode((char_u
*)"", LANGMAP
, FALSE
))
1014 /* ":lmap" mappings exists, toggle use of mappings. */
1016 #ifdef USE_IM_CONTROL
1017 im_set_active(FALSE
); /* Disable input method */
1019 if (b_im_ptr
!= NULL
)
1021 if (State
& LANGMAP
)
1022 *b_im_ptr
= B_IMODE_LMAP
;
1024 *b_im_ptr
= B_IMODE_NONE
;
1027 #ifdef USE_IM_CONTROL
1030 /* There are no ":lmap" mappings, toggle IM. When
1031 * 'imdisable' is set don't try getting the status, it's
1033 if ((p_imdisable
&& b_im_ptr
!= NULL
)
1034 ? *b_im_ptr
== B_IMODE_IM
: im_get_status())
1036 im_set_active(FALSE
); /* Disable input method */
1037 if (b_im_ptr
!= NULL
)
1038 *b_im_ptr
= B_IMODE_NONE
;
1042 im_set_active(TRUE
); /* Enable input method */
1043 if (b_im_ptr
!= NULL
)
1044 *b_im_ptr
= B_IMODE_IM
;
1048 if (b_im_ptr
!= NULL
)
1050 if (b_im_ptr
== &curbuf
->b_p_iminsert
)
1051 set_iminsert_global();
1053 set_imsearch_global();
1056 ui_cursor_shape(); /* may show different cursor shape */
1058 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1059 /* Show/unshow value of 'keymap' in status lines later. */
1060 status_redraw_curbuf();
1062 goto cmdline_not_changed
;
1064 /* case '@': only in very old vi */
1066 /* delete all characters left of the cursor */
1069 i
= ccline
.cmdpos
= 0;
1070 while (i
< ccline
.cmdlen
)
1071 ccline
.cmdbuff
[i
++] = ccline
.cmdbuff
[j
++];
1072 /* Truncate at the end, required for multi-byte chars. */
1073 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
1075 goto cmdline_changed
;
1077 #ifdef FEAT_CLIPBOARD
1079 /* Copy the modeless selection, if there is one. */
1080 if (clip_star
.state
!= SELECT_CLEARED
)
1082 if (clip_star
.state
== SELECT_DONE
)
1083 clip_copy_modeless_selection(TRUE
);
1084 goto cmdline_not_changed
;
1089 case ESC
: /* get here if p_wc != ESC or when ESC typed twice */
1091 /* In exmode it doesn't make sense to return. Except when
1092 * ":normal" runs out of characters. */
1094 #ifdef FEAT_EX_EXTRA
1095 && (ex_normal_busy
== 0 || typebuf
.tb_len
> 0)
1098 goto cmdline_not_changed
;
1100 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1101 putting it in history */
1102 goto returncmd
; /* back to cmd mode */
1104 case Ctrl_R
: /* insert register */
1105 #ifdef USE_ON_FLY_SCROLL
1106 dont_scroll
= TRUE
; /* disallow scrolling here */
1108 putcmdline('"', TRUE
);
1110 i
= c
= plain_vgetc(); /* CTRL-R <char> */
1112 i
= Ctrl_R
; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1114 c
= plain_vgetc(); /* CTRL-R CTRL-R <char> */
1118 * Insert the result of an expression.
1119 * Need to save the current command line, to be able to enter
1125 if (ccline
.cmdfirstc
== '=')/* can't do this recursively */
1132 save_cmdline(&save_ccline
);
1133 c
= get_expr_register();
1134 restore_cmdline(&save_ccline
);
1138 if (c
!= ESC
) /* use ESC to cancel inserting register */
1140 cmdline_paste(c
, i
== Ctrl_R
, FALSE
);
1143 /* When there was a serious error abort getting the
1147 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1148 putting it in history */
1149 goto returncmd
; /* back to cmd mode */
1152 KeyTyped
= FALSE
; /* Don't do p_wc completion. */
1154 if (new_cmdpos
>= 0)
1156 /* set_cmdline_pos() was used */
1157 if (new_cmdpos
> ccline
.cmdlen
)
1158 ccline
.cmdpos
= ccline
.cmdlen
;
1160 ccline
.cmdpos
= new_cmdpos
;
1165 goto cmdline_changed
;
1168 if (showmatches(&xpc
, FALSE
) == EXPAND_NOTHING
)
1169 break; /* Use ^D as normal char instead */
1172 continue; /* don't do incremental search now */
1179 if (ccline
.cmdpos
>= ccline
.cmdlen
)
1181 i
= cmdline_charsize(ccline
.cmdpos
);
1182 if (KeyTyped
&& ccline
.cmdspos
+ i
>= Columns
* Rows
)
1184 ccline
.cmdspos
+= i
;
1187 ccline
.cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
1193 while ((c
== K_S_RIGHT
|| c
== K_C_RIGHT
1194 || (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
)))
1195 && ccline
.cmdbuff
[ccline
.cmdpos
] != ' ');
1198 set_cmdspos_cursor();
1200 goto cmdline_not_changed
;
1205 if (ccline
.cmdpos
== 0)
1206 goto cmdline_not_changed
;
1211 if (has_mbyte
) /* move to first byte of char */
1212 ccline
.cmdpos
-= (*mb_head_off
)(ccline
.cmdbuff
,
1213 ccline
.cmdbuff
+ ccline
.cmdpos
);
1215 ccline
.cmdspos
-= cmdline_charsize(ccline
.cmdpos
);
1217 while (ccline
.cmdpos
> 0
1218 && (c
== K_S_LEFT
|| c
== K_C_LEFT
1219 || (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
)))
1220 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] != ' ');
1223 set_cmdspos_cursor();
1225 goto cmdline_not_changed
;
1228 /* Ignore mouse event or ex_window() result. */
1229 goto cmdline_not_changed
;
1232 /* On Win32 ignore <M-F4>, we get it when closing the window was
1235 if (mod_mask
== MOD_MASK_ALT
)
1237 redrawcmd(); /* somehow the cmdline is cleared */
1238 goto cmdline_not_changed
;
1245 case K_MIDDLERELEASE
:
1246 goto cmdline_not_changed
; /* Ignore mouse */
1250 /* When GUI is active, also paste when 'mouse' is empty */
1253 if (!mouse_has(MOUSE_COMMAND
))
1254 goto cmdline_not_changed
; /* Ignore mouse */
1255 # ifdef FEAT_CLIPBOARD
1256 if (clip_star
.available
)
1257 cmdline_paste('*', TRUE
, TRUE
);
1260 cmdline_paste(0, TRUE
, TRUE
);
1262 goto cmdline_changed
;
1266 cmdline_paste('~', TRUE
, FALSE
);
1268 goto cmdline_changed
;
1274 case K_RIGHTRELEASE
:
1275 /* Ignore drag and release events when the button-down wasn't
1277 if (ignore_drag_release
)
1278 goto cmdline_not_changed
;
1282 if (c
== K_LEFTRELEASE
|| c
== K_RIGHTRELEASE
)
1283 ignore_drag_release
= TRUE
;
1285 ignore_drag_release
= FALSE
;
1287 /* When GUI is active, also move when 'mouse' is empty */
1290 if (!mouse_has(MOUSE_COMMAND
))
1291 goto cmdline_not_changed
; /* Ignore mouse */
1292 # ifdef FEAT_CLIPBOARD
1293 if (mouse_row
< cmdline_row
&& clip_star
.available
)
1295 int button
, is_click
, is_drag
;
1298 * Handle modeless selection.
1300 button
= get_mouse_button(KEY2TERMCAP1(c
),
1301 &is_click
, &is_drag
);
1302 if (mouse_model_popup() && button
== MOUSE_LEFT
1303 && (mod_mask
& MOD_MASK_SHIFT
))
1305 /* Translate shift-left to right button. */
1306 button
= MOUSE_RIGHT
;
1307 mod_mask
&= ~MOD_MASK_SHIFT
;
1309 clip_modeless(button
, is_click
, is_drag
);
1310 goto cmdline_not_changed
;
1315 for (ccline
.cmdpos
= 0; ccline
.cmdpos
< ccline
.cmdlen
;
1318 i
= cmdline_charsize(ccline
.cmdpos
);
1319 if (mouse_row
<= cmdline_row
+ ccline
.cmdspos
/ Columns
1320 && mouse_col
< ccline
.cmdspos
% Columns
+ i
)
1325 /* Count ">" for double-wide char that doesn't fit. */
1326 correct_cmdspos(ccline
.cmdpos
, i
);
1327 ccline
.cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
1328 + ccline
.cmdpos
) - 1;
1331 ccline
.cmdspos
+= i
;
1333 goto cmdline_not_changed
;
1335 /* Mouse scroll wheel: ignored here */
1338 /* Alternate buttons ignored here */
1345 goto cmdline_not_changed
;
1347 #endif /* FEAT_MOUSE */
1350 case K_LEFTMOUSE_NM
: /* mousefocus click, ignored */
1351 case K_LEFTRELEASE_NM
:
1352 goto cmdline_not_changed
;
1354 case K_VER_SCROLLBAR
:
1355 if (msg_scrolled
== 0)
1360 goto cmdline_not_changed
;
1362 case K_HOR_SCROLLBAR
:
1363 if (msg_scrolled
== 0)
1365 gui_do_horiz_scroll();
1368 goto cmdline_not_changed
;
1370 #ifdef FEAT_GUI_TABLINE
1373 /* Don't want to change any tabs here. Make sure the same tab
1374 * is still selected. */
1375 if (gui_use_tabline())
1376 gui_mch_set_curtab(tabpage_index(curtab
));
1377 goto cmdline_not_changed
;
1380 case K_SELECT
: /* end of Select mode mapping - ignore */
1381 goto cmdline_not_changed
;
1383 case Ctrl_B
: /* begin of command line */
1390 goto cmdline_not_changed
;
1392 case Ctrl_E
: /* end of command line */
1397 ccline
.cmdpos
= ccline
.cmdlen
;
1398 set_cmdspos_cursor();
1399 goto cmdline_not_changed
;
1401 case Ctrl_A
: /* all matches */
1402 if (nextwild(&xpc
, WILD_ALL
, 0) == FAIL
)
1404 goto cmdline_changed
;
1407 #ifdef FEAT_SEARCH_EXTRA
1408 if (p_is
&& !cmd_silent
&& (firstc
== '/' || firstc
== '?'))
1410 /* Add a character from under the cursor for 'incsearch' */
1412 && !equalpos(curwin
->w_cursor
, old_cursor
))
1417 if (c
== firstc
|| vim_strchr((char_u
*)(
1418 p_magic
? "\\^$.*[" : "\\^$"), c
)
1421 /* put a backslash before special characters */
1422 stuffcharReadbuff(c
);
1428 goto cmdline_not_changed
;
1432 /* completion: longest common part */
1433 if (nextwild(&xpc
, WILD_LONGEST
, 0) == FAIL
)
1435 goto cmdline_changed
;
1437 case Ctrl_N
: /* next match */
1438 case Ctrl_P
: /* previous match */
1439 if (xpc
.xp_numfiles
> 0)
1441 if (nextwild(&xpc
, (c
== Ctrl_P
) ? WILD_PREV
: WILD_NEXT
, 0)
1444 goto cmdline_changed
;
1456 if (hislen
== 0 || firstc
== NUL
) /* no history */
1457 goto cmdline_not_changed
;
1461 /* save current command string so it can be restored later */
1462 if (lookfor
== NULL
)
1464 if ((lookfor
= vim_strsave(ccline
.cmdbuff
)) == NULL
)
1465 goto cmdline_not_changed
;
1466 lookfor
[ccline
.cmdpos
] = NUL
;
1469 j
= (int)STRLEN(lookfor
);
1472 /* one step backwards */
1473 if (c
== K_UP
|| c
== K_S_UP
|| c
== Ctrl_P
1474 || c
== K_PAGEUP
|| c
== K_KPAGEUP
)
1476 if (hiscnt
== hislen
) /* first time */
1477 hiscnt
= hisidx
[histype
];
1478 else if (hiscnt
== 0 && hisidx
[histype
] != hislen
- 1)
1479 hiscnt
= hislen
- 1;
1480 else if (hiscnt
!= hisidx
[histype
] + 1)
1482 else /* at top of list */
1488 else /* one step forwards */
1490 /* on last entry, clear the line */
1491 if (hiscnt
== hisidx
[histype
])
1497 /* not on a history line, nothing to do */
1498 if (hiscnt
== hislen
)
1500 if (hiscnt
== hislen
- 1) /* wrap around */
1505 if (hiscnt
< 0 || history
[histype
][hiscnt
].hisstr
== NULL
)
1510 if ((c
!= K_UP
&& c
!= K_DOWN
)
1512 || STRNCMP(history
[histype
][hiscnt
].hisstr
,
1513 lookfor
, (size_t)j
) == 0)
1517 if (hiscnt
!= i
) /* jumped to other entry */
1523 vim_free(ccline
.cmdbuff
);
1524 xpc
.xp_context
= EXPAND_NOTHING
;
1525 if (hiscnt
== hislen
)
1526 p
= lookfor
; /* back to the old one */
1528 p
= history
[histype
][hiscnt
].hisstr
;
1530 if (histype
== HIST_SEARCH
1532 && (old_firstc
= p
[STRLEN(p
) + 1]) != firstc
)
1534 /* Correct for the separator character used when
1535 * adding the history entry vs the one used now.
1536 * First loop: count length.
1537 * Second loop: copy the characters. */
1538 for (i
= 0; i
<= 1; ++i
)
1541 for (j
= 0; p
[j
] != NUL
; ++j
)
1543 /* Replace old sep with new sep, unless it is
1545 if (p
[j
] == old_firstc
1546 && (j
== 0 || p
[j
- 1] != '\\'))
1549 ccline
.cmdbuff
[len
] = firstc
;
1553 /* Escape new sep, unless it is already
1556 && (j
== 0 || p
[j
- 1] != '\\'))
1559 ccline
.cmdbuff
[len
] = '\\';
1563 ccline
.cmdbuff
[len
] = p
[j
];
1570 if (ccline
.cmdbuff
== NULL
)
1574 ccline
.cmdbuff
[len
] = NUL
;
1578 alloc_cmdbuff((int)STRLEN(p
));
1579 if (ccline
.cmdbuff
== NULL
)
1581 STRCPY(ccline
.cmdbuff
, p
);
1584 ccline
.cmdpos
= ccline
.cmdlen
= (int)STRLEN(ccline
.cmdbuff
);
1586 goto cmdline_changed
;
1589 goto cmdline_not_changed
;
1595 ignore_drag_release
= TRUE
;
1597 putcmdline('^', TRUE
);
1598 c
= get_literal(); /* get next (two) character(s) */
1599 do_abbr
= FALSE
; /* don't do abbreviation now */
1601 /* may need to remove ^ when composing char was typed */
1602 if (enc_utf8
&& utf_iscomposing(c
) && !cmd_silent
)
1604 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
1611 #ifdef FEAT_DIGRAPHS
1614 ignore_drag_release
= TRUE
;
1616 putcmdline('?', TRUE
);
1617 #ifdef USE_ON_FLY_SCROLL
1618 dont_scroll
= TRUE
; /* disallow scrolling here */
1620 c
= get_digraph(TRUE
);
1625 goto cmdline_not_changed
;
1626 #endif /* FEAT_DIGRAPHS */
1628 #ifdef FEAT_RIGHTLEFT
1629 case Ctrl__
: /* CTRL-_: switch language mode */
1635 cmd_fkmap
= !cmd_fkmap
;
1636 if (cmd_fkmap
) /* in Farsi always in Insert mode */
1637 ccline
.overstrike
= FALSE
;
1639 else /* Hebrew is default */
1641 cmd_hkmap
= !cmd_hkmap
;
1642 goto cmdline_not_changed
;
1649 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1650 putting it in history */
1651 goto returncmd
; /* back to Normal mode */
1655 * Normal character with no special meaning. Just set mod_mask
1656 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1657 * the string <S-Space>. This should only happen after ^V.
1664 * End of switch on command line character.
1665 * We come here if we have a normal character.
1668 if (do_abbr
&& (IS_SPECIAL(c
) || !vim_iswordc(c
)) && ccheck_abbr(
1670 /* Add ABBR_OFF for characters above 0x100, this is
1671 * what check_abbr() expects. */
1672 (has_mbyte
&& c
>= 0x100) ? (c
+ ABBR_OFF
) :
1675 goto cmdline_changed
;
1678 * put the character in the command line
1680 if (IS_SPECIAL(c
) || mod_mask
!= 0)
1681 put_on_cmdline(get_special_key_name(c
, mod_mask
), -1, TRUE
);
1687 j
= (*mb_char2bytes
)(c
, IObuff
);
1688 IObuff
[j
] = NUL
; /* exclude composing chars */
1689 put_on_cmdline(IObuff
, j
, TRUE
);
1695 put_on_cmdline(IObuff
, 1, TRUE
);
1698 goto cmdline_changed
;
1701 * This part implements incremental searches for "/" and "?"
1702 * Jump to cmdline_not_changed when a character has been read but the command
1703 * line did not change. Then we only search and redraw if something changed in
1705 * Jump to cmdline_changed when the command line did change.
1706 * (Sorry for the goto's, I know it is ugly).
1708 cmdline_not_changed
:
1709 #ifdef FEAT_SEARCH_EXTRA
1710 if (!incsearch_postponed
)
1715 #ifdef FEAT_SEARCH_EXTRA
1717 * 'incsearch' highlighting.
1719 if (p_is
&& !cmd_silent
&& (firstc
== '/' || firstc
== '?'))
1726 /* if there is a character waiting, search and redraw later */
1729 incsearch_postponed
= TRUE
;
1732 incsearch_postponed
= FALSE
;
1733 curwin
->w_cursor
= old_cursor
; /* start at old position */
1735 /* If there is no command line, don't do anything */
1736 if (ccline
.cmdlen
== 0)
1740 cursor_off(); /* so the user knows we're busy */
1742 ++emsg_off
; /* So it doesn't beep if bad expr */
1744 /* Set the time limit to half a second. */
1745 profile_setlimit(500L, &tm
);
1747 i
= do_search(NULL
, firstc
, ccline
.cmdbuff
, count
,
1748 SEARCH_KEEP
+ SEARCH_OPT
+ SEARCH_NOOF
+ SEARCH_PEEK
,
1756 /* if interrupted while searching, behave like it failed */
1759 (void)vpeekc(); /* remove <C-C> from input stream */
1760 got_int
= FALSE
; /* don't abandon the command line */
1763 else if (char_avail())
1764 /* cancelled searching because a char was typed */
1765 incsearch_postponed
= TRUE
;
1768 highlight_match
= TRUE
; /* highlight position */
1770 highlight_match
= FALSE
; /* remove highlight */
1772 /* first restore the old curwin values, so the screen is
1773 * positioned in the same way as the actual search command */
1774 curwin
->w_leftcol
= old_leftcol
;
1775 curwin
->w_topline
= old_topline
;
1777 curwin
->w_topfill
= old_topfill
;
1779 curwin
->w_botline
= old_botline
;
1780 changed_cline_bef_curs();
1785 pos_T save_pos
= curwin
->w_cursor
;
1788 * First move cursor to end of match, then to the start. This
1789 * moves the whole match onto the screen when 'nowrap' is set.
1791 curwin
->w_cursor
.lnum
+= search_match_lines
;
1792 curwin
->w_cursor
.col
= search_match_endcol
;
1793 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
1795 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
1796 coladvance((colnr_T
)MAXCOL
);
1799 end_pos
= curwin
->w_cursor
;
1800 curwin
->w_cursor
= save_pos
;
1803 end_pos
= curwin
->w_cursor
; /* shutup gcc 4 */
1806 # ifdef FEAT_WINDOWS
1807 /* May redraw the status line to show the cursor position. */
1808 if (p_ru
&& curwin
->w_status_height
> 0)
1809 curwin
->w_redr_status
= TRUE
;
1812 save_cmdline(&save_ccline
);
1813 update_screen(SOME_VALID
);
1814 restore_cmdline(&save_ccline
);
1816 /* Leave it at the end to make CTRL-R CTRL-W work. */
1818 curwin
->w_cursor
= end_pos
;
1822 did_incsearch
= TRUE
;
1824 #else /* FEAT_SEARCH_EXTRA */
1828 #ifdef FEAT_RIGHTLEFT
1831 || (p_arshape
&& !p_tbidi
&& enc_utf8
)
1834 /* Always redraw the whole command line to fix shaping and
1835 * right-left typing. Not efficient, but it works. */
1842 #ifdef FEAT_RIGHTLEFT
1850 ExpandCleanup(&xpc
);
1853 #ifdef FEAT_SEARCH_EXTRA
1856 curwin
->w_cursor
= old_cursor
;
1857 curwin
->w_curswant
= old_curswant
;
1858 curwin
->w_leftcol
= old_leftcol
;
1859 curwin
->w_topline
= old_topline
;
1861 curwin
->w_topfill
= old_topfill
;
1863 curwin
->w_botline
= old_botline
;
1864 highlight_match
= FALSE
;
1865 validate_cursor(); /* needed for TAB */
1866 redraw_later(SOME_VALID
);
1870 if (ccline
.cmdbuff
!= NULL
)
1873 * Put line in history buffer (":" and "=" only when it was typed).
1876 if (ccline
.cmdlen
&& firstc
!= NUL
1877 && (some_key_typed
|| histype
== HIST_SEARCH
))
1879 add_to_history(histype
, ccline
.cmdbuff
, TRUE
,
1880 histype
== HIST_SEARCH
? firstc
: NUL
);
1883 vim_free(new_last_cmdline
);
1884 new_last_cmdline
= vim_strsave(ccline
.cmdbuff
);
1889 if (gotesc
) /* abandon command line */
1891 vim_free(ccline
.cmdbuff
);
1892 ccline
.cmdbuff
= NULL
;
1893 if (msg_scrolled
== 0)
1896 redraw_cmdline
= TRUE
;
1901 * If the screen was shifted up, redraw the whole screen (later).
1902 * If the line is too long, clear it, so ruler and shown command do
1903 * not get printed in the middle of it.
1906 msg_scroll
= save_msg_scroll
;
1909 /* When the command line was typed, no need for a wait-return prompt. */
1911 need_wait_return
= FALSE
;
1914 #ifdef USE_IM_CONTROL
1915 if (b_im_ptr
!= NULL
&& *b_im_ptr
!= B_IMODE_LMAP
)
1916 im_save_status(b_im_ptr
);
1917 im_set_active(FALSE
);
1923 ui_cursor_shape(); /* may show different cursor shape */
1927 char_u
*p
= ccline
.cmdbuff
;
1929 /* Make ccline empty, getcmdline() may try to use it. */
1930 ccline
.cmdbuff
= NULL
;
1935 #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1937 * Get a command line with a prompt.
1938 * This is prepared to be called recursively from getcmdline() (e.g. by
1939 * f_input() when evaluating an expression from CTRL-R =).
1940 * Returns the command line in allocated memory, or NULL.
1943 getcmdline_prompt(firstc
, prompt
, attr
, xp_context
, xp_arg
)
1945 char_u
*prompt
; /* command line prompt */
1946 int attr
; /* attributes for prompt */
1947 int xp_context
; /* type of expansion */
1948 char_u
*xp_arg
; /* user-defined expansion argument */
1951 struct cmdline_info save_ccline
;
1952 int msg_col_save
= msg_col
;
1954 save_cmdline(&save_ccline
);
1955 ccline
.cmdprompt
= prompt
;
1956 ccline
.cmdattr
= attr
;
1958 ccline
.xp_context
= xp_context
;
1959 ccline
.xp_arg
= xp_arg
;
1960 ccline
.input_fn
= (firstc
== '@');
1962 s
= getcmdline(firstc
, 1L, 0);
1963 restore_cmdline(&save_ccline
);
1964 /* Restore msg_col, the prompt from input() may have changed it. */
1965 msg_col
= msg_col_save
;
1972 * Return TRUE when the text must not be changed and we can't switch to
1973 * another window or buffer. Used when editing the command line, evaluating
1974 * 'balloonexpr', etc.
1980 if (cmdwin_type
!= 0)
1983 return textlock
!= 0;
1987 * Give an error message for a command that isn't allowed while the cmdline
1988 * window is open or editing the cmdline in another way.
1994 if (cmdwin_type
!= 0)
2001 #if defined(FEAT_AUTOCMD) || defined(PROTO)
2003 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
2009 if (curbuf_lock
> 0)
2011 EMSG(_("E788: Not allowed to edit another buffer now"));
2019 cmdline_charsize(idx
)
2022 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2023 if (cmdline_star
> 0) /* showing '*', always 1 position */
2026 return ptr2cells(ccline
.cmdbuff
+ idx
);
2030 * Compute the offset of the cursor on the command line for the prompt and
2036 if (ccline
.cmdfirstc
!= NUL
)
2037 ccline
.cmdspos
= 1 + ccline
.cmdindent
;
2039 ccline
.cmdspos
= 0 + ccline
.cmdindent
;
2043 * Compute the screen position for the cursor on the command line.
2046 set_cmdspos_cursor()
2054 if (m
< 0) /* overflow, Columns or Rows at weird value */
2059 for (i
= 0; i
< ccline
.cmdlen
&& i
< ccline
.cmdpos
; ++i
)
2061 c
= cmdline_charsize(i
);
2063 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2065 correct_cmdspos(i
, c
);
2067 /* If the cmdline doesn't fit, show cursor on last visible char.
2068 * Don't move the cursor itself, so we can still append. */
2069 if ((ccline
.cmdspos
+= c
) >= m
)
2071 ccline
.cmdspos
-= c
;
2076 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
) - 1;
2083 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2084 * character that doesn't fit, so that a ">" must be displayed.
2087 correct_cmdspos(idx
, cells
)
2091 if ((*mb_ptr2len
)(ccline
.cmdbuff
+ idx
) > 1
2092 && (*mb_ptr2cells
)(ccline
.cmdbuff
+ idx
) > 1
2093 && ccline
.cmdspos
% Columns
+ cells
> Columns
)
2099 * Get an Ex command line for the ":" command.
2103 getexline(c
, dummy
, indent
)
2104 int c
; /* normally ':', NUL for ":append" */
2105 void *dummy
; /* cookie not used */
2106 int indent
; /* indent for inside conditionals */
2108 /* When executing a register, remove ':' that's in front of each line. */
2109 if (exec_from_reg
&& vpeekc() == ':')
2111 return getcmdline(c
, 1L, indent
);
2115 * Get an Ex command line for Ex mode.
2116 * In Ex mode we only use the OS supplied line editing features and no
2117 * mappings or abbreviations.
2118 * Returns a string in allocated memory or NULL.
2122 getexmodeline(promptc
, dummy
, indent
)
2123 int promptc
; /* normally ':', NUL for ":append" and '?' for
2125 void *dummy
; /* cookie not used */
2126 int indent
; /* indent for inside conditionals */
2132 int escaped
= FALSE
; /* CTRL-V typed */
2137 /* Switch cursor on now. This avoids that it happens after the "\n", which
2138 * confuses the system function that computes tabstops. */
2141 /* always start in column 0; write a newline if necessary */
2143 if ((msg_col
|| msg_didout
) && promptc
!= '?')
2147 /* indent that is only displayed, not in the line itself */
2150 while (indent
-- > 0)
2155 ga_init2(&line_ga
, 1, 30);
2157 /* autoindent for :insert and :append is in the line itself */
2163 ga_append(&line_ga
, TAB
);
2164 msg_puts((char_u
*)" ");
2167 while (indent
-- > 0)
2169 ga_append(&line_ga
, ' ');
2177 * Get the line, one character at a time.
2182 if (ga_grow(&line_ga
, 40) == FAIL
)
2184 pend
= (char_u
*)line_ga
.ga_data
+ line_ga
.ga_len
;
2186 /* Get one character at a time. Don't use inchar(), it can't handle
2187 * special characters. */
2192 * Handle line editing.
2193 * Previously this was left to the system, putting the terminal in
2194 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
2204 /* CR typed means "enter", which is NL */
2208 if (c1
== BS
|| c1
== K_BS
2209 || c1
== DEL
|| c1
== K_DEL
|| c1
== K_KDEL
)
2211 if (line_ga
.ga_len
> 0)
2229 p
= (char_u
*)line_ga
.ga_data
;
2230 p
[line_ga
.ga_len
] = NUL
;
2231 indent
= get_indent_str(p
, 8);
2232 indent
+= curbuf
->b_p_sw
- indent
% curbuf
->b_p_sw
;
2234 while (get_indent_str(p
, 8) < indent
)
2236 char_u
*s
= skipwhite(p
);
2238 ga_grow(&line_ga
, 1);
2239 mch_memmove(s
+ 1, s
, line_ga
.ga_len
- (s
- p
) + 1);
2244 /* redraw the line */
2247 for (p
= (char_u
*)line_ga
.ga_data
;
2248 p
< (char_u
*)line_ga
.ga_data
+ line_ga
.ga_len
; ++p
)
2255 } while (++vcol
% 8);
2259 msg_outtrans_len(p
, 1);
2260 vcol
+= char2cells(*p
);
2264 windgoto(msg_row
, msg_col
);
2270 /* Delete one shiftwidth. */
2271 p
= (char_u
*)line_ga
.ga_data
;
2272 if (prev_char
== '0' || prev_char
== '^')
2274 if (prev_char
== '^')
2275 ex_keep_indent
= TRUE
;
2277 p
[--line_ga
.ga_len
] = NUL
;
2281 p
[line_ga
.ga_len
] = NUL
;
2282 indent
= get_indent_str(p
, 8);
2284 indent
-= indent
% curbuf
->b_p_sw
;
2286 while (get_indent_str(p
, 8) > indent
)
2288 char_u
*s
= skipwhite(p
);
2290 mch_memmove(s
- 1, s
, line_ga
.ga_len
- (s
- p
) + 1);
2296 if (c1
== Ctrl_V
|| c1
== Ctrl_Q
)
2302 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2309 ((char_u
*)line_ga
.ga_data
)[line_ga
.ga_len
] = c1
;
2314 /* Don't use chartabsize(), 'ts' can be different */
2318 } while (++vcol
% 8);
2323 ((char_u
*)line_ga
.ga_data
) + line_ga
.ga_len
, 1);
2324 vcol
+= char2cells(c1
);
2329 windgoto(msg_row
, msg_col
);
2330 pend
= (char_u
*)(line_ga
.ga_data
) + line_ga
.ga_len
;
2332 /* we are done when a NL is entered, but not when it comes after a
2334 if (line_ga
.ga_len
> 0 && pend
[-1] == '\n'
2335 && (line_ga
.ga_len
<= 1 || pend
[-2] != '\\'))
2347 /* make following messages go to the next line */
2350 if (msg_row
< Rows
- 1)
2352 emsg_on_display
= FALSE
; /* don't want ui_delay() */
2357 return (char_u
*)line_ga
.ga_data
;
2360 # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2361 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
2363 * Return TRUE if ccline.overstrike is on.
2366 cmdline_overstrike()
2368 return ccline
.overstrike
;
2372 * Return TRUE if the cursor is at the end of the cmdline.
2377 return (ccline
.cmdpos
>= ccline
.cmdlen
);
2381 #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
2383 * Return the virtual column number at the current cursor position.
2384 * This is used by the IM code to obtain the start of the preedit string.
2387 cmdline_getvcol_cursor()
2389 if (ccline
.cmdbuff
== NULL
|| ccline
.cmdpos
> ccline
.cmdlen
)
2398 for (col
= 0; i
< ccline
.cmdpos
; ++col
)
2399 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
);
2405 return ccline
.cmdpos
;
2409 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2411 * If part of the command line is an IM preedit string, redraw it with
2412 * IM feedback attributes. The cursor position is restored after drawing.
2417 if ((State
& CMDLINE
)
2419 /* && im_get_status() doesn't work when using SCIM */
2421 && im_is_preediting())
2431 cmdspos
= ((ccline
.cmdfirstc
!= NUL
) ? 1 : 0) + ccline
.cmdindent
;
2436 for (col
= 0; col
< preedit_start_col
2437 && cmdpos
< ccline
.cmdlen
; ++col
)
2439 cmdspos
+= (*mb_ptr2cells
)(ccline
.cmdbuff
+ cmdpos
);
2440 cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ cmdpos
);
2446 cmdspos
+= preedit_start_col
;
2447 cmdpos
+= preedit_start_col
;
2450 msg_row
= cmdline_row
+ (cmdspos
/ (int)Columns
);
2451 msg_col
= cmdspos
% (int)Columns
;
2452 if (msg_row
>= Rows
)
2455 for (col
= 0; cmdpos
< ccline
.cmdlen
; ++col
)
2460 char_attr
= im_get_feedback_attr(col
);
2462 break; /* end of preedit string */
2466 char_len
= (*mb_ptr2len
)(ccline
.cmdbuff
+ cmdpos
);
2471 msg_outtrans_len_attr(ccline
.cmdbuff
+ cmdpos
, char_len
, char_attr
);
2479 #endif /* FEAT_XIM && FEAT_GUI_GTK */
2482 * Allocate a new command line buffer.
2483 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2484 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2491 * give some extra space to avoid having to allocate all the time
2498 ccline
.cmdbuff
= alloc(len
); /* caller should check for out-of-memory */
2499 ccline
.cmdbufflen
= len
;
2503 * Re-allocate the command line to length len + something extra.
2504 * return FAIL for failure, OK otherwise
2507 realloc_cmdbuff(len
)
2513 alloc_cmdbuff(len
); /* will get some more */
2514 if (ccline
.cmdbuff
== NULL
) /* out of memory */
2516 ccline
.cmdbuff
= p
; /* keep the old one */
2519 mch_memmove(ccline
.cmdbuff
, p
, (size_t)ccline
.cmdlen
+ 1);
2522 if (ccline
.xpc
!= NULL
2523 && ccline
.xpc
->xp_pattern
!= NULL
2524 && ccline
.xpc
->xp_context
!= EXPAND_NOTHING
2525 && ccline
.xpc
->xp_context
!= EXPAND_UNSUCCESSFUL
)
2527 int i
= (int)(ccline
.xpc
->xp_pattern
- p
);
2529 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2530 * to point into the newly allocated memory. */
2531 if (i
>= 0 && i
<= ccline
.cmdlen
)
2532 ccline
.xpc
->xp_pattern
= ccline
.cmdbuff
+ i
;
2538 #if defined(FEAT_ARABIC) || defined(PROTO)
2539 static char_u
*arshape_buf
= NULL
;
2541 # if defined(EXITFREE) || defined(PROTO)
2545 vim_free(arshape_buf
);
2551 * Draw part of the cmdline at the current cursor position. But draw stars
2552 * when cmdline_star is TRUE.
2555 draw_cmdline(start
, len
)
2559 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2562 if (cmdline_star
> 0)
2563 for (i
= 0; i
< len
; ++i
)
2568 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ start
+ i
) - 1;
2574 if (p_arshape
&& !p_tbidi
&& enc_utf8
&& len
> 0)
2576 static int buflen
= 0;
2589 * Do arabic shaping into a temporary buffer. This is very
2592 if (len
* 2 + 2 > buflen
)
2594 /* Re-allocate the buffer. We keep it around to avoid a lot of
2595 * alloc()/free() calls. */
2596 vim_free(arshape_buf
);
2597 buflen
= len
* 2 + 2;
2598 arshape_buf
= alloc(buflen
);
2599 if (arshape_buf
== NULL
)
2600 return; /* out of memory */
2603 if (utf_iscomposing(utf_ptr2char(ccline
.cmdbuff
+ start
)))
2605 /* Prepend a space to draw the leading composing char on. */
2606 arshape_buf
[0] = ' ';
2610 for (j
= start
; j
< start
+ len
; j
+= mb_l
)
2612 p
= ccline
.cmdbuff
+ j
;
2613 u8c
= utfc_ptr2char_len(p
, u8cc
, start
+ len
- j
);
2614 mb_l
= utfc_ptr2len_len(p
, start
+ len
- j
);
2615 if (ARABIC_CHAR(u8c
))
2617 /* Do Arabic shaping. */
2620 /* displaying from right to left */
2624 if (j
+ mb_l
>= start
+ len
)
2627 nc
= utf_ptr2char(p
+ mb_l
);
2631 /* displaying from left to right */
2632 if (j
+ mb_l
>= start
+ len
)
2638 pc
= utfc_ptr2char_len(p
+ mb_l
, pcc
,
2639 start
+ len
- j
- mb_l
);
2646 u8c
= arabic_shape(u8c
, NULL
, &u8cc
[0], pc
, pc1
, nc
);
2648 newlen
+= (*mb_char2bytes
)(u8c
, arshape_buf
+ newlen
);
2651 newlen
+= (*mb_char2bytes
)(u8cc
[0], arshape_buf
+ newlen
);
2653 newlen
+= (*mb_char2bytes
)(u8cc
[1],
2654 arshape_buf
+ newlen
);
2660 mch_memmove(arshape_buf
+ newlen
, p
, mb_l
);
2665 msg_outtrans_len(arshape_buf
, newlen
);
2669 msg_outtrans_len(ccline
.cmdbuff
+ start
, len
);
2673 * Put a character on the command line. Shifts the following text to the
2674 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2675 * "c" must be printable (fit in one display cell)!
2678 putcmdline(c
, shift
)
2687 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
2688 msg_no_more
= FALSE
;
2693 * Undo a putcmdline(c, FALSE).
2701 if (ccline
.cmdlen
== ccline
.cmdpos
)
2704 draw_cmdline(ccline
.cmdpos
, 1);
2705 msg_no_more
= FALSE
;
2710 * Put the given string, of the given length, onto the command line.
2711 * If len is -1, then STRLEN() is used to calculate the length.
2712 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2713 * part will be redrawn, otherwise it will not. If this function is called
2714 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2715 * called afterwards.
2718 put_on_cmdline(str
, len
, redraw
)
2729 len
= (int)STRLEN(str
);
2731 /* Check if ccline.cmdbuff needs to be longer */
2732 if (ccline
.cmdlen
+ len
+ 1 >= ccline
.cmdbufflen
)
2733 retval
= realloc_cmdbuff(ccline
.cmdlen
+ len
);
2738 if (!ccline
.overstrike
)
2740 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
+ len
,
2741 ccline
.cmdbuff
+ ccline
.cmdpos
,
2742 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
));
2743 ccline
.cmdlen
+= len
;
2750 /* Count nr of characters in the new string. */
2752 for (i
= 0; i
< len
; i
+= (*mb_ptr2len
)(str
+ i
))
2754 /* Count nr of bytes in cmdline that are overwritten by these
2756 for (i
= ccline
.cmdpos
; i
< ccline
.cmdlen
&& m
> 0;
2757 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
))
2759 if (i
< ccline
.cmdlen
)
2761 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
+ len
,
2762 ccline
.cmdbuff
+ i
, (size_t)(ccline
.cmdlen
- i
));
2763 ccline
.cmdlen
+= ccline
.cmdpos
+ len
- i
;
2766 ccline
.cmdlen
= ccline
.cmdpos
+ len
;
2770 if (ccline
.cmdpos
+ len
> ccline
.cmdlen
)
2771 ccline
.cmdlen
= ccline
.cmdpos
+ len
;
2773 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
, str
, (size_t)len
);
2774 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
2779 /* When the inserted text starts with a composing character,
2780 * backup to the character before it. There could be two of them.
2783 c
= utf_ptr2char(ccline
.cmdbuff
+ ccline
.cmdpos
);
2784 while (ccline
.cmdpos
> 0 && utf_iscomposing(c
))
2786 i
= (*mb_head_off
)(ccline
.cmdbuff
,
2787 ccline
.cmdbuff
+ ccline
.cmdpos
- 1) + 1;
2790 c
= utf_ptr2char(ccline
.cmdbuff
+ ccline
.cmdpos
);
2793 if (i
== 0 && ccline
.cmdpos
> 0 && arabic_maycombine(c
))
2795 /* Check the previous character for Arabic combining pair. */
2796 i
= (*mb_head_off
)(ccline
.cmdbuff
,
2797 ccline
.cmdbuff
+ ccline
.cmdpos
- 1) + 1;
2798 if (arabic_combine(utf_ptr2char(ccline
.cmdbuff
2799 + ccline
.cmdpos
- i
), c
))
2810 /* Also backup the cursor position. */
2811 i
= ptr2cells(ccline
.cmdbuff
+ ccline
.cmdpos
);
2812 ccline
.cmdspos
-= i
;
2823 if (redraw
&& !cmd_silent
)
2827 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
2828 /* Avoid clearing the rest of the line too often. */
2829 if (cmdline_row
!= i
|| ccline
.overstrike
)
2831 msg_no_more
= FALSE
;
2835 * If we are in Farsi command mode, the character input must be in
2836 * Insert mode. So do not advance the cmdpos.
2844 if (m
< 0) /* overflow, Columns or Rows at weird value */
2849 for (i
= 0; i
< len
; ++i
)
2851 c
= cmdline_charsize(ccline
.cmdpos
);
2853 /* count ">" for a double-wide char that doesn't fit. */
2855 correct_cmdspos(ccline
.cmdpos
, c
);
2857 /* Stop cursor at the end of the screen, but do increment the
2858 * insert position, so that entering a very long command
2859 * works, even though you can't see it. */
2860 if (ccline
.cmdspos
+ c
< m
)
2861 ccline
.cmdspos
+= c
;
2865 c
= (*mb_ptr2len
)(ccline
.cmdbuff
+ ccline
.cmdpos
) - 1;
2866 if (c
> len
- i
- 1)
2881 static struct cmdline_info prev_ccline
;
2882 static int prev_ccline_used
= FALSE
;
2885 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2886 * and overwrite it. But get_cmdline_str() may need it, thus make it
2887 * available globally in prev_ccline.
2891 struct cmdline_info
*ccp
;
2893 if (!prev_ccline_used
)
2895 vim_memset(&prev_ccline
, 0, sizeof(struct cmdline_info
));
2896 prev_ccline_used
= TRUE
;
2899 prev_ccline
= ccline
;
2900 ccline
.cmdbuff
= NULL
;
2901 ccline
.cmdprompt
= NULL
;
2906 * Restore ccline after it has been saved with save_cmdline().
2909 restore_cmdline(ccp
)
2910 struct cmdline_info
*ccp
;
2912 ccline
= prev_ccline
;
2916 #if defined(FEAT_EVAL) || defined(PROTO)
2918 * Save the command line into allocated memory. Returns a pointer to be
2919 * passed to restore_cmdline_alloc() later.
2920 * Returns NULL when failed.
2923 save_cmdline_alloc()
2925 struct cmdline_info
*p
;
2927 p
= (struct cmdline_info
*)alloc((unsigned)sizeof(struct cmdline_info
));
2934 * Restore the command line from the return value of save_cmdline_alloc().
2937 restore_cmdline_alloc(p
)
2942 restore_cmdline((struct cmdline_info
*)p
);
2949 * paste a yank register into the command line.
2950 * used by CTRL-R command in command-line mode
2951 * insert_reg() can't be used here, because special characters from the
2952 * register contents will be interpreted as commands.
2954 * return FAIL for failure, OK otherwise
2957 cmdline_paste(regname
, literally
, remcr
)
2959 int literally
; /* Insert text literally instead of "as typed" */
2960 int remcr
; /* remove trailing CR */
2966 struct cmdline_info save_ccline
;
2968 /* check for valid regname; also accept special characters for CTRL-R in
2969 * the command line */
2970 if (regname
!= Ctrl_F
&& regname
!= Ctrl_P
&& regname
!= Ctrl_W
2971 && regname
!= Ctrl_A
&& !valid_yank_reg(regname
, FALSE
))
2974 /* A register containing CTRL-R can cause an endless loop. Allow using
2975 * CTRL-C to break the loop. */
2980 #ifdef FEAT_CLIPBOARD
2981 regname
= may_get_selection(regname
);
2984 /* Need to save and restore ccline. And set "textlock" to avoid nasty
2985 * things like going to another buffer when evaluating an expression. */
2986 save_cmdline(&save_ccline
);
2988 i
= get_spec_reg(regname
, &arg
, &allocated
, TRUE
);
2990 restore_cmdline(&save_ccline
);
2994 /* Got the value of a special register in "arg". */
2998 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2999 * part of the word. */
3001 if (p_is
&& regname
== Ctrl_W
)
3006 /* Locate start of last word in the cmd buffer. */
3007 for (w
= ccline
.cmdbuff
+ ccline
.cmdlen
; w
> ccline
.cmdbuff
; )
3012 len
= (*mb_head_off
)(ccline
.cmdbuff
, w
- 1) + 1;
3013 if (!vim_iswordc(mb_ptr2char(w
- len
)))
3020 if (!vim_iswordc(w
[-1]))
3025 len
= (int)((ccline
.cmdbuff
+ ccline
.cmdlen
) - w
);
3026 if (p_ic
? STRNICMP(w
, arg
, len
) == 0 : STRNCMP(w
, arg
, len
) == 0)
3030 cmdline_paste_str(p
, literally
);
3036 return cmdline_paste_reg(regname
, literally
, remcr
);
3040 * Put a string on the command line.
3041 * When "literally" is TRUE, insert literally.
3042 * When "literally" is FALSE, insert as typed, but don't leave the command
3046 cmdline_paste_str(s
, literally
)
3053 put_on_cmdline(s
, -1, TRUE
);
3058 if (cv
== Ctrl_V
&& s
[1])
3062 c
= mb_cptr2char_adv(&s
);
3066 if (cv
== Ctrl_V
|| c
== ESC
|| c
== Ctrl_C
|| c
== CAR
|| c
== NL
3070 || (c
== Ctrl_BSL
&& *s
== Ctrl_N
))
3071 stuffcharReadbuff(Ctrl_V
);
3072 stuffcharReadbuff(c
);
3076 #ifdef FEAT_WILDMENU
3078 * Delete characters on the command line, from "from" to the current
3085 mch_memmove(ccline
.cmdbuff
+ from
, ccline
.cmdbuff
+ ccline
.cmdpos
,
3086 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
+ 1));
3087 ccline
.cmdlen
-= ccline
.cmdpos
- from
;
3088 ccline
.cmdpos
= from
;
3093 * this function is called when the screen size changes and with incremental
3101 need_wait_return
= FALSE
;
3114 if (ccline
.cmdfirstc
!= NUL
)
3115 msg_putchar(ccline
.cmdfirstc
);
3116 if (ccline
.cmdprompt
!= NULL
)
3118 msg_puts_attr(ccline
.cmdprompt
, ccline
.cmdattr
);
3119 ccline
.cmdindent
= msg_col
+ (msg_row
- cmdline_row
) * Columns
;
3120 /* do the reverse of set_cmdspos() */
3121 if (ccline
.cmdfirstc
!= NUL
)
3125 for (i
= ccline
.cmdindent
; i
> 0; --i
)
3130 * Redraw what is currently on the command line.
3138 /* when 'incsearch' is set there may be no command line while redrawing */
3139 if (ccline
.cmdbuff
== NULL
)
3141 windgoto(cmdline_row
, 0);
3149 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3151 draw_cmdline(0, ccline
.cmdlen
);
3153 msg_no_more
= FALSE
;
3155 set_cmdspos_cursor();
3158 * An emsg() before may have set msg_scroll. This is used in normal mode,
3159 * in cmdline mode we can reset them now.
3161 msg_scroll
= FALSE
; /* next message overwrites cmdline */
3163 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3164 * in cmdline mode */
3165 skip_redraw
= FALSE
;
3171 if (exmode_active
|| msg_scrolled
!= 0)
3172 cmdline_row
= Rows
- 1;
3174 cmdline_row
= W_WINROW(lastwin
) + lastwin
->w_height
3175 + W_STATUS_HEIGHT(lastwin
);
3184 #ifdef FEAT_RIGHTLEFT
3187 msg_row
= cmdline_row
+ (ccline
.cmdspos
/ (int)(Columns
- 1));
3188 msg_col
= (int)Columns
- (ccline
.cmdspos
% (int)(Columns
- 1)) - 1;
3195 msg_row
= cmdline_row
+ (ccline
.cmdspos
/ (int)Columns
);
3196 msg_col
= ccline
.cmdspos
% (int)Columns
;
3197 if (msg_row
>= Rows
)
3201 windgoto(msg_row
, msg_col
);
3202 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3203 redrawcmd_preedit();
3205 #ifdef MCH_CURSOR_SHAPE
3206 mch_update_cursor();
3215 #ifdef FEAT_RIGHTLEFT
3217 msg_col
= Columns
- 1;
3220 msg_col
= 0; /* always start in column 0 */
3221 if (clr
) /* clear the bottom line(s) */
3222 msg_clr_eos(); /* will reset clear_cmdline */
3223 windgoto(cmdline_row
, 0);
3227 * Check the word in front of the cursor for an abbreviation.
3228 * Called when the non-id character "c" has been entered.
3229 * When an abbreviation is recognized it is removed from the text with
3230 * backspaces and the replacement string is inserted, followed by "c".
3236 if (p_paste
|| no_abbr
) /* no abbreviations or in paste mode */
3239 return check_abbr(c
, ccline
.cmdbuff
, ccline
.cmdpos
, 0);
3243 * Return FAIL if this is not an appropriate context in which to do
3244 * completion of anything, return OK if it is (even if there are no matches).
3245 * For the caller, this means that the character is just passed through like a
3246 * normal character (instead of being expanded). This allows :s/^I^D etc.
3249 nextwild(xp
, type
, options
)
3252 int options
; /* extra options for ExpandOne() */
3261 if (xp
->xp_numfiles
== -1)
3263 set_expand_context(xp
);
3264 cmd_showtail
= expand_showtail(xp
);
3267 if (xp
->xp_context
== EXPAND_UNSUCCESSFUL
)
3270 return OK
; /* Something illegal on command line */
3272 if (xp
->xp_context
== EXPAND_NOTHING
)
3274 /* Caller can use the character as a normal char instead */
3278 MSG_PUTS("..."); /* show that we are busy */
3281 i
= (int)(xp
->xp_pattern
- ccline
.cmdbuff
);
3282 oldlen
= ccline
.cmdpos
- i
;
3284 if (type
== WILD_NEXT
|| type
== WILD_PREV
)
3287 * Get next/previous match for a previous expanded pattern.
3289 p2
= ExpandOne(xp
, NULL
, NULL
, 0, type
);
3294 * Translate string into pattern and expand it.
3296 if ((p1
= addstar(&ccline
.cmdbuff
[i
], oldlen
, xp
->xp_context
)) == NULL
)
3300 p2
= ExpandOne(xp
, p1
, vim_strnsave(&ccline
.cmdbuff
[i
], oldlen
),
3301 WILD_HOME_REPLACE
|WILD_ADD_SLASH
|WILD_SILENT
|WILD_ESCAPE
3304 /* longest match: make sure it is not shorter (happens with :help */
3305 if (p2
!= NULL
&& type
== WILD_LONGEST
)
3307 for (j
= 0; j
< oldlen
; ++j
)
3308 if (ccline
.cmdbuff
[i
+ j
] == '*'
3309 || ccline
.cmdbuff
[i
+ j
] == '?')
3311 if ((int)STRLEN(p2
) < j
)
3320 if (p2
!= NULL
&& !got_int
)
3322 difflen
= (int)STRLEN(p2
) - oldlen
;
3323 if (ccline
.cmdlen
+ difflen
> ccline
.cmdbufflen
- 4)
3325 v
= realloc_cmdbuff(ccline
.cmdlen
+ difflen
);
3326 xp
->xp_pattern
= ccline
.cmdbuff
+ i
;
3332 mch_memmove(&ccline
.cmdbuff
[ccline
.cmdpos
+ difflen
],
3333 &ccline
.cmdbuff
[ccline
.cmdpos
],
3334 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
+ 1));
3335 mch_memmove(&ccline
.cmdbuff
[i
], p2
, STRLEN(p2
));
3336 ccline
.cmdlen
+= difflen
;
3337 ccline
.cmdpos
+= difflen
;
3345 /* When expanding a ":map" command and no matches are found, assume that
3346 * the key is supposed to be inserted literally */
3347 if (xp
->xp_context
== EXPAND_MAPPINGS
&& p2
== NULL
)
3350 if (xp
->xp_numfiles
<= 0 && p2
== NULL
)
3352 else if (xp
->xp_numfiles
== 1)
3353 /* free expanded pattern */
3354 (void)ExpandOne(xp
, NULL
, NULL
, 0, WILD_FREE
);
3360 * Do wildcard expansion on the string 'str'.
3361 * Chars that should not be expanded must be preceded with a backslash.
3362 * Return a pointer to allocated memory containing the new string.
3363 * Return NULL for failure.
3365 * "orig" is the originally expanded string, copied to allocated memory. It
3366 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3367 * WILD_PREV "orig" should be NULL.
3369 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3370 * is WILD_EXPAND_FREE or WILD_ALL.
3372 * mode = WILD_FREE: just free previously expanded matches
3373 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3374 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3375 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3376 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3377 * mode = WILD_ALL: return all matches concatenated
3378 * mode = WILD_LONGEST: return longest matched part
3380 * options = WILD_LIST_NOTFOUND: list entries without a match
3381 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3382 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3383 * options = WILD_NO_BEEP: Don't beep for multiple matches
3384 * options = WILD_ADD_SLASH: add a slash after directory names
3385 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3386 * options = WILD_SILENT: don't print warning messages
3387 * options = WILD_ESCAPE: put backslash before special chars
3389 * The variables xp->xp_context and xp->xp_backslash must have been set!
3392 ExpandOne(xp
, str
, orig
, options
, mode
)
3395 char_u
*orig
; /* allocated copy of original of expanded string */
3401 static char_u
*orig_save
= NULL
; /* kept value of orig */
3402 int orig_saved
= FALSE
;
3405 int non_suf_match
; /* number without matching suffix */
3408 * first handle the case of using an old match
3410 if (mode
== WILD_NEXT
|| mode
== WILD_PREV
)
3412 if (xp
->xp_numfiles
> 0)
3414 if (mode
== WILD_PREV
)
3417 findex
= xp
->xp_numfiles
;
3420 else /* mode == WILD_NEXT */
3424 * When wrapping around, return the original string, set findex to
3429 if (orig_save
== NULL
)
3430 findex
= xp
->xp_numfiles
- 1;
3434 if (findex
>= xp
->xp_numfiles
)
3436 if (orig_save
== NULL
)
3441 #ifdef FEAT_WILDMENU
3443 win_redr_status_matches(xp
, xp
->xp_numfiles
, xp
->xp_files
,
3444 findex
, cmd_showtail
);
3447 return vim_strsave(orig_save
);
3448 return vim_strsave(xp
->xp_files
[findex
]);
3454 /* free old names */
3455 if (xp
->xp_numfiles
!= -1 && mode
!= WILD_ALL
&& mode
!= WILD_LONGEST
)
3457 FreeWild(xp
->xp_numfiles
, xp
->xp_files
);
3458 xp
->xp_numfiles
= -1;
3459 vim_free(orig_save
);
3464 if (mode
== WILD_FREE
) /* only release file name */
3467 if (xp
->xp_numfiles
== -1)
3469 vim_free(orig_save
);
3476 if (ExpandFromContext(xp
, str
, &xp
->xp_numfiles
, &xp
->xp_files
,
3479 #ifdef FNAME_ILLEGAL
3480 /* Illegal file name has been silently skipped. But when there
3481 * are wildcards, the real problem is that there was no match,
3482 * causing the pattern to be added, which has illegal characters.
3484 if (!(options
& WILD_SILENT
) && (options
& WILD_LIST_NOTFOUND
))
3485 EMSG2(_(e_nomatch2
), str
);
3488 else if (xp
->xp_numfiles
== 0)
3490 if (!(options
& WILD_SILENT
))
3491 EMSG2(_(e_nomatch2
), str
);
3495 /* Escape the matches for use on the command line. */
3496 ExpandEscape(xp
, str
, xp
->xp_numfiles
, xp
->xp_files
, options
);
3499 * Check for matching suffixes in file names.
3501 if (mode
!= WILD_ALL
&& mode
!= WILD_LONGEST
)
3503 if (xp
->xp_numfiles
)
3504 non_suf_match
= xp
->xp_numfiles
;
3507 if ((xp
->xp_context
== EXPAND_FILES
3508 || xp
->xp_context
== EXPAND_DIRECTORIES
)
3509 && xp
->xp_numfiles
> 1)
3512 * More than one match; check suffix.
3513 * The files will have been sorted on matching suffix in
3514 * expand_wildcards, only need to check the first two.
3517 for (i
= 0; i
< 2; ++i
)
3518 if (match_suffix(xp
->xp_files
[i
]))
3521 if (non_suf_match
!= 1)
3523 /* Can we ever get here unless it's while expanding
3524 * interactively? If not, we can get rid of this all
3525 * together. Don't really want to wait for this message
3526 * (and possibly have to hit return to continue!).
3528 if (!(options
& WILD_SILENT
))
3530 else if (!(options
& WILD_NO_BEEP
))
3533 if (!(non_suf_match
!= 1 && mode
== WILD_EXPAND_FREE
))
3534 ss
= vim_strsave(xp
->xp_files
[0]);
3539 /* Find longest common part */
3540 if (mode
== WILD_LONGEST
&& xp
->xp_numfiles
> 0)
3542 for (len
= 0; xp
->xp_files
[0][len
]; ++len
)
3544 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3546 #ifdef CASE_INSENSITIVE_FILENAME
3547 if (xp
->xp_context
== EXPAND_DIRECTORIES
3548 || xp
->xp_context
== EXPAND_FILES
3549 || xp
->xp_context
== EXPAND_SHELLCMD
3550 || xp
->xp_context
== EXPAND_BUFFERS
)
3552 if (TOLOWER_LOC(xp
->xp_files
[i
][len
]) !=
3553 TOLOWER_LOC(xp
->xp_files
[0][len
]))
3558 if (xp
->xp_files
[i
][len
] != xp
->xp_files
[0][len
])
3561 if (i
< xp
->xp_numfiles
)
3563 if (!(options
& WILD_NO_BEEP
))
3568 ss
= alloc((unsigned)len
+ 1);
3570 vim_strncpy(ss
, xp
->xp_files
[0], (size_t)len
);
3571 findex
= -1; /* next p_wc gets first one */
3574 /* Concatenate all matching names */
3575 if (mode
== WILD_ALL
&& xp
->xp_numfiles
> 0)
3578 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3579 len
+= (long_u
)STRLEN(xp
->xp_files
[i
]) + 1;
3580 ss
= lalloc(len
, TRUE
);
3584 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3586 STRCAT(ss
, xp
->xp_files
[i
]);
3587 if (i
!= xp
->xp_numfiles
- 1)
3588 STRCAT(ss
, (options
& WILD_USE_NL
) ? "\n" : " ");
3593 if (mode
== WILD_EXPAND_FREE
|| mode
== WILD_ALL
)
3596 /* Free "orig" if it wasn't stored in "orig_save". */
3604 * Prepare an expand structure for use.
3610 xp
->xp_pattern
= NULL
;
3611 xp
->xp_backslash
= XP_BS_NONE
;
3612 #ifndef BACKSLASH_IN_FILENAME
3613 xp
->xp_shell
= FALSE
;
3615 xp
->xp_numfiles
= -1;
3616 xp
->xp_files
= NULL
;
3617 #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3623 * Cleanup an expand structure after use.
3629 if (xp
->xp_numfiles
>= 0)
3631 FreeWild(xp
->xp_numfiles
, xp
->xp_files
);
3632 xp
->xp_numfiles
= -1;
3637 ExpandEscape(xp
, str
, numfiles
, files
, options
)
3648 * May change home directory back to "~"
3650 if (options
& WILD_HOME_REPLACE
)
3651 tilde_replace(str
, numfiles
, files
);
3653 if (options
& WILD_ESCAPE
)
3655 if (xp
->xp_context
== EXPAND_FILES
3656 || xp
->xp_context
== EXPAND_SHELLCMD
3657 || xp
->xp_context
== EXPAND_BUFFERS
3658 || xp
->xp_context
== EXPAND_DIRECTORIES
)
3661 * Insert a backslash into a file name before a space, \, %, #
3662 * and wildmatch characters, except '~'.
3664 for (i
= 0; i
< numfiles
; ++i
)
3666 /* for ":set path=" we need to escape spaces twice */
3667 if (xp
->xp_backslash
== XP_BS_THREE
)
3669 p
= vim_strsave_escaped(files
[i
], (char_u
*)" ");
3674 #if defined(BACKSLASH_IN_FILENAME)
3675 p
= vim_strsave_escaped(files
[i
], (char_u
*)" ");
3684 #ifdef BACKSLASH_IN_FILENAME
3685 p
= vim_strsave_fnameescape(files
[i
], FALSE
);
3687 p
= vim_strsave_fnameescape(files
[i
], xp
->xp_shell
);
3695 /* If 'str' starts with "\~", replace "~" at start of
3696 * files[i] with "\~". */
3697 if (str
[0] == '\\' && str
[1] == '~' && files
[i
][0] == '~')
3698 escape_fname(&files
[i
]);
3700 xp
->xp_backslash
= XP_BS_NONE
;
3702 /* If the first file starts with a '+' escape it. Otherwise it
3703 * could be seen as "+cmd". */
3704 if (*files
[0] == '+')
3705 escape_fname(&files
[0]);
3707 else if (xp
->xp_context
== EXPAND_TAGS
)
3710 * Insert a backslash before characters in a tag name that
3711 * would terminate the ":tag" command.
3713 for (i
= 0; i
< numfiles
; ++i
)
3715 p
= vim_strsave_escaped(files
[i
], (char_u
*)"\\|\"");
3727 * Escape special characters in "fname" for when used as a file name argument
3728 * after a Vim command, or, when "shell" is non-zero, a shell command.
3729 * Returns the result in allocated memory.
3732 vim_strsave_fnameescape(fname
, shell
)
3737 #ifdef BACKSLASH_IN_FILENAME
3741 /* Don't escape '[' and '{' if they are in 'isfname'. */
3742 for (p
= PATH_ESC_CHARS
; *p
!= NUL
; ++p
)
3743 if ((*p
!= '[' && *p
!= '{') || !vim_isfilec(*p
))
3746 p
= vim_strsave_escaped(fname
, buf
);
3748 p
= vim_strsave_escaped(fname
, shell
? SHELL_ESC_CHARS
: PATH_ESC_CHARS
);
3749 if (shell
&& csh_like_shell() && p
!= NULL
)
3753 /* For csh and similar shells need to put two backslashes before '!'.
3754 * One is taken by Vim, one by the shell. */
3755 s
= vim_strsave_escaped(p
, (char_u
*)"!");
3761 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3762 * ":write". "cd -" has a special meaning. */
3763 if (*p
== '>' || *p
== '+' || (*p
== '-' && p
[1] == NUL
))
3770 * Put a backslash before the file name in "pp", which is in allocated memory.
3778 p
= alloc((unsigned)(STRLEN(*pp
) + 2));
3789 * For each file name in files[num_files]:
3790 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3793 tilde_replace(orig_pat
, num_files
, files
)
3801 if (orig_pat
[0] == '~' && vim_ispathsep(orig_pat
[1]))
3803 for (i
= 0; i
< num_files
; ++i
)
3805 p
= home_replace_save(NULL
, files
[i
]);
3816 * Show all matches for completion on the command line.
3817 * Returns EXPAND_NOTHING when the character that triggered expansion should
3818 * be inserted like a normal character.
3822 showmatches(xp
, wildmenu
)
3826 #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3828 char_u
**files_found
;
3838 if (xp
->xp_numfiles
== -1)
3840 set_expand_context(xp
);
3841 i
= expand_cmdline(xp
, ccline
.cmdbuff
, ccline
.cmdpos
,
3842 &num_files
, &files_found
);
3843 showtail
= expand_showtail(xp
);
3850 num_files
= xp
->xp_numfiles
;
3851 files_found
= xp
->xp_files
;
3852 showtail
= cmd_showtail
;
3855 #ifdef FEAT_WILDMENU
3859 msg_didany
= FALSE
; /* lines_left will be set */
3860 msg_start(); /* prepare for paging */
3863 cmdline_row
= msg_row
;
3864 msg_didany
= FALSE
; /* lines_left will be set again */
3865 msg_start(); /* prepare for paging */
3866 #ifdef FEAT_WILDMENU
3871 got_int
= FALSE
; /* only int. the completion, not the cmd line */
3872 #ifdef FEAT_WILDMENU
3874 win_redr_status_matches(xp
, num_files
, files_found
, 0, showtail
);
3878 /* find the length of the longest file name */
3880 for (i
= 0; i
< num_files
; ++i
)
3882 if (!showtail
&& (xp
->xp_context
== EXPAND_FILES
3883 || xp
->xp_context
== EXPAND_SHELLCMD
3884 || xp
->xp_context
== EXPAND_BUFFERS
))
3886 home_replace(NULL
, files_found
[i
], NameBuff
, MAXPATHL
, TRUE
);
3887 j
= vim_strsize(NameBuff
);
3890 j
= vim_strsize(L_SHOWFILE(i
));
3895 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3899 /* compute the number of columns and lines for the listing */
3900 maxlen
+= 2; /* two spaces between file names */
3901 columns
= ((int)Columns
+ 2) / maxlen
;
3904 lines
= (num_files
+ columns
- 1) / columns
;
3907 attr
= hl_attr(HLF_D
); /* find out highlighting for directories */
3909 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3911 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T
));
3913 msg_advance(maxlen
- 3);
3914 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T
));
3917 /* list the files line by line */
3918 for (i
= 0; i
< lines
; ++i
)
3921 for (k
= i
; k
< num_files
; k
+= lines
)
3923 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3925 msg_outtrans_attr(files_found
[k
], hl_attr(HLF_D
));
3926 p
= files_found
[k
] + STRLEN(files_found
[k
]) + 1;
3927 msg_advance(maxlen
+ 1);
3929 msg_advance(maxlen
+ 3);
3930 msg_puts_long_attr(p
+ 2, hl_attr(HLF_D
));
3933 for (j
= maxlen
- lastlen
; --j
>= 0; )
3935 if (xp
->xp_context
== EXPAND_FILES
3936 || xp
->xp_context
== EXPAND_SHELLCMD
3937 || xp
->xp_context
== EXPAND_BUFFERS
)
3939 /* highlight directories */
3940 j
= (mch_isdir(files_found
[k
]));
3945 home_replace(NULL
, files_found
[k
], NameBuff
, MAXPATHL
,
3955 lastlen
= msg_outtrans_attr(p
, j
? attr
: 0);
3957 if (msg_col
> 0) /* when not wrapped around */
3962 out_flush(); /* show one line at a time */
3971 * we redraw the command below the lines that we have just listed
3972 * This is a bit tricky, but it saves a lot of screen updating.
3974 cmdline_row
= msg_row
; /* will put it back later */
3977 if (xp
->xp_numfiles
== -1)
3978 FreeWild(num_files
, files_found
);
3984 * Private gettail for showmatches() (and win_redr_status_matches()):
3985 * Find tail of file name path, but ignore trailing "/".
3993 int had_sep
= FALSE
;
3995 for (p
= s
; *p
!= NUL
; )
3997 if (vim_ispathsep(*p
)
3998 #ifdef BACKSLASH_IN_FILENAME
3999 && !rem_backslash(p
)
4014 * Return TRUE if we only need to show the tail of completion matches.
4015 * When not completing file names or there is a wildcard in the path FALSE is
4025 /* When not completing file names a "/" may mean something different. */
4026 if (xp
->xp_context
!= EXPAND_FILES
4027 && xp
->xp_context
!= EXPAND_SHELLCMD
4028 && xp
->xp_context
!= EXPAND_DIRECTORIES
)
4031 end
= gettail(xp
->xp_pattern
);
4032 if (end
== xp
->xp_pattern
) /* there is no path separator */
4035 for (s
= xp
->xp_pattern
; s
< end
; s
++)
4037 /* Skip escaped wildcards. Only when the backslash is not a path
4038 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4039 if (rem_backslash(s
))
4041 else if (vim_strchr((char_u
*)"*?[", *s
) != NULL
)
4048 * Prepare a string for expansion.
4049 * When expanding file names: The string will be used with expand_wildcards().
4050 * Copy the file name into allocated memory and add a '*' at the end.
4051 * When expanding other names: The string will be used with regcomp(). Copy
4052 * the name into allocated memory and prepend "^".
4055 addstar(fname
, len
, context
)
4058 int context
; /* EXPAND_FILES etc. */
4065 if (context
!= EXPAND_FILES
4066 && context
!= EXPAND_SHELLCMD
4067 && context
!= EXPAND_DIRECTORIES
)
4070 * Matching will be done internally (on something other than files).
4071 * So we convert the file-matching-type wildcards into our kind for
4072 * use with vim_regcomp(). First work out how long it will be:
4075 /* For help tags the translation is done in find_help_tags().
4076 * For a tag pattern starting with "/" no translation is needed. */
4077 if (context
== EXPAND_HELP
4078 || context
== EXPAND_COLORS
4079 || context
== EXPAND_COMPILER
4080 || (context
== EXPAND_TAGS
&& fname
[0] == '/'))
4081 retval
= vim_strnsave(fname
, len
);
4084 new_len
= len
+ 2; /* +2 for '^' at start, NUL at end */
4085 for (i
= 0; i
< len
; i
++)
4087 if (fname
[i
] == '*' || fname
[i
] == '~')
4088 new_len
++; /* '*' needs to be replaced by ".*"
4089 '~' needs to be replaced by "\~" */
4091 /* Buffer names are like file names. "." should be literal */
4092 if (context
== EXPAND_BUFFERS
&& fname
[i
] == '.')
4093 new_len
++; /* "." becomes "\." */
4095 /* Custom expansion takes care of special things, match
4096 * backslashes literally (perhaps also for other types?) */
4097 if ((context
== EXPAND_USER_DEFINED
4098 || context
== EXPAND_USER_LIST
) && fname
[i
] == '\\')
4099 new_len
++; /* '\' becomes "\\" */
4101 retval
= alloc(new_len
);
4106 for (i
= 0; i
< len
; i
++, j
++)
4108 /* Skip backslash. But why? At least keep it for custom
4110 if (context
!= EXPAND_USER_DEFINED
4111 && context
!= EXPAND_USER_LIST
4118 case '*': retval
[j
++] = '.';
4120 case '~': retval
[j
++] = '\\';
4122 case '?': retval
[j
] = '.';
4124 case '.': if (context
== EXPAND_BUFFERS
)
4127 case '\\': if (context
== EXPAND_USER_DEFINED
4128 || context
== EXPAND_USER_LIST
)
4132 retval
[j
] = fname
[i
];
4140 retval
= alloc(len
+ 4);
4143 vim_strncpy(retval
, fname
, len
);
4146 * Don't add a star to *, ~, ~user, $var or `cmd`.
4147 * * would become **, which walks the whole tree.
4148 * ~ would be at the start of the file name, but not the tail.
4149 * $ could be anywhere in the tail.
4150 * ` could be anywhere in the file name.
4151 * When the name ends in '$' don't add a star, remove the '$'.
4153 tail
= gettail(retval
);
4154 if ((*retval
!= '~' || tail
!= retval
)
4155 && (len
== 0 || retval
[len
- 1] != '*')
4156 && vim_strchr(tail
, '$') == NULL
4157 && vim_strchr(retval
, '`') == NULL
)
4158 retval
[len
++] = '*';
4159 else if (len
> 0 && retval
[len
- 1] == '$')
4168 * Must parse the command line so far to work out what context we are in.
4169 * Completion can then be done based on that context.
4170 * This routine sets the variables:
4171 * xp->xp_pattern The start of the pattern to be expanded within
4172 * the command line (ends at the cursor).
4173 * xp->xp_context The type of thing to expand. Will be one of:
4175 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4176 * the command line, like an unknown command. Caller
4178 * EXPAND_NOTHING Unrecognised context for completion, use char like
4179 * a normal char, rather than for completion. eg
4181 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4183 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4184 * EXPAND_FILES After command with XFILE set, or after setting
4185 * with P_EXPAND set. eg :e ^I, :w>>^I
4186 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4187 * when we know only directories are of interest. eg
4189 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
4190 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4191 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4192 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4193 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4194 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4195 * EXPAND_EVENTS Complete event names
4196 * EXPAND_SYNTAX Complete :syntax command arguments
4197 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4198 * EXPAND_AUGROUP Complete autocommand group names
4199 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4200 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4201 * eg :unmap a^I , :cunab x^I
4202 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4204 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4205 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4206 * names in expressions, eg :while s^I
4207 * EXPAND_ENV_VARS Complete environment variable names
4210 set_expand_context(xp
)
4213 /* only expansion for ':', '>' and '=' command-lines */
4214 if (ccline
.cmdfirstc
!= ':'
4216 && ccline
.cmdfirstc
!= '>' && ccline
.cmdfirstc
!= '='
4221 xp
->xp_context
= EXPAND_NOTHING
;
4224 set_cmd_context(xp
, ccline
.cmdbuff
, ccline
.cmdlen
, ccline
.cmdpos
);
4228 set_cmd_context(xp
, str
, len
, col
)
4230 char_u
*str
; /* start of command line */
4231 int len
; /* length of command line (excl. NUL) */
4232 int col
; /* position of cursor */
4238 * Avoid a UMR warning from Purify, only save the character if it has been
4242 old_char
= str
[col
];
4247 if (ccline
.cmdfirstc
== '=')
4249 # ifdef FEAT_CMDL_COMPL
4250 /* pass CMD_SIZE because there is no real command */
4251 set_context_for_expression(xp
, str
, CMD_SIZE
);
4254 else if (ccline
.input_fn
)
4256 xp
->xp_context
= ccline
.xp_context
;
4257 xp
->xp_pattern
= ccline
.cmdbuff
;
4258 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
4259 xp
->xp_arg
= ccline
.xp_arg
;
4264 while (nextcomm
!= NULL
)
4265 nextcomm
= set_one_cmd_context(xp
, nextcomm
);
4267 str
[col
] = old_char
;
4271 * Expand the command line "str" from context "xp".
4272 * "xp" must have been set by set_cmd_context().
4273 * xp->xp_pattern points into "str", to where the text that is to be expanded
4275 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4277 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4278 * key that triggered expansion literally.
4279 * Returns EXPAND_OK otherwise.
4282 expand_cmdline(xp
, str
, col
, matchcount
, matches
)
4284 char_u
*str
; /* start of command line */
4285 int col
; /* position of cursor */
4286 int *matchcount
; /* return: nr of matches */
4287 char_u
***matches
; /* return: array of pointers to matches */
4289 char_u
*file_str
= NULL
;
4291 if (xp
->xp_context
== EXPAND_UNSUCCESSFUL
)
4294 return EXPAND_UNSUCCESSFUL
; /* Something illegal on command line */
4296 if (xp
->xp_context
== EXPAND_NOTHING
)
4298 /* Caller can use the character as a normal char instead */
4299 return EXPAND_NOTHING
;
4302 /* add star to file name, or convert to regexp if not exp. files. */
4303 file_str
= addstar(xp
->xp_pattern
,
4304 (int)(str
+ col
- xp
->xp_pattern
), xp
->xp_context
);
4305 if (file_str
== NULL
)
4306 return EXPAND_UNSUCCESSFUL
;
4308 /* find all files that match the description */
4309 if (ExpandFromContext(xp
, file_str
, matchcount
, matches
,
4310 WILD_ADD_SLASH
|WILD_SILENT
) == FAIL
)
4320 #ifdef FEAT_MULTI_LANG
4322 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4324 static void cleanup_help_tags
__ARGS((int num_file
, char_u
**file
));
4327 cleanup_help_tags(num_file
, file
)
4334 for (i
= 0; i
< num_file
; ++i
)
4336 len
= (int)STRLEN(file
[i
]) - 3;
4337 if (len
> 0 && STRCMP(file
[i
] + len
, "@en") == 0)
4339 /* Sorting on priority means the same item in another language may
4340 * be anywhere. Search all items for a match up to the "@en". */
4341 for (j
= 0; j
< num_file
; ++j
)
4343 && (int)STRLEN(file
[j
]) == len
+ 3
4344 && STRNCMP(file
[i
], file
[j
], len
+ 1) == 0)
4354 * Do the expansion based on xp->xp_context and "pat".
4357 ExpandFromContext(xp
, pat
, num_file
, file
, options
)
4364 #ifdef FEAT_CMDL_COMPL
4365 regmatch_T regmatch
;
4370 flags
= EW_DIR
; /* include directories */
4371 if (options
& WILD_LIST_NOTFOUND
)
4372 flags
|= EW_NOTFOUND
;
4373 if (options
& WILD_ADD_SLASH
)
4374 flags
|= EW_ADDSLASH
;
4375 if (options
& WILD_KEEP_ALL
)
4376 flags
|= EW_KEEPALL
;
4377 if (options
& WILD_SILENT
)
4380 if (xp
->xp_context
== EXPAND_FILES
|| xp
->xp_context
== EXPAND_DIRECTORIES
)
4383 * Expand file or directory names.
4385 int free_pat
= FALSE
;
4388 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4390 if (xp
->xp_backslash
!= XP_BS_NONE
)
4393 pat
= vim_strsave(pat
);
4394 for (i
= 0; pat
[i
]; ++i
)
4397 if (xp
->xp_backslash
== XP_BS_THREE
4398 && pat
[i
+ 1] == '\\'
4399 && pat
[i
+ 2] == '\\'
4400 && pat
[i
+ 3] == ' ')
4401 STRMOVE(pat
+ i
, pat
+ i
+ 3);
4402 if (xp
->xp_backslash
== XP_BS_ONE
4403 && pat
[i
+ 1] == ' ')
4404 STRMOVE(pat
+ i
, pat
+ i
+ 1);
4408 if (xp
->xp_context
== EXPAND_FILES
)
4411 flags
= (flags
| EW_DIR
) & ~EW_FILE
;
4412 ret
= expand_wildcards(1, &pat
, num_file
, file
, flags
);
4418 *file
= (char_u
**)"";
4420 if (xp
->xp_context
== EXPAND_HELP
)
4422 /* With an empty argument we would get all the help tags, which is
4423 * very slow. Get matches for "help" instead. */
4424 if (find_help_tags(*pat
== NUL
? (char_u
*)"help" : pat
,
4425 num_file
, file
, FALSE
) == OK
)
4427 #ifdef FEAT_MULTI_LANG
4428 cleanup_help_tags(*num_file
, *file
);
4435 #ifndef FEAT_CMDL_COMPL
4438 if (xp
->xp_context
== EXPAND_SHELLCMD
)
4439 return expand_shellcmd(pat
, num_file
, file
, flags
);
4440 if (xp
->xp_context
== EXPAND_OLD_SETTING
)
4441 return ExpandOldSetting(num_file
, file
);
4442 if (xp
->xp_context
== EXPAND_BUFFERS
)
4443 return ExpandBufnames(pat
, num_file
, file
, options
);
4444 if (xp
->xp_context
== EXPAND_TAGS
4445 || xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
4446 return expand_tags(xp
->xp_context
== EXPAND_TAGS
, pat
, num_file
, file
);
4447 if (xp
->xp_context
== EXPAND_COLORS
)
4448 return ExpandRTDir(pat
, num_file
, file
, "colors");
4449 if (xp
->xp_context
== EXPAND_COMPILER
)
4450 return ExpandRTDir(pat
, num_file
, file
, "compiler");
4451 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4452 if (xp
->xp_context
== EXPAND_USER_LIST
)
4453 return ExpandUserList(xp
, num_file
, file
);
4456 regmatch
.regprog
= vim_regcomp(pat
, p_magic
? RE_MAGIC
: 0);
4457 if (regmatch
.regprog
== NULL
)
4460 /* set ignore-case according to p_ic, p_scs and pat */
4461 regmatch
.rm_ic
= ignorecase(pat
);
4463 if (xp
->xp_context
== EXPAND_SETTINGS
4464 || xp
->xp_context
== EXPAND_BOOL_SETTINGS
)
4465 ret
= ExpandSettings(xp
, ®match
, num_file
, file
);
4466 else if (xp
->xp_context
== EXPAND_MAPPINGS
)
4467 ret
= ExpandMappings(®match
, num_file
, file
);
4468 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4469 else if (xp
->xp_context
== EXPAND_USER_DEFINED
)
4470 ret
= ExpandUserDefined(xp
, ®match
, num_file
, file
);
4474 static struct expgen
4477 char_u
*((*func
)__ARGS((expand_T
*, int)));
4481 {EXPAND_COMMANDS
, get_command_name
, FALSE
},
4482 #ifdef FEAT_USR_CMDS
4483 {EXPAND_USER_COMMANDS
, get_user_commands
, FALSE
},
4484 {EXPAND_USER_CMD_FLAGS
, get_user_cmd_flags
, FALSE
},
4485 {EXPAND_USER_NARGS
, get_user_cmd_nargs
, FALSE
},
4486 {EXPAND_USER_COMPLETE
, get_user_cmd_complete
, FALSE
},
4489 {EXPAND_USER_VARS
, get_user_var_name
, FALSE
},
4490 {EXPAND_FUNCTIONS
, get_function_name
, FALSE
},
4491 {EXPAND_USER_FUNC
, get_user_func_name
, FALSE
},
4492 {EXPAND_EXPRESSION
, get_expr_name
, FALSE
},
4495 {EXPAND_MENUS
, get_menu_name
, FALSE
},
4496 {EXPAND_MENUNAMES
, get_menu_names
, FALSE
},
4499 {EXPAND_SYNTAX
, get_syntax_name
, TRUE
},
4501 {EXPAND_HIGHLIGHT
, get_highlight_name
, TRUE
},
4503 {EXPAND_EVENTS
, get_event_name
, TRUE
},
4504 {EXPAND_AUGROUP
, get_augroup_name
, TRUE
},
4506 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4507 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4508 {EXPAND_LANGUAGE
, get_lang_arg
, TRUE
},
4510 {EXPAND_ENV_VARS
, get_env_name
, TRUE
},
4515 * Find a context in the table and call the ExpandGeneric() with the
4516 * right function to do the expansion.
4519 for (i
= 0; i
< sizeof(tab
) / sizeof(struct expgen
); ++i
)
4520 if (xp
->xp_context
== tab
[i
].context
)
4523 regmatch
.rm_ic
= TRUE
;
4524 ret
= ExpandGeneric(xp
, ®match
, num_file
, file
, tab
[i
].func
);
4529 vim_free(regmatch
.regprog
);
4532 #endif /* FEAT_CMDL_COMPL */
4535 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4537 * Expand a list of names.
4539 * Generic function for command line completion. It calls a function to
4540 * obtain strings, one by one. The strings are matched against a regexp
4541 * program. Matching strings are copied into an array, which is returned.
4543 * Returns OK when no problems encountered, FAIL for error (out of memory).
4546 ExpandGeneric(xp
, regmatch
, num_file
, file
, func
)
4548 regmatch_T
*regmatch
;
4551 char_u
*((*func
)__ARGS((expand_T
*, int)));
4552 /* returns a string from the list */
4559 /* do this loop twice:
4560 * round == 0: count the number of matching names
4561 * round == 1: copy the matching names into allocated memory
4563 for (round
= 0; round
<= 1; ++round
)
4567 str
= (*func
)(xp
, i
);
4568 if (str
== NULL
) /* end of list */
4570 if (*str
== NUL
) /* skip empty strings */
4573 if (vim_regexec(regmatch
, str
, (colnr_T
)0))
4577 str
= vim_strsave_escaped(str
, (char_u
*)" \t\\.");
4578 (*file
)[count
] = str
;
4580 if (func
== get_menu_names
&& str
!= NULL
)
4582 /* test for separator added by get_menu_names() */
4583 str
+= STRLEN(str
) - 1;
4597 *file
= (char_u
**)alloc((unsigned)(count
* sizeof(char_u
*)));
4600 *file
= (char_u
**)"";
4607 /* Sort the results. Keep menu's in the specified order. */
4608 if (xp
->xp_context
!= EXPAND_MENUNAMES
&& xp
->xp_context
!= EXPAND_MENUS
)
4609 sort_strings(*file
, *num_file
);
4611 #ifdef FEAT_CMDL_COMPL
4612 /* Reset the variables used for special highlight names expansion, so that
4613 * they don't show up when getting normal highlight names by ID. */
4614 reset_expand_highlight();
4621 * Complete a shell command.
4622 * Returns FAIL or OK;
4625 expand_shellcmd(filepat
, num_file
, file
, flagsarg
)
4626 char_u
*filepat
; /* pattern to match with command names */
4627 int *num_file
; /* return: number of matches */
4628 char_u
***file
; /* return: array with matches */
4629 int flagsarg
; /* EW_ flags */
4634 int mustfree
= FALSE
;
4636 char_u
*buf
= alloc(MAXPATHL
);
4639 int flags
= flagsarg
;
4645 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4647 pat
= vim_strsave(filepat
);
4648 for (i
= 0; pat
[i
]; ++i
)
4649 if (pat
[i
] == '\\' && pat
[i
+ 1] == ' ')
4650 STRMOVE(pat
+ i
, pat
+ i
+ 1);
4652 flags
|= EW_FILE
| EW_EXEC
;
4654 /* For an absolute name we don't use $PATH. */
4655 if (mch_isFullName(pat
))
4656 path
= (char_u
*)" ";
4657 else if ((pat
[0] == '.' && (vim_ispathsep(pat
[1])
4658 || (pat
[1] == '.' && vim_ispathsep(pat
[2])))))
4659 path
= (char_u
*)".";
4661 path
= vim_getenv((char_u
*)"PATH", &mustfree
);
4664 * Go over all directories in $PATH. Expand matches in that directory and
4665 * collect them in "ga".
4667 ga_init2(&ga
, (int)sizeof(char *), 10);
4668 for (s
= path
; *s
!= NUL
; s
= e
)
4671 ++s
; /* Skip space used for absolute path name. */
4673 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4674 e
= vim_strchr(s
, ';');
4676 e
= vim_strchr(s
, ':');
4682 if (l
> MAXPATHL
- 5)
4684 vim_strncpy(buf
, s
, l
);
4687 vim_strncpy(buf
+ l
, pat
, MAXPATHL
- 1 - l
);
4689 /* Expand matches in one directory of $PATH. */
4690 ret
= expand_wildcards(1, &buf
, num_file
, file
, flags
);
4693 if (ga_grow(&ga
, *num_file
) == FAIL
)
4694 FreeWild(*num_file
, *file
);
4697 for (i
= 0; i
< *num_file
; ++i
)
4702 /* Remove the path again. */
4704 ((char_u
**)ga
.ga_data
)[ga
.ga_len
++] = s
;
4716 *num_file
= ga
.ga_len
;
4726 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4727 static void * call_user_expand_func
__ARGS((void *(*user_expand_func
) __ARGS((char_u
*, int, char_u
**, int)), expand_T
*xp
, int *num_file
, char_u
***file
));
4730 * Call "user_expand_func()" to invoke a user defined VimL function and return
4731 * the result (either a string or a List).
4734 call_user_expand_func(user_expand_func
, xp
, num_file
, file
)
4735 void *(*user_expand_func
) __ARGS((char_u
*, int, char_u
**, int));
4743 int save_current_SID
= current_SID
;
4745 struct cmdline_info save_ccline
;
4747 if (xp
->xp_arg
== NULL
|| xp
->xp_arg
[0] == '\0')
4752 if (ccline
.cmdbuff
== NULL
)
4754 /* Completion from Insert mode, pass fake arguments. */
4756 sprintf((char *)num
, "%d", (int)STRLEN(xp
->xp_pattern
));
4757 args
[1] = xp
->xp_pattern
;
4761 /* Completion on the command line, pass real arguments. */
4762 keep
= ccline
.cmdbuff
[ccline
.cmdlen
];
4763 ccline
.cmdbuff
[ccline
.cmdlen
] = 0;
4764 sprintf((char *)num
, "%d", ccline
.cmdpos
);
4765 args
[1] = ccline
.cmdbuff
;
4767 args
[0] = xp
->xp_pattern
;
4770 /* Save the cmdline, we don't know what the function may do. */
4771 save_ccline
= ccline
;
4772 ccline
.cmdbuff
= NULL
;
4773 ccline
.cmdprompt
= NULL
;
4774 current_SID
= xp
->xp_scriptID
;
4776 ret
= user_expand_func(xp
->xp_arg
, 3, args
, FALSE
);
4778 ccline
= save_ccline
;
4779 current_SID
= save_current_SID
;
4780 if (ccline
.cmdbuff
!= NULL
)
4781 ccline
.cmdbuff
[ccline
.cmdlen
] = keep
;
4787 * Expand names with a function defined by the user.
4790 ExpandUserDefined(xp
, regmatch
, num_file
, file
)
4792 regmatch_T
*regmatch
;
4802 retstr
= call_user_expand_func(call_func_retstr
, xp
, num_file
, file
);
4806 ga_init2(&ga
, (int)sizeof(char *), 3);
4807 for (s
= retstr
; *s
!= NUL
; s
= e
)
4809 e
= vim_strchr(s
, '\n');
4815 if (xp
->xp_pattern
[0] && vim_regexec(regmatch
, s
, (colnr_T
)0) == 0)
4823 if (ga_grow(&ga
, 1) == FAIL
)
4826 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] = vim_strnsave(s
, (int)(e
- s
));
4835 *num_file
= ga
.ga_len
;
4840 * Expand names with a list returned by a function defined by the user.
4843 ExpandUserList(xp
, num_file
, file
)
4852 retlist
= call_user_expand_func(call_func_retlist
, xp
, num_file
, file
);
4853 if (retlist
== NULL
)
4856 ga_init2(&ga
, (int)sizeof(char *), 3);
4857 /* Loop over the items in the list. */
4858 for (li
= retlist
->lv_first
; li
!= NULL
; li
= li
->li_next
)
4860 if (li
->li_tv
.v_type
!= VAR_STRING
)
4861 continue; /* Skip non-string items */
4863 if (ga_grow(&ga
, 1) == FAIL
)
4866 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] =
4867 vim_strsave(li
->li_tv
.vval
.v_string
);
4870 list_unref(retlist
);
4873 *num_file
= ga
.ga_len
;
4879 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4880 * or compiler names.
4883 ExpandRTDir(pat
, num_file
, file
, dirname
)
4887 char *dirname
; /* "colors" or "compiler" */
4896 s
= alloc((unsigned)(STRLEN(pat
) + STRLEN(dirname
) + 7));
4899 sprintf((char *)s
, "%s/%s*.vim", dirname
, pat
);
4900 all
= globpath(p_rtp
, s
, 0);
4905 ga_init2(&ga
, (int)sizeof(char *), 3);
4906 for (s
= all
; *s
!= NUL
; s
= e
)
4908 e
= vim_strchr(s
, '\n');
4911 if (ga_grow(&ga
, 1) == FAIL
)
4913 if (e
- 4 > s
&& STRNICMP(e
- 4, ".vim", 4) == 0)
4915 for (s
= e
- 4; s
> all
; mb_ptr_back(all
, s
))
4916 if (*s
== '\n' || vim_ispathsep(*s
))
4919 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] =
4920 vim_strnsave(s
, (int)(e
- s
- 4));
4928 *num_file
= ga
.ga_len
;
4934 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4936 * Expand "file" for all comma-separated directories in "path".
4937 * Returns an allocated string with all matches concatenated, separated by
4938 * newlines. Returns NULL for an error or no matches.
4941 globpath(path
, file
, expand_options
)
4955 buf
= alloc(MAXPATHL
);
4960 xpc
.xp_context
= EXPAND_FILES
;
4962 ga_init2(&ga
, 1, 100);
4964 /* Loop over all entries in {path}. */
4965 while (*path
!= NUL
)
4967 /* Copy one item of the path to buf[] and concatenate the file name. */
4968 copy_option_part(&path
, buf
, MAXPATHL
, ",");
4969 if (STRLEN(buf
) + STRLEN(file
) + 2 < MAXPATHL
)
4973 if (ExpandFromContext(&xpc
, buf
, &num_p
, &p
,
4974 WILD_SILENT
|expand_options
) != FAIL
&& num_p
> 0)
4976 ExpandEscape(&xpc
, buf
, num_p
, p
, WILD_SILENT
|expand_options
);
4977 for (len
= 0, i
= 0; i
< num_p
; ++i
)
4978 len
+= (int)STRLEN(p
[i
]) + 1;
4980 /* Concatenate new results to previous ones. */
4981 if (ga_grow(&ga
, len
) == OK
)
4983 cur
= (char_u
*)ga
.ga_data
+ ga
.ga_len
;
4984 for (i
= 0; i
< num_p
; ++i
)
4987 cur
+= STRLEN(p
[i
]);
4997 *--cur
= 0; /* Replace trailing newline with NUL */
5000 return (char_u
*)ga
.ga_data
;
5005 #if defined(FEAT_CMDHIST) || defined(PROTO)
5007 /*********************************
5008 * Command line history stuff *
5009 *********************************/
5012 * Translate a history character to the associated type number.
5026 return HIST_SEARCH
; /* must be '?' or '/' */
5030 * Table of history names.
5031 * These names are used in :history and various hist...() functions.
5032 * It is sufficient to give the significant prefix of a history name.
5035 static char *(history_names
[]) =
5046 * init_history() - Initialize the command line history.
5047 * Also used to re-allocate the history when the size changes.
5052 int newlen
; /* new length of history table */
5059 * If size of history table changed, reallocate it
5062 if (newlen
!= hislen
) /* history length changed */
5064 for (type
= 0; type
< HIST_COUNT
; ++type
) /* adjust the tables */
5068 temp
= (histentry_T
*)lalloc(
5069 (long_u
)(newlen
* sizeof(histentry_T
)), TRUE
);
5070 if (temp
== NULL
) /* out of memory! */
5072 if (type
== 0) /* first one: just keep the old length */
5077 /* Already changed one table, now we can only have zero
5078 * length for all tables. */
5086 if (newlen
== 0 || temp
!= NULL
)
5088 if (hisidx
[type
] < 0) /* there are no entries yet */
5090 for (i
= 0; i
< newlen
; ++i
)
5093 temp
[i
].hisstr
= NULL
;
5096 else if (newlen
> hislen
) /* array becomes bigger */
5098 for (i
= 0; i
<= hisidx
[type
]; ++i
)
5099 temp
[i
] = history
[type
][i
];
5101 for ( ; i
<= newlen
- (hislen
- hisidx
[type
]); ++i
)
5104 temp
[i
].hisstr
= NULL
;
5106 for ( ; j
< hislen
; ++i
, ++j
)
5107 temp
[i
] = history
[type
][j
];
5109 else /* array becomes smaller or 0 */
5112 for (i
= newlen
- 1; ; --i
)
5114 if (i
>= 0) /* copy newest entries */
5115 temp
[i
] = history
[type
][j
];
5116 else /* remove older entries */
5117 vim_free(history
[type
][j
].hisstr
);
5120 if (j
== hisidx
[type
])
5123 hisidx
[type
] = newlen
- 1;
5125 vim_free(history
[type
]);
5126 history
[type
] = temp
;
5134 * Check if command line 'str' is already in history.
5135 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5138 in_history(type
, str
, move_to_front
)
5141 int move_to_front
; /* Move the entry to the front if it exists */
5146 if (hisidx
[type
] < 0)
5151 if (history
[type
][i
].hisstr
== NULL
)
5153 if (STRCMP(str
, history
[type
][i
].hisstr
) == 0)
5162 } while (i
!= hisidx
[type
]);
5166 str
= history
[type
][i
].hisstr
;
5167 while (i
!= hisidx
[type
])
5171 history
[type
][last_i
] = history
[type
][i
];
5174 history
[type
][i
].hisstr
= str
;
5175 history
[type
][i
].hisnum
= ++hisnum
[type
];
5182 * Convert history name (from table above) to its HIST_ equivalent.
5183 * When "name" is empty, return "cmd" history.
5184 * Returns -1 for unknown history name.
5191 int len
= (int)STRLEN(name
);
5193 /* No argument: use current history. */
5195 return hist_char2type(ccline
.cmdfirstc
);
5197 for (i
= 0; history_names
[i
] != NULL
; ++i
)
5198 if (STRNICMP(name
, history_names
[i
], len
) == 0)
5201 if (vim_strchr((char_u
*)":=@>?/", name
[0]) != NULL
&& name
[1] == NUL
)
5202 return hist_char2type(name
[0]);
5207 static int last_maptick
= -1; /* last seen maptick */
5210 * Add the given string to the given history. If the string is already in the
5211 * history then it is moved to the front. "histype" may be one of he HIST_
5215 add_to_history(histype
, new_entry
, in_map
, sep
)
5218 int in_map
; /* consider maptick when inside a mapping */
5219 int sep
; /* separator character used (search hist) */
5221 histentry_T
*hisptr
;
5224 if (hislen
== 0) /* no history */
5228 * Searches inside the same mapping overwrite each other, so that only
5229 * the last line is kept. Be careful not to remove a line that was moved
5230 * down, only lines that were added.
5232 if (histype
== HIST_SEARCH
&& in_map
)
5234 if (maptick
== last_maptick
)
5236 /* Current line is from the same mapping, remove it */
5237 hisptr
= &history
[HIST_SEARCH
][hisidx
[HIST_SEARCH
]];
5238 vim_free(hisptr
->hisstr
);
5239 hisptr
->hisstr
= NULL
;
5242 if (--hisidx
[HIST_SEARCH
] < 0)
5243 hisidx
[HIST_SEARCH
] = hislen
- 1;
5247 if (!in_history(histype
, new_entry
, TRUE
))
5249 if (++hisidx
[histype
] == hislen
)
5250 hisidx
[histype
] = 0;
5251 hisptr
= &history
[histype
][hisidx
[histype
]];
5252 vim_free(hisptr
->hisstr
);
5254 /* Store the separator after the NUL of the string. */
5255 len
= (int)STRLEN(new_entry
);
5256 hisptr
->hisstr
= vim_strnsave(new_entry
, len
+ 2);
5257 if (hisptr
->hisstr
!= NULL
)
5258 hisptr
->hisstr
[len
+ 1] = sep
;
5260 hisptr
->hisnum
= ++hisnum
[histype
];
5261 if (histype
== HIST_SEARCH
&& in_map
)
5262 last_maptick
= maptick
;
5266 #if defined(FEAT_EVAL) || defined(PROTO)
5269 * Get identifier of newest history entry.
5270 * "histype" may be one of the HIST_ values.
5273 get_history_idx(histype
)
5276 if (hislen
== 0 || histype
< 0 || histype
>= HIST_COUNT
5277 || hisidx
[histype
] < 0)
5280 return history
[histype
][hisidx
[histype
]].hisnum
;
5283 static struct cmdline_info
*get_ccline_ptr
__ARGS((void));
5286 * Get pointer to the command line info to use. cmdline_paste() may clear
5287 * ccline and put the previous value in prev_ccline.
5289 static struct cmdline_info
*
5292 if ((State
& CMDLINE
) == 0)
5294 if (ccline
.cmdbuff
!= NULL
)
5296 if (prev_ccline_used
&& prev_ccline
.cmdbuff
!= NULL
)
5297 return &prev_ccline
;
5302 * Get the current command line in allocated memory.
5303 * Only works when the command line is being edited.
5304 * Returns NULL when something is wrong.
5309 struct cmdline_info
*p
= get_ccline_ptr();
5313 return vim_strnsave(p
->cmdbuff
, p
->cmdlen
);
5317 * Get the current command line position, counted in bytes.
5318 * Zero is the first position.
5319 * Only works when the command line is being edited.
5320 * Returns -1 when something is wrong.
5325 struct cmdline_info
*p
= get_ccline_ptr();
5333 * Set the command line byte position to "pos". Zero is the first position.
5334 * Only works when the command line is being edited.
5335 * Returns 1 when failed, 0 when OK.
5338 set_cmdline_pos(pos
)
5341 struct cmdline_info
*p
= get_ccline_ptr();
5346 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5347 * changed the command line. */
5356 * Get the current command-line type.
5357 * Returns ':' or '/' or '?' or '@' or '>' or '-'
5358 * Only works when the command line is being edited.
5359 * Returns NUL when something is wrong.
5364 struct cmdline_info
*p
= get_ccline_ptr();
5368 if (p
->cmdfirstc
== NUL
)
5369 return (p
->input_fn
) ? '@' : '-';
5370 return p
->cmdfirstc
;
5374 * Calculate history index from a number:
5375 * num > 0: seen as identifying number of a history entry
5376 * num < 0: relative position in history wrt newest entry
5377 * "histype" may be one of the HIST_ values.
5380 calc_hist_idx(histype
, num
)
5386 int wrapped
= FALSE
;
5388 if (hislen
== 0 || histype
< 0 || histype
>= HIST_COUNT
5389 || (i
= hisidx
[histype
]) < 0 || num
== 0)
5392 hist
= history
[histype
];
5395 while (hist
[i
].hisnum
> num
)
5403 if (hist
[i
].hisnum
== num
&& hist
[i
].hisstr
!= NULL
)
5406 else if (-num
<= hislen
)
5411 if (hist
[i
].hisstr
!= NULL
)
5418 * Get a history entry by its index.
5419 * "histype" may be one of the HIST_ values.
5422 get_history_entry(histype
, idx
)
5426 idx
= calc_hist_idx(histype
, idx
);
5428 return history
[histype
][idx
].hisstr
;
5430 return (char_u
*)"";
5434 * Clear all entries of a history.
5435 * "histype" may be one of the HIST_ values.
5438 clr_history(histype
)
5442 histentry_T
*hisptr
;
5444 if (hislen
!= 0 && histype
>= 0 && histype
< HIST_COUNT
)
5446 hisptr
= history
[histype
];
5447 for (i
= hislen
; i
--;)
5449 vim_free(hisptr
->hisstr
);
5451 hisptr
++->hisstr
= NULL
;
5453 hisidx
[histype
] = -1; /* mark history as cleared */
5454 hisnum
[histype
] = 0; /* reset identifier counter */
5461 * Remove all entries matching {str} from a history.
5462 * "histype" may be one of the HIST_ values.
5465 del_history_entry(histype
, str
)
5469 regmatch_T regmatch
;
5470 histentry_T
*hisptr
;
5476 regmatch
.regprog
= NULL
;
5477 regmatch
.rm_ic
= FALSE
; /* always match case */
5480 && histype
< HIST_COUNT
5482 && (idx
= hisidx
[histype
]) >= 0
5483 && (regmatch
.regprog
= vim_regcomp(str
, RE_MAGIC
+ RE_STRING
))
5489 hisptr
= &history
[histype
][i
];
5490 if (hisptr
->hisstr
== NULL
)
5492 if (vim_regexec(®match
, hisptr
->hisstr
, (colnr_T
)0))
5495 vim_free(hisptr
->hisstr
);
5496 hisptr
->hisstr
= NULL
;
5503 history
[histype
][last
] = *hisptr
;
5504 hisptr
->hisstr
= NULL
;
5513 if (history
[histype
][idx
].hisstr
== NULL
)
5514 hisidx
[histype
] = -1;
5516 vim_free(regmatch
.regprog
);
5521 * Remove an indexed entry from a history.
5522 * "histype" may be one of the HIST_ values.
5525 del_history_idx(histype
, idx
)
5531 i
= calc_hist_idx(histype
, idx
);
5534 idx
= hisidx
[histype
];
5535 vim_free(history
[histype
][i
].hisstr
);
5537 /* When deleting the last added search string in a mapping, reset
5538 * last_maptick, so that the last added search string isn't deleted again.
5540 if (histype
== HIST_SEARCH
&& maptick
== last_maptick
&& i
== idx
)
5545 j
= (i
+ 1) % hislen
;
5546 history
[histype
][i
] = history
[histype
][j
];
5549 history
[histype
][i
].hisstr
= NULL
;
5550 history
[histype
][i
].hisnum
= 0;
5553 hisidx
[histype
] = i
;
5557 #endif /* FEAT_EVAL */
5559 #if defined(FEAT_CRYPT) || defined(PROTO)
5561 * Very specific function to remove the value in ":set key=val" from the
5565 remove_key_from_history()
5570 i
= hisidx
[HIST_CMD
];
5573 p
= history
[HIST_CMD
][i
].hisstr
;
5576 if (STRNCMP(p
, "key", 3) == 0 && !isalpha(p
[3]))
5578 p
= vim_strchr(p
+ 3, '=');
5582 for (i
= 0; p
[i
] && !vim_iswhite(p
[i
]); ++i
)
5583 if (p
[i
] == '\\' && p
[i
+ 1])
5591 #endif /* FEAT_CMDHIST */
5593 #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5595 * Get indices "num1,num2" that specify a range within a list (not a range of
5596 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5597 * Returns OK if parsed successfully, otherwise FAIL.
5600 get_list_range(str
, num1
, num2
)
5609 *str
= skipwhite(*str
);
5610 if (**str
== '-' || vim_isdigit(**str
)) /* parse "from" part of range */
5612 vim_str2nr(*str
, NULL
, &len
, FALSE
, FALSE
, &num
, NULL
);
5617 *str
= skipwhite(*str
);
5618 if (**str
== ',') /* parse "to" part of range */
5620 *str
= skipwhite(*str
+ 1);
5621 vim_str2nr(*str
, NULL
, &len
, FALSE
, FALSE
, &num
, NULL
);
5625 *str
= skipwhite(*str
+ len
);
5627 else if (!first
) /* no number given at all */
5630 else if (first
) /* only one number given */
5636 #if defined(FEAT_CMDHIST) || defined(PROTO)
5638 * :history command - print a history
5645 int histype1
= HIST_CMD
;
5646 int histype2
= HIST_CMD
;
5652 char_u
*arg
= eap
->arg
;
5656 MSG(_("'history' option is zero"));
5660 if (!(VIM_ISDIGIT(*arg
) || *arg
== '-' || *arg
== ','))
5663 while (ASCII_ISALPHA(*end
)
5664 || vim_strchr((char_u
*)":=@>/?", *end
) != NULL
)
5668 histype1
= get_histtype(arg
);
5671 if (STRICMP(arg
, "all") == 0)
5674 histype2
= HIST_COUNT
-1;
5679 EMSG(_(e_trailing
));
5684 histype2
= histype1
;
5689 if (!get_list_range(&end
, &hisidx1
, &hisidx2
) || *end
!= NUL
)
5691 EMSG(_(e_trailing
));
5695 for (; !got_int
&& histype1
<= histype2
; ++histype1
)
5697 STRCPY(IObuff
, "\n # ");
5698 STRCAT(STRCAT(IObuff
, history_names
[histype1
]), " history");
5699 MSG_PUTS_TITLE(IObuff
);
5700 idx
= hisidx
[histype1
];
5701 hist
= history
[histype1
];
5705 j
= (-j
> hislen
) ? 0 : hist
[(hislen
+j
+idx
+1) % hislen
].hisnum
;
5707 k
= (-k
> hislen
) ? 0 : hist
[(hislen
+k
+idx
+1) % hislen
].hisnum
;
5708 if (idx
>= 0 && j
<= k
)
5709 for (i
= idx
+ 1; !got_int
; ++i
)
5713 if (hist
[i
].hisstr
!= NULL
5714 && hist
[i
].hisnum
>= j
&& hist
[i
].hisnum
<= k
)
5717 sprintf((char *)IObuff
, "%c%6d ", i
== idx
? '>' : ' ',
5719 if (vim_strsize(hist
[i
].hisstr
) > (int)Columns
- 10)
5720 trunc_string(hist
[i
].hisstr
, IObuff
+ STRLEN(IObuff
),
5723 STRCAT(IObuff
, hist
[i
].hisstr
);
5724 msg_outtrans(IObuff
);
5734 #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5735 static char_u
**viminfo_history
[HIST_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
5736 static int viminfo_hisidx
[HIST_COUNT
] = {0, 0, 0, 0};
5737 static int viminfo_hislen
[HIST_COUNT
] = {0, 0, 0, 0};
5738 static int viminfo_add_at_front
= FALSE
;
5740 static int hist_type2char
__ARGS((int type
, int use_question
));
5743 * Translate a history type number to the associated character.
5746 hist_type2char(type
, use_question
)
5748 int use_question
; /* use '?' instead of '/' */
5750 if (type
== HIST_CMD
)
5752 if (type
== HIST_SEARCH
)
5759 if (type
== HIST_EXPR
)
5765 * Prepare for reading the history from the viminfo file.
5766 * This allocates history arrays to store the read history lines.
5769 prepare_viminfo_history(asklen
)
5778 viminfo_add_at_front
= (asklen
!= 0);
5779 if (asklen
> hislen
)
5782 for (type
= 0; type
< HIST_COUNT
; ++type
)
5785 * Count the number of empty spaces in the history list. If there are
5786 * more spaces available than we request, then fill them up.
5788 for (i
= 0, num
= 0; i
< hislen
; i
++)
5789 if (history
[type
][i
].hisstr
== NULL
)
5795 viminfo_history
[type
] = NULL
;
5797 viminfo_history
[type
] =
5798 (char_u
**)lalloc((long_u
)(len
* sizeof(char_u
*)), FALSE
);
5799 if (viminfo_history
[type
] == NULL
)
5801 viminfo_hislen
[type
] = len
;
5802 viminfo_hisidx
[type
] = 0;
5807 * Accept a line from the viminfo, store it in the history array when it's
5811 read_viminfo_history(virp
)
5819 type
= hist_char2type(virp
->vir_line
[0]);
5820 if (viminfo_hisidx
[type
] < viminfo_hislen
[type
])
5822 val
= viminfo_readstring(virp
, 1, TRUE
);
5823 if (val
!= NULL
&& *val
!= NUL
)
5825 if (!in_history(type
, val
+ (type
== HIST_SEARCH
),
5826 viminfo_add_at_front
))
5828 /* Need to re-allocate to append the separator byte. */
5830 p
= lalloc(len
+ 2, TRUE
);
5833 if (type
== HIST_SEARCH
)
5835 /* Search entry: Move the separator from the first
5836 * column to after the NUL. */
5837 mch_memmove(p
, val
+ 1, (size_t)len
);
5838 p
[len
] = (*val
== ' ' ? NUL
: *val
);
5842 /* Not a search entry: No separator in the viminfo
5843 * file, add a NUL separator. */
5844 mch_memmove(p
, val
, (size_t)len
+ 1);
5847 viminfo_history
[type
][viminfo_hisidx
[type
]++] = p
;
5853 return viminfo_readline(virp
);
5857 finish_viminfo_history()
5863 for (type
= 0; type
< HIST_COUNT
; ++type
)
5865 if (history
[type
] == NULL
)
5867 idx
= hisidx
[type
] + viminfo_hisidx
[type
];
5872 if (viminfo_add_at_front
)
5876 if (hisidx
[type
] == -1)
5877 hisidx
[type
] = hislen
- 1;
5880 if (history
[type
][idx
].hisstr
!= NULL
)
5882 if (++idx
== hislen
)
5884 } while (idx
!= hisidx
[type
]);
5885 if (idx
!= hisidx
[type
] && --idx
< 0)
5888 for (i
= 0; i
< viminfo_hisidx
[type
]; i
++)
5890 vim_free(history
[type
][idx
].hisstr
);
5891 history
[type
][idx
].hisstr
= viminfo_history
[type
][i
];
5897 for (i
= 0; i
< viminfo_hisidx
[type
]; i
++)
5899 history
[type
][idx
++].hisnum
= ++hisnum
[type
];
5902 vim_free(viminfo_history
[type
]);
5903 viminfo_history
[type
] = NULL
;
5908 write_viminfo_history(fp
)
5920 for (type
= 0; type
< HIST_COUNT
; ++type
)
5922 num_saved
= get_viminfo_parameter(hist_type2char(type
, FALSE
));
5925 if (num_saved
< 0) /* Use default */
5927 fprintf(fp
, _("\n# %s History (newest to oldest):\n"),
5928 type
== HIST_CMD
? _("Command Line") :
5929 type
== HIST_SEARCH
? _("Search String") :
5930 type
== HIST_EXPR
? _("Expression") :
5932 if (num_saved
> hislen
)
5938 p
= history
[type
][i
].hisstr
;
5941 fputc(hist_type2char(type
, TRUE
), fp
);
5942 /* For the search history: put the separator in the second
5943 * column; use a space if there isn't one. */
5944 if (type
== HIST_SEARCH
)
5946 c
= p
[STRLEN(p
) + 1];
5947 putc(c
== NUL
? ' ' : c
, fp
);
5949 viminfo_writestring(fp
, p
);
5956 #endif /* FEAT_VIMINFO */
5958 #if defined(FEAT_FKMAP) || defined(PROTO)
5960 * Write a character at the current cursor+offset position.
5961 * It is directly written into the command buffer block.
5964 cmd_pchar(c
, offset
)
5967 if (ccline
.cmdpos
+ offset
>= ccline
.cmdlen
|| ccline
.cmdpos
+ offset
< 0)
5969 EMSG(_("E198: cmd_pchar beyond the command length"));
5972 ccline
.cmdbuff
[ccline
.cmdpos
+ offset
] = (char_u
)c
;
5973 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
5980 if (ccline
.cmdpos
+ offset
>= ccline
.cmdlen
|| ccline
.cmdpos
+ offset
< 0)
5982 /* EMSG(_("cmd_gchar beyond the command length")); */
5985 return (int)ccline
.cmdbuff
[ccline
.cmdpos
+ offset
];
5989 #if defined(FEAT_CMDWIN) || defined(PROTO)
5991 * Open a window on the current command line and history. Allow editing in
5992 * the window. Returns when the window is closed.
5994 * CR if the command is to be executed
5995 * Ctrl_C if it is to be abandoned
5996 * K_IGNORE if editing continues
6001 struct cmdline_info save_ccline
;
6002 buf_T
*old_curbuf
= curbuf
;
6003 win_T
*old_curwin
= curwin
;
6013 int save_restart_edit
= restart_edit
;
6014 int save_State
= State
;
6015 int save_exmode
= exmode_active
;
6016 #ifdef FEAT_RIGHTLEFT
6017 int save_cmdmsg_rl
= cmdmsg_rl
;
6020 /* Can't do this recursively. Can't do it when typing a password. */
6021 if (cmdwin_type
!= 0
6022 # if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6031 /* Save current window sizes. */
6032 win_size_save(&winsizes
);
6034 # ifdef FEAT_AUTOCMD
6035 /* Don't execute autocommands while creating the window. */
6038 /* don't use a new tab page */
6041 /* Create a window for the command-line buffer. */
6042 if (win_split((int)p_cwh
, WSP_BOT
) == FAIL
)
6045 # ifdef FEAT_AUTOCMD
6050 cmdwin_type
= ccline
.cmdfirstc
;
6051 if (cmdwin_type
== NUL
)
6054 /* Create the command-line buffer empty. */
6055 (void)do_ecmd(0, NULL
, NULL
, NULL
, ECMD_ONE
, ECMD_HIDE
, NULL
);
6056 (void)setfname(curbuf
, (char_u
*)"[Command Line]", NULL
, TRUE
);
6057 set_option_value((char_u
*)"bt", 0L, (char_u
*)"nofile", OPT_LOCAL
);
6058 set_option_value((char_u
*)"swf", 0L, NULL
, OPT_LOCAL
);
6059 curbuf
->b_p_ma
= TRUE
;
6060 # ifdef FEAT_RIGHTLEFT
6061 curwin
->w_p_rl
= cmdmsg_rl
;
6064 # ifdef FEAT_SCROLLBIND
6065 curwin
->w_p_scb
= FALSE
;
6068 # ifdef FEAT_AUTOCMD
6069 /* Do execute autocommands for setting the filetype (load syntax). */
6073 /* Showing the prompt may have set need_wait_return, reset it. */
6074 need_wait_return
= FALSE
;
6076 histtype
= hist_char2type(ccline
.cmdfirstc
);
6077 if (histtype
== HIST_CMD
|| histtype
== HIST_DEBUG
)
6081 add_map((char_u
*)"<buffer> <Tab> <C-X><C-V>", INSERT
);
6082 add_map((char_u
*)"<buffer> <Tab> a<C-X><C-V>", NORMAL
);
6084 set_option_value((char_u
*)"ft", 0L, (char_u
*)"vim", OPT_LOCAL
);
6087 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6088 * sets 'textwidth' to 78). */
6091 /* Fill the buffer with the history. */
6095 i
= hisidx
[histtype
];
6103 if (history
[histtype
][i
].hisstr
!= NULL
)
6104 ml_append(lnum
++, history
[histtype
][i
].hisstr
,
6107 while (i
!= hisidx
[histtype
]);
6111 /* Replace the empty last line with the current command-line and put the
6113 ml_replace(curbuf
->b_ml
.ml_line_count
, ccline
.cmdbuff
, TRUE
);
6114 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6115 curwin
->w_cursor
.col
= ccline
.cmdpos
;
6116 changed_line_abv_curs();
6117 invalidate_botline();
6118 redraw_later(SOME_VALID
);
6120 /* Save the command line info, can be used recursively. */
6121 save_ccline
= ccline
;
6122 ccline
.cmdbuff
= NULL
;
6123 ccline
.cmdprompt
= NULL
;
6125 /* No Ex mode here! */
6133 # ifdef FEAT_AUTOCMD
6134 /* Trigger CmdwinEnter autocommands. */
6135 typestr
[0] = cmdwin_type
;
6137 apply_autocmds(EVENT_CMDWINENTER
, typestr
, typestr
, FALSE
, curbuf
);
6138 if (restart_edit
!= 0) /* autocmd with ":startinsert" */
6139 stuffcharReadbuff(K_NOP
);
6142 i
= RedrawingDisabled
;
6143 RedrawingDisabled
= 0;
6146 * Call the main loop until <CR> or CTRL-C is typed.
6149 main_loop(TRUE
, FALSE
);
6151 RedrawingDisabled
= i
;
6153 # ifdef FEAT_AUTOCMD
6154 /* Trigger CmdwinLeave autocommands. */
6155 apply_autocmds(EVENT_CMDWINLEAVE
, typestr
, typestr
, FALSE
, curbuf
);
6158 /* Restore the command line info. */
6159 ccline
= save_ccline
;
6162 exmode_active
= save_exmode
;
6164 /* Safety check: The old window or buffer was deleted: It's a bug when
6166 if (!win_valid(old_curwin
) || !buf_valid(old_curbuf
))
6168 cmdwin_result
= Ctrl_C
;
6169 EMSG(_("E199: Active window or buffer deleted"));
6173 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6174 /* autocmds may abort script processing */
6175 if (aborting() && cmdwin_result
!= K_IGNORE
)
6176 cmdwin_result
= Ctrl_C
;
6178 /* Set the new command line from the cmdline buffer. */
6179 vim_free(ccline
.cmdbuff
);
6180 if (cmdwin_result
== K_XF1
|| cmdwin_result
== K_XF2
) /* :qa[!] typed */
6182 char *p
= (cmdwin_result
== K_XF2
) ? "qa" : "qa!";
6184 if (histtype
== HIST_CMD
)
6186 /* Execute the command directly. */
6187 ccline
.cmdbuff
= vim_strsave((char_u
*)p
);
6188 cmdwin_result
= CAR
;
6192 /* First need to cancel what we were doing. */
6193 ccline
.cmdbuff
= NULL
;
6194 stuffcharReadbuff(':');
6195 stuffReadbuff((char_u
*)p
);
6196 stuffcharReadbuff(CAR
);
6199 else if (cmdwin_result
== K_XF2
) /* :qa typed */
6201 ccline
.cmdbuff
= vim_strsave((char_u
*)"qa");
6202 cmdwin_result
= CAR
;
6205 ccline
.cmdbuff
= vim_strsave(ml_get_curline());
6206 if (ccline
.cmdbuff
== NULL
)
6207 cmdwin_result
= Ctrl_C
;
6210 ccline
.cmdlen
= (int)STRLEN(ccline
.cmdbuff
);
6211 ccline
.cmdbufflen
= ccline
.cmdlen
+ 1;
6212 ccline
.cmdpos
= curwin
->w_cursor
.col
;
6213 if (ccline
.cmdpos
> ccline
.cmdlen
)
6214 ccline
.cmdpos
= ccline
.cmdlen
;
6215 if (cmdwin_result
== K_IGNORE
)
6217 set_cmdspos_cursor();
6222 # ifdef FEAT_AUTOCMD
6223 /* Don't execute autocommands while deleting the window. */
6228 win_goto(old_curwin
);
6229 win_close(wp
, TRUE
);
6230 close_buffer(NULL
, bp
, DOBUF_WIPE
);
6232 /* Restore window sizes. */
6233 win_size_restore(&winsizes
);
6235 # ifdef FEAT_AUTOCMD
6240 ga_clear(&winsizes
);
6241 restart_edit
= save_restart_edit
;
6242 # ifdef FEAT_RIGHTLEFT
6243 cmdmsg_rl
= save_cmdmsg_rl
;
6251 return cmdwin_result
;
6253 #endif /* FEAT_CMDWIN */
6256 * Used for commands that either take a simple command string argument, or:
6260 * Returns a pointer to allocated memory with {script} or NULL.
6263 script_get(eap
, cmd
)
6268 char *end_pattern
= NULL
;
6272 if (cmd
[0] != '<' || cmd
[1] != '<' || eap
->getline
== NULL
)
6275 ga_init2(&ga
, 1, 0x400);
6278 end_pattern
= (char *)skipwhite(cmd
+ 2);
6284 theline
= eap
->getline(
6286 eap
->cstack
->cs_looplevel
> 0 ? -1 :
6288 NUL
, eap
->cookie
, 0);
6290 if (theline
== NULL
|| STRCMP(end_pattern
, theline
) == 0)
6296 ga_concat(&ga
, theline
);
6297 ga_append(&ga
, '\n');
6300 ga_append(&ga
, NUL
);
6302 return (char_u
*)ga
.ga_data
;