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 int xp_context
; /* type of expansion */
36 char_u
*xp_arg
; /* user-defined expansion arg */
37 int input_fn
; /* when TRUE Invoked for input() function */
41 static struct cmdline_info ccline
; /* current cmdline_info */
43 static int cmd_showtail
; /* Only show path tail in lists ? */
46 static int new_cmdpos
; /* position set by set_cmdline_pos() */
50 typedef struct hist_entry
52 int hisnum
; /* identifying number */
53 char_u
*hisstr
; /* actual entry, separator char after the NUL */
56 static histentry_T
*(history
[HIST_COUNT
]) = {NULL
, NULL
, NULL
, NULL
, NULL
};
57 static int hisidx
[HIST_COUNT
] = {-1, -1, -1, -1, -1}; /* lastused entry */
58 static int hisnum
[HIST_COUNT
] = {0, 0, 0, 0, 0};
59 /* identifying (unique) number of newest history entry */
60 static int hislen
= 0; /* actual length of history tables */
62 static int hist_char2type
__ARGS((int c
));
64 static int in_history
__ARGS((int, char_u
*, int));
66 static int calc_hist_idx
__ARGS((int histype
, int num
));
71 static int cmd_hkmap
= 0; /* Hebrew mapping during command line */
75 static int cmd_fkmap
= 0; /* Farsi mapping during command line */
78 static int cmdline_charsize
__ARGS((int idx
));
79 static void set_cmdspos
__ARGS((void));
80 static void set_cmdspos_cursor
__ARGS((void));
82 static void correct_cmdspos
__ARGS((int idx
, int cells
));
84 static void alloc_cmdbuff
__ARGS((int len
));
85 static int realloc_cmdbuff
__ARGS((int len
));
86 static void draw_cmdline
__ARGS((int start
, int len
));
87 static void save_cmdline
__ARGS((struct cmdline_info
*ccp
));
88 static void restore_cmdline
__ARGS((struct cmdline_info
*ccp
));
89 static int cmdline_paste
__ARGS((int regname
, int literally
, int remcr
));
90 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
91 static void redrawcmd_preedit
__ARGS((void));
94 static void cmdline_del
__ARGS((int from
));
96 static void redrawcmdprompt
__ARGS((void));
97 static void cursorcmd
__ARGS((void));
98 static int ccheck_abbr
__ARGS((int));
99 static int nextwild
__ARGS((expand_T
*xp
, int type
, int options
));
100 static void escape_fname
__ARGS((char_u
**pp
));
101 static int showmatches
__ARGS((expand_T
*xp
, int wildmenu
));
102 static void set_expand_context
__ARGS((expand_T
*xp
));
103 static int ExpandFromContext
__ARGS((expand_T
*xp
, char_u
*, int *, char_u
***, int));
104 static int expand_showtail
__ARGS((expand_T
*xp
));
105 #ifdef FEAT_CMDL_COMPL
106 static int expand_shellcmd
__ARGS((char_u
*filepat
, int *num_file
, char_u
***file
, int flagsarg
));
107 static int ExpandRTDir
__ARGS((char_u
*pat
, int *num_file
, char_u
***file
, char *dirname
));
108 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
109 static int ExpandUserDefined
__ARGS((expand_T
*xp
, regmatch_T
*regmatch
, int *num_file
, char_u
***file
));
110 static int ExpandUserList
__ARGS((expand_T
*xp
, int *num_file
, char_u
***file
));
115 static int ex_window
__ARGS((void));
119 * getcmdline() - accept a command line starting with firstc.
121 * firstc == ':' get ":" command line.
122 * firstc == '/' or '?' get search pattern
123 * firstc == '=' get expression
124 * firstc == '@' get text for input() function
125 * firstc == '>' get text for debug mode
126 * firstc == NUL get text for :insert command
127 * firstc == -1 like NUL, and break on CTRL-C
129 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
132 * Careful: getcmdline() can be called recursively!
134 * Return pointer to allocated string if there is a commandline, NULL
139 getcmdline(firstc
, count
, indent
)
141 long count
; /* only used for incremental search */
142 int indent
; /* indent for inside conditionals */
147 int gotesc
= FALSE
; /* TRUE when <ESC> just typed */
148 int do_abbr
; /* when TRUE check for abbr. */
150 char_u
*lookfor
= NULL
; /* string to match */
151 int hiscnt
; /* current history line in use */
152 int histype
; /* history type to be used */
154 #ifdef FEAT_SEARCH_EXTRA
156 colnr_T old_curswant
;
158 linenr_T old_topline
;
162 linenr_T old_botline
;
163 int did_incsearch
= FALSE
;
164 int incsearch_postponed
= FALSE
;
166 int did_wild_list
= FALSE
; /* did wild_list() recently */
167 int wim_index
= 0; /* index in wim_flags[] */
169 int save_msg_scroll
= msg_scroll
;
170 int save_State
= State
; /* remember State when called */
171 int some_key_typed
= FALSE
; /* one of the keys was typed */
173 /* mouse drag and release events are ignored, unless they are
174 * preceded with a mouse down event */
175 int ignore_drag_release
= TRUE
;
178 int break_ctrl_c
= FALSE
;
181 long *b_im_ptr
= NULL
;
182 #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
183 /* Everything that may work recursively should save and restore the
184 * current command line in save_ccline. That includes update_screen(), a
185 * custom status line may invoke ":normal". */
186 struct cmdline_info save_ccline
;
190 want_sniff_request
= 0;
199 #ifdef FEAT_RIGHTLEFT
200 /* start without Hebrew mapping for a command line */
201 if (firstc
== ':' || firstc
== '=' || firstc
== '>')
205 ccline
.overstrike
= FALSE
; /* always start in insert mode */
206 #ifdef FEAT_SEARCH_EXTRA
207 old_cursor
= curwin
->w_cursor
; /* needs to be restored later */
208 old_curswant
= curwin
->w_curswant
;
209 old_leftcol
= curwin
->w_leftcol
;
210 old_topline
= curwin
->w_topline
;
212 old_topfill
= curwin
->w_topfill
;
214 old_botline
= curwin
->w_botline
;
218 * set some variables for redrawcmd()
220 ccline
.cmdfirstc
= (firstc
== '@' ? 0 : firstc
);
221 ccline
.cmdindent
= (firstc
> 0 ? indent
: 0);
223 /* alloc initial ccline.cmdbuff */
224 alloc_cmdbuff(exmode_active
? 250 : indent
+ 1);
225 if (ccline
.cmdbuff
== NULL
)
226 return NULL
; /* out of memory */
227 ccline
.cmdlen
= ccline
.cmdpos
= 0;
228 ccline
.cmdbuff
[0] = NUL
;
230 /* autoindent for :insert and :append */
233 copy_spaces(ccline
.cmdbuff
, indent
);
234 ccline
.cmdbuff
[indent
] = NUL
;
235 ccline
.cmdpos
= indent
;
236 ccline
.cmdspos
= indent
;
237 ccline
.cmdlen
= indent
;
242 #ifdef FEAT_RIGHTLEFT
243 if (curwin
->w_p_rl
&& *curwin
->w_p_rlc
== 's'
244 && (firstc
== '/' || firstc
== '?'))
250 redir_off
= TRUE
; /* don't redirect the typed command */
254 msg_scrolled
= 0; /* avoid wait_return message */
257 redrawcmdprompt(); /* draw prompt or indent */
260 xpc
.xp_context
= EXPAND_NOTHING
;
261 xpc
.xp_backslash
= XP_BS_NONE
;
262 #ifndef BACKSLASH_IN_FILENAME
263 xpc
.xp_shell
= FALSE
;
266 #if defined(FEAT_EVAL)
269 xpc
.xp_context
= ccline
.xp_context
;
270 xpc
.xp_pattern
= ccline
.cmdbuff
;
271 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
272 xpc
.xp_arg
= ccline
.xp_arg
;
278 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
279 * doing ":@0" when register 0 doesn't contain a CR.
285 if (firstc
== '/' || firstc
== '?' || firstc
== '@')
287 /* Use ":lmap" mappings for search pattern and input(). */
288 if (curbuf
->b_p_imsearch
== B_IMODE_USE_INSERT
)
289 b_im_ptr
= &curbuf
->b_p_iminsert
;
291 b_im_ptr
= &curbuf
->b_p_imsearch
;
292 if (*b_im_ptr
== B_IMODE_LMAP
)
294 #ifdef USE_IM_CONTROL
295 im_set_active(*b_im_ptr
== B_IMODE_IM
);
298 #ifdef USE_IM_CONTROL
299 else if (p_imcmdline
)
307 ui_cursor_shape(); /* may show different cursor shape */
310 /* When inside an autocommand for writing "exiting" may be set and
311 * terminal mode set to cooked. Need to set raw mode here then. */
316 hiscnt
= hislen
; /* set hiscnt to impossible history value */
317 histype
= hist_char2type(firstc
);
321 do_digraph(-1); /* init digraph typahead */
325 * Collect the command string, handling editing keys.
329 redir_off
= TRUE
; /* Don't redirect the typed command.
330 Repeated, because a ":redir" inside
331 completion may switch it on. */
332 #ifdef USE_ON_FLY_SCROLL
333 dont_scroll
= FALSE
; /* allow scrolling here */
335 quit_more
= FALSE
; /* reset after CTRL-D which had a more-prompt */
337 cursorcmd(); /* set the cursor on the right spot */
339 /* Get a character. Ignore K_IGNORE, it should not do anything, such
340 * as stop completion. */
344 } while (c
== K_IGNORE
);
348 some_key_typed
= TRUE
;
349 #ifdef FEAT_RIGHTLEFT
356 if (cmdmsg_rl
&& !KeyStuffed
)
358 /* Invert horizontal movements and operations. Only when
359 * typed by the user directly, not when the result of a
363 case K_RIGHT
: c
= K_LEFT
; break;
364 case K_S_RIGHT
: c
= K_S_LEFT
; break;
365 case K_C_RIGHT
: c
= K_C_LEFT
; break;
366 case K_LEFT
: c
= K_RIGHT
; break;
367 case K_S_LEFT
: c
= K_S_RIGHT
; break;
368 case K_C_LEFT
: c
= K_C_RIGHT
; break;
375 * Ignore got_int when CTRL-C was typed here.
376 * Don't ignore it in :global, we really need to break then, e.g., for
377 * ":g/pat/normal /pat" (without the <CR>).
378 * Don't ignore it for the input() function.
385 #if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
395 /* free old command line when finished moving around in the history
398 && c
!= K_S_DOWN
&& c
!= K_S_UP
399 && c
!= K_DOWN
&& c
!= K_UP
400 && c
!= K_PAGEDOWN
&& c
!= K_PAGEUP
401 && c
!= K_KPAGEDOWN
&& c
!= K_KPAGEUP
402 && c
!= K_LEFT
&& c
!= K_RIGHT
403 && (xpc
.xp_numfiles
> 0 || (c
!= Ctrl_P
&& c
!= Ctrl_N
)))
411 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
413 if (c
!= p_wc
&& c
== K_S_TAB
&& xpc
.xp_numfiles
!= -1)
417 /* Special translations for 'wildmenu' */
418 if (did_wild_list
&& p_wmnu
)
422 else if (c
== K_RIGHT
)
425 /* Hitting CR after "emenu Name.": complete submenu */
426 if (xpc
.xp_context
== EXPAND_MENUNAMES
&& p_wmnu
428 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '.'
429 && ccline
.cmdbuff
[ccline
.cmdpos
- 2] != '\\'
430 && (c
== '\n' || c
== '\r' || c
== K_KENTER
))
434 /* free expanded names when finished walking through matches */
435 if (xpc
.xp_numfiles
!= -1
436 && !(c
== p_wc
&& KeyTyped
) && c
!= p_wcm
437 && c
!= Ctrl_N
&& c
!= Ctrl_P
&& c
!= Ctrl_A
440 (void)ExpandOne(&xpc
, NULL
, NULL
, 0, WILD_FREE
);
441 did_wild_list
= FALSE
;
443 if (!p_wmnu
|| (c
!= K_UP
&& c
!= K_DOWN
))
445 xpc
.xp_context
= EXPAND_NOTHING
;
448 if (p_wmnu
&& wild_menu_showing
!= 0)
451 int old_RedrawingDisabled
= RedrawingDisabled
;
454 RedrawingDisabled
= 0;
456 if (wild_menu_showing
== WM_SCROLLED
)
458 /* Entered command line, move it up */
462 else if (save_p_ls
!= -1)
464 /* restore 'laststatus' and 'winminheight' */
468 save_cmdline(&save_ccline
);
469 update_screen(VALID
); /* redraw the screen NOW */
470 restore_cmdline(&save_ccline
);
476 # ifdef FEAT_VERTSPLIT
477 win_redraw_last_status(topframe
);
479 lastwin
->w_redr_status
= TRUE
;
481 redraw_statuslines();
484 wild_menu_showing
= 0;
486 RedrawingDisabled
= old_RedrawingDisabled
;
492 /* Special translations for 'wildmenu' */
493 if (xpc
.xp_context
== EXPAND_MENUNAMES
&& p_wmnu
)
495 /* Hitting <Down> after "emenu Name.": complete submenu */
496 if (c
== K_DOWN
&& ccline
.cmdpos
> 0
497 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '.')
501 /* Hitting <Up>: Remove one submenu name in front of the
505 j
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
509 /* check for start of menu name */
510 if (ccline
.cmdbuff
[j
] == ' '
511 && ccline
.cmdbuff
[j
- 1] != '\\')
516 /* check for start of submenu name */
517 if (ccline
.cmdbuff
[j
] == '.'
518 && ccline
.cmdbuff
[j
- 1] != '\\')
532 xpc
.xp_context
= EXPAND_NOTHING
;
535 if ((xpc
.xp_context
== EXPAND_FILES
536 || xpc
.xp_context
== EXPAND_SHELLCMD
) && p_wmnu
)
548 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == PATHSEP
549 && (ccline
.cmdpos
< 3
550 || ccline
.cmdbuff
[ccline
.cmdpos
- 2] != '.'
551 || ccline
.cmdbuff
[ccline
.cmdpos
- 3] != '.'))
553 /* go down a directory */
556 else if (STRNCMP(xpc
.xp_pattern
, upseg
+ 1, 3) == 0 && c
== K_DOWN
)
558 /* If in a direct ancestor, strip off one ../ to go down */
562 i
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
567 j
-= (*mb_head_off
)(ccline
.cmdbuff
, ccline
.cmdbuff
+ j
);
569 if (vim_ispathsep(ccline
.cmdbuff
[j
]))
576 && ccline
.cmdbuff
[j
- 1] == '.'
577 && ccline
.cmdbuff
[j
- 2] == '.'
578 && (vim_ispathsep(ccline
.cmdbuff
[j
- 3]) || j
== i
+ 2))
586 /* go up a directory */
589 j
= ccline
.cmdpos
- 1;
590 i
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
595 j
-= (*mb_head_off
)(ccline
.cmdbuff
, ccline
.cmdbuff
+ j
);
597 if (vim_ispathsep(ccline
.cmdbuff
[j
])
598 #ifdef BACKSLASH_IN_FILENAME
599 && vim_strchr(" *?[{`$%#", ccline
.cmdbuff
[j
+ 1])
616 else if (STRNCMP(ccline
.cmdbuff
+ j
, upseg
, 4) == 0)
618 else if (STRNCMP(ccline
.cmdbuff
+ j
, upseg
+ 1, 3) == 0
625 /* TODO this is only for DOS/UNIX systems - need to put in
626 * machine-specific stuff here and in upseg init */
628 put_on_cmdline(upseg
+ 1, 3, FALSE
);
630 else if (ccline
.cmdpos
> i
)
635 #if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
637 && (xpc
.xp_context
== EXPAND_FILES
638 || xpc
.xp_context
== EXPAND_MENUNAMES
)
639 && (c
== K_UP
|| c
== K_DOWN
))
640 xpc
.xp_context
= EXPAND_NOTHING
;
643 #endif /* FEAT_WILDMENU */
645 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
646 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
654 /* CTRL-\ e doesn't work when obtaining an expression. */
655 if (c
!= Ctrl_N
&& c
!= Ctrl_G
656 && (c
!= 'e' || ccline
.cmdfirstc
== '='))
667 * Replace the command line with the result of an expression.
668 * Need to save and restore the current command line, to be
669 * able to enter a new one...
671 if (ccline
.cmdpos
== ccline
.cmdlen
)
672 new_cmdpos
= 99999; /* keep it at the end */
674 new_cmdpos
= ccline
.cmdpos
;
676 save_cmdline(&save_ccline
);
677 c
= get_expr_register();
678 restore_cmdline(&save_ccline
);
681 /* Need to save and restore ccline. And set "textlock"
682 * to avoid nasty things like going to another buffer when
683 * evaluating an expression. */
684 save_cmdline(&save_ccline
);
688 restore_cmdline(&save_ccline
);
690 if (p
!= NULL
&& realloc_cmdbuff((int)STRLEN(p
) + 1) == OK
)
692 ccline
.cmdlen
= (int)STRLEN(p
);
693 STRCPY(ccline
.cmdbuff
, p
);
696 /* Restore the cursor or use the position set with
697 * set_cmdline_pos(). */
698 if (new_cmdpos
> ccline
.cmdlen
)
699 ccline
.cmdpos
= ccline
.cmdlen
;
701 ccline
.cmdpos
= new_cmdpos
;
703 KeyTyped
= FALSE
; /* Don't do p_wc completion. */
705 goto cmdline_changed
;
714 if (c
== Ctrl_G
&& p_im
&& restart_edit
== 0)
716 gotesc
= TRUE
; /* will free ccline.cmdbuff after putting it
718 goto returncmd
; /* back to Normal mode */
723 if (c
== cedit_key
|| c
== K_CMDWIN
)
726 * Open a window to edit the command line (and history).
729 some_key_typed
= TRUE
;
731 # ifdef FEAT_DIGRAPHS
739 if (c
== '\n' || c
== '\r' || c
== K_KENTER
|| (c
== ESC
740 && (!KeyTyped
|| vim_strchr(p_cpo
, CPO_ESC
) != NULL
)))
742 /* In Ex mode a backslash escapes a newline. */
745 && ccline
.cmdpos
== ccline
.cmdlen
747 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '\\')
754 gotesc
= FALSE
; /* Might have typed ESC previously, don't
755 truncate the cmdline now. */
756 if (ccheck_abbr(c
+ ABBR_OFF
))
757 goto cmdline_changed
;
760 windgoto(msg_row
, 0);
768 * Completion for 'wildchar' or 'wildcharm' key.
769 * - hitting <ESC> twice means: abandon command line.
770 * - wildcard expansion is only done when the 'wildchar' key is really
771 * typed, not when it comes from a macro
773 if ((c
== p_wc
&& !gotesc
&& KeyTyped
) || c
== p_wcm
)
775 if (xpc
.xp_numfiles
> 0) /* typed p_wc at least twice */
777 /* if 'wildmode' contains "list" may still need to list */
778 if (xpc
.xp_numfiles
> 1
780 && (wim_flags
[wim_index
] & WIM_LIST
))
782 (void)showmatches(&xpc
, FALSE
);
784 did_wild_list
= TRUE
;
786 if (wim_flags
[wim_index
] & WIM_LONGEST
)
787 res
= nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
788 else if (wim_flags
[wim_index
] & WIM_FULL
)
789 res
= nextwild(&xpc
, WILD_NEXT
, WILD_NO_BEEP
);
791 res
= OK
; /* don't insert 'wildchar' now */
793 else /* typed p_wc first time */
797 /* if 'wildmode' first contains "longest", get longest
799 if (wim_flags
[0] & WIM_LONGEST
)
800 res
= nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
802 res
= nextwild(&xpc
, WILD_EXPAND_KEEP
, WILD_NO_BEEP
);
804 /* if interrupted while completing, behave like it failed */
807 (void)vpeekc(); /* remove <C-C> from input stream */
808 got_int
= FALSE
; /* don't abandon the command line */
809 (void)ExpandOne(&xpc
, NULL
, NULL
, 0, WILD_FREE
);
811 xpc
.xp_context
= EXPAND_NOTHING
;
813 goto cmdline_changed
;
816 /* when more than one match, and 'wildmode' first contains
817 * "list", or no change and 'wildmode' contains "longest,list",
818 * list all matches */
819 if (res
== OK
&& xpc
.xp_numfiles
> 1)
821 /* a "longest" that didn't do anything is skipped (but not
823 if (wim_flags
[0] == WIM_LONGEST
&& ccline
.cmdpos
== j
)
825 if ((wim_flags
[wim_index
] & WIM_LIST
)
827 || (p_wmnu
&& (wim_flags
[wim_index
] & WIM_FULL
) != 0)
831 if (!(wim_flags
[0] & WIM_LONGEST
))
834 int p_wmnu_save
= p_wmnu
;
837 nextwild(&xpc
, WILD_PREV
, 0); /* remove match */
839 p_wmnu
= p_wmnu_save
;
843 (void)showmatches(&xpc
, p_wmnu
844 && ((wim_flags
[wim_index
] & WIM_LIST
) == 0));
846 (void)showmatches(&xpc
, FALSE
);
849 did_wild_list
= TRUE
;
850 if (wim_flags
[wim_index
] & WIM_LONGEST
)
851 nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
852 else if (wim_flags
[wim_index
] & WIM_FULL
)
853 nextwild(&xpc
, WILD_NEXT
, WILD_NO_BEEP
);
859 else if (xpc
.xp_numfiles
== -1)
860 xpc
.xp_context
= EXPAND_NOTHING
;
868 goto cmdline_changed
;
873 /* <S-Tab> goes to last match, in a clumsy way */
874 if (c
== K_S_TAB
&& KeyTyped
)
876 if (nextwild(&xpc
, WILD_EXPAND_KEEP
, 0) == OK
877 && nextwild(&xpc
, WILD_PREV
, 0) == OK
878 && nextwild(&xpc
, WILD_PREV
, 0) == OK
)
879 goto cmdline_changed
;
882 if (c
== NUL
|| c
== K_ZERO
) /* NUL is stored as NL */
885 do_abbr
= TRUE
; /* default: check for abbreviation */
888 * Big switch for a typed command line character.
898 if (cmd_fkmap
&& c
== K_BS
)
905 * delete current character is the same as backspace on next
906 * character, except at end of line
908 if (c
== K_DEL
&& ccline
.cmdpos
!= ccline
.cmdlen
)
911 if (has_mbyte
&& c
== K_DEL
)
912 ccline
.cmdpos
+= mb_off_next(ccline
.cmdbuff
,
913 ccline
.cmdbuff
+ ccline
.cmdpos
);
915 if (ccline
.cmdpos
> 0)
920 p
= ccline
.cmdbuff
+ j
;
924 p
= mb_prevptr(ccline
.cmdbuff
, p
);
927 while (p
> ccline
.cmdbuff
&& vim_isspace(*p
))
928 p
= mb_prevptr(ccline
.cmdbuff
, p
);
930 while (p
> ccline
.cmdbuff
&& mb_get_class(p
) == i
)
931 p
= mb_prevptr(ccline
.cmdbuff
, p
);
932 if (mb_get_class(p
) != i
)
933 p
+= (*mb_ptr2len
)(p
);
940 while (p
> ccline
.cmdbuff
&& vim_isspace(p
[-1]))
942 i
= vim_iswordc(p
[-1]);
943 while (p
> ccline
.cmdbuff
&& !vim_isspace(p
[-1])
944 && vim_iswordc(p
[-1]) == i
)
949 ccline
.cmdpos
= (int)(p
- ccline
.cmdbuff
);
950 ccline
.cmdlen
-= j
- ccline
.cmdpos
;
952 while (i
< ccline
.cmdlen
)
953 ccline
.cmdbuff
[i
++] = ccline
.cmdbuff
[j
++];
955 /* Truncate at the end, required for multi-byte chars. */
956 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
959 else if (ccline
.cmdlen
== 0 && c
!= Ctrl_W
960 && ccline
.cmdprompt
== NULL
&& indent
== 0)
962 /* In ex and debug mode it doesn't make sense to return. */
965 || ccline
.cmdfirstc
== '>'
968 goto cmdline_not_changed
;
970 vim_free(ccline
.cmdbuff
); /* no commandline to return */
971 ccline
.cmdbuff
= NULL
;
974 #ifdef FEAT_RIGHTLEFT
980 msg_putchar(' '); /* delete ':' */
982 redraw_cmdline
= TRUE
;
983 goto returncmd
; /* back to cmd mode */
985 goto cmdline_changed
;
990 /* if Farsi mode set, we are in reverse insert mode -
991 Do not change the mode */
996 ccline
.overstrike
= !ccline
.overstrike
;
998 ui_cursor_shape(); /* may show different cursor shape */
1000 goto cmdline_not_changed
;
1003 if (map_to_exists_mode((char_u
*)"", LANGMAP
, FALSE
))
1005 /* ":lmap" mappings exists, toggle use of mappings. */
1007 #ifdef USE_IM_CONTROL
1008 im_set_active(FALSE
); /* Disable input method */
1010 if (b_im_ptr
!= NULL
)
1012 if (State
& LANGMAP
)
1013 *b_im_ptr
= B_IMODE_LMAP
;
1015 *b_im_ptr
= B_IMODE_NONE
;
1018 #ifdef USE_IM_CONTROL
1021 /* There are no ":lmap" mappings, toggle IM. When
1022 * 'imdisable' is set don't try getting the status, it's
1024 if ((p_imdisable
&& b_im_ptr
!= NULL
)
1025 ? *b_im_ptr
== B_IMODE_IM
: im_get_status())
1027 im_set_active(FALSE
); /* Disable input method */
1028 if (b_im_ptr
!= NULL
)
1029 *b_im_ptr
= B_IMODE_NONE
;
1033 im_set_active(TRUE
); /* Enable input method */
1034 if (b_im_ptr
!= NULL
)
1035 *b_im_ptr
= B_IMODE_IM
;
1039 if (b_im_ptr
!= NULL
)
1041 if (b_im_ptr
== &curbuf
->b_p_iminsert
)
1042 set_iminsert_global();
1044 set_imsearch_global();
1047 ui_cursor_shape(); /* may show different cursor shape */
1049 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1050 /* Show/unshow value of 'keymap' in status lines later. */
1051 status_redraw_curbuf();
1053 goto cmdline_not_changed
;
1055 /* case '@': only in very old vi */
1057 /* delete all characters left of the cursor */
1060 i
= ccline
.cmdpos
= 0;
1061 while (i
< ccline
.cmdlen
)
1062 ccline
.cmdbuff
[i
++] = ccline
.cmdbuff
[j
++];
1063 /* Truncate at the end, required for multi-byte chars. */
1064 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
1066 goto cmdline_changed
;
1068 #ifdef FEAT_CLIPBOARD
1070 /* Copy the modeless selection, if there is one. */
1071 if (clip_star
.state
!= SELECT_CLEARED
)
1073 if (clip_star
.state
== SELECT_DONE
)
1074 clip_copy_modeless_selection(TRUE
);
1075 goto cmdline_not_changed
;
1080 case ESC
: /* get here if p_wc != ESC or when ESC typed twice */
1082 /* In exmode it doesn't make sense to return. Except when
1083 * ":normal" runs out of characters. */
1085 #ifdef FEAT_EX_EXTRA
1086 && (ex_normal_busy
== 0 || typebuf
.tb_len
> 0)
1089 goto cmdline_not_changed
;
1091 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1092 putting it in history */
1093 goto returncmd
; /* back to cmd mode */
1095 case Ctrl_R
: /* insert register */
1096 #ifdef USE_ON_FLY_SCROLL
1097 dont_scroll
= TRUE
; /* disallow scrolling here */
1099 putcmdline('"', TRUE
);
1101 i
= c
= plain_vgetc(); /* CTRL-R <char> */
1103 i
= Ctrl_R
; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1105 c
= plain_vgetc(); /* CTRL-R CTRL-R <char> */
1109 * Insert the result of an expression.
1110 * Need to save the current command line, to be able to enter
1116 if (ccline
.cmdfirstc
== '=')/* can't do this recursively */
1123 save_cmdline(&save_ccline
);
1124 c
= get_expr_register();
1125 restore_cmdline(&save_ccline
);
1129 if (c
!= ESC
) /* use ESC to cancel inserting register */
1131 cmdline_paste(c
, i
== Ctrl_R
, FALSE
);
1134 /* When there was a serious error abort getting the
1138 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1139 putting it in history */
1140 goto returncmd
; /* back to cmd mode */
1143 KeyTyped
= FALSE
; /* Don't do p_wc completion. */
1145 if (new_cmdpos
>= 0)
1147 /* set_cmdline_pos() was used */
1148 if (new_cmdpos
> ccline
.cmdlen
)
1149 ccline
.cmdpos
= ccline
.cmdlen
;
1151 ccline
.cmdpos
= new_cmdpos
;
1156 goto cmdline_changed
;
1159 if (showmatches(&xpc
, FALSE
) == EXPAND_NOTHING
)
1160 break; /* Use ^D as normal char instead */
1163 continue; /* don't do incremental search now */
1170 if (ccline
.cmdpos
>= ccline
.cmdlen
)
1172 i
= cmdline_charsize(ccline
.cmdpos
);
1173 if (KeyTyped
&& ccline
.cmdspos
+ i
>= Columns
* Rows
)
1175 ccline
.cmdspos
+= i
;
1178 ccline
.cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
1184 while ((c
== K_S_RIGHT
|| c
== K_C_RIGHT
1185 || (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
)))
1186 && ccline
.cmdbuff
[ccline
.cmdpos
] != ' ');
1189 set_cmdspos_cursor();
1191 goto cmdline_not_changed
;
1196 if (ccline
.cmdpos
== 0)
1197 goto cmdline_not_changed
;
1202 if (has_mbyte
) /* move to first byte of char */
1203 ccline
.cmdpos
-= (*mb_head_off
)(ccline
.cmdbuff
,
1204 ccline
.cmdbuff
+ ccline
.cmdpos
);
1206 ccline
.cmdspos
-= cmdline_charsize(ccline
.cmdpos
);
1208 while (ccline
.cmdpos
> 0
1209 && (c
== K_S_LEFT
|| c
== K_C_LEFT
1210 || (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
)))
1211 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] != ' ');
1214 set_cmdspos_cursor();
1216 goto cmdline_not_changed
;
1219 /* Ignore mouse event or ex_window() result. */
1220 goto cmdline_not_changed
;
1223 /* On Win32 ignore <M-F4>, we get it when closing the window was
1226 if (mod_mask
== MOD_MASK_ALT
)
1228 redrawcmd(); /* somehow the cmdline is cleared */
1229 goto cmdline_not_changed
;
1236 case K_MIDDLERELEASE
:
1237 goto cmdline_not_changed
; /* Ignore mouse */
1241 /* When GUI is active, also paste when 'mouse' is empty */
1244 if (!mouse_has(MOUSE_COMMAND
))
1245 goto cmdline_not_changed
; /* Ignore mouse */
1246 # ifdef FEAT_CLIPBOARD
1247 if (clip_star
.available
)
1248 cmdline_paste('*', TRUE
, TRUE
);
1251 cmdline_paste(0, TRUE
, TRUE
);
1253 goto cmdline_changed
;
1257 cmdline_paste('~', TRUE
, FALSE
);
1259 goto cmdline_changed
;
1265 case K_RIGHTRELEASE
:
1266 /* Ignore drag and release events when the button-down wasn't
1268 if (ignore_drag_release
)
1269 goto cmdline_not_changed
;
1273 if (c
== K_LEFTRELEASE
|| c
== K_RIGHTRELEASE
)
1274 ignore_drag_release
= TRUE
;
1276 ignore_drag_release
= FALSE
;
1278 /* When GUI is active, also move when 'mouse' is empty */
1281 if (!mouse_has(MOUSE_COMMAND
))
1282 goto cmdline_not_changed
; /* Ignore mouse */
1283 # ifdef FEAT_CLIPBOARD
1284 if (mouse_row
< cmdline_row
&& clip_star
.available
)
1286 int button
, is_click
, is_drag
;
1289 * Handle modeless selection.
1291 button
= get_mouse_button(KEY2TERMCAP1(c
),
1292 &is_click
, &is_drag
);
1293 if (mouse_model_popup() && button
== MOUSE_LEFT
1294 && (mod_mask
& MOD_MASK_SHIFT
))
1296 /* Translate shift-left to right button. */
1297 button
= MOUSE_RIGHT
;
1298 mod_mask
&= ~MOD_MASK_SHIFT
;
1300 clip_modeless(button
, is_click
, is_drag
);
1301 goto cmdline_not_changed
;
1306 for (ccline
.cmdpos
= 0; ccline
.cmdpos
< ccline
.cmdlen
;
1309 i
= cmdline_charsize(ccline
.cmdpos
);
1310 if (mouse_row
<= cmdline_row
+ ccline
.cmdspos
/ Columns
1311 && mouse_col
< ccline
.cmdspos
% Columns
+ i
)
1316 /* Count ">" for double-wide char that doesn't fit. */
1317 correct_cmdspos(ccline
.cmdpos
, i
);
1318 ccline
.cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
1319 + ccline
.cmdpos
) - 1;
1322 ccline
.cmdspos
+= i
;
1324 goto cmdline_not_changed
;
1326 /* Mouse scroll wheel: ignored here */
1329 /* Alternate buttons ignored here */
1336 goto cmdline_not_changed
;
1338 #endif /* FEAT_MOUSE */
1341 case K_LEFTMOUSE_NM
: /* mousefocus click, ignored */
1342 case K_LEFTRELEASE_NM
:
1343 goto cmdline_not_changed
;
1345 case K_VER_SCROLLBAR
:
1346 if (msg_scrolled
== 0)
1351 goto cmdline_not_changed
;
1353 case K_HOR_SCROLLBAR
:
1354 if (msg_scrolled
== 0)
1356 gui_do_horiz_scroll();
1359 goto cmdline_not_changed
;
1361 #ifdef FEAT_GUI_TABLINE
1364 /* Don't want to change any tabs here. Make sure the same tab
1365 * is still selected. */
1366 if (gui_use_tabline())
1367 gui_mch_set_curtab(tabpage_index(curtab
));
1368 goto cmdline_not_changed
;
1371 case K_SELECT
: /* end of Select mode mapping - ignore */
1372 goto cmdline_not_changed
;
1374 case Ctrl_B
: /* begin of command line */
1381 goto cmdline_not_changed
;
1383 case Ctrl_E
: /* end of command line */
1388 ccline
.cmdpos
= ccline
.cmdlen
;
1389 set_cmdspos_cursor();
1390 goto cmdline_not_changed
;
1392 case Ctrl_A
: /* all matches */
1393 if (nextwild(&xpc
, WILD_ALL
, 0) == FAIL
)
1395 goto cmdline_changed
;
1398 #ifdef FEAT_SEARCH_EXTRA
1399 if (p_is
&& !cmd_silent
&& (firstc
== '/' || firstc
== '?'))
1401 /* Add a character from under the cursor for 'incsearch' */
1403 && !equalpos(curwin
->w_cursor
, old_cursor
))
1408 if (c
== firstc
|| vim_strchr((char_u
*)(
1409 p_magic
? "\\^$.*[" : "\\^$"), c
)
1412 /* put a backslash before special characters */
1413 stuffcharReadbuff(c
);
1419 goto cmdline_not_changed
;
1423 /* completion: longest common part */
1424 if (nextwild(&xpc
, WILD_LONGEST
, 0) == FAIL
)
1426 goto cmdline_changed
;
1428 case Ctrl_N
: /* next match */
1429 case Ctrl_P
: /* previous match */
1430 if (xpc
.xp_numfiles
> 0)
1432 if (nextwild(&xpc
, (c
== Ctrl_P
) ? WILD_PREV
: WILD_NEXT
, 0)
1435 goto cmdline_changed
;
1447 if (hislen
== 0 || firstc
== NUL
) /* no history */
1448 goto cmdline_not_changed
;
1452 /* save current command string so it can be restored later */
1453 if (lookfor
== NULL
)
1455 if ((lookfor
= vim_strsave(ccline
.cmdbuff
)) == NULL
)
1456 goto cmdline_not_changed
;
1457 lookfor
[ccline
.cmdpos
] = NUL
;
1460 j
= (int)STRLEN(lookfor
);
1463 /* one step backwards */
1464 if (c
== K_UP
|| c
== K_S_UP
|| c
== Ctrl_P
1465 || c
== K_PAGEUP
|| c
== K_KPAGEUP
)
1467 if (hiscnt
== hislen
) /* first time */
1468 hiscnt
= hisidx
[histype
];
1469 else if (hiscnt
== 0 && hisidx
[histype
] != hislen
- 1)
1470 hiscnt
= hislen
- 1;
1471 else if (hiscnt
!= hisidx
[histype
] + 1)
1473 else /* at top of list */
1479 else /* one step forwards */
1481 /* on last entry, clear the line */
1482 if (hiscnt
== hisidx
[histype
])
1488 /* not on a history line, nothing to do */
1489 if (hiscnt
== hislen
)
1491 if (hiscnt
== hislen
- 1) /* wrap around */
1496 if (hiscnt
< 0 || history
[histype
][hiscnt
].hisstr
== NULL
)
1501 if ((c
!= K_UP
&& c
!= K_DOWN
)
1503 || STRNCMP(history
[histype
][hiscnt
].hisstr
,
1504 lookfor
, (size_t)j
) == 0)
1508 if (hiscnt
!= i
) /* jumped to other entry */
1514 vim_free(ccline
.cmdbuff
);
1515 if (hiscnt
== hislen
)
1516 p
= lookfor
; /* back to the old one */
1518 p
= history
[histype
][hiscnt
].hisstr
;
1520 if (histype
== HIST_SEARCH
1522 && (old_firstc
= p
[STRLEN(p
) + 1]) != firstc
)
1524 /* Correct for the separator character used when
1525 * adding the history entry vs the one used now.
1526 * First loop: count length.
1527 * Second loop: copy the characters. */
1528 for (i
= 0; i
<= 1; ++i
)
1531 for (j
= 0; p
[j
] != NUL
; ++j
)
1533 /* Replace old sep with new sep, unless it is
1535 if (p
[j
] == old_firstc
1536 && (j
== 0 || p
[j
- 1] != '\\'))
1539 ccline
.cmdbuff
[len
] = firstc
;
1543 /* Escape new sep, unless it is already
1546 && (j
== 0 || p
[j
- 1] != '\\'))
1549 ccline
.cmdbuff
[len
] = '\\';
1553 ccline
.cmdbuff
[len
] = p
[j
];
1560 if (ccline
.cmdbuff
== NULL
)
1564 ccline
.cmdbuff
[len
] = NUL
;
1568 alloc_cmdbuff((int)STRLEN(p
));
1569 if (ccline
.cmdbuff
== NULL
)
1571 STRCPY(ccline
.cmdbuff
, p
);
1574 ccline
.cmdpos
= ccline
.cmdlen
= (int)STRLEN(ccline
.cmdbuff
);
1576 goto cmdline_changed
;
1579 goto cmdline_not_changed
;
1585 ignore_drag_release
= TRUE
;
1587 putcmdline('^', TRUE
);
1588 c
= get_literal(); /* get next (two) character(s) */
1589 do_abbr
= FALSE
; /* don't do abbreviation now */
1591 /* may need to remove ^ when composing char was typed */
1592 if (enc_utf8
&& utf_iscomposing(c
) && !cmd_silent
)
1594 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
1601 #ifdef FEAT_DIGRAPHS
1604 ignore_drag_release
= TRUE
;
1606 putcmdline('?', TRUE
);
1607 #ifdef USE_ON_FLY_SCROLL
1608 dont_scroll
= TRUE
; /* disallow scrolling here */
1610 c
= get_digraph(TRUE
);
1615 goto cmdline_not_changed
;
1616 #endif /* FEAT_DIGRAPHS */
1618 #ifdef FEAT_RIGHTLEFT
1619 case Ctrl__
: /* CTRL-_: switch language mode */
1625 cmd_fkmap
= !cmd_fkmap
;
1626 if (cmd_fkmap
) /* in Farsi always in Insert mode */
1627 ccline
.overstrike
= FALSE
;
1629 else /* Hebrew is default */
1631 cmd_hkmap
= !cmd_hkmap
;
1632 goto cmdline_not_changed
;
1639 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1640 putting it in history */
1641 goto returncmd
; /* back to Normal mode */
1645 * Normal character with no special meaning. Just set mod_mask
1646 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1647 * the string <S-Space>. This should only happen after ^V.
1654 * End of switch on command line character.
1655 * We come here if we have a normal character.
1658 if (do_abbr
&& (IS_SPECIAL(c
) || !vim_iswordc(c
)) && ccheck_abbr(
1660 /* Add ABBR_OFF for characters above 0x100, this is
1661 * what check_abbr() expects. */
1662 (has_mbyte
&& c
>= 0x100) ? (c
+ ABBR_OFF
) :
1665 goto cmdline_changed
;
1668 * put the character in the command line
1670 if (IS_SPECIAL(c
) || mod_mask
!= 0)
1671 put_on_cmdline(get_special_key_name(c
, mod_mask
), -1, TRUE
);
1677 j
= (*mb_char2bytes
)(c
, IObuff
);
1678 IObuff
[j
] = NUL
; /* exclude composing chars */
1679 put_on_cmdline(IObuff
, j
, TRUE
);
1685 put_on_cmdline(IObuff
, 1, TRUE
);
1688 goto cmdline_changed
;
1691 * This part implements incremental searches for "/" and "?"
1692 * Jump to cmdline_not_changed when a character has been read but the command
1693 * line did not change. Then we only search and redraw if something changed in
1695 * Jump to cmdline_changed when the command line did change.
1696 * (Sorry for the goto's, I know it is ugly).
1698 cmdline_not_changed
:
1699 #ifdef FEAT_SEARCH_EXTRA
1700 if (!incsearch_postponed
)
1705 #ifdef FEAT_SEARCH_EXTRA
1707 * 'incsearch' highlighting.
1709 if (p_is
&& !cmd_silent
&& (firstc
== '/' || firstc
== '?'))
1716 /* if there is a character waiting, search and redraw later */
1719 incsearch_postponed
= TRUE
;
1722 incsearch_postponed
= FALSE
;
1723 curwin
->w_cursor
= old_cursor
; /* start at old position */
1725 /* If there is no command line, don't do anything */
1726 if (ccline
.cmdlen
== 0)
1730 cursor_off(); /* so the user knows we're busy */
1732 ++emsg_off
; /* So it doesn't beep if bad expr */
1734 /* Set the time limit to half a second. */
1735 profile_setlimit(500L, &tm
);
1737 i
= do_search(NULL
, firstc
, ccline
.cmdbuff
, count
,
1738 SEARCH_KEEP
+ SEARCH_OPT
+ SEARCH_NOOF
+ SEARCH_PEEK
,
1746 /* if interrupted while searching, behave like it failed */
1749 (void)vpeekc(); /* remove <C-C> from input stream */
1750 got_int
= FALSE
; /* don't abandon the command line */
1753 else if (char_avail())
1754 /* cancelled searching because a char was typed */
1755 incsearch_postponed
= TRUE
;
1758 highlight_match
= TRUE
; /* highlight position */
1760 highlight_match
= FALSE
; /* remove highlight */
1762 /* first restore the old curwin values, so the screen is
1763 * positioned in the same way as the actual search command */
1764 curwin
->w_leftcol
= old_leftcol
;
1765 curwin
->w_topline
= old_topline
;
1767 curwin
->w_topfill
= old_topfill
;
1769 curwin
->w_botline
= old_botline
;
1770 changed_cline_bef_curs();
1775 pos_T save_pos
= curwin
->w_cursor
;
1778 * First move cursor to end of match, then to the start. This
1779 * moves the whole match onto the screen when 'nowrap' is set.
1781 curwin
->w_cursor
.lnum
+= search_match_lines
;
1782 curwin
->w_cursor
.col
= search_match_endcol
;
1783 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
1785 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
1786 coladvance((colnr_T
)MAXCOL
);
1789 end_pos
= curwin
->w_cursor
;
1790 curwin
->w_cursor
= save_pos
;
1793 end_pos
= curwin
->w_cursor
; /* shutup gcc 4 */
1796 # ifdef FEAT_WINDOWS
1797 /* May redraw the status line to show the cursor position. */
1798 if (p_ru
&& curwin
->w_status_height
> 0)
1799 curwin
->w_redr_status
= TRUE
;
1802 save_cmdline(&save_ccline
);
1803 update_screen(SOME_VALID
);
1804 restore_cmdline(&save_ccline
);
1806 /* Leave it at the end to make CTRL-R CTRL-W work. */
1808 curwin
->w_cursor
= end_pos
;
1812 did_incsearch
= TRUE
;
1814 #else /* FEAT_SEARCH_EXTRA */
1818 #ifdef FEAT_RIGHTLEFT
1821 || (p_arshape
&& !p_tbidi
&& enc_utf8
)
1824 /* Always redraw the whole command line to fix shaping and
1825 * right-left typing. Not efficient, but it works. */
1832 #ifdef FEAT_RIGHTLEFT
1840 ExpandCleanup(&xpc
);
1842 #ifdef FEAT_SEARCH_EXTRA
1845 curwin
->w_cursor
= old_cursor
;
1846 curwin
->w_curswant
= old_curswant
;
1847 curwin
->w_leftcol
= old_leftcol
;
1848 curwin
->w_topline
= old_topline
;
1850 curwin
->w_topfill
= old_topfill
;
1852 curwin
->w_botline
= old_botline
;
1853 highlight_match
= FALSE
;
1854 validate_cursor(); /* needed for TAB */
1855 redraw_later(SOME_VALID
);
1859 if (ccline
.cmdbuff
!= NULL
)
1862 * Put line in history buffer (":" and "=" only when it was typed).
1865 if (ccline
.cmdlen
&& firstc
!= NUL
1866 && (some_key_typed
|| histype
== HIST_SEARCH
))
1868 add_to_history(histype
, ccline
.cmdbuff
, TRUE
,
1869 histype
== HIST_SEARCH
? firstc
: NUL
);
1872 vim_free(new_last_cmdline
);
1873 new_last_cmdline
= vim_strsave(ccline
.cmdbuff
);
1878 if (gotesc
) /* abandon command line */
1880 vim_free(ccline
.cmdbuff
);
1881 ccline
.cmdbuff
= NULL
;
1882 if (msg_scrolled
== 0)
1885 redraw_cmdline
= TRUE
;
1890 * If the screen was shifted up, redraw the whole screen (later).
1891 * If the line is too long, clear it, so ruler and shown command do
1892 * not get printed in the middle of it.
1895 msg_scroll
= save_msg_scroll
;
1898 /* When the command line was typed, no need for a wait-return prompt. */
1900 need_wait_return
= FALSE
;
1903 #ifdef USE_IM_CONTROL
1904 if (b_im_ptr
!= NULL
&& *b_im_ptr
!= B_IMODE_LMAP
)
1905 im_save_status(b_im_ptr
);
1906 im_set_active(FALSE
);
1912 ui_cursor_shape(); /* may show different cursor shape */
1916 char_u
*p
= ccline
.cmdbuff
;
1918 /* Make ccline empty, getcmdline() may try to use it. */
1919 ccline
.cmdbuff
= NULL
;
1924 #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1926 * Get a command line with a prompt.
1927 * This is prepared to be called recursively from getcmdline() (e.g. by
1928 * f_input() when evaluating an expression from CTRL-R =).
1929 * Returns the command line in allocated memory, or NULL.
1932 getcmdline_prompt(firstc
, prompt
, attr
, xp_context
, xp_arg
)
1934 char_u
*prompt
; /* command line prompt */
1935 int attr
; /* attributes for prompt */
1936 int xp_context
; /* type of expansion */
1937 char_u
*xp_arg
; /* user-defined expansion argument */
1940 struct cmdline_info save_ccline
;
1941 int msg_col_save
= msg_col
;
1943 save_cmdline(&save_ccline
);
1944 ccline
.cmdprompt
= prompt
;
1945 ccline
.cmdattr
= attr
;
1947 ccline
.xp_context
= xp_context
;
1948 ccline
.xp_arg
= xp_arg
;
1949 ccline
.input_fn
= (firstc
== '@');
1951 s
= getcmdline(firstc
, 1L, 0);
1952 restore_cmdline(&save_ccline
);
1953 /* Restore msg_col, the prompt from input() may have changed it. */
1954 msg_col
= msg_col_save
;
1961 * Return TRUE when the text must not be changed and we can't switch to
1962 * another window or buffer. Used when editing the command line, evaluating
1963 * 'balloonexpr', etc.
1969 if (cmdwin_type
!= 0)
1972 return textlock
!= 0;
1976 * Give an error message for a command that isn't allowed while the cmdline
1977 * window is open or editing the cmdline in another way.
1983 if (cmdwin_type
!= 0)
1990 #if defined(FEAT_AUTOCMD) || defined(PROTO)
1992 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
1998 if (curbuf_lock
> 0)
2000 EMSG(_("E788: Not allowed to edit another buffer now"));
2008 cmdline_charsize(idx
)
2011 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2012 if (cmdline_star
> 0) /* showing '*', always 1 position */
2015 return ptr2cells(ccline
.cmdbuff
+ idx
);
2019 * Compute the offset of the cursor on the command line for the prompt and
2025 if (ccline
.cmdfirstc
!= NUL
)
2026 ccline
.cmdspos
= 1 + ccline
.cmdindent
;
2028 ccline
.cmdspos
= 0 + ccline
.cmdindent
;
2032 * Compute the screen position for the cursor on the command line.
2035 set_cmdspos_cursor()
2043 if (m
< 0) /* overflow, Columns or Rows at weird value */
2048 for (i
= 0; i
< ccline
.cmdlen
&& i
< ccline
.cmdpos
; ++i
)
2050 c
= cmdline_charsize(i
);
2052 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2054 correct_cmdspos(i
, c
);
2056 /* If the cmdline doesn't fit, put cursor on last visible char. */
2057 if ((ccline
.cmdspos
+= c
) >= m
)
2059 ccline
.cmdpos
= i
- 1;
2060 ccline
.cmdspos
-= c
;
2065 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
) - 1;
2072 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2073 * character that doesn't fit, so that a ">" must be displayed.
2076 correct_cmdspos(idx
, cells
)
2080 if ((*mb_ptr2len
)(ccline
.cmdbuff
+ idx
) > 1
2081 && (*mb_ptr2cells
)(ccline
.cmdbuff
+ idx
) > 1
2082 && ccline
.cmdspos
% Columns
+ cells
> Columns
)
2088 * Get an Ex command line for the ":" command.
2092 getexline(c
, dummy
, indent
)
2093 int c
; /* normally ':', NUL for ":append" */
2094 void *dummy
; /* cookie not used */
2095 int indent
; /* indent for inside conditionals */
2097 /* When executing a register, remove ':' that's in front of each line. */
2098 if (exec_from_reg
&& vpeekc() == ':')
2100 return getcmdline(c
, 1L, indent
);
2104 * Get an Ex command line for Ex mode.
2105 * In Ex mode we only use the OS supplied line editing features and no
2106 * mappings or abbreviations.
2107 * Returns a string in allocated memory or NULL.
2111 getexmodeline(promptc
, dummy
, indent
)
2112 int promptc
; /* normally ':', NUL for ":append" and '?' for
2114 void *dummy
; /* cookie not used */
2115 int indent
; /* indent for inside conditionals */
2121 int escaped
= FALSE
; /* CTRL-V typed */
2126 /* Switch cursor on now. This avoids that it happens after the "\n", which
2127 * confuses the system function that computes tabstops. */
2130 /* always start in column 0; write a newline if necessary */
2132 if ((msg_col
|| msg_didout
) && promptc
!= '?')
2136 /* indent that is only displayed, not in the line itself */
2139 while (indent
-- > 0)
2144 ga_init2(&line_ga
, 1, 30);
2146 /* autoindent for :insert and :append is in the line itself */
2152 ga_append(&line_ga
, TAB
);
2153 msg_puts((char_u
*)" ");
2156 while (indent
-- > 0)
2158 ga_append(&line_ga
, ' ');
2166 * Get the line, one character at a time.
2171 if (ga_grow(&line_ga
, 40) == FAIL
)
2173 pend
= (char_u
*)line_ga
.ga_data
+ line_ga
.ga_len
;
2175 /* Get one character at a time. Don't use inchar(), it can't handle
2176 * special characters. */
2181 * Handle line editing.
2182 * Previously this was left to the system, putting the terminal in
2183 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
2193 /* CR typed means "enter", which is NL */
2197 if (c1
== BS
|| c1
== K_BS
2198 || c1
== DEL
|| c1
== K_DEL
|| c1
== K_KDEL
)
2200 if (line_ga
.ga_len
> 0)
2218 p
= (char_u
*)line_ga
.ga_data
;
2219 p
[line_ga
.ga_len
] = NUL
;
2220 indent
= get_indent_str(p
, 8);
2221 indent
+= curbuf
->b_p_sw
- indent
% curbuf
->b_p_sw
;
2223 while (get_indent_str(p
, 8) < indent
)
2225 char_u
*s
= skipwhite(p
);
2227 ga_grow(&line_ga
, 1);
2228 mch_memmove(s
+ 1, s
, line_ga
.ga_len
- (s
- p
) + 1);
2233 /* redraw the line */
2236 for (p
= (char_u
*)line_ga
.ga_data
;
2237 p
< (char_u
*)line_ga
.ga_data
+ line_ga
.ga_len
; ++p
)
2244 } while (++vcol
% 8);
2248 msg_outtrans_len(p
, 1);
2249 vcol
+= char2cells(*p
);
2253 windgoto(msg_row
, msg_col
);
2259 /* Delete one shiftwidth. */
2260 p
= (char_u
*)line_ga
.ga_data
;
2261 if (prev_char
== '0' || prev_char
== '^')
2263 if (prev_char
== '^')
2264 ex_keep_indent
= TRUE
;
2266 p
[--line_ga
.ga_len
] = NUL
;
2270 p
[line_ga
.ga_len
] = NUL
;
2271 indent
= get_indent_str(p
, 8);
2273 indent
-= indent
% curbuf
->b_p_sw
;
2275 while (get_indent_str(p
, 8) > indent
)
2277 char_u
*s
= skipwhite(p
);
2279 mch_memmove(s
- 1, s
, line_ga
.ga_len
- (s
- p
) + 1);
2285 if (c1
== Ctrl_V
|| c1
== Ctrl_Q
)
2291 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2298 ((char_u
*)line_ga
.ga_data
)[line_ga
.ga_len
] = c1
;
2303 /* Don't use chartabsize(), 'ts' can be different */
2307 } while (++vcol
% 8);
2312 ((char_u
*)line_ga
.ga_data
) + line_ga
.ga_len
, 1);
2313 vcol
+= char2cells(c1
);
2318 windgoto(msg_row
, msg_col
);
2319 pend
= (char_u
*)(line_ga
.ga_data
) + line_ga
.ga_len
;
2321 /* we are done when a NL is entered, but not when it comes after a
2323 if (line_ga
.ga_len
> 0 && pend
[-1] == '\n'
2324 && (line_ga
.ga_len
<= 1 || pend
[-2] != '\\'))
2336 /* make following messages go to the next line */
2339 if (msg_row
< Rows
- 1)
2341 emsg_on_display
= FALSE
; /* don't want ui_delay() */
2346 return (char_u
*)line_ga
.ga_data
;
2349 # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2350 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
2352 * Return TRUE if ccline.overstrike is on.
2355 cmdline_overstrike()
2357 return ccline
.overstrike
;
2361 * Return TRUE if the cursor is at the end of the cmdline.
2366 return (ccline
.cmdpos
>= ccline
.cmdlen
);
2370 #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
2372 * Return the virtual column number at the current cursor position.
2373 * This is used by the IM code to obtain the start of the preedit string.
2376 cmdline_getvcol_cursor()
2378 if (ccline
.cmdbuff
== NULL
|| ccline
.cmdpos
> ccline
.cmdlen
)
2387 for (col
= 0; i
< ccline
.cmdpos
; ++col
)
2388 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
);
2394 return ccline
.cmdpos
;
2398 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2400 * If part of the command line is an IM preedit string, redraw it with
2401 * IM feedback attributes. The cursor position is restored after drawing.
2406 if ((State
& CMDLINE
)
2408 /* && im_get_status() doesn't work when using SCIM */
2410 && im_is_preediting())
2420 cmdspos
= ((ccline
.cmdfirstc
!= NUL
) ? 1 : 0) + ccline
.cmdindent
;
2425 for (col
= 0; col
< preedit_start_col
2426 && cmdpos
< ccline
.cmdlen
; ++col
)
2428 cmdspos
+= (*mb_ptr2cells
)(ccline
.cmdbuff
+ cmdpos
);
2429 cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ cmdpos
);
2435 cmdspos
+= preedit_start_col
;
2436 cmdpos
+= preedit_start_col
;
2439 msg_row
= cmdline_row
+ (cmdspos
/ (int)Columns
);
2440 msg_col
= cmdspos
% (int)Columns
;
2441 if (msg_row
>= Rows
)
2444 for (col
= 0; cmdpos
< ccline
.cmdlen
; ++col
)
2449 char_attr
= im_get_feedback_attr(col
);
2451 break; /* end of preedit string */
2455 char_len
= (*mb_ptr2len
)(ccline
.cmdbuff
+ cmdpos
);
2460 msg_outtrans_len_attr(ccline
.cmdbuff
+ cmdpos
, char_len
, char_attr
);
2468 #endif /* FEAT_XIM && FEAT_GUI_GTK */
2471 * Allocate a new command line buffer.
2472 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2473 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2480 * give some extra space to avoid having to allocate all the time
2487 ccline
.cmdbuff
= alloc(len
); /* caller should check for out-of-memory */
2488 ccline
.cmdbufflen
= len
;
2492 * Re-allocate the command line to length len + something extra.
2493 * return FAIL for failure, OK otherwise
2496 realloc_cmdbuff(len
)
2502 alloc_cmdbuff(len
); /* will get some more */
2503 if (ccline
.cmdbuff
== NULL
) /* out of memory */
2505 ccline
.cmdbuff
= p
; /* keep the old one */
2508 mch_memmove(ccline
.cmdbuff
, p
, (size_t)ccline
.cmdlen
+ 1);
2513 #if defined(FEAT_ARABIC) || defined(PROTO)
2514 static char_u
*arshape_buf
= NULL
;
2516 # if defined(EXITFREE) || defined(PROTO)
2520 vim_free(arshape_buf
);
2526 * Draw part of the cmdline at the current cursor position. But draw stars
2527 * when cmdline_star is TRUE.
2530 draw_cmdline(start
, len
)
2534 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2537 if (cmdline_star
> 0)
2538 for (i
= 0; i
< len
; ++i
)
2543 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ start
+ i
) - 1;
2549 if (p_arshape
&& !p_tbidi
&& enc_utf8
&& len
> 0)
2551 static int buflen
= 0;
2564 * Do arabic shaping into a temporary buffer. This is very
2567 if (len
* 2 + 2 > buflen
)
2569 /* Re-allocate the buffer. We keep it around to avoid a lot of
2570 * alloc()/free() calls. */
2571 vim_free(arshape_buf
);
2572 buflen
= len
* 2 + 2;
2573 arshape_buf
= alloc(buflen
);
2574 if (arshape_buf
== NULL
)
2575 return; /* out of memory */
2578 if (utf_iscomposing(utf_ptr2char(ccline
.cmdbuff
+ start
)))
2580 /* Prepend a space to draw the leading composing char on. */
2581 arshape_buf
[0] = ' ';
2585 for (j
= start
; j
< start
+ len
; j
+= mb_l
)
2587 p
= ccline
.cmdbuff
+ j
;
2588 u8c
= utfc_ptr2char_len(p
, u8cc
, start
+ len
- j
);
2589 mb_l
= utfc_ptr2len_len(p
, start
+ len
- j
);
2590 if (ARABIC_CHAR(u8c
))
2592 /* Do Arabic shaping. */
2595 /* displaying from right to left */
2599 if (j
+ mb_l
>= start
+ len
)
2602 nc
= utf_ptr2char(p
+ mb_l
);
2606 /* displaying from left to right */
2607 if (j
+ mb_l
>= start
+ len
)
2613 pc
= utfc_ptr2char_len(p
+ mb_l
, pcc
,
2614 start
+ len
- j
- mb_l
);
2621 u8c
= arabic_shape(u8c
, NULL
, &u8cc
[0], pc
, pc1
, nc
);
2623 newlen
+= (*mb_char2bytes
)(u8c
, arshape_buf
+ newlen
);
2626 newlen
+= (*mb_char2bytes
)(u8cc
[0], arshape_buf
+ newlen
);
2628 newlen
+= (*mb_char2bytes
)(u8cc
[1],
2629 arshape_buf
+ newlen
);
2635 mch_memmove(arshape_buf
+ newlen
, p
, mb_l
);
2640 msg_outtrans_len(arshape_buf
, newlen
);
2644 msg_outtrans_len(ccline
.cmdbuff
+ start
, len
);
2648 * Put a character on the command line. Shifts the following text to the
2649 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2650 * "c" must be printable (fit in one display cell)!
2653 putcmdline(c
, shift
)
2662 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
2663 msg_no_more
= FALSE
;
2668 * Undo a putcmdline(c, FALSE).
2676 if (ccline
.cmdlen
== ccline
.cmdpos
)
2679 draw_cmdline(ccline
.cmdpos
, 1);
2680 msg_no_more
= FALSE
;
2685 * Put the given string, of the given length, onto the command line.
2686 * If len is -1, then STRLEN() is used to calculate the length.
2687 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2688 * part will be redrawn, otherwise it will not. If this function is called
2689 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2690 * called afterwards.
2693 put_on_cmdline(str
, len
, redraw
)
2704 len
= (int)STRLEN(str
);
2706 /* Check if ccline.cmdbuff needs to be longer */
2707 if (ccline
.cmdlen
+ len
+ 1 >= ccline
.cmdbufflen
)
2708 retval
= realloc_cmdbuff(ccline
.cmdlen
+ len
);
2713 if (!ccline
.overstrike
)
2715 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
+ len
,
2716 ccline
.cmdbuff
+ ccline
.cmdpos
,
2717 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
));
2718 ccline
.cmdlen
+= len
;
2725 /* Count nr of characters in the new string. */
2727 for (i
= 0; i
< len
; i
+= (*mb_ptr2len
)(str
+ i
))
2729 /* Count nr of bytes in cmdline that are overwritten by these
2731 for (i
= ccline
.cmdpos
; i
< ccline
.cmdlen
&& m
> 0;
2732 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
))
2734 if (i
< ccline
.cmdlen
)
2736 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
+ len
,
2737 ccline
.cmdbuff
+ i
, (size_t)(ccline
.cmdlen
- i
));
2738 ccline
.cmdlen
+= ccline
.cmdpos
+ len
- i
;
2741 ccline
.cmdlen
= ccline
.cmdpos
+ len
;
2745 if (ccline
.cmdpos
+ len
> ccline
.cmdlen
)
2746 ccline
.cmdlen
= ccline
.cmdpos
+ len
;
2748 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
, str
, (size_t)len
);
2749 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
2754 /* When the inserted text starts with a composing character,
2755 * backup to the character before it. There could be two of them.
2758 c
= utf_ptr2char(ccline
.cmdbuff
+ ccline
.cmdpos
);
2759 while (ccline
.cmdpos
> 0 && utf_iscomposing(c
))
2761 i
= (*mb_head_off
)(ccline
.cmdbuff
,
2762 ccline
.cmdbuff
+ ccline
.cmdpos
- 1) + 1;
2765 c
= utf_ptr2char(ccline
.cmdbuff
+ ccline
.cmdpos
);
2768 if (i
== 0 && ccline
.cmdpos
> 0 && arabic_maycombine(c
))
2770 /* Check the previous character for Arabic combining pair. */
2771 i
= (*mb_head_off
)(ccline
.cmdbuff
,
2772 ccline
.cmdbuff
+ ccline
.cmdpos
- 1) + 1;
2773 if (arabic_combine(utf_ptr2char(ccline
.cmdbuff
2774 + ccline
.cmdpos
- i
), c
))
2785 /* Also backup the cursor position. */
2786 i
= ptr2cells(ccline
.cmdbuff
+ ccline
.cmdpos
);
2787 ccline
.cmdspos
-= i
;
2798 if (redraw
&& !cmd_silent
)
2802 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
2803 /* Avoid clearing the rest of the line too often. */
2804 if (cmdline_row
!= i
|| ccline
.overstrike
)
2806 msg_no_more
= FALSE
;
2810 * If we are in Farsi command mode, the character input must be in
2811 * Insert mode. So do not advance the cmdpos.
2819 if (m
< 0) /* overflow, Columns or Rows at weird value */
2824 for (i
= 0; i
< len
; ++i
)
2826 c
= cmdline_charsize(ccline
.cmdpos
);
2828 /* count ">" for a double-wide char that doesn't fit. */
2830 correct_cmdspos(ccline
.cmdpos
, c
);
2832 /* Stop cursor at the end of the screen */
2833 if (ccline
.cmdspos
+ c
>= m
)
2835 ccline
.cmdspos
+= c
;
2839 c
= (*mb_ptr2len
)(ccline
.cmdbuff
+ ccline
.cmdpos
) - 1;
2840 if (c
> len
- i
- 1)
2855 static struct cmdline_info prev_ccline
;
2856 static int prev_ccline_used
= FALSE
;
2859 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2860 * and overwrite it. But get_cmdline_str() may need it, thus make it
2861 * available globally in prev_ccline.
2865 struct cmdline_info
*ccp
;
2867 if (!prev_ccline_used
)
2869 vim_memset(&prev_ccline
, 0, sizeof(struct cmdline_info
));
2870 prev_ccline_used
= TRUE
;
2873 prev_ccline
= ccline
;
2874 ccline
.cmdbuff
= NULL
;
2875 ccline
.cmdprompt
= NULL
;
2879 * Restore ccline after it has been saved with save_cmdline().
2882 restore_cmdline(ccp
)
2883 struct cmdline_info
*ccp
;
2885 ccline
= prev_ccline
;
2889 #if defined(FEAT_EVAL) || defined(PROTO)
2891 * Save the command line into allocated memory. Returns a pointer to be
2892 * passed to restore_cmdline_alloc() later.
2893 * Returns NULL when failed.
2896 save_cmdline_alloc()
2898 struct cmdline_info
*p
;
2900 p
= (struct cmdline_info
*)alloc((unsigned)sizeof(struct cmdline_info
));
2907 * Restore the command line from the return value of save_cmdline_alloc().
2910 restore_cmdline_alloc(p
)
2915 restore_cmdline((struct cmdline_info
*)p
);
2922 * paste a yank register into the command line.
2923 * used by CTRL-R command in command-line mode
2924 * insert_reg() can't be used here, because special characters from the
2925 * register contents will be interpreted as commands.
2927 * return FAIL for failure, OK otherwise
2930 cmdline_paste(regname
, literally
, remcr
)
2932 int literally
; /* Insert text literally instead of "as typed" */
2933 int remcr
; /* remove trailing CR */
2939 struct cmdline_info save_ccline
;
2941 /* check for valid regname; also accept special characters for CTRL-R in
2942 * the command line */
2943 if (regname
!= Ctrl_F
&& regname
!= Ctrl_P
&& regname
!= Ctrl_W
2944 && regname
!= Ctrl_A
&& !valid_yank_reg(regname
, FALSE
))
2947 /* A register containing CTRL-R can cause an endless loop. Allow using
2948 * CTRL-C to break the loop. */
2953 #ifdef FEAT_CLIPBOARD
2954 regname
= may_get_selection(regname
);
2957 /* Need to save and restore ccline. And set "textlock" to avoid nasty
2958 * things like going to another buffer when evaluating an expression. */
2959 save_cmdline(&save_ccline
);
2961 i
= get_spec_reg(regname
, &arg
, &allocated
, TRUE
);
2963 restore_cmdline(&save_ccline
);
2967 /* Got the value of a special register in "arg". */
2971 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2972 * part of the word. */
2974 if (p_is
&& regname
== Ctrl_W
)
2979 /* Locate start of last word in the cmd buffer. */
2980 for (w
= ccline
.cmdbuff
+ ccline
.cmdlen
; w
> ccline
.cmdbuff
; )
2985 len
= (*mb_head_off
)(ccline
.cmdbuff
, w
- 1) + 1;
2986 if (!vim_iswordc(mb_ptr2char(w
- len
)))
2993 if (!vim_iswordc(w
[-1]))
2998 len
= (int)((ccline
.cmdbuff
+ ccline
.cmdlen
) - w
);
2999 if (p_ic
? STRNICMP(w
, arg
, len
) == 0 : STRNCMP(w
, arg
, len
) == 0)
3003 cmdline_paste_str(p
, literally
);
3009 return cmdline_paste_reg(regname
, literally
, remcr
);
3013 * Put a string on the command line.
3014 * When "literally" is TRUE, insert literally.
3015 * When "literally" is FALSE, insert as typed, but don't leave the command
3019 cmdline_paste_str(s
, literally
)
3026 put_on_cmdline(s
, -1, TRUE
);
3031 if (cv
== Ctrl_V
&& s
[1])
3037 s
+= mb_char2len(c
);
3042 if (cv
== Ctrl_V
|| c
== ESC
|| c
== Ctrl_C
|| c
== CAR
|| c
== NL
3046 || (c
== Ctrl_BSL
&& *s
== Ctrl_N
))
3047 stuffcharReadbuff(Ctrl_V
);
3048 stuffcharReadbuff(c
);
3052 #ifdef FEAT_WILDMENU
3054 * Delete characters on the command line, from "from" to the current
3061 mch_memmove(ccline
.cmdbuff
+ from
, ccline
.cmdbuff
+ ccline
.cmdpos
,
3062 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
+ 1));
3063 ccline
.cmdlen
-= ccline
.cmdpos
- from
;
3064 ccline
.cmdpos
= from
;
3069 * this function is called when the screen size changes and with incremental
3077 need_wait_return
= FALSE
;
3090 if (ccline
.cmdfirstc
!= NUL
)
3091 msg_putchar(ccline
.cmdfirstc
);
3092 if (ccline
.cmdprompt
!= NULL
)
3094 msg_puts_attr(ccline
.cmdprompt
, ccline
.cmdattr
);
3095 ccline
.cmdindent
= msg_col
+ (msg_row
- cmdline_row
) * Columns
;
3096 /* do the reverse of set_cmdspos() */
3097 if (ccline
.cmdfirstc
!= NUL
)
3101 for (i
= ccline
.cmdindent
; i
> 0; --i
)
3106 * Redraw what is currently on the command line.
3114 /* when 'incsearch' is set there may be no command line while redrawing */
3115 if (ccline
.cmdbuff
== NULL
)
3117 windgoto(cmdline_row
, 0);
3125 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3127 draw_cmdline(0, ccline
.cmdlen
);
3129 msg_no_more
= FALSE
;
3131 set_cmdspos_cursor();
3134 * An emsg() before may have set msg_scroll. This is used in normal mode,
3135 * in cmdline mode we can reset them now.
3137 msg_scroll
= FALSE
; /* next message overwrites cmdline */
3139 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3140 * in cmdline mode */
3141 skip_redraw
= FALSE
;
3147 if (exmode_active
|| msg_scrolled
!= 0)
3148 cmdline_row
= Rows
- 1;
3150 cmdline_row
= W_WINROW(lastwin
) + lastwin
->w_height
3151 + W_STATUS_HEIGHT(lastwin
);
3160 #ifdef FEAT_RIGHTLEFT
3163 msg_row
= cmdline_row
+ (ccline
.cmdspos
/ (int)(Columns
- 1));
3164 msg_col
= (int)Columns
- (ccline
.cmdspos
% (int)(Columns
- 1)) - 1;
3171 msg_row
= cmdline_row
+ (ccline
.cmdspos
/ (int)Columns
);
3172 msg_col
= ccline
.cmdspos
% (int)Columns
;
3173 if (msg_row
>= Rows
)
3177 windgoto(msg_row
, msg_col
);
3178 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3179 redrawcmd_preedit();
3181 #ifdef MCH_CURSOR_SHAPE
3182 mch_update_cursor();
3191 #ifdef FEAT_RIGHTLEFT
3193 msg_col
= Columns
- 1;
3196 msg_col
= 0; /* always start in column 0 */
3197 if (clr
) /* clear the bottom line(s) */
3198 msg_clr_eos(); /* will reset clear_cmdline */
3199 windgoto(cmdline_row
, 0);
3203 * Check the word in front of the cursor for an abbreviation.
3204 * Called when the non-id character "c" has been entered.
3205 * When an abbreviation is recognized it is removed from the text with
3206 * backspaces and the replacement string is inserted, followed by "c".
3212 if (p_paste
|| no_abbr
) /* no abbreviations or in paste mode */
3215 return check_abbr(c
, ccline
.cmdbuff
, ccline
.cmdpos
, 0);
3219 * Return FAIL if this is not an appropriate context in which to do
3220 * completion of anything, return OK if it is (even if there are no matches).
3221 * For the caller, this means that the character is just passed through like a
3222 * normal character (instead of being expanded). This allows :s/^I^D etc.
3225 nextwild(xp
, type
, options
)
3228 int options
; /* extra options for ExpandOne() */
3237 if (xp
->xp_numfiles
== -1)
3239 set_expand_context(xp
);
3240 cmd_showtail
= expand_showtail(xp
);
3243 if (xp
->xp_context
== EXPAND_UNSUCCESSFUL
)
3246 return OK
; /* Something illegal on command line */
3248 if (xp
->xp_context
== EXPAND_NOTHING
)
3250 /* Caller can use the character as a normal char instead */
3254 MSG_PUTS("..."); /* show that we are busy */
3257 i
= (int)(xp
->xp_pattern
- ccline
.cmdbuff
);
3258 oldlen
= ccline
.cmdpos
- i
;
3260 if (type
== WILD_NEXT
|| type
== WILD_PREV
)
3263 * Get next/previous match for a previous expanded pattern.
3265 p2
= ExpandOne(xp
, NULL
, NULL
, 0, type
);
3270 * Translate string into pattern and expand it.
3272 if ((p1
= addstar(&ccline
.cmdbuff
[i
], oldlen
, xp
->xp_context
)) == NULL
)
3276 p2
= ExpandOne(xp
, p1
, vim_strnsave(&ccline
.cmdbuff
[i
], oldlen
),
3277 WILD_HOME_REPLACE
|WILD_ADD_SLASH
|WILD_SILENT
|WILD_ESCAPE
3280 /* longest match: make sure it is not shorter (happens with :help */
3281 if (p2
!= NULL
&& type
== WILD_LONGEST
)
3283 for (j
= 0; j
< oldlen
; ++j
)
3284 if (ccline
.cmdbuff
[i
+ j
] == '*'
3285 || ccline
.cmdbuff
[i
+ j
] == '?')
3287 if ((int)STRLEN(p2
) < j
)
3296 if (p2
!= NULL
&& !got_int
)
3298 difflen
= (int)STRLEN(p2
) - oldlen
;
3299 if (ccline
.cmdlen
+ difflen
> ccline
.cmdbufflen
- 4)
3301 v
= realloc_cmdbuff(ccline
.cmdlen
+ difflen
);
3302 xp
->xp_pattern
= ccline
.cmdbuff
+ i
;
3308 mch_memmove(&ccline
.cmdbuff
[ccline
.cmdpos
+ difflen
],
3309 &ccline
.cmdbuff
[ccline
.cmdpos
],
3310 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
+ 1));
3311 mch_memmove(&ccline
.cmdbuff
[i
], p2
, STRLEN(p2
));
3312 ccline
.cmdlen
+= difflen
;
3313 ccline
.cmdpos
+= difflen
;
3321 /* When expanding a ":map" command and no matches are found, assume that
3322 * the key is supposed to be inserted literally */
3323 if (xp
->xp_context
== EXPAND_MAPPINGS
&& p2
== NULL
)
3326 if (xp
->xp_numfiles
<= 0 && p2
== NULL
)
3328 else if (xp
->xp_numfiles
== 1)
3329 /* free expanded pattern */
3330 (void)ExpandOne(xp
, NULL
, NULL
, 0, WILD_FREE
);
3336 * Do wildcard expansion on the string 'str'.
3337 * Chars that should not be expanded must be preceded with a backslash.
3338 * Return a pointer to alloced memory containing the new string.
3339 * Return NULL for failure.
3341 * "orig" is the originally expanded string, copied to allocated memory. It
3342 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3343 * WILD_PREV "orig" should be NULL.
3345 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3346 * is WILD_EXPAND_FREE or WILD_ALL.
3348 * mode = WILD_FREE: just free previously expanded matches
3349 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3350 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3351 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3352 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3353 * mode = WILD_ALL: return all matches concatenated
3354 * mode = WILD_LONGEST: return longest matched part
3356 * options = WILD_LIST_NOTFOUND: list entries without a match
3357 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3358 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3359 * options = WILD_NO_BEEP: Don't beep for multiple matches
3360 * options = WILD_ADD_SLASH: add a slash after directory names
3361 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3362 * options = WILD_SILENT: don't print warning messages
3363 * options = WILD_ESCAPE: put backslash before special chars
3365 * The variables xp->xp_context and xp->xp_backslash must have been set!
3368 ExpandOne(xp
, str
, orig
, options
, mode
)
3371 char_u
*orig
; /* allocated copy of original of expanded string */
3377 static char_u
*orig_save
= NULL
; /* kept value of orig */
3378 int orig_saved
= FALSE
;
3381 int non_suf_match
; /* number without matching suffix */
3384 * first handle the case of using an old match
3386 if (mode
== WILD_NEXT
|| mode
== WILD_PREV
)
3388 if (xp
->xp_numfiles
> 0)
3390 if (mode
== WILD_PREV
)
3393 findex
= xp
->xp_numfiles
;
3396 else /* mode == WILD_NEXT */
3400 * When wrapping around, return the original string, set findex to
3405 if (orig_save
== NULL
)
3406 findex
= xp
->xp_numfiles
- 1;
3410 if (findex
>= xp
->xp_numfiles
)
3412 if (orig_save
== NULL
)
3417 #ifdef FEAT_WILDMENU
3419 win_redr_status_matches(xp
, xp
->xp_numfiles
, xp
->xp_files
,
3420 findex
, cmd_showtail
);
3423 return vim_strsave(orig_save
);
3424 return vim_strsave(xp
->xp_files
[findex
]);
3430 /* free old names */
3431 if (xp
->xp_numfiles
!= -1 && mode
!= WILD_ALL
&& mode
!= WILD_LONGEST
)
3433 FreeWild(xp
->xp_numfiles
, xp
->xp_files
);
3434 xp
->xp_numfiles
= -1;
3435 vim_free(orig_save
);
3440 if (mode
== WILD_FREE
) /* only release file name */
3443 if (xp
->xp_numfiles
== -1)
3445 vim_free(orig_save
);
3452 if (ExpandFromContext(xp
, str
, &xp
->xp_numfiles
, &xp
->xp_files
,
3455 #ifdef FNAME_ILLEGAL
3456 /* Illegal file name has been silently skipped. But when there
3457 * are wildcards, the real problem is that there was no match,
3458 * causing the pattern to be added, which has illegal characters.
3460 if (!(options
& WILD_SILENT
) && (options
& WILD_LIST_NOTFOUND
))
3461 EMSG2(_(e_nomatch2
), str
);
3464 else if (xp
->xp_numfiles
== 0)
3466 if (!(options
& WILD_SILENT
))
3467 EMSG2(_(e_nomatch2
), str
);
3471 /* Escape the matches for use on the command line. */
3472 ExpandEscape(xp
, str
, xp
->xp_numfiles
, xp
->xp_files
, options
);
3475 * Check for matching suffixes in file names.
3477 if (mode
!= WILD_ALL
&& mode
!= WILD_LONGEST
)
3479 if (xp
->xp_numfiles
)
3480 non_suf_match
= xp
->xp_numfiles
;
3483 if ((xp
->xp_context
== EXPAND_FILES
3484 || xp
->xp_context
== EXPAND_DIRECTORIES
)
3485 && xp
->xp_numfiles
> 1)
3488 * More than one match; check suffix.
3489 * The files will have been sorted on matching suffix in
3490 * expand_wildcards, only need to check the first two.
3493 for (i
= 0; i
< 2; ++i
)
3494 if (match_suffix(xp
->xp_files
[i
]))
3497 if (non_suf_match
!= 1)
3499 /* Can we ever get here unless it's while expanding
3500 * interactively? If not, we can get rid of this all
3501 * together. Don't really want to wait for this message
3502 * (and possibly have to hit return to continue!).
3504 if (!(options
& WILD_SILENT
))
3506 else if (!(options
& WILD_NO_BEEP
))
3509 if (!(non_suf_match
!= 1 && mode
== WILD_EXPAND_FREE
))
3510 ss
= vim_strsave(xp
->xp_files
[0]);
3515 /* Find longest common part */
3516 if (mode
== WILD_LONGEST
&& xp
->xp_numfiles
> 0)
3518 for (len
= 0; xp
->xp_files
[0][len
]; ++len
)
3520 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3522 #ifdef CASE_INSENSITIVE_FILENAME
3523 if (xp
->xp_context
== EXPAND_DIRECTORIES
3524 || xp
->xp_context
== EXPAND_FILES
3525 || xp
->xp_context
== EXPAND_SHELLCMD
3526 || xp
->xp_context
== EXPAND_BUFFERS
)
3528 if (TOLOWER_LOC(xp
->xp_files
[i
][len
]) !=
3529 TOLOWER_LOC(xp
->xp_files
[0][len
]))
3534 if (xp
->xp_files
[i
][len
] != xp
->xp_files
[0][len
])
3537 if (i
< xp
->xp_numfiles
)
3539 if (!(options
& WILD_NO_BEEP
))
3544 ss
= alloc((unsigned)len
+ 1);
3546 vim_strncpy(ss
, xp
->xp_files
[0], (size_t)len
);
3547 findex
= -1; /* next p_wc gets first one */
3550 /* Concatenate all matching names */
3551 if (mode
== WILD_ALL
&& xp
->xp_numfiles
> 0)
3554 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3555 len
+= (long_u
)STRLEN(xp
->xp_files
[i
]) + 1;
3556 ss
= lalloc(len
, TRUE
);
3560 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3562 STRCAT(ss
, xp
->xp_files
[i
]);
3563 if (i
!= xp
->xp_numfiles
- 1)
3564 STRCAT(ss
, (options
& WILD_USE_NL
) ? "\n" : " ");
3569 if (mode
== WILD_EXPAND_FREE
|| mode
== WILD_ALL
)
3572 /* Free "orig" if it wasn't stored in "orig_save". */
3580 * Prepare an expand structure for use.
3586 xp
->xp_backslash
= XP_BS_NONE
;
3587 #ifndef BACKSLASH_IN_FILENAME
3588 xp
->xp_shell
= FALSE
;
3590 xp
->xp_numfiles
= -1;
3591 xp
->xp_files
= NULL
;
3592 #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3598 * Cleanup an expand structure after use.
3604 if (xp
->xp_numfiles
>= 0)
3606 FreeWild(xp
->xp_numfiles
, xp
->xp_files
);
3607 xp
->xp_numfiles
= -1;
3612 ExpandEscape(xp
, str
, numfiles
, files
, options
)
3623 * May change home directory back to "~"
3625 if (options
& WILD_HOME_REPLACE
)
3626 tilde_replace(str
, numfiles
, files
);
3628 if (options
& WILD_ESCAPE
)
3630 if (xp
->xp_context
== EXPAND_FILES
3631 || xp
->xp_context
== EXPAND_SHELLCMD
3632 || xp
->xp_context
== EXPAND_BUFFERS
3633 || xp
->xp_context
== EXPAND_DIRECTORIES
)
3636 * Insert a backslash into a file name before a space, \, %, #
3637 * and wildmatch characters, except '~'.
3639 for (i
= 0; i
< numfiles
; ++i
)
3641 /* for ":set path=" we need to escape spaces twice */
3642 if (xp
->xp_backslash
== XP_BS_THREE
)
3644 p
= vim_strsave_escaped(files
[i
], (char_u
*)" ");
3649 #if defined(BACKSLASH_IN_FILENAME)
3650 p
= vim_strsave_escaped(files
[i
], (char_u
*)" ");
3659 #ifdef BACKSLASH_IN_FILENAME
3664 /* Don't escape '[' and '{' if they are in 'isfname'. */
3665 for (p
= PATH_ESC_CHARS
; *p
!= NUL
; ++p
)
3666 if ((*p
!= '[' && *p
!= '{') || !vim_isfilec(*p
))
3669 p
= vim_strsave_escaped(files
[i
], buf
);
3672 p
= vim_strsave_escaped(files
[i
],
3673 xp
->xp_shell
? SHELL_ESC_CHARS
: PATH_ESC_CHARS
);
3681 /* If 'str' starts with "\~", replace "~" at start of
3682 * files[i] with "\~". */
3683 if (str
[0] == '\\' && str
[1] == '~' && files
[i
][0] == '~')
3684 escape_fname(&files
[i
]);
3686 xp
->xp_backslash
= XP_BS_NONE
;
3688 /* If the first file starts with a '+' escape it. Otherwise it
3689 * could be seen as "+cmd". */
3690 if (*files
[0] == '+')
3691 escape_fname(&files
[0]);
3693 else if (xp
->xp_context
== EXPAND_TAGS
)
3696 * Insert a backslash before characters in a tag name that
3697 * would terminate the ":tag" command.
3699 for (i
= 0; i
< numfiles
; ++i
)
3701 p
= vim_strsave_escaped(files
[i
], (char_u
*)"\\|\"");
3713 * Put a backslash before the file name in "pp", which is in allocated memory.
3721 p
= alloc((unsigned)(STRLEN(*pp
) + 2));
3732 * For each file name in files[num_files]:
3733 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3736 tilde_replace(orig_pat
, num_files
, files
)
3744 if (orig_pat
[0] == '~' && vim_ispathsep(orig_pat
[1]))
3746 for (i
= 0; i
< num_files
; ++i
)
3748 p
= home_replace_save(NULL
, files
[i
]);
3759 * Show all matches for completion on the command line.
3760 * Returns EXPAND_NOTHING when the character that triggered expansion should
3761 * be inserted like a normal character.
3765 showmatches(xp
, wildmenu
)
3769 #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3771 char_u
**files_found
;
3781 if (xp
->xp_numfiles
== -1)
3783 set_expand_context(xp
);
3784 i
= expand_cmdline(xp
, ccline
.cmdbuff
, ccline
.cmdpos
,
3785 &num_files
, &files_found
);
3786 showtail
= expand_showtail(xp
);
3793 num_files
= xp
->xp_numfiles
;
3794 files_found
= xp
->xp_files
;
3795 showtail
= cmd_showtail
;
3798 #ifdef FEAT_WILDMENU
3802 msg_didany
= FALSE
; /* lines_left will be set */
3803 msg_start(); /* prepare for paging */
3806 cmdline_row
= msg_row
;
3807 msg_didany
= FALSE
; /* lines_left will be set again */
3808 msg_start(); /* prepare for paging */
3809 #ifdef FEAT_WILDMENU
3814 got_int
= FALSE
; /* only int. the completion, not the cmd line */
3815 #ifdef FEAT_WILDMENU
3817 win_redr_status_matches(xp
, num_files
, files_found
, 0, showtail
);
3821 /* find the length of the longest file name */
3823 for (i
= 0; i
< num_files
; ++i
)
3825 if (!showtail
&& (xp
->xp_context
== EXPAND_FILES
3826 || xp
->xp_context
== EXPAND_SHELLCMD
3827 || xp
->xp_context
== EXPAND_BUFFERS
))
3829 home_replace(NULL
, files_found
[i
], NameBuff
, MAXPATHL
, TRUE
);
3830 j
= vim_strsize(NameBuff
);
3833 j
= vim_strsize(L_SHOWFILE(i
));
3838 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3842 /* compute the number of columns and lines for the listing */
3843 maxlen
+= 2; /* two spaces between file names */
3844 columns
= ((int)Columns
+ 2) / maxlen
;
3847 lines
= (num_files
+ columns
- 1) / columns
;
3850 attr
= hl_attr(HLF_D
); /* find out highlighting for directories */
3852 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3854 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T
));
3856 msg_advance(maxlen
- 3);
3857 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T
));
3860 /* list the files line by line */
3861 for (i
= 0; i
< lines
; ++i
)
3864 for (k
= i
; k
< num_files
; k
+= lines
)
3866 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3868 msg_outtrans_attr(files_found
[k
], hl_attr(HLF_D
));
3869 p
= files_found
[k
] + STRLEN(files_found
[k
]) + 1;
3870 msg_advance(maxlen
+ 1);
3872 msg_advance(maxlen
+ 3);
3873 msg_puts_long_attr(p
+ 2, hl_attr(HLF_D
));
3876 for (j
= maxlen
- lastlen
; --j
>= 0; )
3878 if (xp
->xp_context
== EXPAND_FILES
3879 || xp
->xp_context
== EXPAND_SHELLCMD
3880 || xp
->xp_context
== EXPAND_BUFFERS
)
3882 /* highlight directories */
3883 j
= (mch_isdir(files_found
[k
]));
3888 home_replace(NULL
, files_found
[k
], NameBuff
, MAXPATHL
,
3898 lastlen
= msg_outtrans_attr(p
, j
? attr
: 0);
3900 if (msg_col
> 0) /* when not wrapped around */
3905 out_flush(); /* show one line at a time */
3914 * we redraw the command below the lines that we have just listed
3915 * This is a bit tricky, but it saves a lot of screen updating.
3917 cmdline_row
= msg_row
; /* will put it back later */
3920 if (xp
->xp_numfiles
== -1)
3921 FreeWild(num_files
, files_found
);
3927 * Private gettail for showmatches() (and win_redr_status_matches()):
3928 * Find tail of file name path, but ignore trailing "/".
3936 int had_sep
= FALSE
;
3938 for (p
= s
; *p
!= NUL
; )
3940 if (vim_ispathsep(*p
)
3941 #ifdef BACKSLASH_IN_FILENAME
3942 && !rem_backslash(p
)
3957 * Return TRUE if we only need to show the tail of completion matches.
3958 * When not completing file names or there is a wildcard in the path FALSE is
3968 /* When not completing file names a "/" may mean something different. */
3969 if (xp
->xp_context
!= EXPAND_FILES
3970 && xp
->xp_context
!= EXPAND_SHELLCMD
3971 && xp
->xp_context
!= EXPAND_DIRECTORIES
)
3974 end
= gettail(xp
->xp_pattern
);
3975 if (end
== xp
->xp_pattern
) /* there is no path separator */
3978 for (s
= xp
->xp_pattern
; s
< end
; s
++)
3980 /* Skip escaped wildcards. Only when the backslash is not a path
3981 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3982 if (rem_backslash(s
))
3984 else if (vim_strchr((char_u
*)"*?[", *s
) != NULL
)
3991 * Prepare a string for expansion.
3992 * When expanding file names: The string will be used with expand_wildcards().
3993 * Copy the file name into allocated memory and add a '*' at the end.
3994 * When expanding other names: The string will be used with regcomp(). Copy
3995 * the name into allocated memory and prepend "^".
3998 addstar(fname
, len
, context
)
4001 int context
; /* EXPAND_FILES etc. */
4008 if (context
!= EXPAND_FILES
4009 && context
!= EXPAND_SHELLCMD
4010 && context
!= EXPAND_DIRECTORIES
)
4013 * Matching will be done internally (on something other than files).
4014 * So we convert the file-matching-type wildcards into our kind for
4015 * use with vim_regcomp(). First work out how long it will be:
4018 /* For help tags the translation is done in find_help_tags().
4019 * For a tag pattern starting with "/" no translation is needed. */
4020 if (context
== EXPAND_HELP
4021 || context
== EXPAND_COLORS
4022 || context
== EXPAND_COMPILER
4023 || (context
== EXPAND_TAGS
&& fname
[0] == '/'))
4024 retval
= vim_strnsave(fname
, len
);
4027 new_len
= len
+ 2; /* +2 for '^' at start, NUL at end */
4028 for (i
= 0; i
< len
; i
++)
4030 if (fname
[i
] == '*' || fname
[i
] == '~')
4031 new_len
++; /* '*' needs to be replaced by ".*"
4032 '~' needs to be replaced by "\~" */
4034 /* Buffer names are like file names. "." should be literal */
4035 if (context
== EXPAND_BUFFERS
&& fname
[i
] == '.')
4036 new_len
++; /* "." becomes "\." */
4038 /* Custom expansion takes care of special things, match
4039 * backslashes literally (perhaps also for other types?) */
4040 if ((context
== EXPAND_USER_DEFINED
4041 || context
== EXPAND_USER_LIST
) && fname
[i
] == '\\')
4042 new_len
++; /* '\' becomes "\\" */
4044 retval
= alloc(new_len
);
4049 for (i
= 0; i
< len
; i
++, j
++)
4051 /* Skip backslash. But why? At least keep it for custom
4053 if (context
!= EXPAND_USER_DEFINED
4054 && context
!= EXPAND_USER_LIST
4061 case '*': retval
[j
++] = '.';
4063 case '~': retval
[j
++] = '\\';
4065 case '?': retval
[j
] = '.';
4067 case '.': if (context
== EXPAND_BUFFERS
)
4070 case '\\': if (context
== EXPAND_USER_DEFINED
4071 || context
== EXPAND_USER_LIST
)
4075 retval
[j
] = fname
[i
];
4083 retval
= alloc(len
+ 4);
4086 vim_strncpy(retval
, fname
, len
);
4089 * Don't add a star to *, ~, ~user, $var or `cmd`.
4090 * * would become **, which walks the whole tree.
4091 * ~ would be at the start of the file name, but not the tail.
4092 * $ could be anywhere in the tail.
4093 * ` could be anywhere in the file name.
4094 * When the name ends in '$' don't add a star, remove the '$'.
4096 tail
= gettail(retval
);
4097 if ((*retval
!= '~' || tail
!= retval
)
4098 && (len
== 0 || retval
[len
- 1] != '*')
4099 && vim_strchr(tail
, '$') == NULL
4100 && vim_strchr(retval
, '`') == NULL
)
4101 retval
[len
++] = '*';
4102 else if (len
> 0 && retval
[len
- 1] == '$')
4111 * Must parse the command line so far to work out what context we are in.
4112 * Completion can then be done based on that context.
4113 * This routine sets the variables:
4114 * xp->xp_pattern The start of the pattern to be expanded within
4115 * the command line (ends at the cursor).
4116 * xp->xp_context The type of thing to expand. Will be one of:
4118 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4119 * the command line, like an unknown command. Caller
4121 * EXPAND_NOTHING Unrecognised context for completion, use char like
4122 * a normal char, rather than for completion. eg
4124 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4126 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4127 * EXPAND_FILES After command with XFILE set, or after setting
4128 * with P_EXPAND set. eg :e ^I, :w>>^I
4129 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4130 * when we know only directories are of interest. eg
4132 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
4133 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4134 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4135 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4136 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4137 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4138 * EXPAND_EVENTS Complete event names
4139 * EXPAND_SYNTAX Complete :syntax command arguments
4140 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4141 * EXPAND_AUGROUP Complete autocommand group names
4142 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4143 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4144 * eg :unmap a^I , :cunab x^I
4145 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4147 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4148 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4149 * names in expressions, eg :while s^I
4150 * EXPAND_ENV_VARS Complete environment variable names
4153 set_expand_context(xp
)
4156 /* only expansion for ':', '>' and '=' command-lines */
4157 if (ccline
.cmdfirstc
!= ':'
4159 && ccline
.cmdfirstc
!= '>' && ccline
.cmdfirstc
!= '='
4164 xp
->xp_context
= EXPAND_NOTHING
;
4167 set_cmd_context(xp
, ccline
.cmdbuff
, ccline
.cmdlen
, ccline
.cmdpos
);
4171 set_cmd_context(xp
, str
, len
, col
)
4173 char_u
*str
; /* start of command line */
4174 int len
; /* length of command line (excl. NUL) */
4175 int col
; /* position of cursor */
4181 * Avoid a UMR warning from Purify, only save the character if it has been
4185 old_char
= str
[col
];
4190 if (ccline
.cmdfirstc
== '=')
4192 # ifdef FEAT_CMDL_COMPL
4193 /* pass CMD_SIZE because there is no real command */
4194 set_context_for_expression(xp
, str
, CMD_SIZE
);
4197 else if (ccline
.input_fn
)
4199 xp
->xp_context
= ccline
.xp_context
;
4200 xp
->xp_pattern
= ccline
.cmdbuff
;
4201 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
4202 xp
->xp_arg
= ccline
.xp_arg
;
4207 while (nextcomm
!= NULL
)
4208 nextcomm
= set_one_cmd_context(xp
, nextcomm
);
4210 str
[col
] = old_char
;
4214 * Expand the command line "str" from context "xp".
4215 * "xp" must have been set by set_cmd_context().
4216 * xp->xp_pattern points into "str", to where the text that is to be expanded
4218 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4220 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4221 * key that triggered expansion literally.
4222 * Returns EXPAND_OK otherwise.
4225 expand_cmdline(xp
, str
, col
, matchcount
, matches
)
4227 char_u
*str
; /* start of command line */
4228 int col
; /* position of cursor */
4229 int *matchcount
; /* return: nr of matches */
4230 char_u
***matches
; /* return: array of pointers to matches */
4232 char_u
*file_str
= NULL
;
4234 if (xp
->xp_context
== EXPAND_UNSUCCESSFUL
)
4237 return EXPAND_UNSUCCESSFUL
; /* Something illegal on command line */
4239 if (xp
->xp_context
== EXPAND_NOTHING
)
4241 /* Caller can use the character as a normal char instead */
4242 return EXPAND_NOTHING
;
4245 /* add star to file name, or convert to regexp if not exp. files. */
4246 file_str
= addstar(xp
->xp_pattern
,
4247 (int)(str
+ col
- xp
->xp_pattern
), xp
->xp_context
);
4248 if (file_str
== NULL
)
4249 return EXPAND_UNSUCCESSFUL
;
4251 /* find all files that match the description */
4252 if (ExpandFromContext(xp
, file_str
, matchcount
, matches
,
4253 WILD_ADD_SLASH
|WILD_SILENT
) == FAIL
)
4263 #ifdef FEAT_MULTI_LANG
4265 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4267 static void cleanup_help_tags
__ARGS((int num_file
, char_u
**file
));
4270 cleanup_help_tags(num_file
, file
)
4277 for (i
= 0; i
< num_file
; ++i
)
4279 len
= (int)STRLEN(file
[i
]) - 3;
4280 if (len
> 0 && STRCMP(file
[i
] + len
, "@en") == 0)
4282 /* Sorting on priority means the same item in another language may
4283 * be anywhere. Search all items for a match up to the "@en". */
4284 for (j
= 0; j
< num_file
; ++j
)
4286 && (int)STRLEN(file
[j
]) == len
+ 3
4287 && STRNCMP(file
[i
], file
[j
], len
+ 1) == 0)
4297 * Do the expansion based on xp->xp_context and "pat".
4300 ExpandFromContext(xp
, pat
, num_file
, file
, options
)
4307 #ifdef FEAT_CMDL_COMPL
4308 regmatch_T regmatch
;
4313 flags
= EW_DIR
; /* include directories */
4314 if (options
& WILD_LIST_NOTFOUND
)
4315 flags
|= EW_NOTFOUND
;
4316 if (options
& WILD_ADD_SLASH
)
4317 flags
|= EW_ADDSLASH
;
4318 if (options
& WILD_KEEP_ALL
)
4319 flags
|= EW_KEEPALL
;
4320 if (options
& WILD_SILENT
)
4323 if (xp
->xp_context
== EXPAND_FILES
|| xp
->xp_context
== EXPAND_DIRECTORIES
)
4326 * Expand file or directory names.
4328 int free_pat
= FALSE
;
4331 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4333 if (xp
->xp_backslash
!= XP_BS_NONE
)
4336 pat
= vim_strsave(pat
);
4337 for (i
= 0; pat
[i
]; ++i
)
4340 if (xp
->xp_backslash
== XP_BS_THREE
4341 && pat
[i
+ 1] == '\\'
4342 && pat
[i
+ 2] == '\\'
4343 && pat
[i
+ 3] == ' ')
4344 mch_memmove(pat
+ i
, pat
+ i
+ 3,
4345 STRLEN(pat
+ i
+ 3) + 1);
4346 if (xp
->xp_backslash
== XP_BS_ONE
4347 && pat
[i
+ 1] == ' ')
4348 mch_memmove(pat
+ i
, pat
+ i
+ 1, STRLEN(pat
+ i
));
4352 if (xp
->xp_context
== EXPAND_FILES
)
4355 flags
= (flags
| EW_DIR
) & ~EW_FILE
;
4356 ret
= expand_wildcards(1, &pat
, num_file
, file
, flags
);
4362 *file
= (char_u
**)"";
4364 if (xp
->xp_context
== EXPAND_HELP
)
4366 if (find_help_tags(pat
, num_file
, file
, FALSE
) == OK
)
4368 #ifdef FEAT_MULTI_LANG
4369 cleanup_help_tags(*num_file
, *file
);
4376 #ifndef FEAT_CMDL_COMPL
4379 if (xp
->xp_context
== EXPAND_SHELLCMD
)
4380 return expand_shellcmd(pat
, num_file
, file
, flags
);
4381 if (xp
->xp_context
== EXPAND_OLD_SETTING
)
4382 return ExpandOldSetting(num_file
, file
);
4383 if (xp
->xp_context
== EXPAND_BUFFERS
)
4384 return ExpandBufnames(pat
, num_file
, file
, options
);
4385 if (xp
->xp_context
== EXPAND_TAGS
4386 || xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
4387 return expand_tags(xp
->xp_context
== EXPAND_TAGS
, pat
, num_file
, file
);
4388 if (xp
->xp_context
== EXPAND_COLORS
)
4389 return ExpandRTDir(pat
, num_file
, file
, "colors");
4390 if (xp
->xp_context
== EXPAND_COMPILER
)
4391 return ExpandRTDir(pat
, num_file
, file
, "compiler");
4392 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4393 if (xp
->xp_context
== EXPAND_USER_LIST
)
4394 return ExpandUserList(xp
, num_file
, file
);
4397 regmatch
.regprog
= vim_regcomp(pat
, p_magic
? RE_MAGIC
: 0);
4398 if (regmatch
.regprog
== NULL
)
4401 /* set ignore-case according to p_ic, p_scs and pat */
4402 regmatch
.rm_ic
= ignorecase(pat
);
4404 if (xp
->xp_context
== EXPAND_SETTINGS
4405 || xp
->xp_context
== EXPAND_BOOL_SETTINGS
)
4406 ret
= ExpandSettings(xp
, ®match
, num_file
, file
);
4407 else if (xp
->xp_context
== EXPAND_MAPPINGS
)
4408 ret
= ExpandMappings(®match
, num_file
, file
);
4409 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4410 else if (xp
->xp_context
== EXPAND_USER_DEFINED
)
4411 ret
= ExpandUserDefined(xp
, ®match
, num_file
, file
);
4415 static struct expgen
4418 char_u
*((*func
)__ARGS((expand_T
*, int)));
4422 {EXPAND_COMMANDS
, get_command_name
, FALSE
},
4423 #ifdef FEAT_USR_CMDS
4424 {EXPAND_USER_COMMANDS
, get_user_commands
, FALSE
},
4425 {EXPAND_USER_CMD_FLAGS
, get_user_cmd_flags
, FALSE
},
4426 {EXPAND_USER_NARGS
, get_user_cmd_nargs
, FALSE
},
4427 {EXPAND_USER_COMPLETE
, get_user_cmd_complete
, FALSE
},
4430 {EXPAND_USER_VARS
, get_user_var_name
, FALSE
},
4431 {EXPAND_FUNCTIONS
, get_function_name
, FALSE
},
4432 {EXPAND_USER_FUNC
, get_user_func_name
, FALSE
},
4433 {EXPAND_EXPRESSION
, get_expr_name
, FALSE
},
4436 {EXPAND_MENUS
, get_menu_name
, FALSE
},
4437 {EXPAND_MENUNAMES
, get_menu_names
, FALSE
},
4440 {EXPAND_SYNTAX
, get_syntax_name
, TRUE
},
4442 {EXPAND_HIGHLIGHT
, get_highlight_name
, TRUE
},
4444 {EXPAND_EVENTS
, get_event_name
, TRUE
},
4445 {EXPAND_AUGROUP
, get_augroup_name
, TRUE
},
4447 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4448 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4449 {EXPAND_LANGUAGE
, get_lang_arg
, TRUE
},
4451 {EXPAND_ENV_VARS
, get_env_name
, TRUE
},
4456 * Find a context in the table and call the ExpandGeneric() with the
4457 * right function to do the expansion.
4460 for (i
= 0; i
< sizeof(tab
) / sizeof(struct expgen
); ++i
)
4461 if (xp
->xp_context
== tab
[i
].context
)
4464 regmatch
.rm_ic
= TRUE
;
4465 ret
= ExpandGeneric(xp
, ®match
, num_file
, file
, tab
[i
].func
);
4470 vim_free(regmatch
.regprog
);
4473 #endif /* FEAT_CMDL_COMPL */
4476 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4478 * Expand a list of names.
4480 * Generic function for command line completion. It calls a function to
4481 * obtain strings, one by one. The strings are matched against a regexp
4482 * program. Matching strings are copied into an array, which is returned.
4484 * Returns OK when no problems encountered, FAIL for error (out of memory).
4487 ExpandGeneric(xp
, regmatch
, num_file
, file
, func
)
4489 regmatch_T
*regmatch
;
4492 char_u
*((*func
)__ARGS((expand_T
*, int)));
4493 /* returns a string from the list */
4500 /* do this loop twice:
4501 * round == 0: count the number of matching names
4502 * round == 1: copy the matching names into allocated memory
4504 for (round
= 0; round
<= 1; ++round
)
4508 str
= (*func
)(xp
, i
);
4509 if (str
== NULL
) /* end of list */
4511 if (*str
== NUL
) /* skip empty strings */
4514 if (vim_regexec(regmatch
, str
, (colnr_T
)0))
4518 str
= vim_strsave_escaped(str
, (char_u
*)" \t\\.");
4519 (*file
)[count
] = str
;
4521 if (func
== get_menu_names
&& str
!= NULL
)
4523 /* test for separator added by get_menu_names() */
4524 str
+= STRLEN(str
) - 1;
4538 *file
= (char_u
**)alloc((unsigned)(count
* sizeof(char_u
*)));
4541 *file
= (char_u
**)"";
4548 /* Sort the results. Keep menu's in the specified order. */
4549 if (xp
->xp_context
!= EXPAND_MENUNAMES
&& xp
->xp_context
!= EXPAND_MENUS
)
4550 sort_strings(*file
, *num_file
);
4552 #ifdef FEAT_CMDL_COMPL
4553 /* Reset the variables used for special highlight names expansion, so that
4554 * they don't show up when getting normal highlight names by ID. */
4555 reset_expand_highlight();
4562 * Complete a shell command.
4563 * Returns FAIL or OK;
4566 expand_shellcmd(filepat
, num_file
, file
, flagsarg
)
4567 char_u
*filepat
; /* pattern to match with command names */
4568 int *num_file
; /* return: number of matches */
4569 char_u
***file
; /* return: array with matches */
4570 int flagsarg
; /* EW_ flags */
4575 int mustfree
= FALSE
;
4577 char_u
*buf
= alloc(MAXPATHL
);
4580 int flags
= flagsarg
;
4586 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4588 pat
= vim_strsave(filepat
);
4589 for (i
= 0; pat
[i
]; ++i
)
4590 if (pat
[i
] == '\\' && pat
[i
+ 1] == ' ')
4591 mch_memmove(pat
+ i
, pat
+ i
+ 1, STRLEN(pat
+ i
));
4593 flags
|= EW_FILE
| EW_EXEC
;
4595 /* For an absolute name we don't use $PATH. */
4596 if (mch_isFullName(pat
))
4597 path
= (char_u
*)" ";
4598 else if ((pat
[0] == '.' && (vim_ispathsep(pat
[1])
4599 || (pat
[1] == '.' && vim_ispathsep(pat
[2])))))
4600 path
= (char_u
*)".";
4602 path
= vim_getenv((char_u
*)"PATH", &mustfree
);
4605 * Go over all directories in $PATH. Expand matches in that directory and
4606 * collect them in "ga".
4608 ga_init2(&ga
, (int)sizeof(char *), 10);
4609 for (s
= path
; *s
!= NUL
; s
= e
)
4612 ++s
; /* Skip space used for absolute path name. */
4614 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4615 e
= vim_strchr(s
, ';');
4617 e
= vim_strchr(s
, ':');
4623 if (l
> MAXPATHL
- 5)
4625 vim_strncpy(buf
, s
, l
);
4628 vim_strncpy(buf
+ l
, pat
, MAXPATHL
- 1 - l
);
4630 /* Expand matches in one directory of $PATH. */
4631 ret
= expand_wildcards(1, &buf
, num_file
, file
, flags
);
4634 if (ga_grow(&ga
, *num_file
) == FAIL
)
4635 FreeWild(*num_file
, *file
);
4638 for (i
= 0; i
< *num_file
; ++i
)
4643 /* Remove the path again. */
4644 mch_memmove(s
, s
+ l
, STRLEN(s
+ l
) + 1);
4645 ((char_u
**)ga
.ga_data
)[ga
.ga_len
++] = s
;
4657 *num_file
= ga
.ga_len
;
4667 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4668 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
));
4671 * Call "user_expand_func()" to invoke a user defined VimL function and return
4672 * the result (either a string or a List).
4675 call_user_expand_func(user_expand_func
, xp
, num_file
, file
)
4676 void *(*user_expand_func
) __ARGS((char_u
*, int, char_u
**, int));
4684 int save_current_SID
= current_SID
;
4686 struct cmdline_info save_ccline
;
4688 if (xp
->xp_arg
== NULL
|| xp
->xp_arg
[0] == '\0')
4693 if (ccline
.cmdbuff
== NULL
)
4695 /* Completion from Insert mode, pass fake arguments. */
4697 sprintf((char *)num
, "%d", (int)STRLEN(xp
->xp_pattern
));
4698 args
[1] = xp
->xp_pattern
;
4702 /* Completion on the command line, pass real arguments. */
4703 keep
= ccline
.cmdbuff
[ccline
.cmdlen
];
4704 ccline
.cmdbuff
[ccline
.cmdlen
] = 0;
4705 sprintf((char *)num
, "%d", ccline
.cmdpos
);
4706 args
[1] = ccline
.cmdbuff
;
4708 args
[0] = xp
->xp_pattern
;
4711 /* Save the cmdline, we don't know what the function may do. */
4712 save_ccline
= ccline
;
4713 ccline
.cmdbuff
= NULL
;
4714 ccline
.cmdprompt
= NULL
;
4715 current_SID
= xp
->xp_scriptID
;
4717 ret
= user_expand_func(xp
->xp_arg
, 3, args
, FALSE
);
4719 ccline
= save_ccline
;
4720 current_SID
= save_current_SID
;
4721 if (ccline
.cmdbuff
!= NULL
)
4722 ccline
.cmdbuff
[ccline
.cmdlen
] = keep
;
4728 * Expand names with a function defined by the user.
4731 ExpandUserDefined(xp
, regmatch
, num_file
, file
)
4733 regmatch_T
*regmatch
;
4743 retstr
= call_user_expand_func(call_func_retstr
, xp
, num_file
, file
);
4747 ga_init2(&ga
, (int)sizeof(char *), 3);
4748 for (s
= retstr
; *s
!= NUL
; s
= e
)
4750 e
= vim_strchr(s
, '\n');
4756 if (xp
->xp_pattern
[0] && vim_regexec(regmatch
, s
, (colnr_T
)0) == 0)
4764 if (ga_grow(&ga
, 1) == FAIL
)
4767 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] = vim_strnsave(s
, (int)(e
- s
));
4776 *num_file
= ga
.ga_len
;
4781 * Expand names with a list returned by a function defined by the user.
4784 ExpandUserList(xp
, num_file
, file
)
4793 retlist
= call_user_expand_func(call_func_retlist
, xp
, num_file
, file
);
4794 if (retlist
== NULL
)
4797 ga_init2(&ga
, (int)sizeof(char *), 3);
4798 /* Loop over the items in the list. */
4799 for (li
= retlist
->lv_first
; li
!= NULL
; li
= li
->li_next
)
4801 if (li
->li_tv
.v_type
!= VAR_STRING
)
4802 continue; /* Skip non-string items */
4804 if (ga_grow(&ga
, 1) == FAIL
)
4807 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] =
4808 vim_strsave(li
->li_tv
.vval
.v_string
);
4811 list_unref(retlist
);
4814 *num_file
= ga
.ga_len
;
4820 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4821 * or compiler names.
4824 ExpandRTDir(pat
, num_file
, file
, dirname
)
4828 char *dirname
; /* "colors" or "compiler" */
4837 s
= alloc((unsigned)(STRLEN(pat
) + STRLEN(dirname
) + 7));
4840 sprintf((char *)s
, "%s/%s*.vim", dirname
, pat
);
4841 all
= globpath(p_rtp
, s
);
4846 ga_init2(&ga
, (int)sizeof(char *), 3);
4847 for (s
= all
; *s
!= NUL
; s
= e
)
4849 e
= vim_strchr(s
, '\n');
4852 if (ga_grow(&ga
, 1) == FAIL
)
4854 if (e
- 4 > s
&& STRNICMP(e
- 4, ".vim", 4) == 0)
4856 for (s
= e
- 4; s
> all
; mb_ptr_back(all
, s
))
4857 if (*s
== '\n' || vim_ispathsep(*s
))
4860 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] =
4861 vim_strnsave(s
, (int)(e
- s
- 4));
4869 *num_file
= ga
.ga_len
;
4875 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4877 * Expand "file" for all comma-separated directories in "path".
4878 * Returns an allocated string with all matches concatenated, separated by
4879 * newlines. Returns NULL for an error or no matches.
4882 globpath(path
, file
)
4895 buf
= alloc(MAXPATHL
);
4900 xpc
.xp_context
= EXPAND_FILES
;
4902 ga_init2(&ga
, 1, 100);
4904 /* Loop over all entries in {path}. */
4905 while (*path
!= NUL
)
4907 /* Copy one item of the path to buf[] and concatenate the file name. */
4908 copy_option_part(&path
, buf
, MAXPATHL
, ",");
4909 if (STRLEN(buf
) + STRLEN(file
) + 2 < MAXPATHL
)
4913 if (ExpandFromContext(&xpc
, buf
, &num_p
, &p
, WILD_SILENT
) != FAIL
4916 ExpandEscape(&xpc
, buf
, num_p
, p
, WILD_SILENT
);
4917 for (len
= 0, i
= 0; i
< num_p
; ++i
)
4918 len
+= (int)STRLEN(p
[i
]) + 1;
4920 /* Concatenate new results to previous ones. */
4921 if (ga_grow(&ga
, len
) == OK
)
4923 cur
= (char_u
*)ga
.ga_data
+ ga
.ga_len
;
4924 for (i
= 0; i
< num_p
; ++i
)
4927 cur
+= STRLEN(p
[i
]);
4937 *--cur
= 0; /* Replace trailing newline with NUL */
4940 return (char_u
*)ga
.ga_data
;
4945 #if defined(FEAT_CMDHIST) || defined(PROTO)
4947 /*********************************
4948 * Command line history stuff *
4949 *********************************/
4952 * Translate a history character to the associated type number.
4966 return HIST_SEARCH
; /* must be '?' or '/' */
4970 * Table of history names.
4971 * These names are used in :history and various hist...() functions.
4972 * It is sufficient to give the significant prefix of a history name.
4975 static char *(history_names
[]) =
4986 * init_history() - Initialize the command line history.
4987 * Also used to re-allocate the history when the size changes.
4992 int newlen
; /* new length of history table */
4999 * If size of history table changed, reallocate it
5002 if (newlen
!= hislen
) /* history length changed */
5004 for (type
= 0; type
< HIST_COUNT
; ++type
) /* adjust the tables */
5008 temp
= (histentry_T
*)lalloc(
5009 (long_u
)(newlen
* sizeof(histentry_T
)), TRUE
);
5010 if (temp
== NULL
) /* out of memory! */
5012 if (type
== 0) /* first one: just keep the old length */
5017 /* Already changed one table, now we can only have zero
5018 * length for all tables. */
5026 if (newlen
== 0 || temp
!= NULL
)
5028 if (hisidx
[type
] < 0) /* there are no entries yet */
5030 for (i
= 0; i
< newlen
; ++i
)
5033 temp
[i
].hisstr
= NULL
;
5036 else if (newlen
> hislen
) /* array becomes bigger */
5038 for (i
= 0; i
<= hisidx
[type
]; ++i
)
5039 temp
[i
] = history
[type
][i
];
5041 for ( ; i
<= newlen
- (hislen
- hisidx
[type
]); ++i
)
5044 temp
[i
].hisstr
= NULL
;
5046 for ( ; j
< hislen
; ++i
, ++j
)
5047 temp
[i
] = history
[type
][j
];
5049 else /* array becomes smaller or 0 */
5052 for (i
= newlen
- 1; ; --i
)
5054 if (i
>= 0) /* copy newest entries */
5055 temp
[i
] = history
[type
][j
];
5056 else /* remove older entries */
5057 vim_free(history
[type
][j
].hisstr
);
5060 if (j
== hisidx
[type
])
5063 hisidx
[type
] = newlen
- 1;
5065 vim_free(history
[type
]);
5066 history
[type
] = temp
;
5074 * Check if command line 'str' is already in history.
5075 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5078 in_history(type
, str
, move_to_front
)
5081 int move_to_front
; /* Move the entry to the front if it exists */
5086 if (hisidx
[type
] < 0)
5091 if (history
[type
][i
].hisstr
== NULL
)
5093 if (STRCMP(str
, history
[type
][i
].hisstr
) == 0)
5102 } while (i
!= hisidx
[type
]);
5106 str
= history
[type
][i
].hisstr
;
5107 while (i
!= hisidx
[type
])
5111 history
[type
][last_i
] = history
[type
][i
];
5114 history
[type
][i
].hisstr
= str
;
5115 history
[type
][i
].hisnum
= ++hisnum
[type
];
5122 * Convert history name (from table above) to its HIST_ equivalent.
5123 * When "name" is empty, return "cmd" history.
5124 * Returns -1 for unknown history name.
5131 int len
= (int)STRLEN(name
);
5133 /* No argument: use current history. */
5135 return hist_char2type(ccline
.cmdfirstc
);
5137 for (i
= 0; history_names
[i
] != NULL
; ++i
)
5138 if (STRNICMP(name
, history_names
[i
], len
) == 0)
5141 if (vim_strchr((char_u
*)":=@>?/", name
[0]) != NULL
&& name
[1] == NUL
)
5142 return hist_char2type(name
[0]);
5147 static int last_maptick
= -1; /* last seen maptick */
5150 * Add the given string to the given history. If the string is already in the
5151 * history then it is moved to the front. "histype" may be one of he HIST_
5155 add_to_history(histype
, new_entry
, in_map
, sep
)
5158 int in_map
; /* consider maptick when inside a mapping */
5159 int sep
; /* separator character used (search hist) */
5161 histentry_T
*hisptr
;
5164 if (hislen
== 0) /* no history */
5168 * Searches inside the same mapping overwrite each other, so that only
5169 * the last line is kept. Be careful not to remove a line that was moved
5170 * down, only lines that were added.
5172 if (histype
== HIST_SEARCH
&& in_map
)
5174 if (maptick
== last_maptick
)
5176 /* Current line is from the same mapping, remove it */
5177 hisptr
= &history
[HIST_SEARCH
][hisidx
[HIST_SEARCH
]];
5178 vim_free(hisptr
->hisstr
);
5179 hisptr
->hisstr
= NULL
;
5182 if (--hisidx
[HIST_SEARCH
] < 0)
5183 hisidx
[HIST_SEARCH
] = hislen
- 1;
5187 if (!in_history(histype
, new_entry
, TRUE
))
5189 if (++hisidx
[histype
] == hislen
)
5190 hisidx
[histype
] = 0;
5191 hisptr
= &history
[histype
][hisidx
[histype
]];
5192 vim_free(hisptr
->hisstr
);
5194 /* Store the separator after the NUL of the string. */
5195 len
= (int)STRLEN(new_entry
);
5196 hisptr
->hisstr
= vim_strnsave(new_entry
, len
+ 2);
5197 if (hisptr
->hisstr
!= NULL
)
5198 hisptr
->hisstr
[len
+ 1] = sep
;
5200 hisptr
->hisnum
= ++hisnum
[histype
];
5201 if (histype
== HIST_SEARCH
&& in_map
)
5202 last_maptick
= maptick
;
5206 #if defined(FEAT_EVAL) || defined(PROTO)
5209 * Get identifier of newest history entry.
5210 * "histype" may be one of the HIST_ values.
5213 get_history_idx(histype
)
5216 if (hislen
== 0 || histype
< 0 || histype
>= HIST_COUNT
5217 || hisidx
[histype
] < 0)
5220 return history
[histype
][hisidx
[histype
]].hisnum
;
5223 static struct cmdline_info
*get_ccline_ptr
__ARGS((void));
5226 * Get pointer to the command line info to use. cmdline_paste() may clear
5227 * ccline and put the previous value in prev_ccline.
5229 static struct cmdline_info
*
5232 if ((State
& CMDLINE
) == 0)
5234 if (ccline
.cmdbuff
!= NULL
)
5236 if (prev_ccline_used
&& prev_ccline
.cmdbuff
!= NULL
)
5237 return &prev_ccline
;
5242 * Get the current command line in allocated memory.
5243 * Only works when the command line is being edited.
5244 * Returns NULL when something is wrong.
5249 struct cmdline_info
*p
= get_ccline_ptr();
5253 return vim_strnsave(p
->cmdbuff
, p
->cmdlen
);
5257 * Get the current command line position, counted in bytes.
5258 * Zero is the first position.
5259 * Only works when the command line is being edited.
5260 * Returns -1 when something is wrong.
5265 struct cmdline_info
*p
= get_ccline_ptr();
5273 * Set the command line byte position to "pos". Zero is the first position.
5274 * Only works when the command line is being edited.
5275 * Returns 1 when failed, 0 when OK.
5278 set_cmdline_pos(pos
)
5281 struct cmdline_info
*p
= get_ccline_ptr();
5286 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5287 * changed the command line. */
5296 * Get the current command-line type.
5297 * Returns ':' or '/' or '?' or '@' or '>' or '-'
5298 * Only works when the command line is being edited.
5299 * Returns NUL when something is wrong.
5304 struct cmdline_info
*p
= get_ccline_ptr();
5308 if (p
->cmdfirstc
== NUL
)
5309 return (p
->input_fn
) ? '@' : '-';
5310 return p
->cmdfirstc
;
5314 * Calculate history index from a number:
5315 * num > 0: seen as identifying number of a history entry
5316 * num < 0: relative position in history wrt newest entry
5317 * "histype" may be one of the HIST_ values.
5320 calc_hist_idx(histype
, num
)
5326 int wrapped
= FALSE
;
5328 if (hislen
== 0 || histype
< 0 || histype
>= HIST_COUNT
5329 || (i
= hisidx
[histype
]) < 0 || num
== 0)
5332 hist
= history
[histype
];
5335 while (hist
[i
].hisnum
> num
)
5343 if (hist
[i
].hisnum
== num
&& hist
[i
].hisstr
!= NULL
)
5346 else if (-num
<= hislen
)
5351 if (hist
[i
].hisstr
!= NULL
)
5358 * Get a history entry by its index.
5359 * "histype" may be one of the HIST_ values.
5362 get_history_entry(histype
, idx
)
5366 idx
= calc_hist_idx(histype
, idx
);
5368 return history
[histype
][idx
].hisstr
;
5370 return (char_u
*)"";
5374 * Clear all entries of a history.
5375 * "histype" may be one of the HIST_ values.
5378 clr_history(histype
)
5382 histentry_T
*hisptr
;
5384 if (hislen
!= 0 && histype
>= 0 && histype
< HIST_COUNT
)
5386 hisptr
= history
[histype
];
5387 for (i
= hislen
; i
--;)
5389 vim_free(hisptr
->hisstr
);
5391 hisptr
++->hisstr
= NULL
;
5393 hisidx
[histype
] = -1; /* mark history as cleared */
5394 hisnum
[histype
] = 0; /* reset identifier counter */
5401 * Remove all entries matching {str} from a history.
5402 * "histype" may be one of the HIST_ values.
5405 del_history_entry(histype
, str
)
5409 regmatch_T regmatch
;
5410 histentry_T
*hisptr
;
5416 regmatch
.regprog
= NULL
;
5417 regmatch
.rm_ic
= FALSE
; /* always match case */
5420 && histype
< HIST_COUNT
5422 && (idx
= hisidx
[histype
]) >= 0
5423 && (regmatch
.regprog
= vim_regcomp(str
, RE_MAGIC
+ RE_STRING
))
5429 hisptr
= &history
[histype
][i
];
5430 if (hisptr
->hisstr
== NULL
)
5432 if (vim_regexec(®match
, hisptr
->hisstr
, (colnr_T
)0))
5435 vim_free(hisptr
->hisstr
);
5436 hisptr
->hisstr
= NULL
;
5443 history
[histype
][last
] = *hisptr
;
5444 hisptr
->hisstr
= NULL
;
5453 if (history
[histype
][idx
].hisstr
== NULL
)
5454 hisidx
[histype
] = -1;
5456 vim_free(regmatch
.regprog
);
5461 * Remove an indexed entry from a history.
5462 * "histype" may be one of the HIST_ values.
5465 del_history_idx(histype
, idx
)
5471 i
= calc_hist_idx(histype
, idx
);
5474 idx
= hisidx
[histype
];
5475 vim_free(history
[histype
][i
].hisstr
);
5477 /* When deleting the last added search string in a mapping, reset
5478 * last_maptick, so that the last added search string isn't deleted again.
5480 if (histype
== HIST_SEARCH
&& maptick
== last_maptick
&& i
== idx
)
5485 j
= (i
+ 1) % hislen
;
5486 history
[histype
][i
] = history
[histype
][j
];
5489 history
[histype
][i
].hisstr
= NULL
;
5490 history
[histype
][i
].hisnum
= 0;
5493 hisidx
[histype
] = i
;
5497 #endif /* FEAT_EVAL */
5499 #if defined(FEAT_CRYPT) || defined(PROTO)
5501 * Very specific function to remove the value in ":set key=val" from the
5505 remove_key_from_history()
5510 i
= hisidx
[HIST_CMD
];
5513 p
= history
[HIST_CMD
][i
].hisstr
;
5516 if (STRNCMP(p
, "key", 3) == 0 && !isalpha(p
[3]))
5518 p
= vim_strchr(p
+ 3, '=');
5522 for (i
= 0; p
[i
] && !vim_iswhite(p
[i
]); ++i
)
5523 if (p
[i
] == '\\' && p
[i
+ 1])
5525 mch_memmove(p
, p
+ i
, STRLEN(p
+ i
) + 1);
5531 #endif /* FEAT_CMDHIST */
5533 #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5535 * Get indices "num1,num2" that specify a range within a list (not a range of
5536 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5537 * Returns OK if parsed successfully, otherwise FAIL.
5540 get_list_range(str
, num1
, num2
)
5549 *str
= skipwhite(*str
);
5550 if (**str
== '-' || vim_isdigit(**str
)) /* parse "from" part of range */
5552 vim_str2nr(*str
, NULL
, &len
, FALSE
, FALSE
, &num
, NULL
);
5557 *str
= skipwhite(*str
);
5558 if (**str
== ',') /* parse "to" part of range */
5560 *str
= skipwhite(*str
+ 1);
5561 vim_str2nr(*str
, NULL
, &len
, FALSE
, FALSE
, &num
, NULL
);
5565 *str
= skipwhite(*str
+ len
);
5567 else if (!first
) /* no number given at all */
5570 else if (first
) /* only one number given */
5576 #if defined(FEAT_CMDHIST) || defined(PROTO)
5578 * :history command - print a history
5585 int histype1
= HIST_CMD
;
5586 int histype2
= HIST_CMD
;
5592 char_u
*arg
= eap
->arg
;
5596 MSG(_("'history' option is zero"));
5600 if (!(VIM_ISDIGIT(*arg
) || *arg
== '-' || *arg
== ','))
5603 while (ASCII_ISALPHA(*end
)
5604 || vim_strchr((char_u
*)":=@>/?", *end
) != NULL
)
5608 histype1
= get_histtype(arg
);
5611 if (STRICMP(arg
, "all") == 0)
5614 histype2
= HIST_COUNT
-1;
5619 EMSG(_(e_trailing
));
5624 histype2
= histype1
;
5629 if (!get_list_range(&end
, &hisidx1
, &hisidx2
) || *end
!= NUL
)
5631 EMSG(_(e_trailing
));
5635 for (; !got_int
&& histype1
<= histype2
; ++histype1
)
5637 STRCPY(IObuff
, "\n # ");
5638 STRCAT(STRCAT(IObuff
, history_names
[histype1
]), " history");
5639 MSG_PUTS_TITLE(IObuff
);
5640 idx
= hisidx
[histype1
];
5641 hist
= history
[histype1
];
5645 j
= (-j
> hislen
) ? 0 : hist
[(hislen
+j
+idx
+1) % hislen
].hisnum
;
5647 k
= (-k
> hislen
) ? 0 : hist
[(hislen
+k
+idx
+1) % hislen
].hisnum
;
5648 if (idx
>= 0 && j
<= k
)
5649 for (i
= idx
+ 1; !got_int
; ++i
)
5653 if (hist
[i
].hisstr
!= NULL
5654 && hist
[i
].hisnum
>= j
&& hist
[i
].hisnum
<= k
)
5657 sprintf((char *)IObuff
, "%c%6d ", i
== idx
? '>' : ' ',
5659 if (vim_strsize(hist
[i
].hisstr
) > (int)Columns
- 10)
5660 trunc_string(hist
[i
].hisstr
, IObuff
+ STRLEN(IObuff
),
5663 STRCAT(IObuff
, hist
[i
].hisstr
);
5664 msg_outtrans(IObuff
);
5674 #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5675 static char_u
**viminfo_history
[HIST_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
5676 static int viminfo_hisidx
[HIST_COUNT
] = {0, 0, 0, 0};
5677 static int viminfo_hislen
[HIST_COUNT
] = {0, 0, 0, 0};
5678 static int viminfo_add_at_front
= FALSE
;
5680 static int hist_type2char
__ARGS((int type
, int use_question
));
5683 * Translate a history type number to the associated character.
5686 hist_type2char(type
, use_question
)
5688 int use_question
; /* use '?' instead of '/' */
5690 if (type
== HIST_CMD
)
5692 if (type
== HIST_SEARCH
)
5699 if (type
== HIST_EXPR
)
5705 * Prepare for reading the history from the viminfo file.
5706 * This allocates history arrays to store the read history lines.
5709 prepare_viminfo_history(asklen
)
5718 viminfo_add_at_front
= (asklen
!= 0);
5719 if (asklen
> hislen
)
5722 for (type
= 0; type
< HIST_COUNT
; ++type
)
5725 * Count the number of empty spaces in the history list. If there are
5726 * more spaces available than we request, then fill them up.
5728 for (i
= 0, num
= 0; i
< hislen
; i
++)
5729 if (history
[type
][i
].hisstr
== NULL
)
5735 viminfo_history
[type
] = NULL
;
5737 viminfo_history
[type
] =
5738 (char_u
**)lalloc((long_u
)(len
* sizeof(char_u
*)), FALSE
);
5739 if (viminfo_history
[type
] == NULL
)
5741 viminfo_hislen
[type
] = len
;
5742 viminfo_hisidx
[type
] = 0;
5747 * Accept a line from the viminfo, store it in the history array when it's
5751 read_viminfo_history(virp
)
5759 type
= hist_char2type(virp
->vir_line
[0]);
5760 if (viminfo_hisidx
[type
] < viminfo_hislen
[type
])
5762 val
= viminfo_readstring(virp
, 1, TRUE
);
5763 if (val
!= NULL
&& *val
!= NUL
)
5765 if (!in_history(type
, val
+ (type
== HIST_SEARCH
),
5766 viminfo_add_at_front
))
5768 /* Need to re-allocate to append the separator byte. */
5770 p
= lalloc(len
+ 2, TRUE
);
5773 if (type
== HIST_SEARCH
)
5775 /* Search entry: Move the separator from the first
5776 * column to after the NUL. */
5777 mch_memmove(p
, val
+ 1, (size_t)len
);
5778 p
[len
] = (*val
== ' ' ? NUL
: *val
);
5782 /* Not a search entry: No separator in the viminfo
5783 * file, add a NUL separator. */
5784 mch_memmove(p
, val
, (size_t)len
+ 1);
5787 viminfo_history
[type
][viminfo_hisidx
[type
]++] = p
;
5793 return viminfo_readline(virp
);
5797 finish_viminfo_history()
5803 for (type
= 0; type
< HIST_COUNT
; ++type
)
5805 if (history
[type
] == NULL
)
5807 idx
= hisidx
[type
] + viminfo_hisidx
[type
];
5812 if (viminfo_add_at_front
)
5816 if (hisidx
[type
] == -1)
5817 hisidx
[type
] = hislen
- 1;
5820 if (history
[type
][idx
].hisstr
!= NULL
)
5822 if (++idx
== hislen
)
5824 } while (idx
!= hisidx
[type
]);
5825 if (idx
!= hisidx
[type
] && --idx
< 0)
5828 for (i
= 0; i
< viminfo_hisidx
[type
]; i
++)
5830 vim_free(history
[type
][idx
].hisstr
);
5831 history
[type
][idx
].hisstr
= viminfo_history
[type
][i
];
5837 for (i
= 0; i
< viminfo_hisidx
[type
]; i
++)
5839 history
[type
][idx
++].hisnum
= ++hisnum
[type
];
5842 vim_free(viminfo_history
[type
]);
5843 viminfo_history
[type
] = NULL
;
5848 write_viminfo_history(fp
)
5860 for (type
= 0; type
< HIST_COUNT
; ++type
)
5862 num_saved
= get_viminfo_parameter(hist_type2char(type
, FALSE
));
5865 if (num_saved
< 0) /* Use default */
5867 fprintf(fp
, _("\n# %s History (newest to oldest):\n"),
5868 type
== HIST_CMD
? _("Command Line") :
5869 type
== HIST_SEARCH
? _("Search String") :
5870 type
== HIST_EXPR
? _("Expression") :
5872 if (num_saved
> hislen
)
5878 p
= history
[type
][i
].hisstr
;
5881 fputc(hist_type2char(type
, TRUE
), fp
);
5882 /* For the search history: put the separator in the second
5883 * column; use a space if there isn't one. */
5884 if (type
== HIST_SEARCH
)
5886 c
= p
[STRLEN(p
) + 1];
5887 putc(c
== NUL
? ' ' : c
, fp
);
5889 viminfo_writestring(fp
, p
);
5896 #endif /* FEAT_VIMINFO */
5898 #if defined(FEAT_FKMAP) || defined(PROTO)
5900 * Write a character at the current cursor+offset position.
5901 * It is directly written into the command buffer block.
5904 cmd_pchar(c
, offset
)
5907 if (ccline
.cmdpos
+ offset
>= ccline
.cmdlen
|| ccline
.cmdpos
+ offset
< 0)
5909 EMSG(_("E198: cmd_pchar beyond the command length"));
5912 ccline
.cmdbuff
[ccline
.cmdpos
+ offset
] = (char_u
)c
;
5913 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
5920 if (ccline
.cmdpos
+ offset
>= ccline
.cmdlen
|| ccline
.cmdpos
+ offset
< 0)
5922 /* EMSG(_("cmd_gchar beyond the command length")); */
5925 return (int)ccline
.cmdbuff
[ccline
.cmdpos
+ offset
];
5929 #if defined(FEAT_CMDWIN) || defined(PROTO)
5931 * Open a window on the current command line and history. Allow editing in
5932 * the window. Returns when the window is closed.
5934 * CR if the command is to be executed
5935 * Ctrl_C if it is to be abandoned
5936 * K_IGNORE if editing continues
5941 struct cmdline_info save_ccline
;
5942 buf_T
*old_curbuf
= curbuf
;
5943 win_T
*old_curwin
= curwin
;
5951 int save_restart_edit
= restart_edit
;
5952 int save_State
= State
;
5953 int save_exmode
= exmode_active
;
5954 #ifdef FEAT_RIGHTLEFT
5955 int save_cmdmsg_rl
= cmdmsg_rl
;
5958 /* Can't do this recursively. Can't do it when typing a password. */
5959 if (cmdwin_type
!= 0
5960 # if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5969 /* Save current window sizes. */
5970 win_size_save(&winsizes
);
5972 # ifdef FEAT_AUTOCMD
5973 /* Don't execute autocommands while creating the window. */
5976 /* don't use a new tab page */
5979 /* Create a window for the command-line buffer. */
5980 if (win_split((int)p_cwh
, WSP_BOT
) == FAIL
)
5983 # ifdef FEAT_AUTOCMD
5988 cmdwin_type
= ccline
.cmdfirstc
;
5989 if (cmdwin_type
== NUL
)
5992 /* Create the command-line buffer empty. */
5993 (void)do_ecmd(0, NULL
, NULL
, NULL
, ECMD_ONE
, ECMD_HIDE
);
5994 (void)setfname(curbuf
, (char_u
*)"command-line", NULL
, TRUE
);
5995 set_option_value((char_u
*)"bt", 0L, (char_u
*)"nofile", OPT_LOCAL
);
5996 set_option_value((char_u
*)"swf", 0L, NULL
, OPT_LOCAL
);
5997 curbuf
->b_p_ma
= TRUE
;
5998 # ifdef FEAT_RIGHTLEFT
5999 curwin
->w_p_rl
= cmdmsg_rl
;
6002 # ifdef FEAT_SCROLLBIND
6003 curwin
->w_p_scb
= FALSE
;
6006 # ifdef FEAT_AUTOCMD
6007 /* Do execute autocommands for setting the filetype (load syntax). */
6011 /* Showing the prompt may have set need_wait_return, reset it. */
6012 need_wait_return
= FALSE
;
6014 histtype
= hist_char2type(ccline
.cmdfirstc
);
6015 if (histtype
== HIST_CMD
|| histtype
== HIST_DEBUG
)
6019 add_map((char_u
*)"<buffer> <Tab> <C-X><C-V>", INSERT
);
6020 add_map((char_u
*)"<buffer> <Tab> a<C-X><C-V>", NORMAL
);
6022 set_option_value((char_u
*)"ft", 0L, (char_u
*)"vim", OPT_LOCAL
);
6025 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6026 * sets 'textwidth' to 78). */
6029 /* Fill the buffer with the history. */
6033 i
= hisidx
[histtype
];
6041 if (history
[histtype
][i
].hisstr
!= NULL
)
6042 ml_append(lnum
++, history
[histtype
][i
].hisstr
,
6045 while (i
!= hisidx
[histtype
]);
6049 /* Replace the empty last line with the current command-line and put the
6051 ml_replace(curbuf
->b_ml
.ml_line_count
, ccline
.cmdbuff
, TRUE
);
6052 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6053 curwin
->w_cursor
.col
= ccline
.cmdpos
;
6054 changed_line_abv_curs();
6055 invalidate_botline();
6056 redraw_later(SOME_VALID
);
6058 /* Save the command line info, can be used recursively. */
6059 save_ccline
= ccline
;
6060 ccline
.cmdbuff
= NULL
;
6061 ccline
.cmdprompt
= NULL
;
6063 /* No Ex mode here! */
6071 # ifdef FEAT_AUTOCMD
6072 /* Trigger CmdwinEnter autocommands. */
6073 typestr
[0] = cmdwin_type
;
6075 apply_autocmds(EVENT_CMDWINENTER
, typestr
, typestr
, FALSE
, curbuf
);
6076 if (restart_edit
!= 0) /* autocmd with ":startinsert" */
6077 stuffcharReadbuff(K_NOP
);
6080 i
= RedrawingDisabled
;
6081 RedrawingDisabled
= 0;
6084 * Call the main loop until <CR> or CTRL-C is typed.
6087 main_loop(TRUE
, FALSE
);
6089 RedrawingDisabled
= i
;
6091 # ifdef FEAT_AUTOCMD
6092 /* Trigger CmdwinLeave autocommands. */
6093 apply_autocmds(EVENT_CMDWINLEAVE
, typestr
, typestr
, FALSE
, curbuf
);
6096 /* Restore the command line info. */
6097 ccline
= save_ccline
;
6100 exmode_active
= save_exmode
;
6102 /* Safety check: The old window or buffer was deleted: It's a a bug when
6104 if (!win_valid(old_curwin
) || !buf_valid(old_curbuf
))
6106 cmdwin_result
= Ctrl_C
;
6107 EMSG(_("E199: Active window or buffer deleted"));
6111 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6112 /* autocmds may abort script processing */
6113 if (aborting() && cmdwin_result
!= K_IGNORE
)
6114 cmdwin_result
= Ctrl_C
;
6116 /* Set the new command line from the cmdline buffer. */
6117 vim_free(ccline
.cmdbuff
);
6118 if (cmdwin_result
== K_XF1
|| cmdwin_result
== K_XF2
) /* :qa[!] typed */
6120 char *p
= (cmdwin_result
== K_XF2
) ? "qa" : "qa!";
6122 if (histtype
== HIST_CMD
)
6124 /* Execute the command directly. */
6125 ccline
.cmdbuff
= vim_strsave((char_u
*)p
);
6126 cmdwin_result
= CAR
;
6130 /* First need to cancel what we were doing. */
6131 ccline
.cmdbuff
= NULL
;
6132 stuffcharReadbuff(':');
6133 stuffReadbuff((char_u
*)p
);
6134 stuffcharReadbuff(CAR
);
6137 else if (cmdwin_result
== K_XF2
) /* :qa typed */
6139 ccline
.cmdbuff
= vim_strsave((char_u
*)"qa");
6140 cmdwin_result
= CAR
;
6143 ccline
.cmdbuff
= vim_strsave(ml_get_curline());
6144 if (ccline
.cmdbuff
== NULL
)
6145 cmdwin_result
= Ctrl_C
;
6148 ccline
.cmdlen
= (int)STRLEN(ccline
.cmdbuff
);
6149 ccline
.cmdbufflen
= ccline
.cmdlen
+ 1;
6150 ccline
.cmdpos
= curwin
->w_cursor
.col
;
6151 if (ccline
.cmdpos
> ccline
.cmdlen
)
6152 ccline
.cmdpos
= ccline
.cmdlen
;
6153 if (cmdwin_result
== K_IGNORE
)
6155 set_cmdspos_cursor();
6160 # ifdef FEAT_AUTOCMD
6161 /* Don't execute autocommands while deleting the window. */
6166 win_goto(old_curwin
);
6167 win_close(wp
, TRUE
);
6168 close_buffer(NULL
, bp
, DOBUF_WIPE
);
6170 /* Restore window sizes. */
6171 win_size_restore(&winsizes
);
6173 # ifdef FEAT_AUTOCMD
6178 ga_clear(&winsizes
);
6179 restart_edit
= save_restart_edit
;
6180 # ifdef FEAT_RIGHTLEFT
6181 cmdmsg_rl
= save_cmdmsg_rl
;
6189 return cmdwin_result
;
6191 #endif /* FEAT_CMDWIN */
6194 * Used for commands that either take a simple command string argument, or:
6198 * Returns a pointer to allocated memory with {script} or NULL.
6201 script_get(eap
, cmd
)
6206 char *end_pattern
= NULL
;
6210 if (cmd
[0] != '<' || cmd
[1] != '<' || eap
->getline
== NULL
)
6213 ga_init2(&ga
, 1, 0x400);
6216 end_pattern
= (char *)skipwhite(cmd
+ 2);
6222 theline
= eap
->getline(
6224 eap
->cstack
->cs_looplevel
> 0 ? -1 :
6226 NUL
, eap
->cookie
, 0);
6228 if (theline
== NULL
|| STRCMP(end_pattern
, theline
) == 0)
6231 ga_concat(&ga
, theline
);
6232 ga_append(&ga
, '\n');
6235 ga_append(&ga
, NUL
);
6237 return (char_u
*)ga
.ga_data
;