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 */
341 some_key_typed
= TRUE
;
342 #ifdef FEAT_RIGHTLEFT
349 if (cmdmsg_rl
&& !KeyStuffed
)
351 /* Invert horizontal movements and operations. Only when
352 * typed by the user directly, not when the result of a
356 case K_RIGHT
: c
= K_LEFT
; break;
357 case K_S_RIGHT
: c
= K_S_LEFT
; break;
358 case K_C_RIGHT
: c
= K_C_LEFT
; break;
359 case K_LEFT
: c
= K_RIGHT
; break;
360 case K_S_LEFT
: c
= K_S_RIGHT
; break;
361 case K_C_LEFT
: c
= K_C_RIGHT
; break;
368 * Ignore got_int when CTRL-C was typed here.
369 * Don't ignore it in :global, we really need to break then, e.g., for
370 * ":g/pat/normal /pat" (without the <CR>).
371 * Don't ignore it for the input() function.
378 #if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
388 /* free old command line when finished moving around in the history
391 && c
!= K_S_DOWN
&& c
!= K_S_UP
392 && c
!= K_DOWN
&& c
!= K_UP
393 && c
!= K_PAGEDOWN
&& c
!= K_PAGEUP
394 && c
!= K_KPAGEDOWN
&& c
!= K_KPAGEUP
395 && c
!= K_LEFT
&& c
!= K_RIGHT
396 && (xpc
.xp_numfiles
> 0 || (c
!= Ctrl_P
&& c
!= Ctrl_N
)))
404 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
406 if (c
!= p_wc
&& c
== K_S_TAB
&& xpc
.xp_numfiles
!= -1)
410 /* Special translations for 'wildmenu' */
411 if (did_wild_list
&& p_wmnu
)
415 else if (c
== K_RIGHT
)
418 /* Hitting CR after "emenu Name.": complete submenu */
419 if (xpc
.xp_context
== EXPAND_MENUNAMES
&& p_wmnu
421 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '.'
422 && ccline
.cmdbuff
[ccline
.cmdpos
- 2] != '\\'
423 && (c
== '\n' || c
== '\r' || c
== K_KENTER
))
427 /* free expanded names when finished walking through matches */
428 if (xpc
.xp_numfiles
!= -1
429 && !(c
== p_wc
&& KeyTyped
) && c
!= p_wcm
430 && c
!= Ctrl_N
&& c
!= Ctrl_P
&& c
!= Ctrl_A
433 (void)ExpandOne(&xpc
, NULL
, NULL
, 0, WILD_FREE
);
434 did_wild_list
= FALSE
;
436 if (!p_wmnu
|| (c
!= K_UP
&& c
!= K_DOWN
))
438 xpc
.xp_context
= EXPAND_NOTHING
;
441 if (p_wmnu
&& wild_menu_showing
!= 0)
444 int old_RedrawingDisabled
= RedrawingDisabled
;
447 RedrawingDisabled
= 0;
449 if (wild_menu_showing
== WM_SCROLLED
)
451 /* Entered command line, move it up */
455 else if (save_p_ls
!= -1)
457 /* restore 'laststatus' and 'winminheight' */
461 save_cmdline(&save_ccline
);
462 update_screen(VALID
); /* redraw the screen NOW */
463 restore_cmdline(&save_ccline
);
469 # ifdef FEAT_VERTSPLIT
470 win_redraw_last_status(topframe
);
472 lastwin
->w_redr_status
= TRUE
;
474 redraw_statuslines();
477 wild_menu_showing
= 0;
479 RedrawingDisabled
= old_RedrawingDisabled
;
485 /* Special translations for 'wildmenu' */
486 if (xpc
.xp_context
== EXPAND_MENUNAMES
&& p_wmnu
)
488 /* Hitting <Down> after "emenu Name.": complete submenu */
489 if (c
== K_DOWN
&& ccline
.cmdpos
> 0
490 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '.')
494 /* Hitting <Up>: Remove one submenu name in front of the
498 j
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
502 /* check for start of menu name */
503 if (ccline
.cmdbuff
[j
] == ' '
504 && ccline
.cmdbuff
[j
- 1] != '\\')
509 /* check for start of submenu name */
510 if (ccline
.cmdbuff
[j
] == '.'
511 && ccline
.cmdbuff
[j
- 1] != '\\')
525 xpc
.xp_context
= EXPAND_NOTHING
;
528 if ((xpc
.xp_context
== EXPAND_FILES
529 || xpc
.xp_context
== EXPAND_SHELLCMD
) && p_wmnu
)
541 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == PATHSEP
542 && (ccline
.cmdpos
< 3
543 || ccline
.cmdbuff
[ccline
.cmdpos
- 2] != '.'
544 || ccline
.cmdbuff
[ccline
.cmdpos
- 3] != '.'))
546 /* go down a directory */
549 else if (STRNCMP(xpc
.xp_pattern
, upseg
+ 1, 3) == 0 && c
== K_DOWN
)
551 /* If in a direct ancestor, strip off one ../ to go down */
555 i
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
560 j
-= (*mb_head_off
)(ccline
.cmdbuff
, ccline
.cmdbuff
+ j
);
562 if (vim_ispathsep(ccline
.cmdbuff
[j
]))
569 && ccline
.cmdbuff
[j
- 1] == '.'
570 && ccline
.cmdbuff
[j
- 2] == '.'
571 && (vim_ispathsep(ccline
.cmdbuff
[j
- 3]) || j
== i
+ 2))
579 /* go up a directory */
582 j
= ccline
.cmdpos
- 1;
583 i
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
588 j
-= (*mb_head_off
)(ccline
.cmdbuff
, ccline
.cmdbuff
+ j
);
590 if (vim_ispathsep(ccline
.cmdbuff
[j
])
591 #ifdef BACKSLASH_IN_FILENAME
592 && vim_strchr(" *?[{`$%#", ccline
.cmdbuff
[j
+ 1])
609 else if (STRNCMP(ccline
.cmdbuff
+ j
, upseg
, 4) == 0)
611 else if (STRNCMP(ccline
.cmdbuff
+ j
, upseg
+ 1, 3) == 0
618 /* TODO this is only for DOS/UNIX systems - need to put in
619 * machine-specific stuff here and in upseg init */
621 put_on_cmdline(upseg
+ 1, 3, FALSE
);
623 else if (ccline
.cmdpos
> i
)
628 #if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
630 && (xpc
.xp_context
== EXPAND_FILES
631 || xpc
.xp_context
== EXPAND_MENUNAMES
)
632 && (c
== K_UP
|| c
== K_DOWN
))
633 xpc
.xp_context
= EXPAND_NOTHING
;
636 #endif /* FEAT_WILDMENU */
638 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
639 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
647 /* CTRL-\ e doesn't work when obtaining an expression. */
648 if (c
!= Ctrl_N
&& c
!= Ctrl_G
649 && (c
!= 'e' || ccline
.cmdfirstc
== '='))
660 * Replace the command line with the result of an expression.
661 * Need to save and restore the current command line, to be
662 * able to enter a new one...
664 if (ccline
.cmdpos
== ccline
.cmdlen
)
665 new_cmdpos
= 99999; /* keep it at the end */
667 new_cmdpos
= ccline
.cmdpos
;
669 save_cmdline(&save_ccline
);
670 c
= get_expr_register();
671 restore_cmdline(&save_ccline
);
674 /* Need to save and restore ccline. And set "textlock"
675 * to avoid nasty things like going to another buffer when
676 * evaluating an expression. */
677 save_cmdline(&save_ccline
);
681 restore_cmdline(&save_ccline
);
683 if (p
!= NULL
&& realloc_cmdbuff((int)STRLEN(p
) + 1) == OK
)
685 ccline
.cmdlen
= (int)STRLEN(p
);
686 STRCPY(ccline
.cmdbuff
, p
);
689 /* Restore the cursor or use the position set with
690 * set_cmdline_pos(). */
691 if (new_cmdpos
> ccline
.cmdlen
)
692 ccline
.cmdpos
= ccline
.cmdlen
;
694 ccline
.cmdpos
= new_cmdpos
;
696 KeyTyped
= FALSE
; /* Don't do p_wc completion. */
698 goto cmdline_changed
;
707 if (c
== Ctrl_G
&& p_im
&& restart_edit
== 0)
709 gotesc
= TRUE
; /* will free ccline.cmdbuff after putting it
711 goto returncmd
; /* back to Normal mode */
716 if (c
== cedit_key
|| c
== K_CMDWIN
)
719 * Open a window to edit the command line (and history).
722 some_key_typed
= TRUE
;
724 # ifdef FEAT_DIGRAPHS
732 if (c
== '\n' || c
== '\r' || c
== K_KENTER
|| (c
== ESC
733 && (!KeyTyped
|| vim_strchr(p_cpo
, CPO_ESC
) != NULL
)))
735 /* In Ex mode a backslash escapes a newline. */
738 && ccline
.cmdpos
== ccline
.cmdlen
740 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '\\')
747 gotesc
= FALSE
; /* Might have typed ESC previously, don't
748 truncate the cmdline now. */
749 if (ccheck_abbr(c
+ ABBR_OFF
))
750 goto cmdline_changed
;
753 windgoto(msg_row
, 0);
761 * Completion for 'wildchar' or 'wildcharm' key.
762 * - hitting <ESC> twice means: abandon command line.
763 * - wildcard expansion is only done when the 'wildchar' key is really
764 * typed, not when it comes from a macro
766 if ((c
== p_wc
&& !gotesc
&& KeyTyped
) || c
== p_wcm
)
768 if (xpc
.xp_numfiles
> 0) /* typed p_wc at least twice */
770 /* if 'wildmode' contains "list" may still need to list */
771 if (xpc
.xp_numfiles
> 1
773 && (wim_flags
[wim_index
] & WIM_LIST
))
775 (void)showmatches(&xpc
, FALSE
);
777 did_wild_list
= TRUE
;
779 if (wim_flags
[wim_index
] & WIM_LONGEST
)
780 res
= nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
781 else if (wim_flags
[wim_index
] & WIM_FULL
)
782 res
= nextwild(&xpc
, WILD_NEXT
, WILD_NO_BEEP
);
784 res
= OK
; /* don't insert 'wildchar' now */
786 else /* typed p_wc first time */
790 /* if 'wildmode' first contains "longest", get longest
792 if (wim_flags
[0] & WIM_LONGEST
)
793 res
= nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
795 res
= nextwild(&xpc
, WILD_EXPAND_KEEP
, WILD_NO_BEEP
);
797 /* if interrupted while completing, behave like it failed */
800 (void)vpeekc(); /* remove <C-C> from input stream */
801 got_int
= FALSE
; /* don't abandon the command line */
802 (void)ExpandOne(&xpc
, NULL
, NULL
, 0, WILD_FREE
);
804 xpc
.xp_context
= EXPAND_NOTHING
;
806 goto cmdline_changed
;
809 /* when more than one match, and 'wildmode' first contains
810 * "list", or no change and 'wildmode' contains "longest,list",
811 * list all matches */
812 if (res
== OK
&& xpc
.xp_numfiles
> 1)
814 /* a "longest" that didn't do anything is skipped (but not
816 if (wim_flags
[0] == WIM_LONGEST
&& ccline
.cmdpos
== j
)
818 if ((wim_flags
[wim_index
] & WIM_LIST
)
820 || (p_wmnu
&& (wim_flags
[wim_index
] & WIM_FULL
) != 0)
824 if (!(wim_flags
[0] & WIM_LONGEST
))
827 int p_wmnu_save
= p_wmnu
;
830 nextwild(&xpc
, WILD_PREV
, 0); /* remove match */
832 p_wmnu
= p_wmnu_save
;
836 (void)showmatches(&xpc
, p_wmnu
837 && ((wim_flags
[wim_index
] & WIM_LIST
) == 0));
839 (void)showmatches(&xpc
, FALSE
);
842 did_wild_list
= TRUE
;
843 if (wim_flags
[wim_index
] & WIM_LONGEST
)
844 nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
845 else if (wim_flags
[wim_index
] & WIM_FULL
)
846 nextwild(&xpc
, WILD_NEXT
, WILD_NO_BEEP
);
852 else if (xpc
.xp_numfiles
== -1)
853 xpc
.xp_context
= EXPAND_NOTHING
;
861 goto cmdline_changed
;
866 /* <S-Tab> goes to last match, in a clumsy way */
867 if (c
== K_S_TAB
&& KeyTyped
)
869 if (nextwild(&xpc
, WILD_EXPAND_KEEP
, 0) == OK
870 && nextwild(&xpc
, WILD_PREV
, 0) == OK
871 && nextwild(&xpc
, WILD_PREV
, 0) == OK
)
872 goto cmdline_changed
;
875 if (c
== NUL
|| c
== K_ZERO
) /* NUL is stored as NL */
878 do_abbr
= TRUE
; /* default: check for abbreviation */
881 * Big switch for a typed command line character.
891 if (cmd_fkmap
&& c
== K_BS
)
898 * delete current character is the same as backspace on next
899 * character, except at end of line
901 if (c
== K_DEL
&& ccline
.cmdpos
!= ccline
.cmdlen
)
904 if (has_mbyte
&& c
== K_DEL
)
905 ccline
.cmdpos
+= mb_off_next(ccline
.cmdbuff
,
906 ccline
.cmdbuff
+ ccline
.cmdpos
);
908 if (ccline
.cmdpos
> 0)
913 p
= ccline
.cmdbuff
+ j
;
917 p
= mb_prevptr(ccline
.cmdbuff
, p
);
920 while (p
> ccline
.cmdbuff
&& vim_isspace(*p
))
921 p
= mb_prevptr(ccline
.cmdbuff
, p
);
923 while (p
> ccline
.cmdbuff
&& mb_get_class(p
) == i
)
924 p
= mb_prevptr(ccline
.cmdbuff
, p
);
925 if (mb_get_class(p
) != i
)
926 p
+= (*mb_ptr2len
)(p
);
933 while (p
> ccline
.cmdbuff
&& vim_isspace(p
[-1]))
935 i
= vim_iswordc(p
[-1]);
936 while (p
> ccline
.cmdbuff
&& !vim_isspace(p
[-1])
937 && vim_iswordc(p
[-1]) == i
)
942 ccline
.cmdpos
= (int)(p
- ccline
.cmdbuff
);
943 ccline
.cmdlen
-= j
- ccline
.cmdpos
;
945 while (i
< ccline
.cmdlen
)
946 ccline
.cmdbuff
[i
++] = ccline
.cmdbuff
[j
++];
948 /* Truncate at the end, required for multi-byte chars. */
949 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
952 else if (ccline
.cmdlen
== 0 && c
!= Ctrl_W
953 && ccline
.cmdprompt
== NULL
&& indent
== 0)
955 /* In ex and debug mode it doesn't make sense to return. */
958 || ccline
.cmdfirstc
== '>'
961 goto cmdline_not_changed
;
963 vim_free(ccline
.cmdbuff
); /* no commandline to return */
964 ccline
.cmdbuff
= NULL
;
967 #ifdef FEAT_RIGHTLEFT
973 msg_putchar(' '); /* delete ':' */
975 redraw_cmdline
= TRUE
;
976 goto returncmd
; /* back to cmd mode */
978 goto cmdline_changed
;
983 /* if Farsi mode set, we are in reverse insert mode -
984 Do not change the mode */
989 ccline
.overstrike
= !ccline
.overstrike
;
991 ui_cursor_shape(); /* may show different cursor shape */
993 goto cmdline_not_changed
;
996 if (map_to_exists_mode((char_u
*)"", LANGMAP
, FALSE
))
998 /* ":lmap" mappings exists, toggle use of mappings. */
1000 #ifdef USE_IM_CONTROL
1001 im_set_active(FALSE
); /* Disable input method */
1003 if (b_im_ptr
!= NULL
)
1005 if (State
& LANGMAP
)
1006 *b_im_ptr
= B_IMODE_LMAP
;
1008 *b_im_ptr
= B_IMODE_NONE
;
1011 #ifdef USE_IM_CONTROL
1014 /* There are no ":lmap" mappings, toggle IM. When
1015 * 'imdisable' is set don't try getting the status, it's
1017 if ((p_imdisable
&& b_im_ptr
!= NULL
)
1018 ? *b_im_ptr
== B_IMODE_IM
: im_get_status())
1020 im_set_active(FALSE
); /* Disable input method */
1021 if (b_im_ptr
!= NULL
)
1022 *b_im_ptr
= B_IMODE_NONE
;
1026 im_set_active(TRUE
); /* Enable input method */
1027 if (b_im_ptr
!= NULL
)
1028 *b_im_ptr
= B_IMODE_IM
;
1032 if (b_im_ptr
!= NULL
)
1034 if (b_im_ptr
== &curbuf
->b_p_iminsert
)
1035 set_iminsert_global();
1037 set_imsearch_global();
1040 ui_cursor_shape(); /* may show different cursor shape */
1042 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1043 /* Show/unshow value of 'keymap' in status lines later. */
1044 status_redraw_curbuf();
1046 goto cmdline_not_changed
;
1048 /* case '@': only in very old vi */
1050 /* delete all characters left of the cursor */
1053 i
= ccline
.cmdpos
= 0;
1054 while (i
< ccline
.cmdlen
)
1055 ccline
.cmdbuff
[i
++] = ccline
.cmdbuff
[j
++];
1056 /* Truncate at the end, required for multi-byte chars. */
1057 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
1059 goto cmdline_changed
;
1061 #ifdef FEAT_CLIPBOARD
1063 /* Copy the modeless selection, if there is one. */
1064 if (clip_star
.state
!= SELECT_CLEARED
)
1066 if (clip_star
.state
== SELECT_DONE
)
1067 clip_copy_modeless_selection(TRUE
);
1068 goto cmdline_not_changed
;
1073 case ESC
: /* get here if p_wc != ESC or when ESC typed twice */
1075 /* In exmode it doesn't make sense to return. Except when
1076 * ":normal" runs out of characters. */
1078 #ifdef FEAT_EX_EXTRA
1079 && (ex_normal_busy
== 0 || typebuf
.tb_len
> 0)
1082 goto cmdline_not_changed
;
1084 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1085 putting it in history */
1086 goto returncmd
; /* back to cmd mode */
1088 case Ctrl_R
: /* insert register */
1089 #ifdef USE_ON_FLY_SCROLL
1090 dont_scroll
= TRUE
; /* disallow scrolling here */
1092 putcmdline('"', TRUE
);
1094 i
= c
= plain_vgetc(); /* CTRL-R <char> */
1096 i
= Ctrl_R
; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1098 c
= plain_vgetc(); /* CTRL-R CTRL-R <char> */
1102 * Insert the result of an expression.
1103 * Need to save the current command line, to be able to enter
1109 if (ccline
.cmdfirstc
== '=')/* can't do this recursively */
1116 save_cmdline(&save_ccline
);
1117 c
= get_expr_register();
1118 restore_cmdline(&save_ccline
);
1122 if (c
!= ESC
) /* use ESC to cancel inserting register */
1124 cmdline_paste(c
, i
== Ctrl_R
, FALSE
);
1127 /* When there was a serious error abort getting the
1131 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1132 putting it in history */
1133 goto returncmd
; /* back to cmd mode */
1136 KeyTyped
= FALSE
; /* Don't do p_wc completion. */
1138 if (new_cmdpos
>= 0)
1140 /* set_cmdline_pos() was used */
1141 if (new_cmdpos
> ccline
.cmdlen
)
1142 ccline
.cmdpos
= ccline
.cmdlen
;
1144 ccline
.cmdpos
= new_cmdpos
;
1149 goto cmdline_changed
;
1152 if (showmatches(&xpc
, FALSE
) == EXPAND_NOTHING
)
1153 break; /* Use ^D as normal char instead */
1156 continue; /* don't do incremental search now */
1163 if (ccline
.cmdpos
>= ccline
.cmdlen
)
1165 i
= cmdline_charsize(ccline
.cmdpos
);
1166 if (KeyTyped
&& ccline
.cmdspos
+ i
>= Columns
* Rows
)
1168 ccline
.cmdspos
+= i
;
1171 ccline
.cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
1177 while ((c
== K_S_RIGHT
|| c
== K_C_RIGHT
1178 || (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
)))
1179 && ccline
.cmdbuff
[ccline
.cmdpos
] != ' ');
1182 set_cmdspos_cursor();
1184 goto cmdline_not_changed
;
1191 if (ccline
.cmdpos
== 0)
1195 if (has_mbyte
) /* move to first byte of char */
1196 ccline
.cmdpos
-= (*mb_head_off
)(ccline
.cmdbuff
,
1197 ccline
.cmdbuff
+ ccline
.cmdpos
);
1199 ccline
.cmdspos
-= cmdline_charsize(ccline
.cmdpos
);
1201 while ((c
== K_S_LEFT
|| c
== K_C_LEFT
1202 || (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
)))
1203 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] != ' ');
1206 set_cmdspos_cursor();
1208 goto cmdline_not_changed
;
1211 goto cmdline_not_changed
; /* Ignore mouse */
1214 /* On Win32 ignore <M-F4>, we get it when closing the window was
1217 if (mod_mask
== MOD_MASK_ALT
)
1219 redrawcmd(); /* somehow the cmdline is cleared */
1220 goto cmdline_not_changed
;
1227 case K_MIDDLERELEASE
:
1228 goto cmdline_not_changed
; /* Ignore mouse */
1232 /* When GUI is active, also paste when 'mouse' is empty */
1235 if (!mouse_has(MOUSE_COMMAND
))
1236 goto cmdline_not_changed
; /* Ignore mouse */
1237 # ifdef FEAT_CLIPBOARD
1238 if (clip_star
.available
)
1239 cmdline_paste('*', TRUE
, TRUE
);
1242 cmdline_paste(0, TRUE
, TRUE
);
1244 goto cmdline_changed
;
1248 cmdline_paste('~', TRUE
, FALSE
);
1250 goto cmdline_changed
;
1256 case K_RIGHTRELEASE
:
1257 /* Ignore drag and release events when the button-down wasn't
1259 if (ignore_drag_release
)
1260 goto cmdline_not_changed
;
1264 if (c
== K_LEFTRELEASE
|| c
== K_RIGHTRELEASE
)
1265 ignore_drag_release
= TRUE
;
1267 ignore_drag_release
= FALSE
;
1269 /* When GUI is active, also move when 'mouse' is empty */
1272 if (!mouse_has(MOUSE_COMMAND
))
1273 goto cmdline_not_changed
; /* Ignore mouse */
1274 # ifdef FEAT_CLIPBOARD
1275 if (mouse_row
< cmdline_row
&& clip_star
.available
)
1277 int button
, is_click
, is_drag
;
1280 * Handle modeless selection.
1282 button
= get_mouse_button(KEY2TERMCAP1(c
),
1283 &is_click
, &is_drag
);
1284 if (mouse_model_popup() && button
== MOUSE_LEFT
1285 && (mod_mask
& MOD_MASK_SHIFT
))
1287 /* Translate shift-left to right button. */
1288 button
= MOUSE_RIGHT
;
1289 mod_mask
&= ~MOD_MASK_SHIFT
;
1291 clip_modeless(button
, is_click
, is_drag
);
1292 goto cmdline_not_changed
;
1297 for (ccline
.cmdpos
= 0; ccline
.cmdpos
< ccline
.cmdlen
;
1300 i
= cmdline_charsize(ccline
.cmdpos
);
1301 if (mouse_row
<= cmdline_row
+ ccline
.cmdspos
/ Columns
1302 && mouse_col
< ccline
.cmdspos
% Columns
+ i
)
1307 /* Count ">" for double-wide char that doesn't fit. */
1308 correct_cmdspos(ccline
.cmdpos
, i
);
1309 ccline
.cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
1310 + ccline
.cmdpos
) - 1;
1313 ccline
.cmdspos
+= i
;
1315 goto cmdline_not_changed
;
1317 /* Mouse scroll wheel: ignored here */
1320 /* Alternate buttons ignored here */
1327 goto cmdline_not_changed
;
1329 #endif /* FEAT_MOUSE */
1332 case K_LEFTMOUSE_NM
: /* mousefocus click, ignored */
1333 case K_LEFTRELEASE_NM
:
1334 goto cmdline_not_changed
;
1336 case K_VER_SCROLLBAR
:
1337 if (msg_scrolled
== 0)
1342 goto cmdline_not_changed
;
1344 case K_HOR_SCROLLBAR
:
1345 if (msg_scrolled
== 0)
1347 gui_do_horiz_scroll();
1350 goto cmdline_not_changed
;
1352 #ifdef FEAT_GUI_TABLINE
1355 /* Don't want to change any tabs here. Make sure the same tab
1356 * is still selected. */
1357 if (gui_use_tabline())
1358 gui_mch_set_curtab(tabpage_index(curtab
));
1359 goto cmdline_not_changed
;
1362 case K_SELECT
: /* end of Select mode mapping - ignore */
1363 goto cmdline_not_changed
;
1365 case Ctrl_B
: /* begin of command line */
1372 goto cmdline_not_changed
;
1374 case Ctrl_E
: /* end of command line */
1379 ccline
.cmdpos
= ccline
.cmdlen
;
1380 set_cmdspos_cursor();
1381 goto cmdline_not_changed
;
1383 case Ctrl_A
: /* all matches */
1384 if (nextwild(&xpc
, WILD_ALL
, 0) == FAIL
)
1386 goto cmdline_changed
;
1389 #ifdef FEAT_SEARCH_EXTRA
1390 if (p_is
&& !cmd_silent
&& (firstc
== '/' || firstc
== '?'))
1392 /* Add a character from under the cursor for 'incsearch' */
1394 && !equalpos(curwin
->w_cursor
, old_cursor
))
1399 if (c
== firstc
|| vim_strchr((char_u
*)(
1400 p_magic
? "\\^$.*[" : "\\^$"), c
)
1403 /* put a backslash before special characters */
1404 stuffcharReadbuff(c
);
1410 goto cmdline_not_changed
;
1414 /* completion: longest common part */
1415 if (nextwild(&xpc
, WILD_LONGEST
, 0) == FAIL
)
1417 goto cmdline_changed
;
1419 case Ctrl_N
: /* next match */
1420 case Ctrl_P
: /* previous match */
1421 if (xpc
.xp_numfiles
> 0)
1423 if (nextwild(&xpc
, (c
== Ctrl_P
) ? WILD_PREV
: WILD_NEXT
, 0)
1426 goto cmdline_changed
;
1438 if (hislen
== 0 || firstc
== NUL
) /* no history */
1439 goto cmdline_not_changed
;
1443 /* save current command string so it can be restored later */
1444 if (lookfor
== NULL
)
1446 if ((lookfor
= vim_strsave(ccline
.cmdbuff
)) == NULL
)
1447 goto cmdline_not_changed
;
1448 lookfor
[ccline
.cmdpos
] = NUL
;
1451 j
= (int)STRLEN(lookfor
);
1454 /* one step backwards */
1455 if (c
== K_UP
|| c
== K_S_UP
|| c
== Ctrl_P
1456 || c
== K_PAGEUP
|| c
== K_KPAGEUP
)
1458 if (hiscnt
== hislen
) /* first time */
1459 hiscnt
= hisidx
[histype
];
1460 else if (hiscnt
== 0 && hisidx
[histype
] != hislen
- 1)
1461 hiscnt
= hislen
- 1;
1462 else if (hiscnt
!= hisidx
[histype
] + 1)
1464 else /* at top of list */
1470 else /* one step forwards */
1472 /* on last entry, clear the line */
1473 if (hiscnt
== hisidx
[histype
])
1479 /* not on a history line, nothing to do */
1480 if (hiscnt
== hislen
)
1482 if (hiscnt
== hislen
- 1) /* wrap around */
1487 if (hiscnt
< 0 || history
[histype
][hiscnt
].hisstr
== NULL
)
1492 if ((c
!= K_UP
&& c
!= K_DOWN
)
1494 || STRNCMP(history
[histype
][hiscnt
].hisstr
,
1495 lookfor
, (size_t)j
) == 0)
1499 if (hiscnt
!= i
) /* jumped to other entry */
1505 vim_free(ccline
.cmdbuff
);
1506 if (hiscnt
== hislen
)
1507 p
= lookfor
; /* back to the old one */
1509 p
= history
[histype
][hiscnt
].hisstr
;
1511 if (histype
== HIST_SEARCH
1513 && (old_firstc
= p
[STRLEN(p
) + 1]) != firstc
)
1515 /* Correct for the separator character used when
1516 * adding the history entry vs the one used now.
1517 * First loop: count length.
1518 * Second loop: copy the characters. */
1519 for (i
= 0; i
<= 1; ++i
)
1522 for (j
= 0; p
[j
] != NUL
; ++j
)
1524 /* Replace old sep with new sep, unless it is
1526 if (p
[j
] == old_firstc
1527 && (j
== 0 || p
[j
- 1] != '\\'))
1530 ccline
.cmdbuff
[len
] = firstc
;
1534 /* Escape new sep, unless it is already
1537 && (j
== 0 || p
[j
- 1] != '\\'))
1540 ccline
.cmdbuff
[len
] = '\\';
1544 ccline
.cmdbuff
[len
] = p
[j
];
1551 if (ccline
.cmdbuff
== NULL
)
1555 ccline
.cmdbuff
[len
] = NUL
;
1559 alloc_cmdbuff((int)STRLEN(p
));
1560 if (ccline
.cmdbuff
== NULL
)
1562 STRCPY(ccline
.cmdbuff
, p
);
1565 ccline
.cmdpos
= ccline
.cmdlen
= (int)STRLEN(ccline
.cmdbuff
);
1567 goto cmdline_changed
;
1570 goto cmdline_not_changed
;
1576 ignore_drag_release
= TRUE
;
1578 putcmdline('^', TRUE
);
1579 c
= get_literal(); /* get next (two) character(s) */
1580 do_abbr
= FALSE
; /* don't do abbreviation now */
1582 /* may need to remove ^ when composing char was typed */
1583 if (enc_utf8
&& utf_iscomposing(c
) && !cmd_silent
)
1585 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
1592 #ifdef FEAT_DIGRAPHS
1595 ignore_drag_release
= TRUE
;
1597 putcmdline('?', TRUE
);
1598 #ifdef USE_ON_FLY_SCROLL
1599 dont_scroll
= TRUE
; /* disallow scrolling here */
1601 c
= get_digraph(TRUE
);
1606 goto cmdline_not_changed
;
1607 #endif /* FEAT_DIGRAPHS */
1609 #ifdef FEAT_RIGHTLEFT
1610 case Ctrl__
: /* CTRL-_: switch language mode */
1616 cmd_fkmap
= !cmd_fkmap
;
1617 if (cmd_fkmap
) /* in Farsi always in Insert mode */
1618 ccline
.overstrike
= FALSE
;
1620 else /* Hebrew is default */
1622 cmd_hkmap
= !cmd_hkmap
;
1623 goto cmdline_not_changed
;
1630 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1631 putting it in history */
1632 goto returncmd
; /* back to Normal mode */
1636 * Normal character with no special meaning. Just set mod_mask
1637 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1638 * the string <S-Space>. This should only happen after ^V.
1645 * End of switch on command line character.
1646 * We come here if we have a normal character.
1649 if (do_abbr
&& (IS_SPECIAL(c
) || !vim_iswordc(c
)) && ccheck_abbr(
1651 /* Add ABBR_OFF for characters above 0x100, this is
1652 * what check_abbr() expects. */
1653 (has_mbyte
&& c
>= 0x100) ? (c
+ ABBR_OFF
) :
1656 goto cmdline_changed
;
1659 * put the character in the command line
1661 if (IS_SPECIAL(c
) || mod_mask
!= 0)
1662 put_on_cmdline(get_special_key_name(c
, mod_mask
), -1, TRUE
);
1668 j
= (*mb_char2bytes
)(c
, IObuff
);
1669 IObuff
[j
] = NUL
; /* exclude composing chars */
1670 put_on_cmdline(IObuff
, j
, TRUE
);
1676 put_on_cmdline(IObuff
, 1, TRUE
);
1679 goto cmdline_changed
;
1682 * This part implements incremental searches for "/" and "?"
1683 * Jump to cmdline_not_changed when a character has been read but the command
1684 * line did not change. Then we only search and redraw if something changed in
1686 * Jump to cmdline_changed when the command line did change.
1687 * (Sorry for the goto's, I know it is ugly).
1689 cmdline_not_changed
:
1690 #ifdef FEAT_SEARCH_EXTRA
1691 if (!incsearch_postponed
)
1696 #ifdef FEAT_SEARCH_EXTRA
1698 * 'incsearch' highlighting.
1700 if (p_is
&& !cmd_silent
&& (firstc
== '/' || firstc
== '?'))
1704 /* if there is a character waiting, search and redraw later */
1707 incsearch_postponed
= TRUE
;
1710 incsearch_postponed
= FALSE
;
1711 curwin
->w_cursor
= old_cursor
; /* start at old position */
1713 /* If there is no command line, don't do anything */
1714 if (ccline
.cmdlen
== 0)
1718 cursor_off(); /* so the user knows we're busy */
1720 ++emsg_off
; /* So it doesn't beep if bad expr */
1721 i
= do_search(NULL
, firstc
, ccline
.cmdbuff
, count
,
1722 SEARCH_KEEP
+ SEARCH_OPT
+ SEARCH_NOOF
+ SEARCH_PEEK
);
1724 /* if interrupted while searching, behave like it failed */
1727 (void)vpeekc(); /* remove <C-C> from input stream */
1728 got_int
= FALSE
; /* don't abandon the command line */
1731 else if (char_avail())
1732 /* cancelled searching because a char was typed */
1733 incsearch_postponed
= TRUE
;
1736 highlight_match
= TRUE
; /* highlight position */
1738 highlight_match
= FALSE
; /* remove highlight */
1740 /* first restore the old curwin values, so the screen is
1741 * positioned in the same way as the actual search command */
1742 curwin
->w_leftcol
= old_leftcol
;
1743 curwin
->w_topline
= old_topline
;
1745 curwin
->w_topfill
= old_topfill
;
1747 curwin
->w_botline
= old_botline
;
1748 changed_cline_bef_curs();
1753 pos_T save_pos
= curwin
->w_cursor
;
1756 * First move cursor to end of match, then to the start. This
1757 * moves the whole match onto the screen when 'nowrap' is set.
1759 curwin
->w_cursor
.lnum
+= search_match_lines
;
1760 curwin
->w_cursor
.col
= search_match_endcol
;
1761 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
1763 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
1764 coladvance((colnr_T
)MAXCOL
);
1767 end_pos
= curwin
->w_cursor
;
1768 curwin
->w_cursor
= save_pos
;
1771 end_pos
= curwin
->w_cursor
; /* shutup gcc 4 */
1774 # ifdef FEAT_WINDOWS
1775 /* May redraw the status line to show the cursor position. */
1776 if (p_ru
&& curwin
->w_status_height
> 0)
1777 curwin
->w_redr_status
= TRUE
;
1780 save_cmdline(&save_ccline
);
1781 update_screen(SOME_VALID
);
1782 restore_cmdline(&save_ccline
);
1784 /* Leave it at the end to make CTRL-R CTRL-W work. */
1786 curwin
->w_cursor
= end_pos
;
1790 did_incsearch
= TRUE
;
1792 #else /* FEAT_SEARCH_EXTRA */
1796 #ifdef FEAT_RIGHTLEFT
1799 || (p_arshape
&& !p_tbidi
&& enc_utf8
)
1802 /* Always redraw the whole command line to fix shaping and
1803 * right-left typing. Not efficient, but it works. */
1810 #ifdef FEAT_RIGHTLEFT
1818 ExpandCleanup(&xpc
);
1820 #ifdef FEAT_SEARCH_EXTRA
1823 curwin
->w_cursor
= old_cursor
;
1824 curwin
->w_curswant
= old_curswant
;
1825 curwin
->w_leftcol
= old_leftcol
;
1826 curwin
->w_topline
= old_topline
;
1828 curwin
->w_topfill
= old_topfill
;
1830 curwin
->w_botline
= old_botline
;
1831 highlight_match
= FALSE
;
1832 validate_cursor(); /* needed for TAB */
1833 redraw_later(SOME_VALID
);
1837 if (ccline
.cmdbuff
!= NULL
)
1840 * Put line in history buffer (":" and "=" only when it was typed).
1843 if (ccline
.cmdlen
&& firstc
!= NUL
1844 && (some_key_typed
|| histype
== HIST_SEARCH
))
1846 add_to_history(histype
, ccline
.cmdbuff
, TRUE
,
1847 histype
== HIST_SEARCH
? firstc
: NUL
);
1850 vim_free(new_last_cmdline
);
1851 new_last_cmdline
= vim_strsave(ccline
.cmdbuff
);
1856 if (gotesc
) /* abandon command line */
1858 vim_free(ccline
.cmdbuff
);
1859 ccline
.cmdbuff
= NULL
;
1860 if (msg_scrolled
== 0)
1863 redraw_cmdline
= TRUE
;
1868 * If the screen was shifted up, redraw the whole screen (later).
1869 * If the line is too long, clear it, so ruler and shown command do
1870 * not get printed in the middle of it.
1873 msg_scroll
= save_msg_scroll
;
1876 /* When the command line was typed, no need for a wait-return prompt. */
1878 need_wait_return
= FALSE
;
1881 #ifdef USE_IM_CONTROL
1882 if (b_im_ptr
!= NULL
&& *b_im_ptr
!= B_IMODE_LMAP
)
1883 im_save_status(b_im_ptr
);
1884 im_set_active(FALSE
);
1890 ui_cursor_shape(); /* may show different cursor shape */
1894 char_u
*p
= ccline
.cmdbuff
;
1896 /* Make ccline empty, getcmdline() may try to use it. */
1897 ccline
.cmdbuff
= NULL
;
1902 #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1904 * Get a command line with a prompt.
1905 * This is prepared to be called recursively from getcmdline() (e.g. by
1906 * f_input() when evaluating an expression from CTRL-R =).
1907 * Returns the command line in allocated memory, or NULL.
1910 getcmdline_prompt(firstc
, prompt
, attr
, xp_context
, xp_arg
)
1912 char_u
*prompt
; /* command line prompt */
1913 int attr
; /* attributes for prompt */
1914 int xp_context
; /* type of expansion */
1915 char_u
*xp_arg
; /* user-defined expansion argument */
1918 struct cmdline_info save_ccline
;
1919 int msg_col_save
= msg_col
;
1921 save_cmdline(&save_ccline
);
1922 ccline
.cmdprompt
= prompt
;
1923 ccline
.cmdattr
= attr
;
1925 ccline
.xp_context
= xp_context
;
1926 ccline
.xp_arg
= xp_arg
;
1927 ccline
.input_fn
= (firstc
== '@');
1929 s
= getcmdline(firstc
, 1L, 0);
1930 restore_cmdline(&save_ccline
);
1931 /* Restore msg_col, the prompt from input() may have changed it. */
1932 msg_col
= msg_col_save
;
1939 * Return TRUE when the text must not be changed and we can't switch to
1940 * another window or buffer. Used when editing the command line, evaluating
1941 * 'balloonexpr', etc.
1947 if (cmdwin_type
!= 0)
1950 return textlock
!= 0;
1954 * Give an error message for a command that isn't allowed while the cmdline
1955 * window is open or editing the cmdline in another way.
1961 if (cmdwin_type
!= 0)
1968 #if defined(FEAT_AUTOCMD) || defined(PROTO)
1970 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
1976 if (curbuf_lock
> 0)
1978 EMSG(_("E788: Not allowed to edit another buffer now"));
1986 cmdline_charsize(idx
)
1989 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1990 if (cmdline_star
> 0) /* showing '*', always 1 position */
1993 return ptr2cells(ccline
.cmdbuff
+ idx
);
1997 * Compute the offset of the cursor on the command line for the prompt and
2003 if (ccline
.cmdfirstc
!= NUL
)
2004 ccline
.cmdspos
= 1 + ccline
.cmdindent
;
2006 ccline
.cmdspos
= 0 + ccline
.cmdindent
;
2010 * Compute the screen position for the cursor on the command line.
2013 set_cmdspos_cursor()
2021 if (m
< 0) /* overflow, Columns or Rows at weird value */
2026 for (i
= 0; i
< ccline
.cmdlen
&& i
< ccline
.cmdpos
; ++i
)
2028 c
= cmdline_charsize(i
);
2030 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2032 correct_cmdspos(i
, c
);
2034 /* If the cmdline doesn't fit, put cursor on last visible char. */
2035 if ((ccline
.cmdspos
+= c
) >= m
)
2037 ccline
.cmdpos
= i
- 1;
2038 ccline
.cmdspos
-= c
;
2043 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
) - 1;
2050 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2051 * character that doesn't fit, so that a ">" must be displayed.
2054 correct_cmdspos(idx
, cells
)
2058 if ((*mb_ptr2len
)(ccline
.cmdbuff
+ idx
) > 1
2059 && (*mb_ptr2cells
)(ccline
.cmdbuff
+ idx
) > 1
2060 && ccline
.cmdspos
% Columns
+ cells
> Columns
)
2066 * Get an Ex command line for the ":" command.
2070 getexline(c
, dummy
, indent
)
2071 int c
; /* normally ':', NUL for ":append" */
2072 void *dummy
; /* cookie not used */
2073 int indent
; /* indent for inside conditionals */
2075 /* When executing a register, remove ':' that's in front of each line. */
2076 if (exec_from_reg
&& vpeekc() == ':')
2078 return getcmdline(c
, 1L, indent
);
2082 * Get an Ex command line for Ex mode.
2083 * In Ex mode we only use the OS supplied line editing features and no
2084 * mappings or abbreviations.
2085 * Returns a string in allocated memory or NULL.
2089 getexmodeline(promptc
, dummy
, indent
)
2090 int promptc
; /* normally ':', NUL for ":append" and '?' for
2092 void *dummy
; /* cookie not used */
2093 int indent
; /* indent for inside conditionals */
2099 int escaped
= FALSE
; /* CTRL-V typed */
2104 /* Switch cursor on now. This avoids that it happens after the "\n", which
2105 * confuses the system function that computes tabstops. */
2108 /* always start in column 0; write a newline if necessary */
2110 if ((msg_col
|| msg_didout
) && promptc
!= '?')
2114 /* indent that is only displayed, not in the line itself */
2117 while (indent
-- > 0)
2122 ga_init2(&line_ga
, 1, 30);
2124 /* autoindent for :insert and :append is in the line itself */
2130 ga_append(&line_ga
, TAB
);
2131 msg_puts((char_u
*)" ");
2134 while (indent
-- > 0)
2136 ga_append(&line_ga
, ' ');
2144 * Get the line, one character at a time.
2149 if (ga_grow(&line_ga
, 40) == FAIL
)
2151 pend
= (char_u
*)line_ga
.ga_data
+ line_ga
.ga_len
;
2153 /* Get one character at a time. Don't use inchar(), it can't handle
2154 * special characters. */
2159 * Handle line editing.
2160 * Previously this was left to the system, putting the terminal in
2161 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
2171 /* CR typed means "enter", which is NL */
2175 if (c1
== BS
|| c1
== K_BS
2176 || c1
== DEL
|| c1
== K_DEL
|| c1
== K_KDEL
)
2178 if (line_ga
.ga_len
> 0)
2196 p
= (char_u
*)line_ga
.ga_data
;
2197 p
[line_ga
.ga_len
] = NUL
;
2198 indent
= get_indent_str(p
, 8);
2199 indent
+= curbuf
->b_p_sw
- indent
% curbuf
->b_p_sw
;
2201 while (get_indent_str(p
, 8) < indent
)
2203 char_u
*s
= skipwhite(p
);
2205 ga_grow(&line_ga
, 1);
2206 mch_memmove(s
+ 1, s
, line_ga
.ga_len
- (s
- p
) + 1);
2211 /* redraw the line */
2214 for (p
= (char_u
*)line_ga
.ga_data
;
2215 p
< (char_u
*)line_ga
.ga_data
+ line_ga
.ga_len
; ++p
)
2222 } while (++vcol
% 8);
2226 msg_outtrans_len(p
, 1);
2227 vcol
+= char2cells(*p
);
2231 windgoto(msg_row
, msg_col
);
2237 /* Delete one shiftwidth. */
2238 p
= (char_u
*)line_ga
.ga_data
;
2239 if (prev_char
== '0' || prev_char
== '^')
2241 if (prev_char
== '^')
2242 ex_keep_indent
= TRUE
;
2244 p
[--line_ga
.ga_len
] = NUL
;
2248 p
[line_ga
.ga_len
] = NUL
;
2249 indent
= get_indent_str(p
, 8);
2251 indent
-= indent
% curbuf
->b_p_sw
;
2253 while (get_indent_str(p
, 8) > indent
)
2255 char_u
*s
= skipwhite(p
);
2257 mch_memmove(s
- 1, s
, line_ga
.ga_len
- (s
- p
) + 1);
2263 if (c1
== Ctrl_V
|| c1
== Ctrl_Q
)
2269 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2276 ((char_u
*)line_ga
.ga_data
)[line_ga
.ga_len
] = c1
;
2281 /* Don't use chartabsize(), 'ts' can be different */
2285 } while (++vcol
% 8);
2290 ((char_u
*)line_ga
.ga_data
) + line_ga
.ga_len
, 1);
2291 vcol
+= char2cells(c1
);
2296 windgoto(msg_row
, msg_col
);
2297 pend
= (char_u
*)(line_ga
.ga_data
) + line_ga
.ga_len
;
2299 /* we are done when a NL is entered, but not when it comes after a
2301 if (line_ga
.ga_len
> 0 && pend
[-1] == '\n'
2302 && (line_ga
.ga_len
<= 1 || pend
[-2] != '\\'))
2314 /* make following messages go to the next line */
2317 if (msg_row
< Rows
- 1)
2319 emsg_on_display
= FALSE
; /* don't want ui_delay() */
2324 return (char_u
*)line_ga
.ga_data
;
2327 # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2328 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
2330 * Return TRUE if ccline.overstrike is on.
2333 cmdline_overstrike()
2335 return ccline
.overstrike
;
2339 * Return TRUE if the cursor is at the end of the cmdline.
2344 return (ccline
.cmdpos
>= ccline
.cmdlen
);
2348 #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
2350 * Return the virtual column number at the current cursor position.
2351 * This is used by the IM code to obtain the start of the preedit string.
2354 cmdline_getvcol_cursor()
2356 if (ccline
.cmdbuff
== NULL
|| ccline
.cmdpos
> ccline
.cmdlen
)
2365 for (col
= 0; i
< ccline
.cmdpos
; ++col
)
2366 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
);
2372 return ccline
.cmdpos
;
2376 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2378 * If part of the command line is an IM preedit string, redraw it with
2379 * IM feedback attributes. The cursor position is restored after drawing.
2384 if ((State
& CMDLINE
)
2386 /* && im_get_status() doesn't work when using SCIM */
2388 && im_is_preediting())
2398 cmdspos
= ((ccline
.cmdfirstc
!= NUL
) ? 1 : 0) + ccline
.cmdindent
;
2403 for (col
= 0; col
< preedit_start_col
2404 && cmdpos
< ccline
.cmdlen
; ++col
)
2406 cmdspos
+= (*mb_ptr2cells
)(ccline
.cmdbuff
+ cmdpos
);
2407 cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ cmdpos
);
2413 cmdspos
+= preedit_start_col
;
2414 cmdpos
+= preedit_start_col
;
2417 msg_row
= cmdline_row
+ (cmdspos
/ (int)Columns
);
2418 msg_col
= cmdspos
% (int)Columns
;
2419 if (msg_row
>= Rows
)
2422 for (col
= 0; cmdpos
< ccline
.cmdlen
; ++col
)
2427 char_attr
= im_get_feedback_attr(col
);
2429 break; /* end of preedit string */
2433 char_len
= (*mb_ptr2len
)(ccline
.cmdbuff
+ cmdpos
);
2438 msg_outtrans_len_attr(ccline
.cmdbuff
+ cmdpos
, char_len
, char_attr
);
2446 #endif /* FEAT_XIM && FEAT_GUI_GTK */
2449 * Allocate a new command line buffer.
2450 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2451 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2458 * give some extra space to avoid having to allocate all the time
2465 ccline
.cmdbuff
= alloc(len
); /* caller should check for out-of-memory */
2466 ccline
.cmdbufflen
= len
;
2470 * Re-allocate the command line to length len + something extra.
2471 * return FAIL for failure, OK otherwise
2474 realloc_cmdbuff(len
)
2480 alloc_cmdbuff(len
); /* will get some more */
2481 if (ccline
.cmdbuff
== NULL
) /* out of memory */
2483 ccline
.cmdbuff
= p
; /* keep the old one */
2486 mch_memmove(ccline
.cmdbuff
, p
, (size_t)ccline
.cmdlen
+ 1);
2491 #if defined(FEAT_ARABIC) || defined(PROTO)
2492 static char_u
*arshape_buf
= NULL
;
2494 # if defined(EXITFREE) || defined(PROTO)
2498 vim_free(arshape_buf
);
2504 * Draw part of the cmdline at the current cursor position. But draw stars
2505 * when cmdline_star is TRUE.
2508 draw_cmdline(start
, len
)
2512 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2515 if (cmdline_star
> 0)
2516 for (i
= 0; i
< len
; ++i
)
2521 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ start
+ i
) - 1;
2527 if (p_arshape
&& !p_tbidi
&& enc_utf8
&& len
> 0)
2529 static int buflen
= 0;
2542 * Do arabic shaping into a temporary buffer. This is very
2545 if (len
* 2 + 2 > buflen
)
2547 /* Re-allocate the buffer. We keep it around to avoid a lot of
2548 * alloc()/free() calls. */
2549 vim_free(arshape_buf
);
2550 buflen
= len
* 2 + 2;
2551 arshape_buf
= alloc(buflen
);
2552 if (arshape_buf
== NULL
)
2553 return; /* out of memory */
2556 if (utf_iscomposing(utf_ptr2char(ccline
.cmdbuff
+ start
)))
2558 /* Prepend a space to draw the leading composing char on. */
2559 arshape_buf
[0] = ' ';
2563 for (j
= start
; j
< start
+ len
; j
+= mb_l
)
2565 p
= ccline
.cmdbuff
+ j
;
2566 u8c
= utfc_ptr2char_len(p
, u8cc
, start
+ len
- j
);
2567 mb_l
= utfc_ptr2len_len(p
, start
+ len
- j
);
2568 if (ARABIC_CHAR(u8c
))
2570 /* Do Arabic shaping. */
2573 /* displaying from right to left */
2577 if (j
+ mb_l
>= start
+ len
)
2580 nc
= utf_ptr2char(p
+ mb_l
);
2584 /* displaying from left to right */
2585 if (j
+ mb_l
>= start
+ len
)
2591 pc
= utfc_ptr2char_len(p
+ mb_l
, pcc
,
2592 start
+ len
- j
- mb_l
);
2599 u8c
= arabic_shape(u8c
, NULL
, &u8cc
[0], pc
, pc1
, nc
);
2601 newlen
+= (*mb_char2bytes
)(u8c
, arshape_buf
+ newlen
);
2604 newlen
+= (*mb_char2bytes
)(u8cc
[0], arshape_buf
+ newlen
);
2606 newlen
+= (*mb_char2bytes
)(u8cc
[1],
2607 arshape_buf
+ newlen
);
2613 mch_memmove(arshape_buf
+ newlen
, p
, mb_l
);
2618 msg_outtrans_len(arshape_buf
, newlen
);
2622 msg_outtrans_len(ccline
.cmdbuff
+ start
, len
);
2626 * Put a character on the command line. Shifts the following text to the
2627 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2628 * "c" must be printable (fit in one display cell)!
2631 putcmdline(c
, shift
)
2640 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
2641 msg_no_more
= FALSE
;
2646 * Undo a putcmdline(c, FALSE).
2654 if (ccline
.cmdlen
== ccline
.cmdpos
)
2657 draw_cmdline(ccline
.cmdpos
, 1);
2658 msg_no_more
= FALSE
;
2663 * Put the given string, of the given length, onto the command line.
2664 * If len is -1, then STRLEN() is used to calculate the length.
2665 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2666 * part will be redrawn, otherwise it will not. If this function is called
2667 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2668 * called afterwards.
2671 put_on_cmdline(str
, len
, redraw
)
2682 len
= (int)STRLEN(str
);
2684 /* Check if ccline.cmdbuff needs to be longer */
2685 if (ccline
.cmdlen
+ len
+ 1 >= ccline
.cmdbufflen
)
2686 retval
= realloc_cmdbuff(ccline
.cmdlen
+ len
);
2691 if (!ccline
.overstrike
)
2693 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
+ len
,
2694 ccline
.cmdbuff
+ ccline
.cmdpos
,
2695 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
));
2696 ccline
.cmdlen
+= len
;
2703 /* Count nr of characters in the new string. */
2705 for (i
= 0; i
< len
; i
+= (*mb_ptr2len
)(str
+ i
))
2707 /* Count nr of bytes in cmdline that are overwritten by these
2709 for (i
= ccline
.cmdpos
; i
< ccline
.cmdlen
&& m
> 0;
2710 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
))
2712 if (i
< ccline
.cmdlen
)
2714 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
+ len
,
2715 ccline
.cmdbuff
+ i
, (size_t)(ccline
.cmdlen
- i
));
2716 ccline
.cmdlen
+= ccline
.cmdpos
+ len
- i
;
2719 ccline
.cmdlen
= ccline
.cmdpos
+ len
;
2723 if (ccline
.cmdpos
+ len
> ccline
.cmdlen
)
2724 ccline
.cmdlen
= ccline
.cmdpos
+ len
;
2726 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
, str
, (size_t)len
);
2727 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
2732 /* When the inserted text starts with a composing character,
2733 * backup to the character before it. There could be two of them.
2736 c
= utf_ptr2char(ccline
.cmdbuff
+ ccline
.cmdpos
);
2737 while (ccline
.cmdpos
> 0 && utf_iscomposing(c
))
2739 i
= (*mb_head_off
)(ccline
.cmdbuff
,
2740 ccline
.cmdbuff
+ ccline
.cmdpos
- 1) + 1;
2743 c
= utf_ptr2char(ccline
.cmdbuff
+ ccline
.cmdpos
);
2746 if (i
== 0 && ccline
.cmdpos
> 0 && arabic_maycombine(c
))
2748 /* Check the previous character for Arabic combining pair. */
2749 i
= (*mb_head_off
)(ccline
.cmdbuff
,
2750 ccline
.cmdbuff
+ ccline
.cmdpos
- 1) + 1;
2751 if (arabic_combine(utf_ptr2char(ccline
.cmdbuff
2752 + ccline
.cmdpos
- i
), c
))
2763 /* Also backup the cursor position. */
2764 i
= ptr2cells(ccline
.cmdbuff
+ ccline
.cmdpos
);
2765 ccline
.cmdspos
-= i
;
2776 if (redraw
&& !cmd_silent
)
2780 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
2781 /* Avoid clearing the rest of the line too often. */
2782 if (cmdline_row
!= i
|| ccline
.overstrike
)
2784 msg_no_more
= FALSE
;
2788 * If we are in Farsi command mode, the character input must be in
2789 * Insert mode. So do not advance the cmdpos.
2797 if (m
< 0) /* overflow, Columns or Rows at weird value */
2802 for (i
= 0; i
< len
; ++i
)
2804 c
= cmdline_charsize(ccline
.cmdpos
);
2806 /* count ">" for a double-wide char that doesn't fit. */
2808 correct_cmdspos(ccline
.cmdpos
, c
);
2810 /* Stop cursor at the end of the screen */
2811 if (ccline
.cmdspos
+ c
>= m
)
2813 ccline
.cmdspos
+= c
;
2817 c
= (*mb_ptr2len
)(ccline
.cmdbuff
+ ccline
.cmdpos
) - 1;
2818 if (c
> len
- i
- 1)
2833 static struct cmdline_info prev_ccline
;
2834 static int prev_ccline_used
= FALSE
;
2837 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2838 * and overwrite it. But get_cmdline_str() may need it, thus make it
2839 * available globally in prev_ccline.
2843 struct cmdline_info
*ccp
;
2845 if (!prev_ccline_used
)
2847 vim_memset(&prev_ccline
, 0, sizeof(struct cmdline_info
));
2848 prev_ccline_used
= TRUE
;
2851 prev_ccline
= ccline
;
2852 ccline
.cmdbuff
= NULL
;
2853 ccline
.cmdprompt
= NULL
;
2857 * Restore ccline after it has been saved with save_cmdline().
2860 restore_cmdline(ccp
)
2861 struct cmdline_info
*ccp
;
2863 ccline
= prev_ccline
;
2867 #if defined(FEAT_EVAL) || defined(PROTO)
2869 * Save the command line into allocated memory. Returns a pointer to be
2870 * passed to restore_cmdline_alloc() later.
2871 * Returns NULL when failed.
2874 save_cmdline_alloc()
2876 struct cmdline_info
*p
;
2878 p
= (struct cmdline_info
*)alloc((unsigned)sizeof(struct cmdline_info
));
2885 * Restore the command line from the return value of save_cmdline_alloc().
2888 restore_cmdline_alloc(p
)
2893 restore_cmdline((struct cmdline_info
*)p
);
2900 * paste a yank register into the command line.
2901 * used by CTRL-R command in command-line mode
2902 * insert_reg() can't be used here, because special characters from the
2903 * register contents will be interpreted as commands.
2905 * return FAIL for failure, OK otherwise
2908 cmdline_paste(regname
, literally
, remcr
)
2910 int literally
; /* Insert text literally instead of "as typed" */
2911 int remcr
; /* remove trailing CR */
2917 struct cmdline_info save_ccline
;
2919 /* check for valid regname; also accept special characters for CTRL-R in
2920 * the command line */
2921 if (regname
!= Ctrl_F
&& regname
!= Ctrl_P
&& regname
!= Ctrl_W
2922 && regname
!= Ctrl_A
&& !valid_yank_reg(regname
, FALSE
))
2925 /* A register containing CTRL-R can cause an endless loop. Allow using
2926 * CTRL-C to break the loop. */
2931 #ifdef FEAT_CLIPBOARD
2932 regname
= may_get_selection(regname
);
2935 /* Need to save and restore ccline. And set "textlock" to avoid nasty
2936 * things like going to another buffer when evaluating an expression. */
2937 save_cmdline(&save_ccline
);
2939 i
= get_spec_reg(regname
, &arg
, &allocated
, TRUE
);
2941 restore_cmdline(&save_ccline
);
2945 /* Got the value of a special register in "arg". */
2949 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2950 * part of the word. */
2952 if (p_is
&& regname
== Ctrl_W
)
2957 /* Locate start of last word in the cmd buffer. */
2958 for (w
= ccline
.cmdbuff
+ ccline
.cmdlen
; w
> ccline
.cmdbuff
; )
2963 len
= (*mb_head_off
)(ccline
.cmdbuff
, w
- 1) + 1;
2964 if (!vim_iswordc(mb_ptr2char(w
- len
)))
2971 if (!vim_iswordc(w
[-1]))
2976 len
= (int)((ccline
.cmdbuff
+ ccline
.cmdlen
) - w
);
2977 if (p_ic
? STRNICMP(w
, arg
, len
) == 0 : STRNCMP(w
, arg
, len
) == 0)
2981 cmdline_paste_str(p
, literally
);
2987 return cmdline_paste_reg(regname
, literally
, remcr
);
2991 * Put a string on the command line.
2992 * When "literally" is TRUE, insert literally.
2993 * When "literally" is FALSE, insert as typed, but don't leave the command
2997 cmdline_paste_str(s
, literally
)
3004 put_on_cmdline(s
, -1, TRUE
);
3009 if (cv
== Ctrl_V
&& s
[1])
3015 s
+= mb_char2len(c
);
3020 if (cv
== Ctrl_V
|| c
== ESC
|| c
== Ctrl_C
|| c
== CAR
|| c
== NL
3024 || (c
== Ctrl_BSL
&& *s
== Ctrl_N
))
3025 stuffcharReadbuff(Ctrl_V
);
3026 stuffcharReadbuff(c
);
3030 #ifdef FEAT_WILDMENU
3032 * Delete characters on the command line, from "from" to the current
3039 mch_memmove(ccline
.cmdbuff
+ from
, ccline
.cmdbuff
+ ccline
.cmdpos
,
3040 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
+ 1));
3041 ccline
.cmdlen
-= ccline
.cmdpos
- from
;
3042 ccline
.cmdpos
= from
;
3047 * this function is called when the screen size changes and with incremental
3055 need_wait_return
= FALSE
;
3068 if (ccline
.cmdfirstc
!= NUL
)
3069 msg_putchar(ccline
.cmdfirstc
);
3070 if (ccline
.cmdprompt
!= NULL
)
3072 msg_puts_attr(ccline
.cmdprompt
, ccline
.cmdattr
);
3073 ccline
.cmdindent
= msg_col
+ (msg_row
- cmdline_row
) * Columns
;
3074 /* do the reverse of set_cmdspos() */
3075 if (ccline
.cmdfirstc
!= NUL
)
3079 for (i
= ccline
.cmdindent
; i
> 0; --i
)
3084 * Redraw what is currently on the command line.
3092 /* when 'incsearch' is set there may be no command line while redrawing */
3093 if (ccline
.cmdbuff
== NULL
)
3095 windgoto(cmdline_row
, 0);
3103 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3105 draw_cmdline(0, ccline
.cmdlen
);
3107 msg_no_more
= FALSE
;
3109 set_cmdspos_cursor();
3112 * An emsg() before may have set msg_scroll. This is used in normal mode,
3113 * in cmdline mode we can reset them now.
3115 msg_scroll
= FALSE
; /* next message overwrites cmdline */
3117 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3118 * in cmdline mode */
3119 skip_redraw
= FALSE
;
3125 if (exmode_active
|| msg_scrolled
!= 0)
3126 cmdline_row
= Rows
- 1;
3128 cmdline_row
= W_WINROW(lastwin
) + lastwin
->w_height
3129 + W_STATUS_HEIGHT(lastwin
);
3138 #ifdef FEAT_RIGHTLEFT
3141 msg_row
= cmdline_row
+ (ccline
.cmdspos
/ (int)(Columns
- 1));
3142 msg_col
= (int)Columns
- (ccline
.cmdspos
% (int)(Columns
- 1)) - 1;
3149 msg_row
= cmdline_row
+ (ccline
.cmdspos
/ (int)Columns
);
3150 msg_col
= ccline
.cmdspos
% (int)Columns
;
3151 if (msg_row
>= Rows
)
3155 windgoto(msg_row
, msg_col
);
3156 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3157 redrawcmd_preedit();
3159 #ifdef MCH_CURSOR_SHAPE
3160 mch_update_cursor();
3169 #ifdef FEAT_RIGHTLEFT
3171 msg_col
= Columns
- 1;
3174 msg_col
= 0; /* always start in column 0 */
3175 if (clr
) /* clear the bottom line(s) */
3176 msg_clr_eos(); /* will reset clear_cmdline */
3177 windgoto(cmdline_row
, 0);
3181 * Check the word in front of the cursor for an abbreviation.
3182 * Called when the non-id character "c" has been entered.
3183 * When an abbreviation is recognized it is removed from the text with
3184 * backspaces and the replacement string is inserted, followed by "c".
3190 if (p_paste
|| no_abbr
) /* no abbreviations or in paste mode */
3193 return check_abbr(c
, ccline
.cmdbuff
, ccline
.cmdpos
, 0);
3197 * Return FAIL if this is not an appropriate context in which to do
3198 * completion of anything, return OK if it is (even if there are no matches).
3199 * For the caller, this means that the character is just passed through like a
3200 * normal character (instead of being expanded). This allows :s/^I^D etc.
3203 nextwild(xp
, type
, options
)
3206 int options
; /* extra options for ExpandOne() */
3215 if (xp
->xp_numfiles
== -1)
3217 set_expand_context(xp
);
3218 cmd_showtail
= expand_showtail(xp
);
3221 if (xp
->xp_context
== EXPAND_UNSUCCESSFUL
)
3224 return OK
; /* Something illegal on command line */
3226 if (xp
->xp_context
== EXPAND_NOTHING
)
3228 /* Caller can use the character as a normal char instead */
3232 MSG_PUTS("..."); /* show that we are busy */
3235 i
= (int)(xp
->xp_pattern
- ccline
.cmdbuff
);
3236 oldlen
= ccline
.cmdpos
- i
;
3238 if (type
== WILD_NEXT
|| type
== WILD_PREV
)
3241 * Get next/previous match for a previous expanded pattern.
3243 p2
= ExpandOne(xp
, NULL
, NULL
, 0, type
);
3248 * Translate string into pattern and expand it.
3250 if ((p1
= addstar(&ccline
.cmdbuff
[i
], oldlen
, xp
->xp_context
)) == NULL
)
3254 p2
= ExpandOne(xp
, p1
, vim_strnsave(&ccline
.cmdbuff
[i
], oldlen
),
3255 WILD_HOME_REPLACE
|WILD_ADD_SLASH
|WILD_SILENT
|WILD_ESCAPE
3258 /* longest match: make sure it is not shorter (happens with :help */
3259 if (p2
!= NULL
&& type
== WILD_LONGEST
)
3261 for (j
= 0; j
< oldlen
; ++j
)
3262 if (ccline
.cmdbuff
[i
+ j
] == '*'
3263 || ccline
.cmdbuff
[i
+ j
] == '?')
3265 if ((int)STRLEN(p2
) < j
)
3274 if (p2
!= NULL
&& !got_int
)
3276 difflen
= (int)STRLEN(p2
) - oldlen
;
3277 if (ccline
.cmdlen
+ difflen
> ccline
.cmdbufflen
- 4)
3279 v
= realloc_cmdbuff(ccline
.cmdlen
+ difflen
);
3280 xp
->xp_pattern
= ccline
.cmdbuff
+ i
;
3286 mch_memmove(&ccline
.cmdbuff
[ccline
.cmdpos
+ difflen
],
3287 &ccline
.cmdbuff
[ccline
.cmdpos
],
3288 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
+ 1));
3289 mch_memmove(&ccline
.cmdbuff
[i
], p2
, STRLEN(p2
));
3290 ccline
.cmdlen
+= difflen
;
3291 ccline
.cmdpos
+= difflen
;
3299 /* When expanding a ":map" command and no matches are found, assume that
3300 * the key is supposed to be inserted literally */
3301 if (xp
->xp_context
== EXPAND_MAPPINGS
&& p2
== NULL
)
3304 if (xp
->xp_numfiles
<= 0 && p2
== NULL
)
3306 else if (xp
->xp_numfiles
== 1)
3307 /* free expanded pattern */
3308 (void)ExpandOne(xp
, NULL
, NULL
, 0, WILD_FREE
);
3314 * Do wildcard expansion on the string 'str'.
3315 * Chars that should not be expanded must be preceded with a backslash.
3316 * Return a pointer to alloced memory containing the new string.
3317 * Return NULL for failure.
3319 * "orig" is the originally expanded string, copied to allocated memory. It
3320 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3321 * WILD_PREV "orig" should be NULL.
3323 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3324 * is WILD_EXPAND_FREE or WILD_ALL.
3326 * mode = WILD_FREE: just free previously expanded matches
3327 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3328 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3329 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3330 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3331 * mode = WILD_ALL: return all matches concatenated
3332 * mode = WILD_LONGEST: return longest matched part
3334 * options = WILD_LIST_NOTFOUND: list entries without a match
3335 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3336 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3337 * options = WILD_NO_BEEP: Don't beep for multiple matches
3338 * options = WILD_ADD_SLASH: add a slash after directory names
3339 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3340 * options = WILD_SILENT: don't print warning messages
3341 * options = WILD_ESCAPE: put backslash before special chars
3343 * The variables xp->xp_context and xp->xp_backslash must have been set!
3346 ExpandOne(xp
, str
, orig
, options
, mode
)
3349 char_u
*orig
; /* allocated copy of original of expanded string */
3355 static char_u
*orig_save
= NULL
; /* kept value of orig */
3358 int non_suf_match
; /* number without matching suffix */
3361 * first handle the case of using an old match
3363 if (mode
== WILD_NEXT
|| mode
== WILD_PREV
)
3365 if (xp
->xp_numfiles
> 0)
3367 if (mode
== WILD_PREV
)
3370 findex
= xp
->xp_numfiles
;
3373 else /* mode == WILD_NEXT */
3377 * When wrapping around, return the original string, set findex to
3382 if (orig_save
== NULL
)
3383 findex
= xp
->xp_numfiles
- 1;
3387 if (findex
>= xp
->xp_numfiles
)
3389 if (orig_save
== NULL
)
3394 #ifdef FEAT_WILDMENU
3396 win_redr_status_matches(xp
, xp
->xp_numfiles
, xp
->xp_files
,
3397 findex
, cmd_showtail
);
3400 return vim_strsave(orig_save
);
3401 return vim_strsave(xp
->xp_files
[findex
]);
3407 /* free old names */
3408 if (xp
->xp_numfiles
!= -1 && mode
!= WILD_ALL
&& mode
!= WILD_LONGEST
)
3410 FreeWild(xp
->xp_numfiles
, xp
->xp_files
);
3411 xp
->xp_numfiles
= -1;
3412 vim_free(orig_save
);
3417 if (mode
== WILD_FREE
) /* only release file name */
3420 if (xp
->xp_numfiles
== -1)
3422 vim_free(orig_save
);
3428 if (ExpandFromContext(xp
, str
, &xp
->xp_numfiles
, &xp
->xp_files
,
3431 #ifdef FNAME_ILLEGAL
3432 /* Illegal file name has been silently skipped. But when there
3433 * are wildcards, the real problem is that there was no match,
3434 * causing the pattern to be added, which has illegal characters.
3436 if (!(options
& WILD_SILENT
) && (options
& WILD_LIST_NOTFOUND
))
3437 EMSG2(_(e_nomatch2
), str
);
3440 else if (xp
->xp_numfiles
== 0)
3442 if (!(options
& WILD_SILENT
))
3443 EMSG2(_(e_nomatch2
), str
);
3447 /* Escape the matches for use on the command line. */
3448 ExpandEscape(xp
, str
, xp
->xp_numfiles
, xp
->xp_files
, options
);
3451 * Check for matching suffixes in file names.
3453 if (mode
!= WILD_ALL
&& mode
!= WILD_LONGEST
)
3455 if (xp
->xp_numfiles
)
3456 non_suf_match
= xp
->xp_numfiles
;
3459 if ((xp
->xp_context
== EXPAND_FILES
3460 || xp
->xp_context
== EXPAND_DIRECTORIES
)
3461 && xp
->xp_numfiles
> 1)
3464 * More than one match; check suffix.
3465 * The files will have been sorted on matching suffix in
3466 * expand_wildcards, only need to check the first two.
3469 for (i
= 0; i
< 2; ++i
)
3470 if (match_suffix(xp
->xp_files
[i
]))
3473 if (non_suf_match
!= 1)
3475 /* Can we ever get here unless it's while expanding
3476 * interactively? If not, we can get rid of this all
3477 * together. Don't really want to wait for this message
3478 * (and possibly have to hit return to continue!).
3480 if (!(options
& WILD_SILENT
))
3482 else if (!(options
& WILD_NO_BEEP
))
3485 if (!(non_suf_match
!= 1 && mode
== WILD_EXPAND_FREE
))
3486 ss
= vim_strsave(xp
->xp_files
[0]);
3491 /* Find longest common part */
3492 if (mode
== WILD_LONGEST
&& xp
->xp_numfiles
> 0)
3494 for (len
= 0; xp
->xp_files
[0][len
]; ++len
)
3496 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3498 #ifdef CASE_INSENSITIVE_FILENAME
3499 if (xp
->xp_context
== EXPAND_DIRECTORIES
3500 || xp
->xp_context
== EXPAND_FILES
3501 || xp
->xp_context
== EXPAND_SHELLCMD
3502 || xp
->xp_context
== EXPAND_BUFFERS
)
3504 if (TOLOWER_LOC(xp
->xp_files
[i
][len
]) !=
3505 TOLOWER_LOC(xp
->xp_files
[0][len
]))
3510 if (xp
->xp_files
[i
][len
] != xp
->xp_files
[0][len
])
3513 if (i
< xp
->xp_numfiles
)
3515 if (!(options
& WILD_NO_BEEP
))
3520 ss
= alloc((unsigned)len
+ 1);
3522 vim_strncpy(ss
, xp
->xp_files
[0], (size_t)len
);
3523 findex
= -1; /* next p_wc gets first one */
3526 /* Concatenate all matching names */
3527 if (mode
== WILD_ALL
&& xp
->xp_numfiles
> 0)
3530 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3531 len
+= (long_u
)STRLEN(xp
->xp_files
[i
]) + 1;
3532 ss
= lalloc(len
, TRUE
);
3536 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3538 STRCAT(ss
, xp
->xp_files
[i
]);
3539 if (i
!= xp
->xp_numfiles
- 1)
3540 STRCAT(ss
, (options
& WILD_USE_NL
) ? "\n" : " ");
3545 if (mode
== WILD_EXPAND_FREE
|| mode
== WILD_ALL
)
3548 /* Free "orig" if it wasn't stored in "orig_save". */
3549 if (orig
!= orig_save
)
3556 * Prepare an expand structure for use.
3562 xp
->xp_backslash
= XP_BS_NONE
;
3563 #ifndef BACKSLASH_IN_FILENAME
3564 xp
->xp_shell
= FALSE
;
3566 xp
->xp_numfiles
= -1;
3567 xp
->xp_files
= NULL
;
3568 #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3574 * Cleanup an expand structure after use.
3580 if (xp
->xp_numfiles
>= 0)
3582 FreeWild(xp
->xp_numfiles
, xp
->xp_files
);
3583 xp
->xp_numfiles
= -1;
3588 ExpandEscape(xp
, str
, numfiles
, files
, options
)
3599 * May change home directory back to "~"
3601 if (options
& WILD_HOME_REPLACE
)
3602 tilde_replace(str
, numfiles
, files
);
3604 if (options
& WILD_ESCAPE
)
3606 if (xp
->xp_context
== EXPAND_FILES
3607 || xp
->xp_context
== EXPAND_SHELLCMD
3608 || xp
->xp_context
== EXPAND_BUFFERS
3609 || xp
->xp_context
== EXPAND_DIRECTORIES
)
3612 * Insert a backslash into a file name before a space, \, %, #
3613 * and wildmatch characters, except '~'.
3615 for (i
= 0; i
< numfiles
; ++i
)
3617 /* for ":set path=" we need to escape spaces twice */
3618 if (xp
->xp_backslash
== XP_BS_THREE
)
3620 p
= vim_strsave_escaped(files
[i
], (char_u
*)" ");
3625 #if defined(BACKSLASH_IN_FILENAME)
3626 p
= vim_strsave_escaped(files
[i
], (char_u
*)" ");
3635 #ifdef BACKSLASH_IN_FILENAME
3640 /* Don't escape '[' and '{' if they are in 'isfname'. */
3641 for (p
= PATH_ESC_CHARS
; *p
!= NUL
; ++p
)
3642 if ((*p
!= '[' && *p
!= '{') || !vim_isfilec(*p
))
3645 p
= vim_strsave_escaped(files
[i
], buf
);
3648 p
= vim_strsave_escaped(files
[i
],
3649 xp
->xp_shell
? SHELL_ESC_CHARS
: PATH_ESC_CHARS
);
3657 /* If 'str' starts with "\~", replace "~" at start of
3658 * files[i] with "\~". */
3659 if (str
[0] == '\\' && str
[1] == '~' && files
[i
][0] == '~')
3660 escape_fname(&files
[i
]);
3662 xp
->xp_backslash
= XP_BS_NONE
;
3664 /* If the first file starts with a '+' escape it. Otherwise it
3665 * could be seen as "+cmd". */
3666 if (*files
[0] == '+')
3667 escape_fname(&files
[0]);
3669 else if (xp
->xp_context
== EXPAND_TAGS
)
3672 * Insert a backslash before characters in a tag name that
3673 * would terminate the ":tag" command.
3675 for (i
= 0; i
< numfiles
; ++i
)
3677 p
= vim_strsave_escaped(files
[i
], (char_u
*)"\\|\"");
3689 * Put a backslash before the file name in "pp", which is in allocated memory.
3697 p
= alloc((unsigned)(STRLEN(*pp
) + 2));
3708 * For each file name in files[num_files]:
3709 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3712 tilde_replace(orig_pat
, num_files
, files
)
3720 if (orig_pat
[0] == '~' && vim_ispathsep(orig_pat
[1]))
3722 for (i
= 0; i
< num_files
; ++i
)
3724 p
= home_replace_save(NULL
, files
[i
]);
3735 * Show all matches for completion on the command line.
3736 * Returns EXPAND_NOTHING when the character that triggered expansion should
3737 * be inserted like a normal character.
3741 showmatches(xp
, wildmenu
)
3745 #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3747 char_u
**files_found
;
3757 if (xp
->xp_numfiles
== -1)
3759 set_expand_context(xp
);
3760 i
= expand_cmdline(xp
, ccline
.cmdbuff
, ccline
.cmdpos
,
3761 &num_files
, &files_found
);
3762 showtail
= expand_showtail(xp
);
3769 num_files
= xp
->xp_numfiles
;
3770 files_found
= xp
->xp_files
;
3771 showtail
= cmd_showtail
;
3774 #ifdef FEAT_WILDMENU
3778 msg_didany
= FALSE
; /* lines_left will be set */
3779 msg_start(); /* prepare for paging */
3782 cmdline_row
= msg_row
;
3783 msg_didany
= FALSE
; /* lines_left will be set again */
3784 msg_start(); /* prepare for paging */
3785 #ifdef FEAT_WILDMENU
3790 got_int
= FALSE
; /* only int. the completion, not the cmd line */
3791 #ifdef FEAT_WILDMENU
3793 win_redr_status_matches(xp
, num_files
, files_found
, 0, showtail
);
3797 /* find the length of the longest file name */
3799 for (i
= 0; i
< num_files
; ++i
)
3801 if (!showtail
&& (xp
->xp_context
== EXPAND_FILES
3802 || xp
->xp_context
== EXPAND_SHELLCMD
3803 || xp
->xp_context
== EXPAND_BUFFERS
))
3805 home_replace(NULL
, files_found
[i
], NameBuff
, MAXPATHL
, TRUE
);
3806 j
= vim_strsize(NameBuff
);
3809 j
= vim_strsize(L_SHOWFILE(i
));
3814 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3818 /* compute the number of columns and lines for the listing */
3819 maxlen
+= 2; /* two spaces between file names */
3820 columns
= ((int)Columns
+ 2) / maxlen
;
3823 lines
= (num_files
+ columns
- 1) / columns
;
3826 attr
= hl_attr(HLF_D
); /* find out highlighting for directories */
3828 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3830 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T
));
3832 msg_advance(maxlen
- 3);
3833 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T
));
3836 /* list the files line by line */
3837 for (i
= 0; i
< lines
; ++i
)
3840 for (k
= i
; k
< num_files
; k
+= lines
)
3842 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3844 msg_outtrans_attr(files_found
[k
], hl_attr(HLF_D
));
3845 p
= files_found
[k
] + STRLEN(files_found
[k
]) + 1;
3846 msg_advance(maxlen
+ 1);
3848 msg_advance(maxlen
+ 3);
3849 msg_puts_long_attr(p
+ 2, hl_attr(HLF_D
));
3852 for (j
= maxlen
- lastlen
; --j
>= 0; )
3854 if (xp
->xp_context
== EXPAND_FILES
3855 || xp
->xp_context
== EXPAND_SHELLCMD
3856 || xp
->xp_context
== EXPAND_BUFFERS
)
3858 /* highlight directories */
3859 j
= (mch_isdir(files_found
[k
]));
3864 home_replace(NULL
, files_found
[k
], NameBuff
, MAXPATHL
,
3874 lastlen
= msg_outtrans_attr(p
, j
? attr
: 0);
3876 if (msg_col
> 0) /* when not wrapped around */
3881 out_flush(); /* show one line at a time */
3890 * we redraw the command below the lines that we have just listed
3891 * This is a bit tricky, but it saves a lot of screen updating.
3893 cmdline_row
= msg_row
; /* will put it back later */
3896 if (xp
->xp_numfiles
== -1)
3897 FreeWild(num_files
, files_found
);
3903 * Private gettail for showmatches() (and win_redr_status_matches()):
3904 * Find tail of file name path, but ignore trailing "/".
3912 int had_sep
= FALSE
;
3914 for (p
= s
; *p
!= NUL
; )
3916 if (vim_ispathsep(*p
)
3917 #ifdef BACKSLASH_IN_FILENAME
3918 && !rem_backslash(p
)
3933 * Return TRUE if we only need to show the tail of completion matches.
3934 * When not completing file names or there is a wildcard in the path FALSE is
3944 /* When not completing file names a "/" may mean something different. */
3945 if (xp
->xp_context
!= EXPAND_FILES
3946 && xp
->xp_context
!= EXPAND_SHELLCMD
3947 && xp
->xp_context
!= EXPAND_DIRECTORIES
)
3950 end
= gettail(xp
->xp_pattern
);
3951 if (end
== xp
->xp_pattern
) /* there is no path separator */
3954 for (s
= xp
->xp_pattern
; s
< end
; s
++)
3956 /* Skip escaped wildcards. Only when the backslash is not a path
3957 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3958 if (rem_backslash(s
))
3960 else if (vim_strchr((char_u
*)"*?[", *s
) != NULL
)
3967 * Prepare a string for expansion.
3968 * When expanding file names: The string will be used with expand_wildcards().
3969 * Copy the file name into allocated memory and add a '*' at the end.
3970 * When expanding other names: The string will be used with regcomp(). Copy
3971 * the name into allocated memory and prepend "^".
3974 addstar(fname
, len
, context
)
3977 int context
; /* EXPAND_FILES etc. */
3984 if (context
!= EXPAND_FILES
3985 && context
!= EXPAND_SHELLCMD
3986 && context
!= EXPAND_DIRECTORIES
)
3989 * Matching will be done internally (on something other than files).
3990 * So we convert the file-matching-type wildcards into our kind for
3991 * use with vim_regcomp(). First work out how long it will be:
3994 /* For help tags the translation is done in find_help_tags().
3995 * For a tag pattern starting with "/" no translation is needed. */
3996 if (context
== EXPAND_HELP
3997 || context
== EXPAND_COLORS
3998 || context
== EXPAND_COMPILER
3999 || (context
== EXPAND_TAGS
&& fname
[0] == '/'))
4000 retval
= vim_strnsave(fname
, len
);
4003 new_len
= len
+ 2; /* +2 for '^' at start, NUL at end */
4004 for (i
= 0; i
< len
; i
++)
4006 if (fname
[i
] == '*' || fname
[i
] == '~')
4007 new_len
++; /* '*' needs to be replaced by ".*"
4008 '~' needs to be replaced by "\~" */
4010 /* Buffer names are like file names. "." should be literal */
4011 if (context
== EXPAND_BUFFERS
&& fname
[i
] == '.')
4012 new_len
++; /* "." becomes "\." */
4014 /* Custom expansion takes care of special things, match
4015 * backslashes literally (perhaps also for other types?) */
4016 if ((context
== EXPAND_USER_DEFINED
4017 || context
== EXPAND_USER_LIST
) && fname
[i
] == '\\')
4018 new_len
++; /* '\' becomes "\\" */
4020 retval
= alloc(new_len
);
4025 for (i
= 0; i
< len
; i
++, j
++)
4027 /* Skip backslash. But why? At least keep it for custom
4029 if (context
!= EXPAND_USER_DEFINED
4030 && context
!= EXPAND_USER_LIST
4037 case '*': retval
[j
++] = '.';
4039 case '~': retval
[j
++] = '\\';
4041 case '?': retval
[j
] = '.';
4043 case '.': if (context
== EXPAND_BUFFERS
)
4046 case '\\': if (context
== EXPAND_USER_DEFINED
4047 || context
== EXPAND_USER_LIST
)
4051 retval
[j
] = fname
[i
];
4059 retval
= alloc(len
+ 4);
4062 vim_strncpy(retval
, fname
, len
);
4065 * Don't add a star to *, ~, ~user, $var or `cmd`.
4066 * * would become **, which walks the whole tree.
4067 * ~ would be at the start of the file name, but not the tail.
4068 * $ could be anywhere in the tail.
4069 * ` could be anywhere in the file name.
4071 tail
= gettail(retval
);
4072 if ((*retval
!= '~' || tail
!= retval
)
4073 && (len
== 0 || retval
[len
- 1] != '*')
4074 && vim_strchr(tail
, '$') == NULL
4075 && vim_strchr(retval
, '`') == NULL
)
4076 retval
[len
++] = '*';
4084 * Must parse the command line so far to work out what context we are in.
4085 * Completion can then be done based on that context.
4086 * This routine sets the variables:
4087 * xp->xp_pattern The start of the pattern to be expanded within
4088 * the command line (ends at the cursor).
4089 * xp->xp_context The type of thing to expand. Will be one of:
4091 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4092 * the command line, like an unknown command. Caller
4094 * EXPAND_NOTHING Unrecognised context for completion, use char like
4095 * a normal char, rather than for completion. eg
4097 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4099 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4100 * EXPAND_FILES After command with XFILE set, or after setting
4101 * with P_EXPAND set. eg :e ^I, :w>>^I
4102 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4103 * when we know only directories are of interest. eg
4105 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
4106 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4107 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4108 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4109 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4110 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4111 * EXPAND_EVENTS Complete event names
4112 * EXPAND_SYNTAX Complete :syntax command arguments
4113 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4114 * EXPAND_AUGROUP Complete autocommand group names
4115 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4116 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4117 * eg :unmap a^I , :cunab x^I
4118 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4120 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4121 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4122 * names in expressions, eg :while s^I
4123 * EXPAND_ENV_VARS Complete environment variable names
4126 set_expand_context(xp
)
4129 /* only expansion for ':', '>' and '=' command-lines */
4130 if (ccline
.cmdfirstc
!= ':'
4132 && ccline
.cmdfirstc
!= '>' && ccline
.cmdfirstc
!= '='
4137 xp
->xp_context
= EXPAND_NOTHING
;
4140 set_cmd_context(xp
, ccline
.cmdbuff
, ccline
.cmdlen
, ccline
.cmdpos
);
4144 set_cmd_context(xp
, str
, len
, col
)
4146 char_u
*str
; /* start of command line */
4147 int len
; /* length of command line (excl. NUL) */
4148 int col
; /* position of cursor */
4154 * Avoid a UMR warning from Purify, only save the character if it has been
4158 old_char
= str
[col
];
4163 if (ccline
.cmdfirstc
== '=')
4165 # ifdef FEAT_CMDL_COMPL
4166 /* pass CMD_SIZE because there is no real command */
4167 set_context_for_expression(xp
, str
, CMD_SIZE
);
4170 else if (ccline
.input_fn
)
4172 xp
->xp_context
= ccline
.xp_context
;
4173 xp
->xp_pattern
= ccline
.cmdbuff
;
4174 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
4175 xp
->xp_arg
= ccline
.xp_arg
;
4180 while (nextcomm
!= NULL
)
4181 nextcomm
= set_one_cmd_context(xp
, nextcomm
);
4183 str
[col
] = old_char
;
4187 * Expand the command line "str" from context "xp".
4188 * "xp" must have been set by set_cmd_context().
4189 * xp->xp_pattern points into "str", to where the text that is to be expanded
4191 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4193 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4194 * key that triggered expansion literally.
4195 * Returns EXPAND_OK otherwise.
4198 expand_cmdline(xp
, str
, col
, matchcount
, matches
)
4200 char_u
*str
; /* start of command line */
4201 int col
; /* position of cursor */
4202 int *matchcount
; /* return: nr of matches */
4203 char_u
***matches
; /* return: array of pointers to matches */
4205 char_u
*file_str
= NULL
;
4207 if (xp
->xp_context
== EXPAND_UNSUCCESSFUL
)
4210 return EXPAND_UNSUCCESSFUL
; /* Something illegal on command line */
4212 if (xp
->xp_context
== EXPAND_NOTHING
)
4214 /* Caller can use the character as a normal char instead */
4215 return EXPAND_NOTHING
;
4218 /* add star to file name, or convert to regexp if not exp. files. */
4219 file_str
= addstar(xp
->xp_pattern
,
4220 (int)(str
+ col
- xp
->xp_pattern
), xp
->xp_context
);
4221 if (file_str
== NULL
)
4222 return EXPAND_UNSUCCESSFUL
;
4224 /* find all files that match the description */
4225 if (ExpandFromContext(xp
, file_str
, matchcount
, matches
,
4226 WILD_ADD_SLASH
|WILD_SILENT
) == FAIL
)
4236 #ifdef FEAT_MULTI_LANG
4238 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4240 static void cleanup_help_tags
__ARGS((int num_file
, char_u
**file
));
4243 cleanup_help_tags(num_file
, file
)
4250 for (i
= 0; i
< num_file
; ++i
)
4252 len
= (int)STRLEN(file
[i
]) - 3;
4253 if (len
> 0 && STRCMP(file
[i
] + len
, "@en") == 0)
4255 /* Sorting on priority means the same item in another language may
4256 * be anywhere. Search all items for a match up to the "@en". */
4257 for (j
= 0; j
< num_file
; ++j
)
4259 && (int)STRLEN(file
[j
]) == len
+ 3
4260 && STRNCMP(file
[i
], file
[j
], len
+ 1) == 0)
4270 * Do the expansion based on xp->xp_context and "pat".
4273 ExpandFromContext(xp
, pat
, num_file
, file
, options
)
4280 #ifdef FEAT_CMDL_COMPL
4281 regmatch_T regmatch
;
4286 flags
= EW_DIR
; /* include directories */
4287 if (options
& WILD_LIST_NOTFOUND
)
4288 flags
|= EW_NOTFOUND
;
4289 if (options
& WILD_ADD_SLASH
)
4290 flags
|= EW_ADDSLASH
;
4291 if (options
& WILD_KEEP_ALL
)
4292 flags
|= EW_KEEPALL
;
4293 if (options
& WILD_SILENT
)
4296 if (xp
->xp_context
== EXPAND_FILES
|| xp
->xp_context
== EXPAND_DIRECTORIES
)
4299 * Expand file or directory names.
4301 int free_pat
= FALSE
;
4304 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4306 if (xp
->xp_backslash
!= XP_BS_NONE
)
4309 pat
= vim_strsave(pat
);
4310 for (i
= 0; pat
[i
]; ++i
)
4313 if (xp
->xp_backslash
== XP_BS_THREE
4314 && pat
[i
+ 1] == '\\'
4315 && pat
[i
+ 2] == '\\'
4316 && pat
[i
+ 3] == ' ')
4317 mch_memmove(pat
+ i
, pat
+ i
+ 3,
4318 STRLEN(pat
+ i
+ 3) + 1);
4319 if (xp
->xp_backslash
== XP_BS_ONE
4320 && pat
[i
+ 1] == ' ')
4321 mch_memmove(pat
+ i
, pat
+ i
+ 1, STRLEN(pat
+ i
));
4325 if (xp
->xp_context
== EXPAND_FILES
)
4328 flags
= (flags
| EW_DIR
) & ~EW_FILE
;
4329 ret
= expand_wildcards(1, &pat
, num_file
, file
, flags
);
4335 *file
= (char_u
**)"";
4337 if (xp
->xp_context
== EXPAND_HELP
)
4339 if (find_help_tags(pat
, num_file
, file
, FALSE
) == OK
)
4341 #ifdef FEAT_MULTI_LANG
4342 cleanup_help_tags(*num_file
, *file
);
4349 #ifndef FEAT_CMDL_COMPL
4352 if (xp
->xp_context
== EXPAND_SHELLCMD
)
4353 return expand_shellcmd(pat
, num_file
, file
, flags
);
4354 if (xp
->xp_context
== EXPAND_OLD_SETTING
)
4355 return ExpandOldSetting(num_file
, file
);
4356 if (xp
->xp_context
== EXPAND_BUFFERS
)
4357 return ExpandBufnames(pat
, num_file
, file
, options
);
4358 if (xp
->xp_context
== EXPAND_TAGS
4359 || xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
4360 return expand_tags(xp
->xp_context
== EXPAND_TAGS
, pat
, num_file
, file
);
4361 if (xp
->xp_context
== EXPAND_COLORS
)
4362 return ExpandRTDir(pat
, num_file
, file
, "colors");
4363 if (xp
->xp_context
== EXPAND_COMPILER
)
4364 return ExpandRTDir(pat
, num_file
, file
, "compiler");
4365 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4366 if (xp
->xp_context
== EXPAND_USER_LIST
)
4367 return ExpandUserList(xp
, num_file
, file
);
4370 regmatch
.regprog
= vim_regcomp(pat
, p_magic
? RE_MAGIC
: 0);
4371 if (regmatch
.regprog
== NULL
)
4374 /* set ignore-case according to p_ic, p_scs and pat */
4375 regmatch
.rm_ic
= ignorecase(pat
);
4377 if (xp
->xp_context
== EXPAND_SETTINGS
4378 || xp
->xp_context
== EXPAND_BOOL_SETTINGS
)
4379 ret
= ExpandSettings(xp
, ®match
, num_file
, file
);
4380 else if (xp
->xp_context
== EXPAND_MAPPINGS
)
4381 ret
= ExpandMappings(®match
, num_file
, file
);
4382 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4383 else if (xp
->xp_context
== EXPAND_USER_DEFINED
)
4384 ret
= ExpandUserDefined(xp
, ®match
, num_file
, file
);
4388 static struct expgen
4391 char_u
*((*func
)__ARGS((expand_T
*, int)));
4395 {EXPAND_COMMANDS
, get_command_name
, FALSE
},
4396 #ifdef FEAT_USR_CMDS
4397 {EXPAND_USER_COMMANDS
, get_user_commands
, FALSE
},
4398 {EXPAND_USER_CMD_FLAGS
, get_user_cmd_flags
, FALSE
},
4399 {EXPAND_USER_NARGS
, get_user_cmd_nargs
, FALSE
},
4400 {EXPAND_USER_COMPLETE
, get_user_cmd_complete
, FALSE
},
4403 {EXPAND_USER_VARS
, get_user_var_name
, FALSE
},
4404 {EXPAND_FUNCTIONS
, get_function_name
, FALSE
},
4405 {EXPAND_USER_FUNC
, get_user_func_name
, FALSE
},
4406 {EXPAND_EXPRESSION
, get_expr_name
, FALSE
},
4409 {EXPAND_MENUS
, get_menu_name
, FALSE
},
4410 {EXPAND_MENUNAMES
, get_menu_names
, FALSE
},
4413 {EXPAND_SYNTAX
, get_syntax_name
, TRUE
},
4415 {EXPAND_HIGHLIGHT
, get_highlight_name
, TRUE
},
4417 {EXPAND_EVENTS
, get_event_name
, TRUE
},
4418 {EXPAND_AUGROUP
, get_augroup_name
, TRUE
},
4420 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4421 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4422 {EXPAND_LANGUAGE
, get_lang_arg
, TRUE
},
4424 {EXPAND_ENV_VARS
, get_env_name
, TRUE
},
4429 * Find a context in the table and call the ExpandGeneric() with the
4430 * right function to do the expansion.
4433 for (i
= 0; i
< sizeof(tab
) / sizeof(struct expgen
); ++i
)
4434 if (xp
->xp_context
== tab
[i
].context
)
4437 regmatch
.rm_ic
= TRUE
;
4438 ret
= ExpandGeneric(xp
, ®match
, num_file
, file
, tab
[i
].func
);
4443 vim_free(regmatch
.regprog
);
4446 #endif /* FEAT_CMDL_COMPL */
4449 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4451 * Expand a list of names.
4453 * Generic function for command line completion. It calls a function to
4454 * obtain strings, one by one. The strings are matched against a regexp
4455 * program. Matching strings are copied into an array, which is returned.
4457 * Returns OK when no problems encountered, FAIL for error (out of memory).
4460 ExpandGeneric(xp
, regmatch
, num_file
, file
, func
)
4462 regmatch_T
*regmatch
;
4465 char_u
*((*func
)__ARGS((expand_T
*, int)));
4466 /* returns a string from the list */
4473 /* do this loop twice:
4474 * round == 0: count the number of matching names
4475 * round == 1: copy the matching names into allocated memory
4477 for (round
= 0; round
<= 1; ++round
)
4481 str
= (*func
)(xp
, i
);
4482 if (str
== NULL
) /* end of list */
4484 if (*str
== NUL
) /* skip empty strings */
4487 if (vim_regexec(regmatch
, str
, (colnr_T
)0))
4491 str
= vim_strsave_escaped(str
, (char_u
*)" \t\\.");
4492 (*file
)[count
] = str
;
4494 if (func
== get_menu_names
&& str
!= NULL
)
4496 /* test for separator added by get_menu_names() */
4497 str
+= STRLEN(str
) - 1;
4511 *file
= (char_u
**)alloc((unsigned)(count
* sizeof(char_u
*)));
4514 *file
= (char_u
**)"";
4521 /* Sort the results. Keep menu's in the specified order. */
4522 if (xp
->xp_context
!= EXPAND_MENUNAMES
&& xp
->xp_context
!= EXPAND_MENUS
)
4523 sort_strings(*file
, *num_file
);
4525 #ifdef FEAT_CMDL_COMPL
4526 /* Reset the variables used for special highlight names expansion, so that
4527 * they don't show up when getting normal highlight names by ID. */
4528 reset_expand_highlight();
4535 * Complete a shell command.
4536 * Returns FAIL or OK;
4539 expand_shellcmd(filepat
, num_file
, file
, flagsarg
)
4540 char_u
*filepat
; /* pattern to match with command names */
4541 int *num_file
; /* return: number of matches */
4542 char_u
***file
; /* return: array with matches */
4543 int flagsarg
; /* EW_ flags */
4548 int mustfree
= FALSE
;
4550 char_u
*buf
= alloc(MAXPATHL
);
4553 int flags
= flagsarg
;
4559 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4561 pat
= vim_strsave(filepat
);
4562 for (i
= 0; pat
[i
]; ++i
)
4563 if (pat
[i
] == '\\' && pat
[i
+ 1] == ' ')
4564 mch_memmove(pat
+ i
, pat
+ i
+ 1, STRLEN(pat
+ i
));
4566 flags
|= EW_FILE
| EW_EXEC
;
4568 /* For an absolute name we don't use $PATH. */
4569 if (mch_isFullName(pat
))
4570 path
= (char_u
*)" ";
4571 else if ((pat
[0] == '.' && (vim_ispathsep(pat
[1])
4572 || (pat
[1] == '.' && vim_ispathsep(pat
[2])))))
4573 path
= (char_u
*)".";
4575 path
= vim_getenv((char_u
*)"PATH", &mustfree
);
4578 * Go over all directories in $PATH. Expand matches in that directory and
4579 * collect them in "ga".
4581 ga_init2(&ga
, (int)sizeof(char *), 10);
4582 for (s
= path
; *s
!= NUL
; s
= e
)
4585 ++s
; /* Skip space used for absolute path name. */
4587 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4588 e
= vim_strchr(s
, ';');
4590 e
= vim_strchr(s
, ':');
4596 if (l
> MAXPATHL
- 5)
4598 vim_strncpy(buf
, s
, l
);
4601 vim_strncpy(buf
+ l
, pat
, MAXPATHL
- 1 - l
);
4603 /* Expand matches in one directory of $PATH. */
4604 ret
= expand_wildcards(1, &buf
, num_file
, file
, flags
);
4607 if (ga_grow(&ga
, *num_file
) == FAIL
)
4608 FreeWild(*num_file
, *file
);
4611 for (i
= 0; i
< *num_file
; ++i
)
4616 /* Remove the path again. */
4617 mch_memmove(s
, s
+ l
, STRLEN(s
+ l
) + 1);
4618 ((char_u
**)ga
.ga_data
)[ga
.ga_len
++] = s
;
4630 *num_file
= ga
.ga_len
;
4640 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4641 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
));
4644 * call "user_expand_func()" to invoke a user defined VimL function and return
4645 * the result (either a string or a List).
4648 call_user_expand_func(user_expand_func
, xp
, num_file
, file
)
4649 void *(*user_expand_func
) __ARGS((char_u
*, int, char_u
**, int));
4657 int save_current_SID
= current_SID
;
4659 struct cmdline_info save_ccline
;
4661 if (xp
->xp_arg
== NULL
|| xp
->xp_arg
[0] == '\0')
4666 keep
= ccline
.cmdbuff
[ccline
.cmdlen
];
4667 ccline
.cmdbuff
[ccline
.cmdlen
] = 0;
4668 sprintf((char *)num
, "%d", ccline
.cmdpos
);
4669 args
[0] = xp
->xp_pattern
;
4670 args
[1] = ccline
.cmdbuff
;
4673 /* Save the cmdline, we don't know what the function may do. */
4674 save_ccline
= ccline
;
4675 ccline
.cmdbuff
= NULL
;
4676 ccline
.cmdprompt
= NULL
;
4677 current_SID
= xp
->xp_scriptID
;
4679 ret
= user_expand_func(xp
->xp_arg
, 3, args
, FALSE
);
4681 ccline
= save_ccline
;
4682 current_SID
= save_current_SID
;
4684 ccline
.cmdbuff
[ccline
.cmdlen
] = keep
;
4690 * Expand names with a function defined by the user.
4693 ExpandUserDefined(xp
, regmatch
, num_file
, file
)
4695 regmatch_T
*regmatch
;
4705 retstr
= call_user_expand_func(call_func_retstr
, xp
, num_file
, file
);
4709 ga_init2(&ga
, (int)sizeof(char *), 3);
4710 for (s
= retstr
; *s
!= NUL
; s
= e
)
4712 e
= vim_strchr(s
, '\n');
4718 if (xp
->xp_pattern
[0] && vim_regexec(regmatch
, s
, (colnr_T
)0) == 0)
4726 if (ga_grow(&ga
, 1) == FAIL
)
4729 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] = vim_strnsave(s
, (int)(e
- s
));
4738 *num_file
= ga
.ga_len
;
4743 * Expand names with a list returned by a function defined by the user.
4746 ExpandUserList(xp
, num_file
, file
)
4755 retlist
= call_user_expand_func(call_func_retlist
, xp
, num_file
, file
);
4756 if (retlist
== NULL
)
4759 ga_init2(&ga
, (int)sizeof(char *), 3);
4760 /* Loop over the items in the list. */
4761 for (li
= retlist
->lv_first
; li
!= NULL
; li
= li
->li_next
)
4763 if (li
->li_tv
.v_type
!= VAR_STRING
)
4764 continue; /* Skip non-string items */
4766 if (ga_grow(&ga
, 1) == FAIL
)
4769 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] =
4770 vim_strsave(li
->li_tv
.vval
.v_string
);
4773 list_unref(retlist
);
4776 *num_file
= ga
.ga_len
;
4782 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4783 * or compiler names.
4786 ExpandRTDir(pat
, num_file
, file
, dirname
)
4790 char *dirname
; /* "colors" or "compiler" */
4799 s
= alloc((unsigned)(STRLEN(pat
) + STRLEN(dirname
) + 7));
4802 sprintf((char *)s
, "%s/%s*.vim", dirname
, pat
);
4803 all
= globpath(p_rtp
, s
);
4808 ga_init2(&ga
, (int)sizeof(char *), 3);
4809 for (s
= all
; *s
!= NUL
; s
= e
)
4811 e
= vim_strchr(s
, '\n');
4814 if (ga_grow(&ga
, 1) == FAIL
)
4816 if (e
- 4 > s
&& STRNICMP(e
- 4, ".vim", 4) == 0)
4818 for (s
= e
- 4; s
> all
; mb_ptr_back(all
, s
))
4819 if (*s
== '\n' || vim_ispathsep(*s
))
4822 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] =
4823 vim_strnsave(s
, (int)(e
- s
- 4));
4831 *num_file
= ga
.ga_len
;
4837 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4839 * Expand "file" for all comma-separated directories in "path".
4840 * Returns an allocated string with all matches concatenated, separated by
4841 * newlines. Returns NULL for an error or no matches.
4844 globpath(path
, file
)
4857 buf
= alloc(MAXPATHL
);
4862 xpc
.xp_context
= EXPAND_FILES
;
4864 ga_init2(&ga
, 1, 100);
4866 /* Loop over all entries in {path}. */
4867 while (*path
!= NUL
)
4869 /* Copy one item of the path to buf[] and concatenate the file name. */
4870 copy_option_part(&path
, buf
, MAXPATHL
, ",");
4871 if (STRLEN(buf
) + STRLEN(file
) + 2 < MAXPATHL
)
4875 if (ExpandFromContext(&xpc
, buf
, &num_p
, &p
, WILD_SILENT
) != FAIL
4878 ExpandEscape(&xpc
, buf
, num_p
, p
, WILD_SILENT
);
4879 for (len
= 0, i
= 0; i
< num_p
; ++i
)
4880 len
+= (int)STRLEN(p
[i
]) + 1;
4882 /* Concatenate new results to previous ones. */
4883 if (ga_grow(&ga
, len
) == OK
)
4885 cur
= (char_u
*)ga
.ga_data
+ ga
.ga_len
;
4886 for (i
= 0; i
< num_p
; ++i
)
4889 cur
+= STRLEN(p
[i
]);
4899 *--cur
= 0; /* Replace trailing newline with NUL */
4902 return (char_u
*)ga
.ga_data
;
4907 #if defined(FEAT_CMDHIST) || defined(PROTO)
4909 /*********************************
4910 * Command line history stuff *
4911 *********************************/
4914 * Translate a history character to the associated type number.
4928 return HIST_SEARCH
; /* must be '?' or '/' */
4932 * Table of history names.
4933 * These names are used in :history and various hist...() functions.
4934 * It is sufficient to give the significant prefix of a history name.
4937 static char *(history_names
[]) =
4948 * init_history() - Initialize the command line history.
4949 * Also used to re-allocate the history when the size changes.
4954 int newlen
; /* new length of history table */
4961 * If size of history table changed, reallocate it
4964 if (newlen
!= hislen
) /* history length changed */
4966 for (type
= 0; type
< HIST_COUNT
; ++type
) /* adjust the tables */
4970 temp
= (histentry_T
*)lalloc(
4971 (long_u
)(newlen
* sizeof(histentry_T
)), TRUE
);
4972 if (temp
== NULL
) /* out of memory! */
4974 if (type
== 0) /* first one: just keep the old length */
4979 /* Already changed one table, now we can only have zero
4980 * length for all tables. */
4988 if (newlen
== 0 || temp
!= NULL
)
4990 if (hisidx
[type
] < 0) /* there are no entries yet */
4992 for (i
= 0; i
< newlen
; ++i
)
4995 temp
[i
].hisstr
= NULL
;
4998 else if (newlen
> hislen
) /* array becomes bigger */
5000 for (i
= 0; i
<= hisidx
[type
]; ++i
)
5001 temp
[i
] = history
[type
][i
];
5003 for ( ; i
<= newlen
- (hislen
- hisidx
[type
]); ++i
)
5006 temp
[i
].hisstr
= NULL
;
5008 for ( ; j
< hislen
; ++i
, ++j
)
5009 temp
[i
] = history
[type
][j
];
5011 else /* array becomes smaller or 0 */
5014 for (i
= newlen
- 1; ; --i
)
5016 if (i
>= 0) /* copy newest entries */
5017 temp
[i
] = history
[type
][j
];
5018 else /* remove older entries */
5019 vim_free(history
[type
][j
].hisstr
);
5022 if (j
== hisidx
[type
])
5025 hisidx
[type
] = newlen
- 1;
5027 vim_free(history
[type
]);
5028 history
[type
] = temp
;
5036 * Check if command line 'str' is already in history.
5037 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5040 in_history(type
, str
, move_to_front
)
5043 int move_to_front
; /* Move the entry to the front if it exists */
5048 if (hisidx
[type
] < 0)
5053 if (history
[type
][i
].hisstr
== NULL
)
5055 if (STRCMP(str
, history
[type
][i
].hisstr
) == 0)
5064 } while (i
!= hisidx
[type
]);
5068 str
= history
[type
][i
].hisstr
;
5069 while (i
!= hisidx
[type
])
5073 history
[type
][last_i
] = history
[type
][i
];
5076 history
[type
][i
].hisstr
= str
;
5077 history
[type
][i
].hisnum
= ++hisnum
[type
];
5084 * Convert history name (from table above) to its HIST_ equivalent.
5085 * When "name" is empty, return "cmd" history.
5086 * Returns -1 for unknown history name.
5093 int len
= (int)STRLEN(name
);
5095 /* No argument: use current history. */
5097 return hist_char2type(ccline
.cmdfirstc
);
5099 for (i
= 0; history_names
[i
] != NULL
; ++i
)
5100 if (STRNICMP(name
, history_names
[i
], len
) == 0)
5103 if (vim_strchr((char_u
*)":=@>?/", name
[0]) != NULL
&& name
[1] == NUL
)
5104 return hist_char2type(name
[0]);
5109 static int last_maptick
= -1; /* last seen maptick */
5112 * Add the given string to the given history. If the string is already in the
5113 * history then it is moved to the front. "histype" may be one of he HIST_
5117 add_to_history(histype
, new_entry
, in_map
, sep
)
5120 int in_map
; /* consider maptick when inside a mapping */
5121 int sep
; /* separator character used (search hist) */
5123 histentry_T
*hisptr
;
5126 if (hislen
== 0) /* no history */
5130 * Searches inside the same mapping overwrite each other, so that only
5131 * the last line is kept. Be careful not to remove a line that was moved
5132 * down, only lines that were added.
5134 if (histype
== HIST_SEARCH
&& in_map
)
5136 if (maptick
== last_maptick
)
5138 /* Current line is from the same mapping, remove it */
5139 hisptr
= &history
[HIST_SEARCH
][hisidx
[HIST_SEARCH
]];
5140 vim_free(hisptr
->hisstr
);
5141 hisptr
->hisstr
= NULL
;
5144 if (--hisidx
[HIST_SEARCH
] < 0)
5145 hisidx
[HIST_SEARCH
] = hislen
- 1;
5149 if (!in_history(histype
, new_entry
, TRUE
))
5151 if (++hisidx
[histype
] == hislen
)
5152 hisidx
[histype
] = 0;
5153 hisptr
= &history
[histype
][hisidx
[histype
]];
5154 vim_free(hisptr
->hisstr
);
5156 /* Store the separator after the NUL of the string. */
5157 len
= (int)STRLEN(new_entry
);
5158 hisptr
->hisstr
= vim_strnsave(new_entry
, len
+ 2);
5159 if (hisptr
->hisstr
!= NULL
)
5160 hisptr
->hisstr
[len
+ 1] = sep
;
5162 hisptr
->hisnum
= ++hisnum
[histype
];
5163 if (histype
== HIST_SEARCH
&& in_map
)
5164 last_maptick
= maptick
;
5168 #if defined(FEAT_EVAL) || defined(PROTO)
5171 * Get identifier of newest history entry.
5172 * "histype" may be one of the HIST_ values.
5175 get_history_idx(histype
)
5178 if (hislen
== 0 || histype
< 0 || histype
>= HIST_COUNT
5179 || hisidx
[histype
] < 0)
5182 return history
[histype
][hisidx
[histype
]].hisnum
;
5185 static struct cmdline_info
*get_ccline_ptr
__ARGS((void));
5188 * Get pointer to the command line info to use. cmdline_paste() may clear
5189 * ccline and put the previous value in prev_ccline.
5191 static struct cmdline_info
*
5194 if ((State
& CMDLINE
) == 0)
5196 if (ccline
.cmdbuff
!= NULL
)
5198 if (prev_ccline_used
&& prev_ccline
.cmdbuff
!= NULL
)
5199 return &prev_ccline
;
5204 * Get the current command line in allocated memory.
5205 * Only works when the command line is being edited.
5206 * Returns NULL when something is wrong.
5211 struct cmdline_info
*p
= get_ccline_ptr();
5215 return vim_strnsave(p
->cmdbuff
, p
->cmdlen
);
5219 * Get the current command line position, counted in bytes.
5220 * Zero is the first position.
5221 * Only works when the command line is being edited.
5222 * Returns -1 when something is wrong.
5227 struct cmdline_info
*p
= get_ccline_ptr();
5235 * Set the command line byte position to "pos". Zero is the first position.
5236 * Only works when the command line is being edited.
5237 * Returns 1 when failed, 0 when OK.
5240 set_cmdline_pos(pos
)
5243 struct cmdline_info
*p
= get_ccline_ptr();
5248 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5249 * changed the command line. */
5258 * Get the current command-line type.
5259 * Returns ':' or '/' or '?' or '@' or '>' or '-'
5260 * Only works when the command line is being edited.
5261 * Returns NUL when something is wrong.
5266 struct cmdline_info
*p
= get_ccline_ptr();
5270 if (p
->cmdfirstc
== NUL
)
5271 return (p
->input_fn
) ? '@' : '-';
5272 return p
->cmdfirstc
;
5276 * Calculate history index from a number:
5277 * num > 0: seen as identifying number of a history entry
5278 * num < 0: relative position in history wrt newest entry
5279 * "histype" may be one of the HIST_ values.
5282 calc_hist_idx(histype
, num
)
5288 int wrapped
= FALSE
;
5290 if (hislen
== 0 || histype
< 0 || histype
>= HIST_COUNT
5291 || (i
= hisidx
[histype
]) < 0 || num
== 0)
5294 hist
= history
[histype
];
5297 while (hist
[i
].hisnum
> num
)
5305 if (hist
[i
].hisnum
== num
&& hist
[i
].hisstr
!= NULL
)
5308 else if (-num
<= hislen
)
5313 if (hist
[i
].hisstr
!= NULL
)
5320 * Get a history entry by its index.
5321 * "histype" may be one of the HIST_ values.
5324 get_history_entry(histype
, idx
)
5328 idx
= calc_hist_idx(histype
, idx
);
5330 return history
[histype
][idx
].hisstr
;
5332 return (char_u
*)"";
5336 * Clear all entries of a history.
5337 * "histype" may be one of the HIST_ values.
5340 clr_history(histype
)
5344 histentry_T
*hisptr
;
5346 if (hislen
!= 0 && histype
>= 0 && histype
< HIST_COUNT
)
5348 hisptr
= history
[histype
];
5349 for (i
= hislen
; i
--;)
5351 vim_free(hisptr
->hisstr
);
5353 hisptr
++->hisstr
= NULL
;
5355 hisidx
[histype
] = -1; /* mark history as cleared */
5356 hisnum
[histype
] = 0; /* reset identifier counter */
5363 * Remove all entries matching {str} from a history.
5364 * "histype" may be one of the HIST_ values.
5367 del_history_entry(histype
, str
)
5371 regmatch_T regmatch
;
5372 histentry_T
*hisptr
;
5378 regmatch
.regprog
= NULL
;
5379 regmatch
.rm_ic
= FALSE
; /* always match case */
5382 && histype
< HIST_COUNT
5384 && (idx
= hisidx
[histype
]) >= 0
5385 && (regmatch
.regprog
= vim_regcomp(str
, RE_MAGIC
+ RE_STRING
))
5391 hisptr
= &history
[histype
][i
];
5392 if (hisptr
->hisstr
== NULL
)
5394 if (vim_regexec(®match
, hisptr
->hisstr
, (colnr_T
)0))
5397 vim_free(hisptr
->hisstr
);
5398 hisptr
->hisstr
= NULL
;
5405 history
[histype
][last
] = *hisptr
;
5406 hisptr
->hisstr
= NULL
;
5415 if (history
[histype
][idx
].hisstr
== NULL
)
5416 hisidx
[histype
] = -1;
5418 vim_free(regmatch
.regprog
);
5423 * Remove an indexed entry from a history.
5424 * "histype" may be one of the HIST_ values.
5427 del_history_idx(histype
, idx
)
5433 i
= calc_hist_idx(histype
, idx
);
5436 idx
= hisidx
[histype
];
5437 vim_free(history
[histype
][i
].hisstr
);
5439 /* When deleting the last added search string in a mapping, reset
5440 * last_maptick, so that the last added search string isn't deleted again.
5442 if (histype
== HIST_SEARCH
&& maptick
== last_maptick
&& i
== idx
)
5447 j
= (i
+ 1) % hislen
;
5448 history
[histype
][i
] = history
[histype
][j
];
5451 history
[histype
][i
].hisstr
= NULL
;
5452 history
[histype
][i
].hisnum
= 0;
5455 hisidx
[histype
] = i
;
5459 #endif /* FEAT_EVAL */
5461 #if defined(FEAT_CRYPT) || defined(PROTO)
5463 * Very specific function to remove the value in ":set key=val" from the
5467 remove_key_from_history()
5472 i
= hisidx
[HIST_CMD
];
5475 p
= history
[HIST_CMD
][i
].hisstr
;
5478 if (STRNCMP(p
, "key", 3) == 0 && !isalpha(p
[3]))
5480 p
= vim_strchr(p
+ 3, '=');
5484 for (i
= 0; p
[i
] && !vim_iswhite(p
[i
]); ++i
)
5485 if (p
[i
] == '\\' && p
[i
+ 1])
5487 mch_memmove(p
, p
+ i
, STRLEN(p
+ i
) + 1);
5493 #endif /* FEAT_CMDHIST */
5495 #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5497 * Get indices "num1,num2" that specify a range within a list (not a range of
5498 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5499 * Returns OK if parsed successfully, otherwise FAIL.
5502 get_list_range(str
, num1
, num2
)
5511 *str
= skipwhite(*str
);
5512 if (**str
== '-' || vim_isdigit(**str
)) /* parse "from" part of range */
5514 vim_str2nr(*str
, NULL
, &len
, FALSE
, FALSE
, &num
, NULL
);
5519 *str
= skipwhite(*str
);
5520 if (**str
== ',') /* parse "to" part of range */
5522 *str
= skipwhite(*str
+ 1);
5523 vim_str2nr(*str
, NULL
, &len
, FALSE
, FALSE
, &num
, NULL
);
5527 *str
= skipwhite(*str
+ len
);
5529 else if (!first
) /* no number given at all */
5532 else if (first
) /* only one number given */
5538 #if defined(FEAT_CMDHIST) || defined(PROTO)
5540 * :history command - print a history
5547 int histype1
= HIST_CMD
;
5548 int histype2
= HIST_CMD
;
5554 char_u
*arg
= eap
->arg
;
5558 MSG(_("'history' option is zero"));
5562 if (!(VIM_ISDIGIT(*arg
) || *arg
== '-' || *arg
== ','))
5565 while (ASCII_ISALPHA(*end
)
5566 || vim_strchr((char_u
*)":=@>/?", *end
) != NULL
)
5570 histype1
= get_histtype(arg
);
5573 if (STRICMP(arg
, "all") == 0)
5576 histype2
= HIST_COUNT
-1;
5581 EMSG(_(e_trailing
));
5586 histype2
= histype1
;
5591 if (!get_list_range(&end
, &hisidx1
, &hisidx2
) || *end
!= NUL
)
5593 EMSG(_(e_trailing
));
5597 for (; !got_int
&& histype1
<= histype2
; ++histype1
)
5599 STRCPY(IObuff
, "\n # ");
5600 STRCAT(STRCAT(IObuff
, history_names
[histype1
]), " history");
5601 MSG_PUTS_TITLE(IObuff
);
5602 idx
= hisidx
[histype1
];
5603 hist
= history
[histype1
];
5607 j
= (-j
> hislen
) ? 0 : hist
[(hislen
+j
+idx
+1) % hislen
].hisnum
;
5609 k
= (-k
> hislen
) ? 0 : hist
[(hislen
+k
+idx
+1) % hislen
].hisnum
;
5610 if (idx
>= 0 && j
<= k
)
5611 for (i
= idx
+ 1; !got_int
; ++i
)
5615 if (hist
[i
].hisstr
!= NULL
5616 && hist
[i
].hisnum
>= j
&& hist
[i
].hisnum
<= k
)
5619 sprintf((char *)IObuff
, "%c%6d ", i
== idx
? '>' : ' ',
5621 if (vim_strsize(hist
[i
].hisstr
) > (int)Columns
- 10)
5622 trunc_string(hist
[i
].hisstr
, IObuff
+ STRLEN(IObuff
),
5625 STRCAT(IObuff
, hist
[i
].hisstr
);
5626 msg_outtrans(IObuff
);
5636 #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5637 static char_u
**viminfo_history
[HIST_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
5638 static int viminfo_hisidx
[HIST_COUNT
] = {0, 0, 0, 0};
5639 static int viminfo_hislen
[HIST_COUNT
] = {0, 0, 0, 0};
5640 static int viminfo_add_at_front
= FALSE
;
5642 static int hist_type2char
__ARGS((int type
, int use_question
));
5645 * Translate a history type number to the associated character.
5648 hist_type2char(type
, use_question
)
5650 int use_question
; /* use '?' instead of '/' */
5652 if (type
== HIST_CMD
)
5654 if (type
== HIST_SEARCH
)
5661 if (type
== HIST_EXPR
)
5667 * Prepare for reading the history from the viminfo file.
5668 * This allocates history arrays to store the read history lines.
5671 prepare_viminfo_history(asklen
)
5680 viminfo_add_at_front
= (asklen
!= 0);
5681 if (asklen
> hislen
)
5684 for (type
= 0; type
< HIST_COUNT
; ++type
)
5687 * Count the number of empty spaces in the history list. If there are
5688 * more spaces available than we request, then fill them up.
5690 for (i
= 0, num
= 0; i
< hislen
; i
++)
5691 if (history
[type
][i
].hisstr
== NULL
)
5697 viminfo_history
[type
] = NULL
;
5699 viminfo_history
[type
] =
5700 (char_u
**)lalloc((long_u
)(len
* sizeof(char_u
*)), FALSE
);
5701 if (viminfo_history
[type
] == NULL
)
5703 viminfo_hislen
[type
] = len
;
5704 viminfo_hisidx
[type
] = 0;
5709 * Accept a line from the viminfo, store it in the history array when it's
5713 read_viminfo_history(virp
)
5721 type
= hist_char2type(virp
->vir_line
[0]);
5722 if (viminfo_hisidx
[type
] < viminfo_hislen
[type
])
5724 val
= viminfo_readstring(virp
, 1, TRUE
);
5725 if (val
!= NULL
&& *val
!= NUL
)
5727 if (!in_history(type
, val
+ (type
== HIST_SEARCH
),
5728 viminfo_add_at_front
))
5730 /* Need to re-allocate to append the separator byte. */
5732 p
= lalloc(len
+ 2, TRUE
);
5735 if (type
== HIST_SEARCH
)
5737 /* Search entry: Move the separator from the first
5738 * column to after the NUL. */
5739 mch_memmove(p
, val
+ 1, (size_t)len
);
5740 p
[len
] = (*val
== ' ' ? NUL
: *val
);
5744 /* Not a search entry: No separator in the viminfo
5745 * file, add a NUL separator. */
5746 mch_memmove(p
, val
, (size_t)len
+ 1);
5749 viminfo_history
[type
][viminfo_hisidx
[type
]++] = p
;
5755 return viminfo_readline(virp
);
5759 finish_viminfo_history()
5765 for (type
= 0; type
< HIST_COUNT
; ++type
)
5767 if (history
[type
] == NULL
)
5769 idx
= hisidx
[type
] + viminfo_hisidx
[type
];
5774 if (viminfo_add_at_front
)
5778 if (hisidx
[type
] == -1)
5779 hisidx
[type
] = hislen
- 1;
5782 if (history
[type
][idx
].hisstr
!= NULL
)
5784 if (++idx
== hislen
)
5786 } while (idx
!= hisidx
[type
]);
5787 if (idx
!= hisidx
[type
] && --idx
< 0)
5790 for (i
= 0; i
< viminfo_hisidx
[type
]; i
++)
5792 vim_free(history
[type
][idx
].hisstr
);
5793 history
[type
][idx
].hisstr
= viminfo_history
[type
][i
];
5799 for (i
= 0; i
< viminfo_hisidx
[type
]; i
++)
5801 history
[type
][idx
++].hisnum
= ++hisnum
[type
];
5804 vim_free(viminfo_history
[type
]);
5805 viminfo_history
[type
] = NULL
;
5810 write_viminfo_history(fp
)
5822 for (type
= 0; type
< HIST_COUNT
; ++type
)
5824 num_saved
= get_viminfo_parameter(hist_type2char(type
, FALSE
));
5827 if (num_saved
< 0) /* Use default */
5829 fprintf(fp
, _("\n# %s History (newest to oldest):\n"),
5830 type
== HIST_CMD
? _("Command Line") :
5831 type
== HIST_SEARCH
? _("Search String") :
5832 type
== HIST_EXPR
? _("Expression") :
5834 if (num_saved
> hislen
)
5840 p
= history
[type
][i
].hisstr
;
5843 fputc(hist_type2char(type
, TRUE
), fp
);
5844 /* For the search history: put the separator in the second
5845 * column; use a space if there isn't one. */
5846 if (type
== HIST_SEARCH
)
5848 c
= p
[STRLEN(p
) + 1];
5849 putc(c
== NUL
? ' ' : c
, fp
);
5851 viminfo_writestring(fp
, p
);
5858 #endif /* FEAT_VIMINFO */
5860 #if defined(FEAT_FKMAP) || defined(PROTO)
5862 * Write a character at the current cursor+offset position.
5863 * It is directly written into the command buffer block.
5866 cmd_pchar(c
, offset
)
5869 if (ccline
.cmdpos
+ offset
>= ccline
.cmdlen
|| ccline
.cmdpos
+ offset
< 0)
5871 EMSG(_("E198: cmd_pchar beyond the command length"));
5874 ccline
.cmdbuff
[ccline
.cmdpos
+ offset
] = (char_u
)c
;
5875 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
5882 if (ccline
.cmdpos
+ offset
>= ccline
.cmdlen
|| ccline
.cmdpos
+ offset
< 0)
5884 /* EMSG(_("cmd_gchar beyond the command length")); */
5887 return (int)ccline
.cmdbuff
[ccline
.cmdpos
+ offset
];
5891 #if defined(FEAT_CMDWIN) || defined(PROTO)
5893 * Open a window on the current command line and history. Allow editing in
5894 * the window. Returns when the window is closed.
5896 * CR if the command is to be executed
5897 * Ctrl_C if it is to be abandoned
5898 * K_IGNORE if editing continues
5903 struct cmdline_info save_ccline
;
5904 buf_T
*old_curbuf
= curbuf
;
5905 win_T
*old_curwin
= curwin
;
5913 int save_restart_edit
= restart_edit
;
5914 int save_State
= State
;
5915 int save_exmode
= exmode_active
;
5916 #ifdef FEAT_RIGHTLEFT
5917 int save_cmdmsg_rl
= cmdmsg_rl
;
5920 /* Can't do this recursively. Can't do it when typing a password. */
5921 if (cmdwin_type
!= 0
5922 # if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5931 /* Save current window sizes. */
5932 win_size_save(&winsizes
);
5934 # ifdef FEAT_AUTOCMD
5935 /* Don't execute autocommands while creating the window. */
5938 /* don't use a new tab page */
5941 /* Create a window for the command-line buffer. */
5942 if (win_split((int)p_cwh
, WSP_BOT
) == FAIL
)
5945 # ifdef FEAT_AUTOCMD
5950 cmdwin_type
= ccline
.cmdfirstc
;
5951 if (cmdwin_type
== NUL
)
5954 /* Create the command-line buffer empty. */
5955 (void)do_ecmd(0, NULL
, NULL
, NULL
, ECMD_ONE
, ECMD_HIDE
);
5956 (void)setfname(curbuf
, (char_u
*)"command-line", NULL
, TRUE
);
5957 set_option_value((char_u
*)"bt", 0L, (char_u
*)"nofile", OPT_LOCAL
);
5958 set_option_value((char_u
*)"swf", 0L, NULL
, OPT_LOCAL
);
5959 curbuf
->b_p_ma
= TRUE
;
5960 # ifdef FEAT_RIGHTLEFT
5961 curwin
->w_p_rl
= cmdmsg_rl
;
5964 # ifdef FEAT_SCROLLBIND
5965 curwin
->w_p_scb
= FALSE
;
5968 # ifdef FEAT_AUTOCMD
5969 /* Do execute autocommands for setting the filetype (load syntax). */
5973 /* Showing the prompt may have set need_wait_return, reset it. */
5974 need_wait_return
= FALSE
;
5976 histtype
= hist_char2type(ccline
.cmdfirstc
);
5977 if (histtype
== HIST_CMD
|| histtype
== HIST_DEBUG
)
5981 add_map((char_u
*)"<buffer> <Tab> <C-X><C-V>", INSERT
);
5982 add_map((char_u
*)"<buffer> <Tab> a<C-X><C-V>", NORMAL
);
5984 set_option_value((char_u
*)"ft", 0L, (char_u
*)"vim", OPT_LOCAL
);
5987 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5988 * sets 'textwidth' to 78). */
5991 /* Fill the buffer with the history. */
5995 i
= hisidx
[histtype
];
6003 if (history
[histtype
][i
].hisstr
!= NULL
)
6004 ml_append(lnum
++, history
[histtype
][i
].hisstr
,
6007 while (i
!= hisidx
[histtype
]);
6011 /* Replace the empty last line with the current command-line and put the
6013 ml_replace(curbuf
->b_ml
.ml_line_count
, ccline
.cmdbuff
, TRUE
);
6014 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6015 curwin
->w_cursor
.col
= ccline
.cmdpos
;
6016 changed_line_abv_curs();
6017 invalidate_botline();
6018 redraw_later(SOME_VALID
);
6020 /* Save the command line info, can be used recursively. */
6021 save_ccline
= ccline
;
6022 ccline
.cmdbuff
= NULL
;
6023 ccline
.cmdprompt
= NULL
;
6025 /* No Ex mode here! */
6033 # ifdef FEAT_AUTOCMD
6034 /* Trigger CmdwinEnter autocommands. */
6035 typestr
[0] = cmdwin_type
;
6037 apply_autocmds(EVENT_CMDWINENTER
, typestr
, typestr
, FALSE
, curbuf
);
6038 if (restart_edit
!= 0) /* autocmd with ":startinsert" */
6039 stuffcharReadbuff(K_NOP
);
6042 i
= RedrawingDisabled
;
6043 RedrawingDisabled
= 0;
6046 * Call the main loop until <CR> or CTRL-C is typed.
6049 main_loop(TRUE
, FALSE
);
6051 RedrawingDisabled
= i
;
6053 # ifdef FEAT_AUTOCMD
6054 /* Trigger CmdwinLeave autocommands. */
6055 apply_autocmds(EVENT_CMDWINLEAVE
, typestr
, typestr
, FALSE
, curbuf
);
6058 /* Restore the command line info. */
6059 ccline
= save_ccline
;
6062 exmode_active
= save_exmode
;
6064 /* Safety check: The old window or buffer was deleted: It's a a bug when
6066 if (!win_valid(old_curwin
) || !buf_valid(old_curbuf
))
6068 cmdwin_result
= Ctrl_C
;
6069 EMSG(_("E199: Active window or buffer deleted"));
6073 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6074 /* autocmds may abort script processing */
6075 if (aborting() && cmdwin_result
!= K_IGNORE
)
6076 cmdwin_result
= Ctrl_C
;
6078 /* Set the new command line from the cmdline buffer. */
6079 vim_free(ccline
.cmdbuff
);
6080 if (cmdwin_result
== K_XF1
|| cmdwin_result
== K_XF2
) /* :qa[!] typed */
6082 char *p
= (cmdwin_result
== K_XF2
) ? "qa" : "qa!";
6084 if (histtype
== HIST_CMD
)
6086 /* Execute the command directly. */
6087 ccline
.cmdbuff
= vim_strsave((char_u
*)p
);
6088 cmdwin_result
= CAR
;
6092 /* First need to cancel what we were doing. */
6093 ccline
.cmdbuff
= NULL
;
6094 stuffcharReadbuff(':');
6095 stuffReadbuff((char_u
*)p
);
6096 stuffcharReadbuff(CAR
);
6099 else if (cmdwin_result
== K_XF2
) /* :qa typed */
6101 ccline
.cmdbuff
= vim_strsave((char_u
*)"qa");
6102 cmdwin_result
= CAR
;
6105 ccline
.cmdbuff
= vim_strsave(ml_get_curline());
6106 if (ccline
.cmdbuff
== NULL
)
6107 cmdwin_result
= Ctrl_C
;
6110 ccline
.cmdlen
= (int)STRLEN(ccline
.cmdbuff
);
6111 ccline
.cmdbufflen
= ccline
.cmdlen
+ 1;
6112 ccline
.cmdpos
= curwin
->w_cursor
.col
;
6113 if (ccline
.cmdpos
> ccline
.cmdlen
)
6114 ccline
.cmdpos
= ccline
.cmdlen
;
6115 if (cmdwin_result
== K_IGNORE
)
6117 set_cmdspos_cursor();
6122 # ifdef FEAT_AUTOCMD
6123 /* Don't execute autocommands while deleting the window. */
6128 win_goto(old_curwin
);
6129 win_close(wp
, TRUE
);
6130 close_buffer(NULL
, bp
, DOBUF_WIPE
);
6132 /* Restore window sizes. */
6133 win_size_restore(&winsizes
);
6135 # ifdef FEAT_AUTOCMD
6140 ga_clear(&winsizes
);
6141 restart_edit
= save_restart_edit
;
6142 # ifdef FEAT_RIGHTLEFT
6143 cmdmsg_rl
= save_cmdmsg_rl
;
6151 return cmdwin_result
;
6153 #endif /* FEAT_CMDWIN */
6156 * Used for commands that either take a simple command string argument, or:
6160 * Returns a pointer to allocated memory with {script} or NULL.
6163 script_get(eap
, cmd
)
6168 char *end_pattern
= NULL
;
6172 if (cmd
[0] != '<' || cmd
[1] != '<' || eap
->getline
== NULL
)
6175 ga_init2(&ga
, 1, 0x400);
6178 end_pattern
= (char *)skipwhite(cmd
+ 2);
6184 theline
= eap
->getline(
6186 eap
->cstack
->cs_looplevel
> 0 ? -1 :
6188 NUL
, eap
->cookie
, 0);
6190 if (theline
== NULL
|| STRCMP(end_pattern
, theline
) == 0)
6193 ga_concat(&ga
, theline
);
6194 ga_append(&ga
, '\n');
6197 ga_append(&ga
, NUL
);
6199 return (char_u
*)ga
.ga_data
;