1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * ex_getln.c: Functions for entering and editing an Ex command line.
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
23 char_u
*cmdbuff
; /* pointer to command line buffer */
24 int cmdbufflen
; /* length of cmdbuff */
25 int cmdlen
; /* number of chars in command line */
26 int cmdpos
; /* current cursor position */
27 int cmdspos
; /* cursor column on screen */
28 int cmdfirstc
; /* ':', '/', '?', '=' or NUL */
29 int cmdindent
; /* number of spaces before cmdline */
30 char_u
*cmdprompt
; /* message in front of cmdline */
31 int cmdattr
; /* attributes for prompt */
32 int overstrike
; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
34 expand_T
*xpc
; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
36 int xp_context
; /* type of expansion */
38 char_u
*xp_arg
; /* user-defined expansion arg */
39 int input_fn
; /* when TRUE Invoked for input() function */
43 /* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47 static struct cmdline_info ccline
;
49 static int cmd_showtail
; /* Only show path tail in lists ? */
52 static int new_cmdpos
; /* position set by set_cmdline_pos() */
56 typedef struct hist_entry
58 int hisnum
; /* identifying number */
59 char_u
*hisstr
; /* actual entry, separator char after the NUL */
62 static histentry_T
*(history
[HIST_COUNT
]) = {NULL
, NULL
, NULL
, NULL
, NULL
};
63 static int hisidx
[HIST_COUNT
] = {-1, -1, -1, -1, -1}; /* lastused entry */
64 static int hisnum
[HIST_COUNT
] = {0, 0, 0, 0, 0};
65 /* identifying (unique) number of newest history entry */
66 static int hislen
= 0; /* actual length of history tables */
68 static int hist_char2type
__ARGS((int c
));
70 static int in_history
__ARGS((int, char_u
*, int));
72 static int calc_hist_idx
__ARGS((int histype
, int num
));
77 static int cmd_hkmap
= 0; /* Hebrew mapping during command line */
81 static int cmd_fkmap
= 0; /* Farsi mapping during command line */
84 static int cmdline_charsize
__ARGS((int idx
));
85 static void set_cmdspos
__ARGS((void));
86 static void set_cmdspos_cursor
__ARGS((void));
88 static void correct_cmdspos
__ARGS((int idx
, int cells
));
90 static void alloc_cmdbuff
__ARGS((int len
));
91 static int realloc_cmdbuff
__ARGS((int len
));
92 static void draw_cmdline
__ARGS((int start
, int len
));
93 static void save_cmdline
__ARGS((struct cmdline_info
*ccp
));
94 static void restore_cmdline
__ARGS((struct cmdline_info
*ccp
));
95 static int cmdline_paste
__ARGS((int regname
, int literally
, int remcr
));
96 #if defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
97 static void redrawcmd_preedit
__ARGS((void));
100 static void cmdline_del
__ARGS((int from
));
102 static void redrawcmdprompt
__ARGS((void));
103 static void cursorcmd
__ARGS((void));
104 static int ccheck_abbr
__ARGS((int));
105 static int nextwild
__ARGS((expand_T
*xp
, int type
, int options
));
106 static void escape_fname
__ARGS((char_u
**pp
));
107 static int showmatches
__ARGS((expand_T
*xp
, int wildmenu
));
108 static void set_expand_context
__ARGS((expand_T
*xp
));
109 static int ExpandFromContext
__ARGS((expand_T
*xp
, char_u
*, int *, char_u
***, int));
110 static int expand_showtail
__ARGS((expand_T
*xp
));
111 #ifdef FEAT_CMDL_COMPL
112 static int expand_shellcmd
__ARGS((char_u
*filepat
, int *num_file
, char_u
***file
, int flagsarg
));
113 static int ExpandRTDir
__ARGS((char_u
*pat
, int *num_file
, char_u
***file
, char *dirname
));
114 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
115 static int ExpandUserDefined
__ARGS((expand_T
*xp
, regmatch_T
*regmatch
, int *num_file
, char_u
***file
));
116 static int ExpandUserList
__ARGS((expand_T
*xp
, int *num_file
, char_u
***file
));
121 static int ex_window
__ARGS((void));
125 * getcmdline() - accept a command line starting with firstc.
127 * firstc == ':' get ":" command line.
128 * firstc == '/' or '?' get search pattern
129 * firstc == '=' get expression
130 * firstc == '@' get text for input() function
131 * firstc == '>' get text for debug mode
132 * firstc == NUL get text for :insert command
133 * firstc == -1 like NUL, and break on CTRL-C
135 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
138 * Careful: getcmdline() can be called recursively!
140 * Return pointer to allocated string if there is a commandline, NULL
144 getcmdline(firstc
, count
, indent
)
146 long count UNUSED
; /* only used for incremental search */
147 int indent
; /* indent for inside conditionals */
152 int gotesc
= FALSE
; /* TRUE when <ESC> just typed */
153 int do_abbr
; /* when TRUE check for abbr. */
155 char_u
*lookfor
= NULL
; /* string to match */
156 int hiscnt
; /* current history line in use */
157 int histype
; /* history type to be used */
159 #ifdef FEAT_SEARCH_EXTRA
161 colnr_T old_curswant
;
163 linenr_T old_topline
;
167 linenr_T old_botline
;
168 int did_incsearch
= FALSE
;
169 int incsearch_postponed
= FALSE
;
171 int did_wild_list
= FALSE
; /* did wild_list() recently */
172 int wim_index
= 0; /* index in wim_flags[] */
174 int save_msg_scroll
= msg_scroll
;
175 int save_State
= State
; /* remember State when called */
176 int some_key_typed
= FALSE
; /* one of the keys was typed */
178 /* mouse drag and release events are ignored, unless they are
179 * preceded with a mouse down event */
180 int ignore_drag_release
= TRUE
;
183 int break_ctrl_c
= FALSE
;
186 long *b_im_ptr
= NULL
;
187 #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
188 /* Everything that may work recursively should save and restore the
189 * current command line in save_ccline. That includes update_screen(), a
190 * custom status line may invoke ":normal". */
191 struct cmdline_info save_ccline
;
194 int migemo_enabled
= 0;
205 want_sniff_request
= 0;
214 #ifdef FEAT_RIGHTLEFT
215 /* start without Hebrew mapping for a command line */
216 if (firstc
== ':' || firstc
== '=' || firstc
== '>')
220 ccline
.overstrike
= FALSE
; /* always start in insert mode */
221 #ifdef FEAT_SEARCH_EXTRA
222 old_cursor
= curwin
->w_cursor
; /* needs to be restored later */
223 old_curswant
= curwin
->w_curswant
;
224 old_leftcol
= curwin
->w_leftcol
;
225 old_topline
= curwin
->w_topline
;
227 old_topfill
= curwin
->w_topfill
;
229 old_botline
= curwin
->w_botline
;
233 * set some variables for redrawcmd()
235 ccline
.cmdfirstc
= (firstc
== '@' ? 0 : firstc
);
236 ccline
.cmdindent
= (firstc
> 0 ? indent
: 0);
238 /* alloc initial ccline.cmdbuff */
239 alloc_cmdbuff(exmode_active
? 250 : indent
+ 1);
240 if (ccline
.cmdbuff
== NULL
)
241 return NULL
; /* out of memory */
242 ccline
.cmdlen
= ccline
.cmdpos
= 0;
243 ccline
.cmdbuff
[0] = NUL
;
245 /* autoindent for :insert and :append */
248 copy_spaces(ccline
.cmdbuff
, indent
);
249 ccline
.cmdbuff
[indent
] = NUL
;
250 ccline
.cmdpos
= indent
;
251 ccline
.cmdspos
= indent
;
252 ccline
.cmdlen
= indent
;
258 #ifdef FEAT_RIGHTLEFT
259 if (curwin
->w_p_rl
&& *curwin
->w_p_rlc
== 's'
260 && (firstc
== '/' || firstc
== '?'))
266 redir_off
= TRUE
; /* don't redirect the typed command */
270 msg_scrolled
= 0; /* avoid wait_return message */
273 redrawcmdprompt(); /* draw prompt or indent */
276 xpc
.xp_context
= EXPAND_NOTHING
;
277 xpc
.xp_backslash
= XP_BS_NONE
;
278 #ifndef BACKSLASH_IN_FILENAME
279 xpc
.xp_shell
= FALSE
;
282 #if defined(FEAT_EVAL)
285 xpc
.xp_context
= ccline
.xp_context
;
286 xpc
.xp_pattern
= ccline
.cmdbuff
;
287 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
288 xpc
.xp_arg
= ccline
.xp_arg
;
294 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
295 * doing ":@0" when register 0 doesn't contain a CR.
301 if (firstc
== '/' || firstc
== '?' || firstc
== '@')
303 /* Use ":lmap" mappings for search pattern and input(). */
304 if (curbuf
->b_p_imsearch
== B_IMODE_USE_INSERT
)
305 b_im_ptr
= &curbuf
->b_p_iminsert
;
307 b_im_ptr
= &curbuf
->b_p_imsearch
;
308 if (*b_im_ptr
== B_IMODE_LMAP
)
310 #ifdef USE_IM_CONTROL
311 im_set_active(*b_im_ptr
== B_IMODE_IM
);
314 #ifdef USE_IM_CONTROL
315 else if (p_imcmdline
)
323 ui_cursor_shape(); /* may show different cursor shape */
326 /* When inside an autocommand for writing "exiting" may be set and
327 * terminal mode set to cooked. Need to set raw mode here then. */
332 hiscnt
= hislen
; /* set hiscnt to impossible history value */
333 histype
= hist_char2type(firstc
);
337 do_digraph(-1); /* init digraph typeahead */
341 * Collect the command string, handling editing keys.
345 redir_off
= TRUE
; /* Don't redirect the typed command.
346 Repeated, because a ":redir" inside
347 completion may switch it on. */
348 #ifdef USE_ON_FLY_SCROLL
349 dont_scroll
= FALSE
; /* allow scrolling here */
351 quit_more
= FALSE
; /* reset after CTRL-D which had a more-prompt */
353 cursorcmd(); /* set the cursor on the right spot */
355 /* Get a character. Ignore K_IGNORE, it should not do anything, such
356 * as stop completion. */
360 } while (c
== K_IGNORE
);
364 some_key_typed
= TRUE
;
365 #ifdef FEAT_RIGHTLEFT
372 if (cmdmsg_rl
&& !KeyStuffed
)
374 /* Invert horizontal movements and operations. Only when
375 * typed by the user directly, not when the result of a
379 case K_RIGHT
: c
= K_LEFT
; break;
380 case K_S_RIGHT
: c
= K_S_LEFT
; break;
381 case K_C_RIGHT
: c
= K_C_LEFT
; break;
382 case K_LEFT
: c
= K_RIGHT
; break;
383 case K_S_LEFT
: c
= K_S_RIGHT
; break;
384 case K_C_LEFT
: c
= K_C_RIGHT
; break;
391 * Ignore got_int when CTRL-C was typed here.
392 * Don't ignore it in :global, we really need to break then, e.g., for
393 * ":g/pat/normal /pat" (without the <CR>).
394 * Don't ignore it for the input() function.
401 #if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
411 /* free old command line when finished moving around in the history
414 && c
!= K_S_DOWN
&& c
!= K_S_UP
415 && c
!= K_DOWN
&& c
!= K_UP
416 && c
!= K_PAGEDOWN
&& c
!= K_PAGEUP
417 && c
!= K_KPAGEDOWN
&& c
!= K_KPAGEUP
418 && c
!= K_LEFT
&& c
!= K_RIGHT
419 && (xpc
.xp_numfiles
> 0 || (c
!= Ctrl_P
&& c
!= Ctrl_N
)))
427 * When there are matching completions to select <S-Tab> works like
428 * CTRL-P (unless 'wc' is <S-Tab>).
430 if (c
!= p_wc
&& c
== K_S_TAB
&& xpc
.xp_numfiles
> 0)
434 /* Special translations for 'wildmenu' */
435 if (did_wild_list
&& p_wmnu
)
439 else if (c
== K_RIGHT
)
442 /* Hitting CR after "emenu Name.": complete submenu */
443 if (xpc
.xp_context
== EXPAND_MENUNAMES
&& p_wmnu
445 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '.'
446 && ccline
.cmdbuff
[ccline
.cmdpos
- 2] != '\\'
447 && (c
== '\n' || c
== '\r' || c
== K_KENTER
))
451 /* free expanded names when finished walking through matches */
452 if (xpc
.xp_numfiles
!= -1
453 && !(c
== p_wc
&& KeyTyped
) && c
!= p_wcm
454 && c
!= Ctrl_N
&& c
!= Ctrl_P
&& c
!= Ctrl_A
457 (void)ExpandOne(&xpc
, NULL
, NULL
, 0, WILD_FREE
);
458 did_wild_list
= FALSE
;
460 if (!p_wmnu
|| (c
!= K_UP
&& c
!= K_DOWN
))
462 xpc
.xp_context
= EXPAND_NOTHING
;
465 if (p_wmnu
&& wild_menu_showing
!= 0)
468 int old_RedrawingDisabled
= RedrawingDisabled
;
471 RedrawingDisabled
= 0;
473 if (wild_menu_showing
== WM_SCROLLED
)
475 /* Entered command line, move it up */
479 else if (save_p_ls
!= -1)
481 /* restore 'laststatus' and 'winminheight' */
485 save_cmdline(&save_ccline
);
486 update_screen(VALID
); /* redraw the screen NOW */
487 restore_cmdline(&save_ccline
);
493 # ifdef FEAT_VERTSPLIT
494 win_redraw_last_status(topframe
);
496 lastwin
->w_redr_status
= TRUE
;
498 redraw_statuslines();
501 wild_menu_showing
= 0;
503 RedrawingDisabled
= old_RedrawingDisabled
;
509 /* Special translations for 'wildmenu' */
510 if (xpc
.xp_context
== EXPAND_MENUNAMES
&& p_wmnu
)
512 /* Hitting <Down> after "emenu Name.": complete submenu */
513 if (c
== K_DOWN
&& ccline
.cmdpos
> 0
514 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '.')
518 /* Hitting <Up>: Remove one submenu name in front of the
522 j
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
526 /* check for start of menu name */
527 if (ccline
.cmdbuff
[j
] == ' '
528 && ccline
.cmdbuff
[j
- 1] != '\\')
533 /* check for start of submenu name */
534 if (ccline
.cmdbuff
[j
] == '.'
535 && ccline
.cmdbuff
[j
- 1] != '\\')
549 xpc
.xp_context
= EXPAND_NOTHING
;
552 if ((xpc
.xp_context
== EXPAND_FILES
553 || xpc
.xp_context
== EXPAND_DIRECTORIES
554 || xpc
.xp_context
== EXPAND_SHELLCMD
) && p_wmnu
)
566 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == PATHSEP
567 && (ccline
.cmdpos
< 3
568 || ccline
.cmdbuff
[ccline
.cmdpos
- 2] != '.'
569 || ccline
.cmdbuff
[ccline
.cmdpos
- 3] != '.'))
571 /* go down a directory */
574 else if (STRNCMP(xpc
.xp_pattern
, upseg
+ 1, 3) == 0 && c
== K_DOWN
)
576 /* If in a direct ancestor, strip off one ../ to go down */
580 i
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
585 j
-= (*mb_head_off
)(ccline
.cmdbuff
, ccline
.cmdbuff
+ j
);
587 if (vim_ispathsep(ccline
.cmdbuff
[j
]))
594 && ccline
.cmdbuff
[j
- 1] == '.'
595 && ccline
.cmdbuff
[j
- 2] == '.'
596 && (vim_ispathsep(ccline
.cmdbuff
[j
- 3]) || j
== i
+ 2))
604 /* go up a directory */
607 j
= ccline
.cmdpos
- 1;
608 i
= (int)(xpc
.xp_pattern
- ccline
.cmdbuff
);
613 j
-= (*mb_head_off
)(ccline
.cmdbuff
, ccline
.cmdbuff
+ j
);
615 if (vim_ispathsep(ccline
.cmdbuff
[j
])
616 #ifdef BACKSLASH_IN_FILENAME
617 && vim_strchr(" *?[{`$%#", ccline
.cmdbuff
[j
+ 1])
634 else if (STRNCMP(ccline
.cmdbuff
+ j
, upseg
, 4) == 0)
636 else if (STRNCMP(ccline
.cmdbuff
+ j
, upseg
+ 1, 3) == 0
643 /* TODO this is only for DOS/UNIX systems - need to put in
644 * machine-specific stuff here and in upseg init */
646 put_on_cmdline(upseg
+ 1, 3, FALSE
);
648 else if (ccline
.cmdpos
> i
)
653 #if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
655 && (xpc
.xp_context
== EXPAND_FILES
656 || xpc
.xp_context
== EXPAND_MENUNAMES
)
657 && (c
== K_UP
|| c
== K_DOWN
))
658 xpc
.xp_context
= EXPAND_NOTHING
;
661 #endif /* FEAT_WILDMENU */
663 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
664 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
672 /* CTRL-\ e doesn't work when obtaining an expression. */
673 if (c
!= Ctrl_N
&& c
!= Ctrl_G
674 && (c
!= 'e' || ccline
.cmdfirstc
== '='))
685 * Replace the command line with the result of an expression.
686 * Need to save and restore the current command line, to be
687 * able to enter a new one...
689 if (ccline
.cmdpos
== ccline
.cmdlen
)
690 new_cmdpos
= 99999; /* keep it at the end */
692 new_cmdpos
= ccline
.cmdpos
;
694 save_cmdline(&save_ccline
);
695 c
= get_expr_register();
696 restore_cmdline(&save_ccline
);
699 /* Need to save and restore ccline. And set "textlock"
700 * to avoid nasty things like going to another buffer when
701 * evaluating an expression. */
702 save_cmdline(&save_ccline
);
706 restore_cmdline(&save_ccline
);
708 if (p
!= NULL
&& realloc_cmdbuff((int)STRLEN(p
) + 1) == OK
)
710 ccline
.cmdlen
= (int)STRLEN(p
);
711 STRCPY(ccline
.cmdbuff
, p
);
714 /* Restore the cursor or use the position set with
715 * set_cmdline_pos(). */
716 if (new_cmdpos
> ccline
.cmdlen
)
717 ccline
.cmdpos
= ccline
.cmdlen
;
719 ccline
.cmdpos
= new_cmdpos
;
721 KeyTyped
= FALSE
; /* Don't do p_wc completion. */
723 goto cmdline_changed
;
732 if (c
== Ctrl_G
&& p_im
&& restart_edit
== 0)
734 gotesc
= TRUE
; /* will free ccline.cmdbuff after putting it
736 goto returncmd
; /* back to Normal mode */
741 if (c
== cedit_key
|| c
== K_CMDWIN
)
744 * Open a window to edit the command line (and history).
747 some_key_typed
= TRUE
;
749 # ifdef FEAT_DIGRAPHS
757 if (c
== '\n' || c
== '\r' || c
== K_KENTER
|| (c
== ESC
758 && (!KeyTyped
|| vim_strchr(p_cpo
, CPO_ESC
) != NULL
)))
760 /* In Ex mode a backslash escapes a newline. */
763 && ccline
.cmdpos
== ccline
.cmdlen
765 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] == '\\')
772 gotesc
= FALSE
; /* Might have typed ESC previously, don't
773 truncate the cmdline now. */
774 if (ccheck_abbr(c
+ ABBR_OFF
))
775 goto cmdline_changed
;
778 windgoto(msg_row
, 0);
786 * Completion for 'wildchar' or 'wildcharm' key.
787 * - hitting <ESC> twice means: abandon command line.
788 * - wildcard expansion is only done when the 'wildchar' key is really
789 * typed, not when it comes from a macro
791 if ((c
== p_wc
&& !gotesc
&& KeyTyped
) || c
== p_wcm
)
793 if (xpc
.xp_numfiles
> 0) /* typed p_wc at least twice */
795 /* if 'wildmode' contains "list" may still need to list */
796 if (xpc
.xp_numfiles
> 1
798 && (wim_flags
[wim_index
] & WIM_LIST
))
800 (void)showmatches(&xpc
, FALSE
);
802 did_wild_list
= TRUE
;
804 if (wim_flags
[wim_index
] & WIM_LONGEST
)
805 res
= nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
806 else if (wim_flags
[wim_index
] & WIM_FULL
)
807 res
= nextwild(&xpc
, WILD_NEXT
, WILD_NO_BEEP
);
809 res
= OK
; /* don't insert 'wildchar' now */
811 else /* typed p_wc first time */
815 /* if 'wildmode' first contains "longest", get longest
817 if (wim_flags
[0] & WIM_LONGEST
)
818 res
= nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
820 res
= nextwild(&xpc
, WILD_EXPAND_KEEP
, WILD_NO_BEEP
);
822 /* if interrupted while completing, behave like it failed */
825 (void)vpeekc(); /* remove <C-C> from input stream */
826 got_int
= FALSE
; /* don't abandon the command line */
827 (void)ExpandOne(&xpc
, NULL
, NULL
, 0, WILD_FREE
);
829 xpc
.xp_context
= EXPAND_NOTHING
;
831 goto cmdline_changed
;
834 /* when more than one match, and 'wildmode' first contains
835 * "list", or no change and 'wildmode' contains "longest,list",
836 * list all matches */
837 if (res
== OK
&& xpc
.xp_numfiles
> 1)
839 /* a "longest" that didn't do anything is skipped (but not
841 if (wim_flags
[0] == WIM_LONGEST
&& ccline
.cmdpos
== j
)
843 if ((wim_flags
[wim_index
] & WIM_LIST
)
845 || (p_wmnu
&& (wim_flags
[wim_index
] & WIM_FULL
) != 0)
849 if (!(wim_flags
[0] & WIM_LONGEST
))
852 int p_wmnu_save
= p_wmnu
;
855 nextwild(&xpc
, WILD_PREV
, 0); /* remove match */
857 p_wmnu
= p_wmnu_save
;
861 (void)showmatches(&xpc
, p_wmnu
862 && ((wim_flags
[wim_index
] & WIM_LIST
) == 0));
864 (void)showmatches(&xpc
, FALSE
);
867 did_wild_list
= TRUE
;
868 if (wim_flags
[wim_index
] & WIM_LONGEST
)
869 nextwild(&xpc
, WILD_LONGEST
, WILD_NO_BEEP
);
870 else if (wim_flags
[wim_index
] & WIM_FULL
)
871 nextwild(&xpc
, WILD_NEXT
, WILD_NO_BEEP
);
877 else if (xpc
.xp_numfiles
== -1)
878 xpc
.xp_context
= EXPAND_NOTHING
;
886 goto cmdline_changed
;
891 /* <S-Tab> goes to last match, in a clumsy way */
892 if (c
== K_S_TAB
&& KeyTyped
)
894 if (nextwild(&xpc
, WILD_EXPAND_KEEP
, 0) == OK
895 && nextwild(&xpc
, WILD_PREV
, 0) == OK
896 && nextwild(&xpc
, WILD_PREV
, 0) == OK
)
897 goto cmdline_changed
;
900 if (c
== NUL
|| c
== K_ZERO
) /* NUL is stored as NL */
903 do_abbr
= TRUE
; /* default: check for abbreviation */
906 * Big switch for a typed command line character.
916 if (cmd_fkmap
&& c
== K_BS
)
923 * delete current character is the same as backspace on next
924 * character, except at end of line
926 if (c
== K_DEL
&& ccline
.cmdpos
!= ccline
.cmdlen
)
929 if (has_mbyte
&& c
== K_DEL
)
930 ccline
.cmdpos
+= mb_off_next(ccline
.cmdbuff
,
931 ccline
.cmdbuff
+ ccline
.cmdpos
);
933 if (ccline
.cmdpos
> 0)
938 p
= ccline
.cmdbuff
+ j
;
942 p
= mb_prevptr(ccline
.cmdbuff
, p
);
945 while (p
> ccline
.cmdbuff
&& vim_isspace(*p
))
946 p
= mb_prevptr(ccline
.cmdbuff
, p
);
948 while (p
> ccline
.cmdbuff
&& mb_get_class(p
) == i
)
949 p
= mb_prevptr(ccline
.cmdbuff
, p
);
950 if (mb_get_class(p
) != i
)
951 p
+= (*mb_ptr2len
)(p
);
958 while (p
> ccline
.cmdbuff
&& vim_isspace(p
[-1]))
960 i
= vim_iswordc(p
[-1]);
961 while (p
> ccline
.cmdbuff
&& !vim_isspace(p
[-1])
962 && vim_iswordc(p
[-1]) == i
)
967 ccline
.cmdpos
= (int)(p
- ccline
.cmdbuff
);
968 ccline
.cmdlen
-= j
- ccline
.cmdpos
;
970 while (i
< ccline
.cmdlen
)
971 ccline
.cmdbuff
[i
++] = ccline
.cmdbuff
[j
++];
973 /* Truncate at the end, required for multi-byte chars. */
974 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
977 else if (ccline
.cmdlen
== 0 && c
!= Ctrl_W
978 && ccline
.cmdprompt
== NULL
&& indent
== 0)
980 /* In ex and debug mode it doesn't make sense to return. */
983 || ccline
.cmdfirstc
== '>'
986 goto cmdline_not_changed
;
988 vim_free(ccline
.cmdbuff
); /* no commandline to return */
989 ccline
.cmdbuff
= NULL
;
992 #ifdef FEAT_RIGHTLEFT
998 msg_putchar(' '); /* delete ':' */
1000 redraw_cmdline
= TRUE
;
1001 goto returncmd
; /* back to cmd mode */
1003 goto cmdline_changed
;
1008 /* if Farsi mode set, we are in reverse insert mode -
1009 Do not change the mode */
1014 ccline
.overstrike
= !ccline
.overstrike
;
1016 ui_cursor_shape(); /* may show different cursor shape */
1018 goto cmdline_not_changed
;
1021 if (map_to_exists_mode((char_u
*)"", LANGMAP
, FALSE
))
1023 /* ":lmap" mappings exists, toggle use of mappings. */
1025 #ifdef USE_IM_CONTROL
1026 im_set_active(FALSE
); /* Disable input method */
1028 if (b_im_ptr
!= NULL
)
1030 if (State
& LANGMAP
)
1031 *b_im_ptr
= B_IMODE_LMAP
;
1033 *b_im_ptr
= B_IMODE_NONE
;
1036 #ifdef USE_IM_CONTROL
1039 /* There are no ":lmap" mappings, toggle IM. When
1040 * 'imdisable' is set don't try getting the status, it's
1042 if ((p_imdisable
&& b_im_ptr
!= NULL
)
1043 ? *b_im_ptr
== B_IMODE_IM
: im_get_status())
1045 im_set_active(FALSE
); /* Disable input method */
1046 if (b_im_ptr
!= NULL
)
1047 *b_im_ptr
= B_IMODE_NONE
;
1051 im_set_active(TRUE
); /* Enable input method */
1052 if (b_im_ptr
!= NULL
)
1053 *b_im_ptr
= B_IMODE_IM
;
1057 if (b_im_ptr
!= NULL
)
1059 if (b_im_ptr
== &curbuf
->b_p_iminsert
)
1060 set_iminsert_global();
1062 set_imsearch_global();
1065 ui_cursor_shape(); /* may show different cursor shape */
1067 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1068 /* Show/unshow value of 'keymap' in status lines later. */
1069 status_redraw_curbuf();
1071 goto cmdline_not_changed
;
1073 /* case '@': only in very old vi */
1075 /* delete all characters left of the cursor */
1078 i
= ccline
.cmdpos
= 0;
1079 while (i
< ccline
.cmdlen
)
1080 ccline
.cmdbuff
[i
++] = ccline
.cmdbuff
[j
++];
1081 /* Truncate at the end, required for multi-byte chars. */
1082 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
1084 goto cmdline_changed
;
1086 #ifdef FEAT_CLIPBOARD
1088 /* Copy the modeless selection, if there is one. */
1089 if (clip_star
.state
!= SELECT_CLEARED
)
1091 if (clip_star
.state
== SELECT_DONE
)
1092 clip_copy_modeless_selection(TRUE
);
1093 goto cmdline_not_changed
;
1098 case ESC
: /* get here if p_wc != ESC or when ESC typed twice */
1100 /* In exmode it doesn't make sense to return. Except when
1101 * ":normal" runs out of characters. */
1103 #ifdef FEAT_EX_EXTRA
1104 && (ex_normal_busy
== 0 || typebuf
.tb_len
> 0)
1107 goto cmdline_not_changed
;
1109 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1110 putting it in history */
1111 goto returncmd
; /* back to cmd mode */
1113 case Ctrl_R
: /* insert register */
1114 #ifdef USE_ON_FLY_SCROLL
1115 dont_scroll
= TRUE
; /* disallow scrolling here */
1117 putcmdline('"', TRUE
);
1119 i
= c
= plain_vgetc(); /* CTRL-R <char> */
1121 i
= Ctrl_R
; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1123 c
= plain_vgetc(); /* CTRL-R CTRL-R <char> */
1127 * Insert the result of an expression.
1128 * Need to save the current command line, to be able to enter
1134 if (ccline
.cmdfirstc
== '=')/* can't do this recursively */
1141 save_cmdline(&save_ccline
);
1142 c
= get_expr_register();
1143 restore_cmdline(&save_ccline
);
1147 if (c
!= ESC
) /* use ESC to cancel inserting register */
1149 cmdline_paste(c
, i
== Ctrl_R
, FALSE
);
1152 /* When there was a serious error abort getting the
1156 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1157 putting it in history */
1158 goto returncmd
; /* back to cmd mode */
1161 KeyTyped
= FALSE
; /* Don't do p_wc completion. */
1163 if (new_cmdpos
>= 0)
1165 /* set_cmdline_pos() was used */
1166 if (new_cmdpos
> ccline
.cmdlen
)
1167 ccline
.cmdpos
= ccline
.cmdlen
;
1169 ccline
.cmdpos
= new_cmdpos
;
1174 goto cmdline_changed
;
1177 if (showmatches(&xpc
, FALSE
) == EXPAND_NOTHING
)
1178 break; /* Use ^D as normal char instead */
1181 continue; /* don't do incremental search now */
1188 if (ccline
.cmdpos
>= ccline
.cmdlen
)
1190 i
= cmdline_charsize(ccline
.cmdpos
);
1191 if (KeyTyped
&& ccline
.cmdspos
+ i
>= Columns
* Rows
)
1193 ccline
.cmdspos
+= i
;
1196 ccline
.cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
1202 while ((c
== K_S_RIGHT
|| c
== K_C_RIGHT
1203 || (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
)))
1204 && ccline
.cmdbuff
[ccline
.cmdpos
] != ' ');
1207 set_cmdspos_cursor();
1209 goto cmdline_not_changed
;
1214 if (ccline
.cmdpos
== 0)
1215 goto cmdline_not_changed
;
1220 if (has_mbyte
) /* move to first byte of char */
1221 ccline
.cmdpos
-= (*mb_head_off
)(ccline
.cmdbuff
,
1222 ccline
.cmdbuff
+ ccline
.cmdpos
);
1224 ccline
.cmdspos
-= cmdline_charsize(ccline
.cmdpos
);
1226 while (ccline
.cmdpos
> 0
1227 && (c
== K_S_LEFT
|| c
== K_C_LEFT
1228 || (mod_mask
& (MOD_MASK_SHIFT
|MOD_MASK_CTRL
)))
1229 && ccline
.cmdbuff
[ccline
.cmdpos
- 1] != ' ');
1232 set_cmdspos_cursor();
1234 goto cmdline_not_changed
;
1237 /* Ignore mouse event or ex_window() result. */
1238 goto cmdline_not_changed
;
1241 /* On Win32 ignore <M-F4>, we get it when closing the window was
1244 if (mod_mask
== MOD_MASK_ALT
)
1246 redrawcmd(); /* somehow the cmdline is cleared */
1247 goto cmdline_not_changed
;
1254 case K_MIDDLERELEASE
:
1255 goto cmdline_not_changed
; /* Ignore mouse */
1259 /* When GUI is active, also paste when 'mouse' is empty */
1262 if (!mouse_has(MOUSE_COMMAND
))
1263 goto cmdline_not_changed
; /* Ignore mouse */
1264 # ifdef FEAT_CLIPBOARD
1265 if (clip_star
.available
)
1266 cmdline_paste('*', TRUE
, TRUE
);
1269 cmdline_paste(0, TRUE
, TRUE
);
1271 goto cmdline_changed
;
1275 cmdline_paste('~', TRUE
, FALSE
);
1277 goto cmdline_changed
;
1283 case K_RIGHTRELEASE
:
1284 /* Ignore drag and release events when the button-down wasn't
1286 if (ignore_drag_release
)
1287 goto cmdline_not_changed
;
1291 if (c
== K_LEFTRELEASE
|| c
== K_RIGHTRELEASE
)
1292 ignore_drag_release
= TRUE
;
1294 ignore_drag_release
= FALSE
;
1296 /* When GUI is active, also move when 'mouse' is empty */
1299 if (!mouse_has(MOUSE_COMMAND
))
1300 goto cmdline_not_changed
; /* Ignore mouse */
1301 # ifdef FEAT_CLIPBOARD
1302 if (mouse_row
< cmdline_row
&& clip_star
.available
)
1304 int button
, is_click
, is_drag
;
1307 * Handle modeless selection.
1309 button
= get_mouse_button(KEY2TERMCAP1(c
),
1310 &is_click
, &is_drag
);
1311 if (mouse_model_popup() && button
== MOUSE_LEFT
1312 && (mod_mask
& MOD_MASK_SHIFT
))
1314 /* Translate shift-left to right button. */
1315 button
= MOUSE_RIGHT
;
1316 mod_mask
&= ~MOD_MASK_SHIFT
;
1318 clip_modeless(button
, is_click
, is_drag
);
1319 goto cmdline_not_changed
;
1324 for (ccline
.cmdpos
= 0; ccline
.cmdpos
< ccline
.cmdlen
;
1327 i
= cmdline_charsize(ccline
.cmdpos
);
1328 if (mouse_row
<= cmdline_row
+ ccline
.cmdspos
/ Columns
1329 && mouse_col
< ccline
.cmdspos
% Columns
+ i
)
1334 /* Count ">" for double-wide char that doesn't fit. */
1335 correct_cmdspos(ccline
.cmdpos
, i
);
1336 ccline
.cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
1337 + ccline
.cmdpos
) - 1;
1340 ccline
.cmdspos
+= i
;
1342 goto cmdline_not_changed
;
1344 /* Mouse scroll wheel: ignored here */
1347 /* Alternate buttons ignored here */
1354 goto cmdline_not_changed
;
1356 #endif /* FEAT_MOUSE */
1359 case K_LEFTMOUSE_NM
: /* mousefocus click, ignored */
1360 case K_LEFTRELEASE_NM
:
1361 goto cmdline_not_changed
;
1363 case K_VER_SCROLLBAR
:
1364 if (msg_scrolled
== 0)
1369 goto cmdline_not_changed
;
1371 case K_HOR_SCROLLBAR
:
1372 if (msg_scrolled
== 0)
1374 gui_do_horiz_scroll();
1377 goto cmdline_not_changed
;
1379 #ifdef FEAT_GUI_TABLINE
1382 /* Don't want to change any tabs here. Make sure the same tab
1383 * is still selected. */
1384 if (gui_use_tabline())
1385 gui_mch_set_curtab(tabpage_index(curtab
));
1386 goto cmdline_not_changed
;
1389 case K_SELECT
: /* end of Select mode mapping - ignore */
1390 goto cmdline_not_changed
;
1392 case Ctrl_B
: /* begin of command line */
1399 goto cmdline_not_changed
;
1401 case Ctrl_E
: /* end of command line */
1406 ccline
.cmdpos
= ccline
.cmdlen
;
1407 set_cmdspos_cursor();
1408 goto cmdline_not_changed
;
1410 case Ctrl_A
: /* all matches */
1411 if (nextwild(&xpc
, WILD_ALL
, 0) == FAIL
)
1413 goto cmdline_changed
;
1416 #ifdef FEAT_SEARCH_EXTRA
1417 if (p_is
&& !cmd_silent
&& (firstc
== '/' || firstc
== '?'))
1419 /* Add a character from under the cursor for 'incsearch' */
1421 && !equalpos(curwin
->w_cursor
, old_cursor
))
1426 if (c
== firstc
|| vim_strchr((char_u
*)(
1427 p_magic
? "\\^$.*[" : "\\^$"), c
)
1430 /* put a backslash before special characters */
1431 stuffcharReadbuff(c
);
1437 goto cmdline_not_changed
;
1441 /* completion: longest common part */
1442 if (nextwild(&xpc
, WILD_LONGEST
, 0) == FAIL
)
1444 goto cmdline_changed
;
1446 case Ctrl_N
: /* next match */
1447 case Ctrl_P
: /* previous match */
1448 if (xpc
.xp_numfiles
> 0)
1450 if (nextwild(&xpc
, (c
== Ctrl_P
) ? WILD_PREV
: WILD_NEXT
, 0)
1453 goto cmdline_changed
;
1465 if (hislen
== 0 || firstc
== NUL
) /* no history */
1466 goto cmdline_not_changed
;
1470 /* save current command string so it can be restored later */
1471 if (lookfor
== NULL
)
1473 if ((lookfor
= vim_strsave(ccline
.cmdbuff
)) == NULL
)
1474 goto cmdline_not_changed
;
1475 lookfor
[ccline
.cmdpos
] = NUL
;
1478 j
= (int)STRLEN(lookfor
);
1481 /* one step backwards */
1482 if (c
== K_UP
|| c
== K_S_UP
|| c
== Ctrl_P
1483 || c
== K_PAGEUP
|| c
== K_KPAGEUP
)
1485 if (hiscnt
== hislen
) /* first time */
1486 hiscnt
= hisidx
[histype
];
1487 else if (hiscnt
== 0 && hisidx
[histype
] != hislen
- 1)
1488 hiscnt
= hislen
- 1;
1489 else if (hiscnt
!= hisidx
[histype
] + 1)
1491 else /* at top of list */
1497 else /* one step forwards */
1499 /* on last entry, clear the line */
1500 if (hiscnt
== hisidx
[histype
])
1506 /* not on a history line, nothing to do */
1507 if (hiscnt
== hislen
)
1509 if (hiscnt
== hislen
- 1) /* wrap around */
1514 if (hiscnt
< 0 || history
[histype
][hiscnt
].hisstr
== NULL
)
1519 if ((c
!= K_UP
&& c
!= K_DOWN
)
1521 || STRNCMP(history
[histype
][hiscnt
].hisstr
,
1522 lookfor
, (size_t)j
) == 0)
1526 if (hiscnt
!= i
) /* jumped to other entry */
1532 vim_free(ccline
.cmdbuff
);
1533 xpc
.xp_context
= EXPAND_NOTHING
;
1534 if (hiscnt
== hislen
)
1535 p
= lookfor
; /* back to the old one */
1537 p
= history
[histype
][hiscnt
].hisstr
;
1539 if (histype
== HIST_SEARCH
1541 && (old_firstc
= p
[STRLEN(p
) + 1]) != firstc
)
1543 /* Correct for the separator character used when
1544 * adding the history entry vs the one used now.
1545 * First loop: count length.
1546 * Second loop: copy the characters. */
1547 for (i
= 0; i
<= 1; ++i
)
1550 for (j
= 0; p
[j
] != NUL
; ++j
)
1552 /* Replace old sep with new sep, unless it is
1554 if (p
[j
] == old_firstc
1555 && (j
== 0 || p
[j
- 1] != '\\'))
1558 ccline
.cmdbuff
[len
] = firstc
;
1562 /* Escape new sep, unless it is already
1565 && (j
== 0 || p
[j
- 1] != '\\'))
1568 ccline
.cmdbuff
[len
] = '\\';
1572 ccline
.cmdbuff
[len
] = p
[j
];
1579 if (ccline
.cmdbuff
== NULL
)
1583 ccline
.cmdbuff
[len
] = NUL
;
1587 alloc_cmdbuff((int)STRLEN(p
));
1588 if (ccline
.cmdbuff
== NULL
)
1590 STRCPY(ccline
.cmdbuff
, p
);
1593 ccline
.cmdpos
= ccline
.cmdlen
= (int)STRLEN(ccline
.cmdbuff
);
1595 goto cmdline_changed
;
1598 goto cmdline_not_changed
;
1604 ignore_drag_release
= TRUE
;
1606 putcmdline('^', TRUE
);
1607 c
= get_literal(); /* get next (two) character(s) */
1608 do_abbr
= FALSE
; /* don't do abbreviation now */
1610 /* may need to remove ^ when composing char was typed */
1611 if (enc_utf8
&& utf_iscomposing(c
) && !cmd_silent
)
1613 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
1620 #ifdef FEAT_DIGRAPHS
1623 ignore_drag_release
= TRUE
;
1625 putcmdline('?', TRUE
);
1626 #ifdef USE_ON_FLY_SCROLL
1627 dont_scroll
= TRUE
; /* disallow scrolling here */
1629 c
= get_digraph(TRUE
);
1634 goto cmdline_not_changed
;
1635 #endif /* FEAT_DIGRAPHS */
1637 #ifdef FEAT_RIGHTLEFT
1638 case Ctrl__
: /* CTRL-_: switch language mode */
1644 cmd_fkmap
= !cmd_fkmap
;
1645 if (cmd_fkmap
) /* in Farsi always in Insert mode */
1646 ccline
.overstrike
= FALSE
;
1648 else /* Hebrew is default */
1650 cmd_hkmap
= !cmd_hkmap
;
1651 goto cmdline_not_changed
;
1658 gotesc
= TRUE
; /* will free ccline.cmdbuff after
1659 putting it in history */
1660 goto returncmd
; /* back to Normal mode */
1664 * Normal character with no special meaning. Just set mod_mask
1665 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1666 * the string <S-Space>. This should only happen after ^V.
1673 * End of switch on command line character.
1674 * We come here if we have a normal character.
1677 if (do_abbr
&& (IS_SPECIAL(c
) || !vim_iswordc(c
)) && ccheck_abbr(
1679 /* Add ABBR_OFF for characters above 0x100, this is
1680 * what check_abbr() expects. */
1681 (has_mbyte
&& c
>= 0x100) ? (c
+ ABBR_OFF
) :
1684 goto cmdline_changed
;
1687 * put the character in the command line
1689 if (IS_SPECIAL(c
) || mod_mask
!= 0)
1690 put_on_cmdline(get_special_key_name(c
, mod_mask
), -1, TRUE
);
1696 j
= (*mb_char2bytes
)(c
, IObuff
);
1697 IObuff
[j
] = NUL
; /* exclude composing chars */
1698 put_on_cmdline(IObuff
, j
, TRUE
);
1704 put_on_cmdline(IObuff
, 1, TRUE
);
1707 goto cmdline_changed
;
1710 * This part implements incremental searches for "/" and "?"
1711 * Jump to cmdline_not_changed when a character has been read but the command
1712 * line did not change. Then we only search and redraw if something changed in
1714 * Jump to cmdline_changed when the command line did change.
1715 * (Sorry for the goto's, I know it is ugly).
1717 cmdline_not_changed
:
1718 #ifdef FEAT_SEARCH_EXTRA
1719 if (!incsearch_postponed
)
1724 #ifdef FEAT_SEARCH_EXTRA
1726 * 'incsearch' highlighting.
1728 if (p_is
&& !cmd_silent
&& (firstc
== '/' || firstc
== '?'))
1735 /* if there is a character waiting, search and redraw later */
1738 incsearch_postponed
= TRUE
;
1741 incsearch_postponed
= FALSE
;
1742 curwin
->w_cursor
= old_cursor
; /* start at old position */
1744 /* If there is no command line, don't do anything */
1745 if (ccline
.cmdlen
== 0)
1749 int search_options
= (SEARCH_KEEP
+ SEARCH_OPT
1750 + SEARCH_NOOF
+ SEARCH_PEEK
);
1752 cursor_off(); /* so the user knows we're busy */
1754 ++emsg_off
; /* So it doesn't beep if bad expr */
1757 search_options
|= SEARCH_MIGEMO
;
1760 /* Set the time limit to half a second. */
1761 profile_setlimit(500L, &tm
);
1763 i
= do_search(NULL
, firstc
, ccline
.cmdbuff
, count
,
1772 /* if interrupted while searching, behave like it failed */
1775 (void)vpeekc(); /* remove <C-C> from input stream */
1776 got_int
= FALSE
; /* don't abandon the command line */
1779 else if (char_avail())
1780 /* cancelled searching because a char was typed */
1781 incsearch_postponed
= TRUE
;
1784 highlight_match
= TRUE
; /* highlight position */
1786 highlight_match
= FALSE
; /* remove highlight */
1788 /* first restore the old curwin values, so the screen is
1789 * positioned in the same way as the actual search command */
1790 curwin
->w_leftcol
= old_leftcol
;
1791 curwin
->w_topline
= old_topline
;
1793 curwin
->w_topfill
= old_topfill
;
1795 curwin
->w_botline
= old_botline
;
1796 changed_cline_bef_curs();
1801 pos_T save_pos
= curwin
->w_cursor
;
1804 * First move cursor to end of match, then to the start. This
1805 * moves the whole match onto the screen when 'nowrap' is set.
1807 curwin
->w_cursor
.lnum
+= search_match_lines
;
1808 curwin
->w_cursor
.col
= search_match_endcol
;
1809 if (curwin
->w_cursor
.lnum
> curbuf
->b_ml
.ml_line_count
)
1811 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
1812 coladvance((colnr_T
)MAXCOL
);
1815 end_pos
= curwin
->w_cursor
;
1816 curwin
->w_cursor
= save_pos
;
1819 end_pos
= curwin
->w_cursor
; /* shutup gcc 4 */
1822 # ifdef FEAT_WINDOWS
1823 /* May redraw the status line to show the cursor position. */
1824 if (p_ru
&& curwin
->w_status_height
> 0)
1825 curwin
->w_redr_status
= TRUE
;
1828 save_cmdline(&save_ccline
);
1829 update_screen(SOME_VALID
);
1830 restore_cmdline(&save_ccline
);
1832 /* Leave it at the end to make CTRL-R CTRL-W work. */
1834 curwin
->w_cursor
= end_pos
;
1838 did_incsearch
= TRUE
;
1840 #else /* FEAT_SEARCH_EXTRA */
1844 #ifdef FEAT_RIGHTLEFT
1847 || (p_arshape
&& !p_tbidi
&& enc_utf8
)
1850 /* Always redraw the whole command line to fix shaping and
1851 * right-left typing. Not efficient, but it works. */
1858 #ifdef FEAT_RIGHTLEFT
1866 ExpandCleanup(&xpc
);
1869 #ifdef FEAT_SEARCH_EXTRA
1872 curwin
->w_cursor
= old_cursor
;
1873 curwin
->w_curswant
= old_curswant
;
1874 curwin
->w_leftcol
= old_leftcol
;
1875 curwin
->w_topline
= old_topline
;
1877 curwin
->w_topfill
= old_topfill
;
1879 curwin
->w_botline
= old_botline
;
1880 highlight_match
= FALSE
;
1881 validate_cursor(); /* needed for TAB */
1882 redraw_later(SOME_VALID
);
1886 if (ccline
.cmdbuff
!= NULL
)
1889 * Put line in history buffer (":" and "=" only when it was typed).
1892 if (ccline
.cmdlen
&& firstc
!= NUL
1893 && (some_key_typed
|| histype
== HIST_SEARCH
))
1895 add_to_history(histype
, ccline
.cmdbuff
, TRUE
,
1896 histype
== HIST_SEARCH
? firstc
: NUL
);
1899 vim_free(new_last_cmdline
);
1900 new_last_cmdline
= vim_strsave(ccline
.cmdbuff
);
1905 if (gotesc
) /* abandon command line */
1907 vim_free(ccline
.cmdbuff
);
1908 ccline
.cmdbuff
= NULL
;
1909 if (msg_scrolled
== 0)
1912 redraw_cmdline
= TRUE
;
1917 * If the screen was shifted up, redraw the whole screen (later).
1918 * If the line is too long, clear it, so ruler and shown command do
1919 * not get printed in the middle of it.
1922 msg_scroll
= save_msg_scroll
;
1925 /* When the command line was typed, no need for a wait-return prompt. */
1927 need_wait_return
= FALSE
;
1930 #ifdef USE_IM_CONTROL
1931 if (b_im_ptr
!= NULL
&& *b_im_ptr
!= B_IMODE_LMAP
)
1932 im_save_status(b_im_ptr
);
1933 im_set_active(FALSE
);
1939 ui_cursor_shape(); /* may show different cursor shape */
1943 char_u
*p
= ccline
.cmdbuff
;
1945 /* Make ccline empty, getcmdline() may try to use it. */
1946 ccline
.cmdbuff
= NULL
;
1951 #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1953 * Get a command line with a prompt.
1954 * This is prepared to be called recursively from getcmdline() (e.g. by
1955 * f_input() when evaluating an expression from CTRL-R =).
1956 * Returns the command line in allocated memory, or NULL.
1959 getcmdline_prompt(firstc
, prompt
, attr
, xp_context
, xp_arg
)
1961 char_u
*prompt
; /* command line prompt */
1962 int attr
; /* attributes for prompt */
1963 int xp_context
; /* type of expansion */
1964 char_u
*xp_arg
; /* user-defined expansion argument */
1967 struct cmdline_info save_ccline
;
1968 int msg_col_save
= msg_col
;
1970 save_cmdline(&save_ccline
);
1971 ccline
.cmdprompt
= prompt
;
1972 ccline
.cmdattr
= attr
;
1974 ccline
.xp_context
= xp_context
;
1975 ccline
.xp_arg
= xp_arg
;
1976 ccline
.input_fn
= (firstc
== '@');
1978 s
= getcmdline(firstc
, 1L, 0);
1979 restore_cmdline(&save_ccline
);
1980 /* Restore msg_col, the prompt from input() may have changed it. */
1981 msg_col
= msg_col_save
;
1988 * Return TRUE when the text must not be changed and we can't switch to
1989 * another window or buffer. Used when editing the command line, evaluating
1990 * 'balloonexpr', etc.
1996 if (cmdwin_type
!= 0)
1999 return textlock
!= 0;
2003 * Give an error message for a command that isn't allowed while the cmdline
2004 * window is open or editing the cmdline in another way.
2010 if (cmdwin_type
!= 0)
2017 #if defined(FEAT_AUTOCMD) || defined(PROTO)
2019 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2020 * and give an error message.
2025 if (curbuf_lock
> 0)
2027 EMSG(_("E788: Not allowed to edit another buffer now"));
2030 return allbuf_locked();
2034 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2040 if (allbuf_lock
> 0)
2042 EMSG(_("E811: Not allowed to change buffer information now"));
2050 cmdline_charsize(idx
)
2053 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2054 if (cmdline_star
> 0) /* showing '*', always 1 position */
2057 return ptr2cells(ccline
.cmdbuff
+ idx
);
2061 * Compute the offset of the cursor on the command line for the prompt and
2067 if (ccline
.cmdfirstc
!= NUL
)
2068 ccline
.cmdspos
= 1 + ccline
.cmdindent
;
2070 ccline
.cmdspos
= 0 + ccline
.cmdindent
;
2074 * Compute the screen position for the cursor on the command line.
2077 set_cmdspos_cursor()
2085 if (m
< 0) /* overflow, Columns or Rows at weird value */
2090 for (i
= 0; i
< ccline
.cmdlen
&& i
< ccline
.cmdpos
; ++i
)
2092 c
= cmdline_charsize(i
);
2094 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2096 correct_cmdspos(i
, c
);
2098 /* If the cmdline doesn't fit, show cursor on last visible char.
2099 * Don't move the cursor itself, so we can still append. */
2100 if ((ccline
.cmdspos
+= c
) >= m
)
2102 ccline
.cmdspos
-= c
;
2107 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
) - 1;
2114 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2115 * character that doesn't fit, so that a ">" must be displayed.
2118 correct_cmdspos(idx
, cells
)
2122 if ((*mb_ptr2len
)(ccline
.cmdbuff
+ idx
) > 1
2123 && (*mb_ptr2cells
)(ccline
.cmdbuff
+ idx
) > 1
2124 && ccline
.cmdspos
% Columns
+ cells
> Columns
)
2130 * Get an Ex command line for the ":" command.
2133 getexline(c
, cookie
, indent
)
2134 int c
; /* normally ':', NUL for ":append" */
2135 void *cookie UNUSED
;
2136 int indent
; /* indent for inside conditionals */
2138 /* When executing a register, remove ':' that's in front of each line. */
2139 if (exec_from_reg
&& vpeekc() == ':')
2141 return getcmdline(c
, 1L, indent
);
2145 * Get an Ex command line for Ex mode.
2146 * In Ex mode we only use the OS supplied line editing features and no
2147 * mappings or abbreviations.
2148 * Returns a string in allocated memory or NULL.
2151 getexmodeline(promptc
, cookie
, indent
)
2152 int promptc
; /* normally ':', NUL for ":append" and '?' for
2154 void *cookie UNUSED
;
2155 int indent
; /* indent for inside conditionals */
2161 int escaped
= FALSE
; /* CTRL-V typed */
2166 /* Switch cursor on now. This avoids that it happens after the "\n", which
2167 * confuses the system function that computes tabstops. */
2170 /* always start in column 0; write a newline if necessary */
2172 if ((msg_col
|| msg_didout
) && promptc
!= '?')
2176 /* indent that is only displayed, not in the line itself */
2179 while (indent
-- > 0)
2184 ga_init2(&line_ga
, 1, 30);
2186 /* autoindent for :insert and :append is in the line itself */
2192 ga_append(&line_ga
, TAB
);
2193 msg_puts((char_u
*)" ");
2196 while (indent
-- > 0)
2198 ga_append(&line_ga
, ' ');
2206 * Get the line, one character at a time.
2211 if (ga_grow(&line_ga
, 40) == FAIL
)
2213 pend
= (char_u
*)line_ga
.ga_data
+ line_ga
.ga_len
;
2215 /* Get one character at a time. Don't use inchar(), it can't handle
2216 * special characters. */
2221 * Handle line editing.
2222 * Previously this was left to the system, putting the terminal in
2223 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
2233 /* CR typed means "enter", which is NL */
2237 if (c1
== BS
|| c1
== K_BS
2238 || c1
== DEL
|| c1
== K_DEL
|| c1
== K_KDEL
)
2240 if (line_ga
.ga_len
> 0)
2258 p
= (char_u
*)line_ga
.ga_data
;
2259 p
[line_ga
.ga_len
] = NUL
;
2260 indent
= get_indent_str(p
, 8);
2261 indent
+= curbuf
->b_p_sw
- indent
% curbuf
->b_p_sw
;
2263 while (get_indent_str(p
, 8) < indent
)
2265 char_u
*s
= skipwhite(p
);
2267 ga_grow(&line_ga
, 1);
2268 mch_memmove(s
+ 1, s
, line_ga
.ga_len
- (s
- p
) + 1);
2273 /* redraw the line */
2276 for (p
= (char_u
*)line_ga
.ga_data
;
2277 p
< (char_u
*)line_ga
.ga_data
+ line_ga
.ga_len
; ++p
)
2284 } while (++vcol
% 8);
2288 msg_outtrans_len(p
, 1);
2289 vcol
+= char2cells(*p
);
2293 windgoto(msg_row
, msg_col
);
2299 /* Delete one shiftwidth. */
2300 p
= (char_u
*)line_ga
.ga_data
;
2301 if (prev_char
== '0' || prev_char
== '^')
2303 if (prev_char
== '^')
2304 ex_keep_indent
= TRUE
;
2306 p
[--line_ga
.ga_len
] = NUL
;
2310 p
[line_ga
.ga_len
] = NUL
;
2311 indent
= get_indent_str(p
, 8);
2313 indent
-= indent
% curbuf
->b_p_sw
;
2315 while (get_indent_str(p
, 8) > indent
)
2317 char_u
*s
= skipwhite(p
);
2319 mch_memmove(s
- 1, s
, line_ga
.ga_len
- (s
- p
) + 1);
2325 if (c1
== Ctrl_V
|| c1
== Ctrl_Q
)
2331 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2338 ((char_u
*)line_ga
.ga_data
)[line_ga
.ga_len
] = c1
;
2343 /* Don't use chartabsize(), 'ts' can be different */
2347 } while (++vcol
% 8);
2352 ((char_u
*)line_ga
.ga_data
) + line_ga
.ga_len
, 1);
2353 vcol
+= char2cells(c1
);
2358 windgoto(msg_row
, msg_col
);
2359 pend
= (char_u
*)(line_ga
.ga_data
) + line_ga
.ga_len
;
2361 /* we are done when a NL is entered, but not when it comes after a
2363 if (line_ga
.ga_len
> 0 && pend
[-1] == '\n'
2364 && (line_ga
.ga_len
<= 1 || pend
[-2] != '\\'))
2376 /* make following messages go to the next line */
2379 if (msg_row
< Rows
- 1)
2381 emsg_on_display
= FALSE
; /* don't want ui_delay() */
2386 return (char_u
*)line_ga
.ga_data
;
2389 # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2390 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
2392 * Return TRUE if ccline.overstrike is on.
2395 cmdline_overstrike()
2397 return ccline
.overstrike
;
2401 * Return TRUE if the cursor is at the end of the cmdline.
2406 return (ccline
.cmdpos
>= ccline
.cmdlen
);
2410 #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))) \
2413 * Return the virtual column number at the current cursor position.
2414 * This is used by the IM code to obtain the start of the preedit string.
2417 cmdline_getvcol_cursor()
2419 if (ccline
.cmdbuff
== NULL
|| ccline
.cmdpos
> ccline
.cmdlen
)
2428 for (col
= 0; i
< ccline
.cmdpos
; ++col
)
2429 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
);
2435 return ccline
.cmdpos
;
2439 #if defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
2441 * If part of the command line is an IM preedit string, redraw it with
2442 * IM feedback attributes. The cursor position is restored after drawing.
2447 if ((State
& CMDLINE
)
2448 # ifndef FEAT_GUI_MACVIM
2451 /* && im_get_status() doesn't work when using SCIM */
2453 && im_is_preediting())
2463 cmdspos
= ((ccline
.cmdfirstc
!= NUL
) ? 1 : 0) + ccline
.cmdindent
;
2468 for (col
= 0; col
< preedit_start_col
2469 && cmdpos
< ccline
.cmdlen
; ++col
)
2471 cmdspos
+= (*mb_ptr2cells
)(ccline
.cmdbuff
+ cmdpos
);
2472 cmdpos
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ cmdpos
);
2478 cmdspos
+= preedit_start_col
;
2479 cmdpos
+= preedit_start_col
;
2482 msg_row
= cmdline_row
+ (cmdspos
/ (int)Columns
);
2483 msg_col
= cmdspos
% (int)Columns
;
2484 if (msg_row
>= Rows
)
2487 for (col
= 0; cmdpos
< ccline
.cmdlen
; ++col
)
2492 char_attr
= im_get_feedback_attr(col
);
2494 break; /* end of preedit string */
2498 char_len
= (*mb_ptr2len
)(ccline
.cmdbuff
+ cmdpos
);
2503 msg_outtrans_len_attr(ccline
.cmdbuff
+ cmdpos
, char_len
, char_attr
);
2511 #endif /* FEAT_XIM && (FEAT_GUI_GTK || FEAT_GUI_MACVIM) */
2514 * Allocate a new command line buffer.
2515 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2516 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2523 * give some extra space to avoid having to allocate all the time
2530 ccline
.cmdbuff
= alloc(len
); /* caller should check for out-of-memory */
2531 ccline
.cmdbufflen
= len
;
2535 * Re-allocate the command line to length len + something extra.
2536 * return FAIL for failure, OK otherwise
2539 realloc_cmdbuff(len
)
2545 alloc_cmdbuff(len
); /* will get some more */
2546 if (ccline
.cmdbuff
== NULL
) /* out of memory */
2548 ccline
.cmdbuff
= p
; /* keep the old one */
2551 mch_memmove(ccline
.cmdbuff
, p
, (size_t)ccline
.cmdlen
+ 1);
2554 if (ccline
.xpc
!= NULL
2555 && ccline
.xpc
->xp_pattern
!= NULL
2556 && ccline
.xpc
->xp_context
!= EXPAND_NOTHING
2557 && ccline
.xpc
->xp_context
!= EXPAND_UNSUCCESSFUL
)
2559 int i
= (int)(ccline
.xpc
->xp_pattern
- p
);
2561 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2562 * to point into the newly allocated memory. */
2563 if (i
>= 0 && i
<= ccline
.cmdlen
)
2564 ccline
.xpc
->xp_pattern
= ccline
.cmdbuff
+ i
;
2570 #if defined(FEAT_ARABIC) || defined(PROTO)
2571 static char_u
*arshape_buf
= NULL
;
2573 # if defined(EXITFREE) || defined(PROTO)
2577 vim_free(arshape_buf
);
2583 * Draw part of the cmdline at the current cursor position. But draw stars
2584 * when cmdline_star is TRUE.
2587 draw_cmdline(start
, len
)
2591 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2594 if (cmdline_star
> 0)
2595 for (i
= 0; i
< len
; ++i
)
2600 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ start
+ i
) - 1;
2606 if (p_arshape
&& !p_tbidi
&& enc_utf8
&& len
> 0)
2608 static int buflen
= 0;
2621 * Do arabic shaping into a temporary buffer. This is very
2624 if (len
* 2 + 2 > buflen
)
2626 /* Re-allocate the buffer. We keep it around to avoid a lot of
2627 * alloc()/free() calls. */
2628 vim_free(arshape_buf
);
2629 buflen
= len
* 2 + 2;
2630 arshape_buf
= alloc(buflen
);
2631 if (arshape_buf
== NULL
)
2632 return; /* out of memory */
2635 if (utf_iscomposing(utf_ptr2char(ccline
.cmdbuff
+ start
)))
2637 /* Prepend a space to draw the leading composing char on. */
2638 arshape_buf
[0] = ' ';
2642 for (j
= start
; j
< start
+ len
; j
+= mb_l
)
2644 p
= ccline
.cmdbuff
+ j
;
2645 u8c
= utfc_ptr2char_len(p
, u8cc
, start
+ len
- j
);
2646 mb_l
= utfc_ptr2len_len(p
, start
+ len
- j
);
2647 if (ARABIC_CHAR(u8c
))
2649 /* Do Arabic shaping. */
2652 /* displaying from right to left */
2656 if (j
+ mb_l
>= start
+ len
)
2659 nc
= utf_ptr2char(p
+ mb_l
);
2663 /* displaying from left to right */
2664 if (j
+ mb_l
>= start
+ len
)
2670 pc
= utfc_ptr2char_len(p
+ mb_l
, pcc
,
2671 start
+ len
- j
- mb_l
);
2678 u8c
= arabic_shape(u8c
, NULL
, &u8cc
[0], pc
, pc1
, nc
);
2680 newlen
+= (*mb_char2bytes
)(u8c
, arshape_buf
+ newlen
);
2683 newlen
+= (*mb_char2bytes
)(u8cc
[0], arshape_buf
+ newlen
);
2685 newlen
+= (*mb_char2bytes
)(u8cc
[1],
2686 arshape_buf
+ newlen
);
2692 mch_memmove(arshape_buf
+ newlen
, p
, mb_l
);
2697 msg_outtrans_len(arshape_buf
, newlen
);
2701 msg_outtrans_len(ccline
.cmdbuff
+ start
, len
);
2705 * Put a character on the command line. Shifts the following text to the
2706 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2707 * "c" must be printable (fit in one display cell)!
2710 putcmdline(c
, shift
)
2719 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
2720 msg_no_more
= FALSE
;
2725 * Undo a putcmdline(c, FALSE).
2733 if (ccline
.cmdlen
== ccline
.cmdpos
)
2736 draw_cmdline(ccline
.cmdpos
, 1);
2737 msg_no_more
= FALSE
;
2742 * Put the given string, of the given length, onto the command line.
2743 * If len is -1, then STRLEN() is used to calculate the length.
2744 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2745 * part will be redrawn, otherwise it will not. If this function is called
2746 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2747 * called afterwards.
2750 put_on_cmdline(str
, len
, redraw
)
2761 len
= (int)STRLEN(str
);
2763 /* Check if ccline.cmdbuff needs to be longer */
2764 if (ccline
.cmdlen
+ len
+ 1 >= ccline
.cmdbufflen
)
2765 retval
= realloc_cmdbuff(ccline
.cmdlen
+ len
);
2770 if (!ccline
.overstrike
)
2772 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
+ len
,
2773 ccline
.cmdbuff
+ ccline
.cmdpos
,
2774 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
));
2775 ccline
.cmdlen
+= len
;
2782 /* Count nr of characters in the new string. */
2784 for (i
= 0; i
< len
; i
+= (*mb_ptr2len
)(str
+ i
))
2786 /* Count nr of bytes in cmdline that are overwritten by these
2788 for (i
= ccline
.cmdpos
; i
< ccline
.cmdlen
&& m
> 0;
2789 i
+= (*mb_ptr2len
)(ccline
.cmdbuff
+ i
))
2791 if (i
< ccline
.cmdlen
)
2793 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
+ len
,
2794 ccline
.cmdbuff
+ i
, (size_t)(ccline
.cmdlen
- i
));
2795 ccline
.cmdlen
+= ccline
.cmdpos
+ len
- i
;
2798 ccline
.cmdlen
= ccline
.cmdpos
+ len
;
2802 if (ccline
.cmdpos
+ len
> ccline
.cmdlen
)
2803 ccline
.cmdlen
= ccline
.cmdpos
+ len
;
2805 mch_memmove(ccline
.cmdbuff
+ ccline
.cmdpos
, str
, (size_t)len
);
2806 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
2811 /* When the inserted text starts with a composing character,
2812 * backup to the character before it. There could be two of them.
2815 c
= utf_ptr2char(ccline
.cmdbuff
+ ccline
.cmdpos
);
2816 while (ccline
.cmdpos
> 0 && utf_iscomposing(c
))
2818 i
= (*mb_head_off
)(ccline
.cmdbuff
,
2819 ccline
.cmdbuff
+ ccline
.cmdpos
- 1) + 1;
2822 c
= utf_ptr2char(ccline
.cmdbuff
+ ccline
.cmdpos
);
2825 if (i
== 0 && ccline
.cmdpos
> 0 && arabic_maycombine(c
))
2827 /* Check the previous character for Arabic combining pair. */
2828 i
= (*mb_head_off
)(ccline
.cmdbuff
,
2829 ccline
.cmdbuff
+ ccline
.cmdpos
- 1) + 1;
2830 if (arabic_combine(utf_ptr2char(ccline
.cmdbuff
2831 + ccline
.cmdpos
- i
), c
))
2842 /* Also backup the cursor position. */
2843 i
= ptr2cells(ccline
.cmdbuff
+ ccline
.cmdpos
);
2844 ccline
.cmdspos
-= i
;
2855 if (redraw
&& !cmd_silent
)
2859 draw_cmdline(ccline
.cmdpos
, ccline
.cmdlen
- ccline
.cmdpos
);
2860 /* Avoid clearing the rest of the line too often. */
2861 if (cmdline_row
!= i
|| ccline
.overstrike
)
2863 msg_no_more
= FALSE
;
2867 * If we are in Farsi command mode, the character input must be in
2868 * Insert mode. So do not advance the cmdpos.
2876 if (m
< 0) /* overflow, Columns or Rows at weird value */
2881 for (i
= 0; i
< len
; ++i
)
2883 c
= cmdline_charsize(ccline
.cmdpos
);
2885 /* count ">" for a double-wide char that doesn't fit. */
2887 correct_cmdspos(ccline
.cmdpos
, c
);
2889 /* Stop cursor at the end of the screen, but do increment the
2890 * insert position, so that entering a very long command
2891 * works, even though you can't see it. */
2892 if (ccline
.cmdspos
+ c
< m
)
2893 ccline
.cmdspos
+= c
;
2897 c
= (*mb_ptr2len
)(ccline
.cmdbuff
+ ccline
.cmdpos
) - 1;
2898 if (c
> len
- i
- 1)
2913 static struct cmdline_info prev_ccline
;
2914 static int prev_ccline_used
= FALSE
;
2917 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2918 * and overwrite it. But get_cmdline_str() may need it, thus make it
2919 * available globally in prev_ccline.
2923 struct cmdline_info
*ccp
;
2925 if (!prev_ccline_used
)
2927 vim_memset(&prev_ccline
, 0, sizeof(struct cmdline_info
));
2928 prev_ccline_used
= TRUE
;
2931 prev_ccline
= ccline
;
2932 ccline
.cmdbuff
= NULL
;
2933 ccline
.cmdprompt
= NULL
;
2938 * Restore ccline after it has been saved with save_cmdline().
2941 restore_cmdline(ccp
)
2942 struct cmdline_info
*ccp
;
2944 ccline
= prev_ccline
;
2948 #if defined(FEAT_EVAL) || defined(PROTO)
2950 * Save the command line into allocated memory. Returns a pointer to be
2951 * passed to restore_cmdline_alloc() later.
2952 * Returns NULL when failed.
2955 save_cmdline_alloc()
2957 struct cmdline_info
*p
;
2959 p
= (struct cmdline_info
*)alloc((unsigned)sizeof(struct cmdline_info
));
2966 * Restore the command line from the return value of save_cmdline_alloc().
2969 restore_cmdline_alloc(p
)
2974 restore_cmdline((struct cmdline_info
*)p
);
2981 * paste a yank register into the command line.
2982 * used by CTRL-R command in command-line mode
2983 * insert_reg() can't be used here, because special characters from the
2984 * register contents will be interpreted as commands.
2986 * return FAIL for failure, OK otherwise
2989 cmdline_paste(regname
, literally
, remcr
)
2991 int literally
; /* Insert text literally instead of "as typed" */
2992 int remcr
; /* remove trailing CR */
2998 struct cmdline_info save_ccline
;
3000 /* check for valid regname; also accept special characters for CTRL-R in
3001 * the command line */
3002 if (regname
!= Ctrl_F
&& regname
!= Ctrl_P
&& regname
!= Ctrl_W
3003 && regname
!= Ctrl_A
&& !valid_yank_reg(regname
, FALSE
))
3006 /* A register containing CTRL-R can cause an endless loop. Allow using
3007 * CTRL-C to break the loop. */
3012 #ifdef FEAT_CLIPBOARD
3013 regname
= may_get_selection(regname
);
3016 /* Need to save and restore ccline. And set "textlock" to avoid nasty
3017 * things like going to another buffer when evaluating an expression. */
3018 save_cmdline(&save_ccline
);
3020 i
= get_spec_reg(regname
, &arg
, &allocated
, TRUE
);
3022 restore_cmdline(&save_ccline
);
3026 /* Got the value of a special register in "arg". */
3030 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3031 * part of the word. */
3033 if (p_is
&& regname
== Ctrl_W
)
3038 /* Locate start of last word in the cmd buffer. */
3039 for (w
= ccline
.cmdbuff
+ ccline
.cmdlen
; w
> ccline
.cmdbuff
; )
3044 len
= (*mb_head_off
)(ccline
.cmdbuff
, w
- 1) + 1;
3045 if (!vim_iswordc(mb_ptr2char(w
- len
)))
3052 if (!vim_iswordc(w
[-1]))
3057 len
= (int)((ccline
.cmdbuff
+ ccline
.cmdlen
) - w
);
3058 if (p_ic
? STRNICMP(w
, arg
, len
) == 0 : STRNCMP(w
, arg
, len
) == 0)
3062 cmdline_paste_str(p
, literally
);
3068 return cmdline_paste_reg(regname
, literally
, remcr
);
3072 * Put a string on the command line.
3073 * When "literally" is TRUE, insert literally.
3074 * When "literally" is FALSE, insert as typed, but don't leave the command
3078 cmdline_paste_str(s
, literally
)
3085 put_on_cmdline(s
, -1, TRUE
);
3090 if (cv
== Ctrl_V
&& s
[1])
3094 c
= mb_cptr2char_adv(&s
);
3098 if (cv
== Ctrl_V
|| c
== ESC
|| c
== Ctrl_C
|| c
== CAR
|| c
== NL
3102 || (c
== Ctrl_BSL
&& *s
== Ctrl_N
))
3103 stuffcharReadbuff(Ctrl_V
);
3104 stuffcharReadbuff(c
);
3108 #ifdef FEAT_WILDMENU
3110 * Delete characters on the command line, from "from" to the current
3117 mch_memmove(ccline
.cmdbuff
+ from
, ccline
.cmdbuff
+ ccline
.cmdpos
,
3118 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
+ 1));
3119 ccline
.cmdlen
-= ccline
.cmdpos
- from
;
3120 ccline
.cmdpos
= from
;
3125 * this function is called when the screen size changes and with incremental
3133 need_wait_return
= FALSE
;
3146 if (ccline
.cmdfirstc
!= NUL
)
3147 msg_putchar(ccline
.cmdfirstc
);
3148 if (ccline
.cmdprompt
!= NULL
)
3150 msg_puts_attr(ccline
.cmdprompt
, ccline
.cmdattr
);
3151 ccline
.cmdindent
= msg_col
+ (msg_row
- cmdline_row
) * Columns
;
3152 /* do the reverse of set_cmdspos() */
3153 if (ccline
.cmdfirstc
!= NUL
)
3157 for (i
= ccline
.cmdindent
; i
> 0; --i
)
3162 * Redraw what is currently on the command line.
3170 /* when 'incsearch' is set there may be no command line while redrawing */
3171 if (ccline
.cmdbuff
== NULL
)
3173 windgoto(cmdline_row
, 0);
3181 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3183 draw_cmdline(0, ccline
.cmdlen
);
3185 msg_no_more
= FALSE
;
3187 set_cmdspos_cursor();
3190 * An emsg() before may have set msg_scroll. This is used in normal mode,
3191 * in cmdline mode we can reset them now.
3193 msg_scroll
= FALSE
; /* next message overwrites cmdline */
3195 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3196 * in cmdline mode */
3197 skip_redraw
= FALSE
;
3203 if (exmode_active
|| msg_scrolled
!= 0)
3204 cmdline_row
= Rows
- 1;
3206 cmdline_row
= W_WINROW(lastwin
) + lastwin
->w_height
3207 + W_STATUS_HEIGHT(lastwin
);
3216 #ifdef FEAT_RIGHTLEFT
3219 msg_row
= cmdline_row
+ (ccline
.cmdspos
/ (int)(Columns
- 1));
3220 msg_col
= (int)Columns
- (ccline
.cmdspos
% (int)(Columns
- 1)) - 1;
3227 msg_row
= cmdline_row
+ (ccline
.cmdspos
/ (int)Columns
);
3228 msg_col
= ccline
.cmdspos
% (int)Columns
;
3229 if (msg_row
>= Rows
)
3233 windgoto(msg_row
, msg_col
);
3234 #if defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
3235 redrawcmd_preedit();
3237 #ifdef MCH_CURSOR_SHAPE
3238 mch_update_cursor();
3247 #ifdef FEAT_RIGHTLEFT
3249 msg_col
= Columns
- 1;
3252 msg_col
= 0; /* always start in column 0 */
3253 if (clr
) /* clear the bottom line(s) */
3254 msg_clr_eos(); /* will reset clear_cmdline */
3255 windgoto(cmdline_row
, 0);
3259 * Check the word in front of the cursor for an abbreviation.
3260 * Called when the non-id character "c" has been entered.
3261 * When an abbreviation is recognized it is removed from the text with
3262 * backspaces and the replacement string is inserted, followed by "c".
3268 if (p_paste
|| no_abbr
) /* no abbreviations or in paste mode */
3271 return check_abbr(c
, ccline
.cmdbuff
, ccline
.cmdpos
, 0);
3275 * Return FAIL if this is not an appropriate context in which to do
3276 * completion of anything, return OK if it is (even if there are no matches).
3277 * For the caller, this means that the character is just passed through like a
3278 * normal character (instead of being expanded). This allows :s/^I^D etc.
3281 nextwild(xp
, type
, options
)
3284 int options
; /* extra options for ExpandOne() */
3293 if (xp
->xp_numfiles
== -1)
3295 set_expand_context(xp
);
3296 cmd_showtail
= expand_showtail(xp
);
3299 if (xp
->xp_context
== EXPAND_UNSUCCESSFUL
)
3302 return OK
; /* Something illegal on command line */
3304 if (xp
->xp_context
== EXPAND_NOTHING
)
3306 /* Caller can use the character as a normal char instead */
3310 MSG_PUTS("..."); /* show that we are busy */
3313 i
= (int)(xp
->xp_pattern
- ccline
.cmdbuff
);
3314 oldlen
= ccline
.cmdpos
- i
;
3316 if (type
== WILD_NEXT
|| type
== WILD_PREV
)
3319 * Get next/previous match for a previous expanded pattern.
3321 p2
= ExpandOne(xp
, NULL
, NULL
, 0, type
);
3326 * Translate string into pattern and expand it.
3328 if ((p1
= addstar(&ccline
.cmdbuff
[i
], oldlen
, xp
->xp_context
)) == NULL
)
3332 p2
= ExpandOne(xp
, p1
, vim_strnsave(&ccline
.cmdbuff
[i
], oldlen
),
3333 WILD_HOME_REPLACE
|WILD_ADD_SLASH
|WILD_SILENT
|WILD_ESCAPE
3336 /* longest match: make sure it is not shorter (happens with :help */
3337 if (p2
!= NULL
&& type
== WILD_LONGEST
)
3339 for (j
= 0; j
< oldlen
; ++j
)
3340 if (ccline
.cmdbuff
[i
+ j
] == '*'
3341 || ccline
.cmdbuff
[i
+ j
] == '?')
3343 if ((int)STRLEN(p2
) < j
)
3352 if (p2
!= NULL
&& !got_int
)
3354 difflen
= (int)STRLEN(p2
) - oldlen
;
3355 if (ccline
.cmdlen
+ difflen
> ccline
.cmdbufflen
- 4)
3357 v
= realloc_cmdbuff(ccline
.cmdlen
+ difflen
);
3358 xp
->xp_pattern
= ccline
.cmdbuff
+ i
;
3364 mch_memmove(&ccline
.cmdbuff
[ccline
.cmdpos
+ difflen
],
3365 &ccline
.cmdbuff
[ccline
.cmdpos
],
3366 (size_t)(ccline
.cmdlen
- ccline
.cmdpos
+ 1));
3367 mch_memmove(&ccline
.cmdbuff
[i
], p2
, STRLEN(p2
));
3368 ccline
.cmdlen
+= difflen
;
3369 ccline
.cmdpos
+= difflen
;
3377 /* When expanding a ":map" command and no matches are found, assume that
3378 * the key is supposed to be inserted literally */
3379 if (xp
->xp_context
== EXPAND_MAPPINGS
&& p2
== NULL
)
3382 if (xp
->xp_numfiles
<= 0 && p2
== NULL
)
3384 else if (xp
->xp_numfiles
== 1)
3385 /* free expanded pattern */
3386 (void)ExpandOne(xp
, NULL
, NULL
, 0, WILD_FREE
);
3392 * Do wildcard expansion on the string 'str'.
3393 * Chars that should not be expanded must be preceded with a backslash.
3394 * Return a pointer to allocated memory containing the new string.
3395 * Return NULL for failure.
3397 * "orig" is the originally expanded string, copied to allocated memory. It
3398 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3399 * WILD_PREV "orig" should be NULL.
3401 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3402 * is WILD_EXPAND_FREE or WILD_ALL.
3404 * mode = WILD_FREE: just free previously expanded matches
3405 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3406 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3407 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3408 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3409 * mode = WILD_ALL: return all matches concatenated
3410 * mode = WILD_LONGEST: return longest matched part
3412 * options = WILD_LIST_NOTFOUND: list entries without a match
3413 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3414 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3415 * options = WILD_NO_BEEP: Don't beep for multiple matches
3416 * options = WILD_ADD_SLASH: add a slash after directory names
3417 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3418 * options = WILD_SILENT: don't print warning messages
3419 * options = WILD_ESCAPE: put backslash before special chars
3421 * The variables xp->xp_context and xp->xp_backslash must have been set!
3424 ExpandOne(xp
, str
, orig
, options
, mode
)
3427 char_u
*orig
; /* allocated copy of original of expanded string */
3433 static char_u
*orig_save
= NULL
; /* kept value of orig */
3434 int orig_saved
= FALSE
;
3437 int non_suf_match
; /* number without matching suffix */
3440 * first handle the case of using an old match
3442 if (mode
== WILD_NEXT
|| mode
== WILD_PREV
)
3444 if (xp
->xp_numfiles
> 0)
3446 if (mode
== WILD_PREV
)
3449 findex
= xp
->xp_numfiles
;
3452 else /* mode == WILD_NEXT */
3456 * When wrapping around, return the original string, set findex to
3461 if (orig_save
== NULL
)
3462 findex
= xp
->xp_numfiles
- 1;
3466 if (findex
>= xp
->xp_numfiles
)
3468 if (orig_save
== NULL
)
3473 #ifdef FEAT_WILDMENU
3475 win_redr_status_matches(xp
, xp
->xp_numfiles
, xp
->xp_files
,
3476 findex
, cmd_showtail
);
3479 return vim_strsave(orig_save
);
3480 return vim_strsave(xp
->xp_files
[findex
]);
3486 /* free old names */
3487 if (xp
->xp_numfiles
!= -1 && mode
!= WILD_ALL
&& mode
!= WILD_LONGEST
)
3489 FreeWild(xp
->xp_numfiles
, xp
->xp_files
);
3490 xp
->xp_numfiles
= -1;
3491 vim_free(orig_save
);
3496 if (mode
== WILD_FREE
) /* only release file name */
3499 if (xp
->xp_numfiles
== -1)
3501 vim_free(orig_save
);
3508 if (ExpandFromContext(xp
, str
, &xp
->xp_numfiles
, &xp
->xp_files
,
3511 #ifdef FNAME_ILLEGAL
3512 /* Illegal file name has been silently skipped. But when there
3513 * are wildcards, the real problem is that there was no match,
3514 * causing the pattern to be added, which has illegal characters.
3516 if (!(options
& WILD_SILENT
) && (options
& WILD_LIST_NOTFOUND
))
3517 EMSG2(_(e_nomatch2
), str
);
3520 else if (xp
->xp_numfiles
== 0)
3522 if (!(options
& WILD_SILENT
))
3523 EMSG2(_(e_nomatch2
), str
);
3527 /* Escape the matches for use on the command line. */
3528 ExpandEscape(xp
, str
, xp
->xp_numfiles
, xp
->xp_files
, options
);
3531 * Check for matching suffixes in file names.
3533 if (mode
!= WILD_ALL
&& mode
!= WILD_LONGEST
)
3535 if (xp
->xp_numfiles
)
3536 non_suf_match
= xp
->xp_numfiles
;
3539 if ((xp
->xp_context
== EXPAND_FILES
3540 || xp
->xp_context
== EXPAND_DIRECTORIES
)
3541 && xp
->xp_numfiles
> 1)
3544 * More than one match; check suffix.
3545 * The files will have been sorted on matching suffix in
3546 * expand_wildcards, only need to check the first two.
3549 for (i
= 0; i
< 2; ++i
)
3550 if (match_suffix(xp
->xp_files
[i
]))
3553 if (non_suf_match
!= 1)
3555 /* Can we ever get here unless it's while expanding
3556 * interactively? If not, we can get rid of this all
3557 * together. Don't really want to wait for this message
3558 * (and possibly have to hit return to continue!).
3560 if (!(options
& WILD_SILENT
))
3562 else if (!(options
& WILD_NO_BEEP
))
3565 if (!(non_suf_match
!= 1 && mode
== WILD_EXPAND_FREE
))
3566 ss
= vim_strsave(xp
->xp_files
[0]);
3571 /* Find longest common part */
3572 if (mode
== WILD_LONGEST
&& xp
->xp_numfiles
> 0)
3574 for (len
= 0; xp
->xp_files
[0][len
]; ++len
)
3576 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3578 #ifdef CASE_INSENSITIVE_FILENAME
3579 if (xp
->xp_context
== EXPAND_DIRECTORIES
3580 || xp
->xp_context
== EXPAND_FILES
3581 || xp
->xp_context
== EXPAND_SHELLCMD
3582 || xp
->xp_context
== EXPAND_BUFFERS
)
3584 if (TOLOWER_LOC(xp
->xp_files
[i
][len
]) !=
3585 TOLOWER_LOC(xp
->xp_files
[0][len
]))
3590 if (xp
->xp_files
[i
][len
] != xp
->xp_files
[0][len
])
3593 if (i
< xp
->xp_numfiles
)
3595 if (!(options
& WILD_NO_BEEP
))
3600 ss
= alloc((unsigned)len
+ 1);
3602 vim_strncpy(ss
, xp
->xp_files
[0], (size_t)len
);
3603 findex
= -1; /* next p_wc gets first one */
3606 /* Concatenate all matching names */
3607 if (mode
== WILD_ALL
&& xp
->xp_numfiles
> 0)
3610 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3611 len
+= (long_u
)STRLEN(xp
->xp_files
[i
]) + 1;
3612 ss
= lalloc(len
, TRUE
);
3616 for (i
= 0; i
< xp
->xp_numfiles
; ++i
)
3618 STRCAT(ss
, xp
->xp_files
[i
]);
3619 if (i
!= xp
->xp_numfiles
- 1)
3620 STRCAT(ss
, (options
& WILD_USE_NL
) ? "\n" : " ");
3625 if (mode
== WILD_EXPAND_FREE
|| mode
== WILD_ALL
)
3628 /* Free "orig" if it wasn't stored in "orig_save". */
3636 * Prepare an expand structure for use.
3642 xp
->xp_pattern
= NULL
;
3643 xp
->xp_backslash
= XP_BS_NONE
;
3644 #ifndef BACKSLASH_IN_FILENAME
3645 xp
->xp_shell
= FALSE
;
3647 xp
->xp_numfiles
= -1;
3648 xp
->xp_files
= NULL
;
3649 #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3655 * Cleanup an expand structure after use.
3661 if (xp
->xp_numfiles
>= 0)
3663 FreeWild(xp
->xp_numfiles
, xp
->xp_files
);
3664 xp
->xp_numfiles
= -1;
3669 ExpandEscape(xp
, str
, numfiles
, files
, options
)
3680 * May change home directory back to "~"
3682 if (options
& WILD_HOME_REPLACE
)
3683 tilde_replace(str
, numfiles
, files
);
3685 if (options
& WILD_ESCAPE
)
3687 if (xp
->xp_context
== EXPAND_FILES
3688 || xp
->xp_context
== EXPAND_SHELLCMD
3689 || xp
->xp_context
== EXPAND_BUFFERS
3690 || xp
->xp_context
== EXPAND_DIRECTORIES
)
3693 * Insert a backslash into a file name before a space, \, %, #
3694 * and wildmatch characters, except '~'.
3696 for (i
= 0; i
< numfiles
; ++i
)
3698 /* for ":set path=" we need to escape spaces twice */
3699 if (xp
->xp_backslash
== XP_BS_THREE
)
3701 p
= vim_strsave_escaped(files
[i
], (char_u
*)" ");
3706 #if defined(BACKSLASH_IN_FILENAME)
3707 p
= vim_strsave_escaped(files
[i
], (char_u
*)" ");
3716 #ifdef BACKSLASH_IN_FILENAME
3717 p
= vim_strsave_fnameescape(files
[i
], FALSE
);
3719 p
= vim_strsave_fnameescape(files
[i
], xp
->xp_shell
);
3727 /* If 'str' starts with "\~", replace "~" at start of
3728 * files[i] with "\~". */
3729 if (str
[0] == '\\' && str
[1] == '~' && files
[i
][0] == '~')
3730 escape_fname(&files
[i
]);
3732 xp
->xp_backslash
= XP_BS_NONE
;
3734 /* If the first file starts with a '+' escape it. Otherwise it
3735 * could be seen as "+cmd". */
3736 if (*files
[0] == '+')
3737 escape_fname(&files
[0]);
3739 else if (xp
->xp_context
== EXPAND_TAGS
)
3742 * Insert a backslash before characters in a tag name that
3743 * would terminate the ":tag" command.
3745 for (i
= 0; i
< numfiles
; ++i
)
3747 p
= vim_strsave_escaped(files
[i
], (char_u
*)"\\|\"");
3759 * Escape special characters in "fname" for when used as a file name argument
3760 * after a Vim command, or, when "shell" is non-zero, a shell command.
3761 * Returns the result in allocated memory.
3764 vim_strsave_fnameescape(fname
, shell
)
3769 #ifdef BACKSLASH_IN_FILENAME
3773 /* Don't escape '[' and '{' if they are in 'isfname'. */
3774 for (p
= PATH_ESC_CHARS
; *p
!= NUL
; ++p
)
3775 if ((*p
!= '[' && *p
!= '{') || !vim_isfilec(*p
))
3778 p
= vim_strsave_escaped(fname
, buf
);
3780 p
= vim_strsave_escaped(fname
, shell
? SHELL_ESC_CHARS
: PATH_ESC_CHARS
);
3781 if (shell
&& csh_like_shell() && p
!= NULL
)
3785 /* For csh and similar shells need to put two backslashes before '!'.
3786 * One is taken by Vim, one by the shell. */
3787 s
= vim_strsave_escaped(p
, (char_u
*)"!");
3793 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3794 * ":write". "cd -" has a special meaning. */
3795 if (*p
== '>' || *p
== '+' || (*p
== '-' && p
[1] == NUL
))
3802 * Put a backslash before the file name in "pp", which is in allocated memory.
3810 p
= alloc((unsigned)(STRLEN(*pp
) + 2));
3821 * For each file name in files[num_files]:
3822 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3825 tilde_replace(orig_pat
, num_files
, files
)
3833 if (orig_pat
[0] == '~' && vim_ispathsep(orig_pat
[1]))
3835 for (i
= 0; i
< num_files
; ++i
)
3837 p
= home_replace_save(NULL
, files
[i
]);
3848 * Show all matches for completion on the command line.
3849 * Returns EXPAND_NOTHING when the character that triggered expansion should
3850 * be inserted like a normal character.
3853 showmatches(xp
, wildmenu
)
3855 int wildmenu UNUSED
;
3857 #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3859 char_u
**files_found
;
3869 if (xp
->xp_numfiles
== -1)
3871 set_expand_context(xp
);
3872 i
= expand_cmdline(xp
, ccline
.cmdbuff
, ccline
.cmdpos
,
3873 &num_files
, &files_found
);
3874 showtail
= expand_showtail(xp
);
3881 num_files
= xp
->xp_numfiles
;
3882 files_found
= xp
->xp_files
;
3883 showtail
= cmd_showtail
;
3886 #ifdef FEAT_WILDMENU
3890 msg_didany
= FALSE
; /* lines_left will be set */
3891 msg_start(); /* prepare for paging */
3894 cmdline_row
= msg_row
;
3895 msg_didany
= FALSE
; /* lines_left will be set again */
3896 msg_start(); /* prepare for paging */
3897 #ifdef FEAT_WILDMENU
3902 got_int
= FALSE
; /* only int. the completion, not the cmd line */
3903 #ifdef FEAT_WILDMENU
3905 win_redr_status_matches(xp
, num_files
, files_found
, 0, showtail
);
3909 /* find the length of the longest file name */
3911 for (i
= 0; i
< num_files
; ++i
)
3913 if (!showtail
&& (xp
->xp_context
== EXPAND_FILES
3914 || xp
->xp_context
== EXPAND_SHELLCMD
3915 || xp
->xp_context
== EXPAND_BUFFERS
))
3917 home_replace(NULL
, files_found
[i
], NameBuff
, MAXPATHL
, TRUE
);
3918 j
= vim_strsize(NameBuff
);
3921 j
= vim_strsize(L_SHOWFILE(i
));
3926 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3930 /* compute the number of columns and lines for the listing */
3931 maxlen
+= 2; /* two spaces between file names */
3932 columns
= ((int)Columns
+ 2) / maxlen
;
3935 lines
= (num_files
+ columns
- 1) / columns
;
3938 attr
= hl_attr(HLF_D
); /* find out highlighting for directories */
3940 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3942 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T
));
3944 msg_advance(maxlen
- 3);
3945 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T
));
3948 /* list the files line by line */
3949 for (i
= 0; i
< lines
; ++i
)
3952 for (k
= i
; k
< num_files
; k
+= lines
)
3954 if (xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
3956 msg_outtrans_attr(files_found
[k
], hl_attr(HLF_D
));
3957 p
= files_found
[k
] + STRLEN(files_found
[k
]) + 1;
3958 msg_advance(maxlen
+ 1);
3960 msg_advance(maxlen
+ 3);
3961 msg_puts_long_attr(p
+ 2, hl_attr(HLF_D
));
3964 for (j
= maxlen
- lastlen
; --j
>= 0; )
3966 if (xp
->xp_context
== EXPAND_FILES
3967 || xp
->xp_context
== EXPAND_SHELLCMD
3968 || xp
->xp_context
== EXPAND_BUFFERS
)
3970 /* highlight directories */
3971 j
= (mch_isdir(files_found
[k
]));
3976 home_replace(NULL
, files_found
[k
], NameBuff
, MAXPATHL
,
3986 lastlen
= msg_outtrans_attr(p
, j
? attr
: 0);
3988 if (msg_col
> 0) /* when not wrapped around */
3993 out_flush(); /* show one line at a time */
4002 * we redraw the command below the lines that we have just listed
4003 * This is a bit tricky, but it saves a lot of screen updating.
4005 cmdline_row
= msg_row
; /* will put it back later */
4008 if (xp
->xp_numfiles
== -1)
4009 FreeWild(num_files
, files_found
);
4015 * Private gettail for showmatches() (and win_redr_status_matches()):
4016 * Find tail of file name path, but ignore trailing "/".
4024 int had_sep
= FALSE
;
4026 for (p
= s
; *p
!= NUL
; )
4028 if (vim_ispathsep(*p
)
4029 #ifdef BACKSLASH_IN_FILENAME
4030 && !rem_backslash(p
)
4045 * Return TRUE if we only need to show the tail of completion matches.
4046 * When not completing file names or there is a wildcard in the path FALSE is
4056 /* When not completing file names a "/" may mean something different. */
4057 if (xp
->xp_context
!= EXPAND_FILES
4058 && xp
->xp_context
!= EXPAND_SHELLCMD
4059 && xp
->xp_context
!= EXPAND_DIRECTORIES
)
4062 end
= gettail(xp
->xp_pattern
);
4063 if (end
== xp
->xp_pattern
) /* there is no path separator */
4066 for (s
= xp
->xp_pattern
; s
< end
; s
++)
4068 /* Skip escaped wildcards. Only when the backslash is not a path
4069 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4070 if (rem_backslash(s
))
4072 else if (vim_strchr((char_u
*)"*?[", *s
) != NULL
)
4079 * Prepare a string for expansion.
4080 * When expanding file names: The string will be used with expand_wildcards().
4081 * Copy the file name into allocated memory and add a '*' at the end.
4082 * When expanding other names: The string will be used with regcomp(). Copy
4083 * the name into allocated memory and prepend "^".
4086 addstar(fname
, len
, context
)
4089 int context
; /* EXPAND_FILES etc. */
4096 if (context
!= EXPAND_FILES
4097 && context
!= EXPAND_SHELLCMD
4098 && context
!= EXPAND_DIRECTORIES
)
4101 * Matching will be done internally (on something other than files).
4102 * So we convert the file-matching-type wildcards into our kind for
4103 * use with vim_regcomp(). First work out how long it will be:
4106 /* For help tags the translation is done in find_help_tags().
4107 * For a tag pattern starting with "/" no translation is needed. */
4108 if (context
== EXPAND_HELP
4109 || context
== EXPAND_COLORS
4110 || context
== EXPAND_COMPILER
4111 || (context
== EXPAND_TAGS
&& fname
[0] == '/'))
4112 retval
= vim_strnsave(fname
, len
);
4115 new_len
= len
+ 2; /* +2 for '^' at start, NUL at end */
4116 for (i
= 0; i
< len
; i
++)
4118 if (fname
[i
] == '*' || fname
[i
] == '~')
4119 new_len
++; /* '*' needs to be replaced by ".*"
4120 '~' needs to be replaced by "\~" */
4122 /* Buffer names are like file names. "." should be literal */
4123 if (context
== EXPAND_BUFFERS
&& fname
[i
] == '.')
4124 new_len
++; /* "." becomes "\." */
4126 /* Custom expansion takes care of special things, match
4127 * backslashes literally (perhaps also for other types?) */
4128 if ((context
== EXPAND_USER_DEFINED
4129 || context
== EXPAND_USER_LIST
) && fname
[i
] == '\\')
4130 new_len
++; /* '\' becomes "\\" */
4132 retval
= alloc(new_len
);
4137 for (i
= 0; i
< len
; i
++, j
++)
4139 /* Skip backslash. But why? At least keep it for custom
4141 if (context
!= EXPAND_USER_DEFINED
4142 && context
!= EXPAND_USER_LIST
4149 case '*': retval
[j
++] = '.';
4151 case '~': retval
[j
++] = '\\';
4153 case '?': retval
[j
] = '.';
4155 case '.': if (context
== EXPAND_BUFFERS
)
4158 case '\\': if (context
== EXPAND_USER_DEFINED
4159 || context
== EXPAND_USER_LIST
)
4163 retval
[j
] = fname
[i
];
4171 retval
= alloc(len
+ 4);
4174 vim_strncpy(retval
, fname
, len
);
4177 * Don't add a star to *, ~, ~user, $var or `cmd`.
4178 * * would become **, which walks the whole tree.
4179 * ~ would be at the start of the file name, but not the tail.
4180 * $ could be anywhere in the tail.
4181 * ` could be anywhere in the file name.
4182 * When the name ends in '$' don't add a star, remove the '$'.
4184 tail
= gettail(retval
);
4185 if ((*retval
!= '~' || tail
!= retval
)
4186 && (len
== 0 || retval
[len
- 1] != '*')
4187 && vim_strchr(tail
, '$') == NULL
4188 && vim_strchr(retval
, '`') == NULL
)
4189 retval
[len
++] = '*';
4190 else if (len
> 0 && retval
[len
- 1] == '$')
4199 * Must parse the command line so far to work out what context we are in.
4200 * Completion can then be done based on that context.
4201 * This routine sets the variables:
4202 * xp->xp_pattern The start of the pattern to be expanded within
4203 * the command line (ends at the cursor).
4204 * xp->xp_context The type of thing to expand. Will be one of:
4206 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4207 * the command line, like an unknown command. Caller
4209 * EXPAND_NOTHING Unrecognised context for completion, use char like
4210 * a normal char, rather than for completion. eg
4212 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4214 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4215 * EXPAND_FILES After command with XFILE set, or after setting
4216 * with P_EXPAND set. eg :e ^I, :w>>^I
4217 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4218 * when we know only directories are of interest. eg
4220 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
4221 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4222 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4223 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4224 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4225 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4226 * EXPAND_EVENTS Complete event names
4227 * EXPAND_SYNTAX Complete :syntax command arguments
4228 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4229 * EXPAND_AUGROUP Complete autocommand group names
4230 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4231 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4232 * eg :unmap a^I , :cunab x^I
4233 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4235 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4236 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4237 * names in expressions, eg :while s^I
4238 * EXPAND_ENV_VARS Complete environment variable names
4241 set_expand_context(xp
)
4244 /* only expansion for ':', '>' and '=' command-lines */
4245 if (ccline
.cmdfirstc
!= ':'
4247 && ccline
.cmdfirstc
!= '>' && ccline
.cmdfirstc
!= '='
4252 xp
->xp_context
= EXPAND_NOTHING
;
4255 set_cmd_context(xp
, ccline
.cmdbuff
, ccline
.cmdlen
, ccline
.cmdpos
);
4259 set_cmd_context(xp
, str
, len
, col
)
4261 char_u
*str
; /* start of command line */
4262 int len
; /* length of command line (excl. NUL) */
4263 int col
; /* position of cursor */
4269 * Avoid a UMR warning from Purify, only save the character if it has been
4273 old_char
= str
[col
];
4278 if (ccline
.cmdfirstc
== '=')
4280 # ifdef FEAT_CMDL_COMPL
4281 /* pass CMD_SIZE because there is no real command */
4282 set_context_for_expression(xp
, str
, CMD_SIZE
);
4285 else if (ccline
.input_fn
)
4287 xp
->xp_context
= ccline
.xp_context
;
4288 xp
->xp_pattern
= ccline
.cmdbuff
;
4289 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
4290 xp
->xp_arg
= ccline
.xp_arg
;
4295 while (nextcomm
!= NULL
)
4296 nextcomm
= set_one_cmd_context(xp
, nextcomm
);
4298 str
[col
] = old_char
;
4302 * Expand the command line "str" from context "xp".
4303 * "xp" must have been set by set_cmd_context().
4304 * xp->xp_pattern points into "str", to where the text that is to be expanded
4306 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4308 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4309 * key that triggered expansion literally.
4310 * Returns EXPAND_OK otherwise.
4313 expand_cmdline(xp
, str
, col
, matchcount
, matches
)
4315 char_u
*str
; /* start of command line */
4316 int col
; /* position of cursor */
4317 int *matchcount
; /* return: nr of matches */
4318 char_u
***matches
; /* return: array of pointers to matches */
4320 char_u
*file_str
= NULL
;
4322 if (xp
->xp_context
== EXPAND_UNSUCCESSFUL
)
4325 return EXPAND_UNSUCCESSFUL
; /* Something illegal on command line */
4327 if (xp
->xp_context
== EXPAND_NOTHING
)
4329 /* Caller can use the character as a normal char instead */
4330 return EXPAND_NOTHING
;
4333 /* add star to file name, or convert to regexp if not exp. files. */
4334 file_str
= addstar(xp
->xp_pattern
,
4335 (int)(str
+ col
- xp
->xp_pattern
), xp
->xp_context
);
4336 if (file_str
== NULL
)
4337 return EXPAND_UNSUCCESSFUL
;
4339 /* find all files that match the description */
4340 if (ExpandFromContext(xp
, file_str
, matchcount
, matches
,
4341 WILD_ADD_SLASH
|WILD_SILENT
) == FAIL
)
4351 #ifdef FEAT_MULTI_LANG
4353 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4355 static void cleanup_help_tags
__ARGS((int num_file
, char_u
**file
));
4358 cleanup_help_tags(num_file
, file
)
4365 for (i
= 0; i
< num_file
; ++i
)
4367 len
= (int)STRLEN(file
[i
]) - 3;
4368 if (len
> 0 && STRCMP(file
[i
] + len
, "@en") == 0)
4370 /* Sorting on priority means the same item in another language may
4371 * be anywhere. Search all items for a match up to the "@en". */
4372 for (j
= 0; j
< num_file
; ++j
)
4374 && (int)STRLEN(file
[j
]) == len
+ 3
4375 && STRNCMP(file
[i
], file
[j
], len
+ 1) == 0)
4385 * Do the expansion based on xp->xp_context and "pat".
4388 ExpandFromContext(xp
, pat
, num_file
, file
, options
)
4395 #ifdef FEAT_CMDL_COMPL
4396 regmatch_T regmatch
;
4401 flags
= EW_DIR
; /* include directories */
4402 if (options
& WILD_LIST_NOTFOUND
)
4403 flags
|= EW_NOTFOUND
;
4404 if (options
& WILD_ADD_SLASH
)
4405 flags
|= EW_ADDSLASH
;
4406 if (options
& WILD_KEEP_ALL
)
4407 flags
|= EW_KEEPALL
;
4408 if (options
& WILD_SILENT
)
4411 if (xp
->xp_context
== EXPAND_FILES
|| xp
->xp_context
== EXPAND_DIRECTORIES
)
4414 * Expand file or directory names.
4416 int free_pat
= FALSE
;
4419 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4421 if (xp
->xp_backslash
!= XP_BS_NONE
)
4424 pat
= vim_strsave(pat
);
4425 for (i
= 0; pat
[i
]; ++i
)
4428 if (xp
->xp_backslash
== XP_BS_THREE
4429 && pat
[i
+ 1] == '\\'
4430 && pat
[i
+ 2] == '\\'
4431 && pat
[i
+ 3] == ' ')
4432 STRMOVE(pat
+ i
, pat
+ i
+ 3);
4433 if (xp
->xp_backslash
== XP_BS_ONE
4434 && pat
[i
+ 1] == ' ')
4435 STRMOVE(pat
+ i
, pat
+ i
+ 1);
4439 if (xp
->xp_context
== EXPAND_FILES
)
4442 flags
= (flags
| EW_DIR
) & ~EW_FILE
;
4443 ret
= expand_wildcards(1, &pat
, num_file
, file
, flags
);
4449 *file
= (char_u
**)"";
4451 if (xp
->xp_context
== EXPAND_HELP
)
4453 /* With an empty argument we would get all the help tags, which is
4454 * very slow. Get matches for "help" instead. */
4455 if (find_help_tags(*pat
== NUL
? (char_u
*)"help" : pat
,
4456 num_file
, file
, FALSE
) == OK
)
4458 #ifdef FEAT_MULTI_LANG
4459 cleanup_help_tags(*num_file
, *file
);
4466 #ifndef FEAT_CMDL_COMPL
4469 if (xp
->xp_context
== EXPAND_SHELLCMD
)
4470 return expand_shellcmd(pat
, num_file
, file
, flags
);
4471 if (xp
->xp_context
== EXPAND_OLD_SETTING
)
4472 return ExpandOldSetting(num_file
, file
);
4473 if (xp
->xp_context
== EXPAND_BUFFERS
)
4474 return ExpandBufnames(pat
, num_file
, file
, options
);
4475 if (xp
->xp_context
== EXPAND_TAGS
4476 || xp
->xp_context
== EXPAND_TAGS_LISTFILES
)
4477 return expand_tags(xp
->xp_context
== EXPAND_TAGS
, pat
, num_file
, file
);
4478 if (xp
->xp_context
== EXPAND_COLORS
)
4479 return ExpandRTDir(pat
, num_file
, file
, "colors");
4480 if (xp
->xp_context
== EXPAND_COMPILER
)
4481 return ExpandRTDir(pat
, num_file
, file
, "compiler");
4482 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4483 if (xp
->xp_context
== EXPAND_USER_LIST
)
4484 return ExpandUserList(xp
, num_file
, file
);
4487 regmatch
.regprog
= vim_regcomp(pat
, p_magic
? RE_MAGIC
: 0);
4488 if (regmatch
.regprog
== NULL
)
4491 /* set ignore-case according to p_ic, p_scs and pat */
4492 regmatch
.rm_ic
= ignorecase(pat
);
4494 if (xp
->xp_context
== EXPAND_SETTINGS
4495 || xp
->xp_context
== EXPAND_BOOL_SETTINGS
)
4496 ret
= ExpandSettings(xp
, ®match
, num_file
, file
);
4497 else if (xp
->xp_context
== EXPAND_MAPPINGS
)
4498 ret
= ExpandMappings(®match
, num_file
, file
);
4499 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4500 else if (xp
->xp_context
== EXPAND_USER_DEFINED
)
4501 ret
= ExpandUserDefined(xp
, ®match
, num_file
, file
);
4505 static struct expgen
4508 char_u
*((*func
)__ARGS((expand_T
*, int)));
4512 {EXPAND_COMMANDS
, get_command_name
, FALSE
},
4513 #ifdef FEAT_USR_CMDS
4514 {EXPAND_USER_COMMANDS
, get_user_commands
, FALSE
},
4515 {EXPAND_USER_CMD_FLAGS
, get_user_cmd_flags
, FALSE
},
4516 {EXPAND_USER_NARGS
, get_user_cmd_nargs
, FALSE
},
4517 {EXPAND_USER_COMPLETE
, get_user_cmd_complete
, FALSE
},
4520 {EXPAND_USER_VARS
, get_user_var_name
, FALSE
},
4521 {EXPAND_FUNCTIONS
, get_function_name
, FALSE
},
4522 {EXPAND_USER_FUNC
, get_user_func_name
, FALSE
},
4523 {EXPAND_EXPRESSION
, get_expr_name
, FALSE
},
4526 {EXPAND_MENUS
, get_menu_name
, FALSE
},
4527 {EXPAND_MENUNAMES
, get_menu_names
, FALSE
},
4530 {EXPAND_SYNTAX
, get_syntax_name
, TRUE
},
4532 {EXPAND_HIGHLIGHT
, get_highlight_name
, TRUE
},
4534 {EXPAND_EVENTS
, get_event_name
, TRUE
},
4535 {EXPAND_AUGROUP
, get_augroup_name
, TRUE
},
4538 {EXPAND_CSCOPE
, get_cscope_name
, TRUE
},
4541 {EXPAND_SIGN
, get_sign_name
, TRUE
},
4543 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4544 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4545 {EXPAND_LANGUAGE
, get_lang_arg
, TRUE
},
4547 {EXPAND_ENV_VARS
, get_env_name
, TRUE
},
4548 #ifdef FEAT_GUI_MACVIM
4549 {EXPAND_MACACTION
, get_macaction_name
, FALSE
},
4555 * Find a context in the table and call the ExpandGeneric() with the
4556 * right function to do the expansion.
4559 for (i
= 0; i
< (int)(sizeof(tab
) / sizeof(struct expgen
)); ++i
)
4560 if (xp
->xp_context
== tab
[i
].context
)
4563 regmatch
.rm_ic
= TRUE
;
4564 ret
= ExpandGeneric(xp
, ®match
, num_file
, file
, tab
[i
].func
);
4569 vim_free(regmatch
.regprog
);
4572 #endif /* FEAT_CMDL_COMPL */
4575 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4577 * Expand a list of names.
4579 * Generic function for command line completion. It calls a function to
4580 * obtain strings, one by one. The strings are matched against a regexp
4581 * program. Matching strings are copied into an array, which is returned.
4583 * Returns OK when no problems encountered, FAIL for error (out of memory).
4586 ExpandGeneric(xp
, regmatch
, num_file
, file
, func
)
4588 regmatch_T
*regmatch
;
4591 char_u
*((*func
)__ARGS((expand_T
*, int)));
4592 /* returns a string from the list */
4599 /* do this loop twice:
4600 * round == 0: count the number of matching names
4601 * round == 1: copy the matching names into allocated memory
4603 for (round
= 0; round
<= 1; ++round
)
4607 str
= (*func
)(xp
, i
);
4608 if (str
== NULL
) /* end of list */
4610 if (*str
== NUL
) /* skip empty strings */
4613 if (vim_regexec(regmatch
, str
, (colnr_T
)0))
4617 str
= vim_strsave_escaped(str
, (char_u
*)" \t\\.");
4618 (*file
)[count
] = str
;
4620 if (func
== get_menu_names
&& str
!= NULL
)
4622 /* test for separator added by get_menu_names() */
4623 str
+= STRLEN(str
) - 1;
4637 *file
= (char_u
**)alloc((unsigned)(count
* sizeof(char_u
*)));
4640 *file
= (char_u
**)"";
4647 /* Sort the results. Keep menu's in the specified order. */
4648 if (xp
->xp_context
!= EXPAND_MENUNAMES
&& xp
->xp_context
!= EXPAND_MENUS
)
4649 sort_strings(*file
, *num_file
);
4651 #ifdef FEAT_CMDL_COMPL
4652 /* Reset the variables used for special highlight names expansion, so that
4653 * they don't show up when getting normal highlight names by ID. */
4654 reset_expand_highlight();
4661 * Complete a shell command.
4662 * Returns FAIL or OK;
4665 expand_shellcmd(filepat
, num_file
, file
, flagsarg
)
4666 char_u
*filepat
; /* pattern to match with command names */
4667 int *num_file
; /* return: number of matches */
4668 char_u
***file
; /* return: array with matches */
4669 int flagsarg
; /* EW_ flags */
4674 int mustfree
= FALSE
;
4676 char_u
*buf
= alloc(MAXPATHL
);
4679 int flags
= flagsarg
;
4685 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4687 pat
= vim_strsave(filepat
);
4688 for (i
= 0; pat
[i
]; ++i
)
4689 if (pat
[i
] == '\\' && pat
[i
+ 1] == ' ')
4690 STRMOVE(pat
+ i
, pat
+ i
+ 1);
4692 flags
|= EW_FILE
| EW_EXEC
;
4694 /* For an absolute name we don't use $PATH. */
4695 if (mch_isFullName(pat
))
4696 path
= (char_u
*)" ";
4697 else if ((pat
[0] == '.' && (vim_ispathsep(pat
[1])
4698 || (pat
[1] == '.' && vim_ispathsep(pat
[2])))))
4699 path
= (char_u
*)".";
4701 path
= vim_getenv((char_u
*)"PATH", &mustfree
);
4704 * Go over all directories in $PATH. Expand matches in that directory and
4705 * collect them in "ga".
4707 ga_init2(&ga
, (int)sizeof(char *), 10);
4708 for (s
= path
; *s
!= NUL
; s
= e
)
4711 ++s
; /* Skip space used for absolute path name. */
4713 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4714 e
= vim_strchr(s
, ';');
4716 e
= vim_strchr(s
, ':');
4722 if (l
> MAXPATHL
- 5)
4724 vim_strncpy(buf
, s
, l
);
4727 vim_strncpy(buf
+ l
, pat
, MAXPATHL
- 1 - l
);
4729 /* Expand matches in one directory of $PATH. */
4730 ret
= expand_wildcards(1, &buf
, num_file
, file
, flags
);
4733 if (ga_grow(&ga
, *num_file
) == FAIL
)
4734 FreeWild(*num_file
, *file
);
4737 for (i
= 0; i
< *num_file
; ++i
)
4742 /* Remove the path again. */
4744 ((char_u
**)ga
.ga_data
)[ga
.ga_len
++] = s
;
4756 *num_file
= ga
.ga_len
;
4766 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4767 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
));
4770 * Call "user_expand_func()" to invoke a user defined VimL function and return
4771 * the result (either a string or a List).
4774 call_user_expand_func(user_expand_func
, xp
, num_file
, file
)
4775 void *(*user_expand_func
) __ARGS((char_u
*, int, char_u
**, int));
4783 int save_current_SID
= current_SID
;
4785 struct cmdline_info save_ccline
;
4787 if (xp
->xp_arg
== NULL
|| xp
->xp_arg
[0] == '\0')
4792 if (ccline
.cmdbuff
== NULL
)
4794 /* Completion from Insert mode, pass fake arguments. */
4796 sprintf((char *)num
, "%d", (int)STRLEN(xp
->xp_pattern
));
4797 args
[1] = xp
->xp_pattern
;
4801 /* Completion on the command line, pass real arguments. */
4802 keep
= ccline
.cmdbuff
[ccline
.cmdlen
];
4803 ccline
.cmdbuff
[ccline
.cmdlen
] = 0;
4804 sprintf((char *)num
, "%d", ccline
.cmdpos
);
4805 args
[1] = ccline
.cmdbuff
;
4807 args
[0] = xp
->xp_pattern
;
4810 /* Save the cmdline, we don't know what the function may do. */
4811 save_ccline
= ccline
;
4812 ccline
.cmdbuff
= NULL
;
4813 ccline
.cmdprompt
= NULL
;
4814 current_SID
= xp
->xp_scriptID
;
4816 ret
= user_expand_func(xp
->xp_arg
, 3, args
, FALSE
);
4818 ccline
= save_ccline
;
4819 current_SID
= save_current_SID
;
4820 if (ccline
.cmdbuff
!= NULL
)
4821 ccline
.cmdbuff
[ccline
.cmdlen
] = keep
;
4827 * Expand names with a function defined by the user.
4830 ExpandUserDefined(xp
, regmatch
, num_file
, file
)
4832 regmatch_T
*regmatch
;
4842 retstr
= call_user_expand_func(call_func_retstr
, xp
, num_file
, file
);
4846 ga_init2(&ga
, (int)sizeof(char *), 3);
4847 for (s
= retstr
; *s
!= NUL
; s
= e
)
4849 e
= vim_strchr(s
, '\n');
4855 if (xp
->xp_pattern
[0] && vim_regexec(regmatch
, s
, (colnr_T
)0) == 0)
4863 if (ga_grow(&ga
, 1) == FAIL
)
4866 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] = vim_strnsave(s
, (int)(e
- s
));
4875 *num_file
= ga
.ga_len
;
4880 * Expand names with a list returned by a function defined by the user.
4883 ExpandUserList(xp
, num_file
, file
)
4892 retlist
= call_user_expand_func(call_func_retlist
, xp
, num_file
, file
);
4893 if (retlist
== NULL
)
4896 ga_init2(&ga
, (int)sizeof(char *), 3);
4897 /* Loop over the items in the list. */
4898 for (li
= retlist
->lv_first
; li
!= NULL
; li
= li
->li_next
)
4900 if (li
->li_tv
.v_type
!= VAR_STRING
|| li
->li_tv
.vval
.v_string
== NULL
)
4901 continue; /* Skip non-string items and empty strings */
4903 if (ga_grow(&ga
, 1) == FAIL
)
4906 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] =
4907 vim_strsave(li
->li_tv
.vval
.v_string
);
4910 list_unref(retlist
);
4913 *num_file
= ga
.ga_len
;
4919 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4920 * or compiler names.
4923 ExpandRTDir(pat
, num_file
, file
, dirname
)
4927 char *dirname
; /* "colors" or "compiler" */
4936 s
= alloc((unsigned)(STRLEN(pat
) + STRLEN(dirname
) + 7));
4939 sprintf((char *)s
, "%s/%s*.vim", dirname
, pat
);
4940 all
= globpath(p_rtp
, s
, 0);
4945 ga_init2(&ga
, (int)sizeof(char *), 3);
4946 for (s
= all
; *s
!= NUL
; s
= e
)
4948 e
= vim_strchr(s
, '\n');
4951 if (ga_grow(&ga
, 1) == FAIL
)
4953 if (e
- 4 > s
&& STRNICMP(e
- 4, ".vim", 4) == 0)
4955 for (s
= e
- 4; s
> all
; mb_ptr_back(all
, s
))
4956 if (*s
== '\n' || vim_ispathsep(*s
))
4959 ((char_u
**)ga
.ga_data
)[ga
.ga_len
] =
4960 vim_strnsave(s
, (int)(e
- s
- 4));
4968 *num_file
= ga
.ga_len
;
4974 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4976 * Expand "file" for all comma-separated directories in "path".
4977 * Returns an allocated string with all matches concatenated, separated by
4978 * newlines. Returns NULL for an error or no matches.
4981 globpath(path
, file
, expand_options
)
4995 buf
= alloc(MAXPATHL
);
5000 xpc
.xp_context
= EXPAND_FILES
;
5002 ga_init2(&ga
, 1, 100);
5004 /* Loop over all entries in {path}. */
5005 while (*path
!= NUL
)
5007 /* Copy one item of the path to buf[] and concatenate the file name. */
5008 copy_option_part(&path
, buf
, MAXPATHL
, ",");
5009 if (STRLEN(buf
) + STRLEN(file
) + 2 < MAXPATHL
)
5013 if (ExpandFromContext(&xpc
, buf
, &num_p
, &p
,
5014 WILD_SILENT
|expand_options
) != FAIL
&& num_p
> 0)
5016 ExpandEscape(&xpc
, buf
, num_p
, p
, WILD_SILENT
|expand_options
);
5017 for (len
= 0, i
= 0; i
< num_p
; ++i
)
5018 len
+= (int)STRLEN(p
[i
]) + 1;
5020 /* Concatenate new results to previous ones. */
5021 if (ga_grow(&ga
, len
) == OK
)
5023 cur
= (char_u
*)ga
.ga_data
+ ga
.ga_len
;
5024 for (i
= 0; i
< num_p
; ++i
)
5027 cur
+= STRLEN(p
[i
]);
5037 *--cur
= 0; /* Replace trailing newline with NUL */
5040 return (char_u
*)ga
.ga_data
;
5045 #if defined(FEAT_CMDHIST) || defined(PROTO)
5047 /*********************************
5048 * Command line history stuff *
5049 *********************************/
5052 * Translate a history character to the associated type number.
5066 return HIST_SEARCH
; /* must be '?' or '/' */
5070 * Table of history names.
5071 * These names are used in :history and various hist...() functions.
5072 * It is sufficient to give the significant prefix of a history name.
5075 static char *(history_names
[]) =
5086 * init_history() - Initialize the command line history.
5087 * Also used to re-allocate the history when the size changes.
5092 int newlen
; /* new length of history table */
5099 * If size of history table changed, reallocate it
5102 if (newlen
!= hislen
) /* history length changed */
5104 for (type
= 0; type
< HIST_COUNT
; ++type
) /* adjust the tables */
5108 temp
= (histentry_T
*)lalloc(
5109 (long_u
)(newlen
* sizeof(histentry_T
)), TRUE
);
5110 if (temp
== NULL
) /* out of memory! */
5112 if (type
== 0) /* first one: just keep the old length */
5117 /* Already changed one table, now we can only have zero
5118 * length for all tables. */
5126 if (newlen
== 0 || temp
!= NULL
)
5128 if (hisidx
[type
] < 0) /* there are no entries yet */
5130 for (i
= 0; i
< newlen
; ++i
)
5133 temp
[i
].hisstr
= NULL
;
5136 else if (newlen
> hislen
) /* array becomes bigger */
5138 for (i
= 0; i
<= hisidx
[type
]; ++i
)
5139 temp
[i
] = history
[type
][i
];
5141 for ( ; i
<= newlen
- (hislen
- hisidx
[type
]); ++i
)
5144 temp
[i
].hisstr
= NULL
;
5146 for ( ; j
< hislen
; ++i
, ++j
)
5147 temp
[i
] = history
[type
][j
];
5149 else /* array becomes smaller or 0 */
5152 for (i
= newlen
- 1; ; --i
)
5154 if (i
>= 0) /* copy newest entries */
5155 temp
[i
] = history
[type
][j
];
5156 else /* remove older entries */
5157 vim_free(history
[type
][j
].hisstr
);
5160 if (j
== hisidx
[type
])
5163 hisidx
[type
] = newlen
- 1;
5165 vim_free(history
[type
]);
5166 history
[type
] = temp
;
5174 * Check if command line 'str' is already in history.
5175 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5178 in_history(type
, str
, move_to_front
)
5181 int move_to_front
; /* Move the entry to the front if it exists */
5186 if (hisidx
[type
] < 0)
5191 if (history
[type
][i
].hisstr
== NULL
)
5193 if (STRCMP(str
, history
[type
][i
].hisstr
) == 0)
5202 } while (i
!= hisidx
[type
]);
5206 str
= history
[type
][i
].hisstr
;
5207 while (i
!= hisidx
[type
])
5211 history
[type
][last_i
] = history
[type
][i
];
5214 history
[type
][i
].hisstr
= str
;
5215 history
[type
][i
].hisnum
= ++hisnum
[type
];
5222 * Convert history name (from table above) to its HIST_ equivalent.
5223 * When "name" is empty, return "cmd" history.
5224 * Returns -1 for unknown history name.
5231 int len
= (int)STRLEN(name
);
5233 /* No argument: use current history. */
5235 return hist_char2type(ccline
.cmdfirstc
);
5237 for (i
= 0; history_names
[i
] != NULL
; ++i
)
5238 if (STRNICMP(name
, history_names
[i
], len
) == 0)
5241 if (vim_strchr((char_u
*)":=@>?/", name
[0]) != NULL
&& name
[1] == NUL
)
5242 return hist_char2type(name
[0]);
5247 static int last_maptick
= -1; /* last seen maptick */
5250 * Add the given string to the given history. If the string is already in the
5251 * history then it is moved to the front. "histype" may be one of he HIST_
5255 add_to_history(histype
, new_entry
, in_map
, sep
)
5258 int in_map
; /* consider maptick when inside a mapping */
5259 int sep
; /* separator character used (search hist) */
5261 histentry_T
*hisptr
;
5264 if (hislen
== 0) /* no history */
5268 * Searches inside the same mapping overwrite each other, so that only
5269 * the last line is kept. Be careful not to remove a line that was moved
5270 * down, only lines that were added.
5272 if (histype
== HIST_SEARCH
&& in_map
)
5274 if (maptick
== last_maptick
)
5276 /* Current line is from the same mapping, remove it */
5277 hisptr
= &history
[HIST_SEARCH
][hisidx
[HIST_SEARCH
]];
5278 vim_free(hisptr
->hisstr
);
5279 hisptr
->hisstr
= NULL
;
5282 if (--hisidx
[HIST_SEARCH
] < 0)
5283 hisidx
[HIST_SEARCH
] = hislen
- 1;
5287 if (!in_history(histype
, new_entry
, TRUE
))
5289 if (++hisidx
[histype
] == hislen
)
5290 hisidx
[histype
] = 0;
5291 hisptr
= &history
[histype
][hisidx
[histype
]];
5292 vim_free(hisptr
->hisstr
);
5294 /* Store the separator after the NUL of the string. */
5295 len
= (int)STRLEN(new_entry
);
5296 hisptr
->hisstr
= vim_strnsave(new_entry
, len
+ 2);
5297 if (hisptr
->hisstr
!= NULL
)
5298 hisptr
->hisstr
[len
+ 1] = sep
;
5300 hisptr
->hisnum
= ++hisnum
[histype
];
5301 if (histype
== HIST_SEARCH
&& in_map
)
5302 last_maptick
= maptick
;
5306 #if defined(FEAT_EVAL) || defined(PROTO)
5309 * Get identifier of newest history entry.
5310 * "histype" may be one of the HIST_ values.
5313 get_history_idx(histype
)
5316 if (hislen
== 0 || histype
< 0 || histype
>= HIST_COUNT
5317 || hisidx
[histype
] < 0)
5320 return history
[histype
][hisidx
[histype
]].hisnum
;
5323 static struct cmdline_info
*get_ccline_ptr
__ARGS((void));
5326 * Get pointer to the command line info to use. cmdline_paste() may clear
5327 * ccline and put the previous value in prev_ccline.
5329 static struct cmdline_info
*
5332 if ((State
& CMDLINE
) == 0)
5334 if (ccline
.cmdbuff
!= NULL
)
5336 if (prev_ccline_used
&& prev_ccline
.cmdbuff
!= NULL
)
5337 return &prev_ccline
;
5342 * Get the current command line in allocated memory.
5343 * Only works when the command line is being edited.
5344 * Returns NULL when something is wrong.
5349 struct cmdline_info
*p
= get_ccline_ptr();
5353 return vim_strnsave(p
->cmdbuff
, p
->cmdlen
);
5357 * Get the current command line position, counted in bytes.
5358 * Zero is the first position.
5359 * Only works when the command line is being edited.
5360 * Returns -1 when something is wrong.
5365 struct cmdline_info
*p
= get_ccline_ptr();
5373 * Set the command line byte position to "pos". Zero is the first position.
5374 * Only works when the command line is being edited.
5375 * Returns 1 when failed, 0 when OK.
5378 set_cmdline_pos(pos
)
5381 struct cmdline_info
*p
= get_ccline_ptr();
5386 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5387 * changed the command line. */
5396 * Get the current command-line type.
5397 * Returns ':' or '/' or '?' or '@' or '>' or '-'
5398 * Only works when the command line is being edited.
5399 * Returns NUL when something is wrong.
5404 struct cmdline_info
*p
= get_ccline_ptr();
5408 if (p
->cmdfirstc
== NUL
)
5409 return (p
->input_fn
) ? '@' : '-';
5410 return p
->cmdfirstc
;
5414 * Calculate history index from a number:
5415 * num > 0: seen as identifying number of a history entry
5416 * num < 0: relative position in history wrt newest entry
5417 * "histype" may be one of the HIST_ values.
5420 calc_hist_idx(histype
, num
)
5426 int wrapped
= FALSE
;
5428 if (hislen
== 0 || histype
< 0 || histype
>= HIST_COUNT
5429 || (i
= hisidx
[histype
]) < 0 || num
== 0)
5432 hist
= history
[histype
];
5435 while (hist
[i
].hisnum
> num
)
5443 if (hist
[i
].hisnum
== num
&& hist
[i
].hisstr
!= NULL
)
5446 else if (-num
<= hislen
)
5451 if (hist
[i
].hisstr
!= NULL
)
5458 * Get a history entry by its index.
5459 * "histype" may be one of the HIST_ values.
5462 get_history_entry(histype
, idx
)
5466 idx
= calc_hist_idx(histype
, idx
);
5468 return history
[histype
][idx
].hisstr
;
5470 return (char_u
*)"";
5474 * Clear all entries of a history.
5475 * "histype" may be one of the HIST_ values.
5478 clr_history(histype
)
5482 histentry_T
*hisptr
;
5484 if (hislen
!= 0 && histype
>= 0 && histype
< HIST_COUNT
)
5486 hisptr
= history
[histype
];
5487 for (i
= hislen
; i
--;)
5489 vim_free(hisptr
->hisstr
);
5491 hisptr
++->hisstr
= NULL
;
5493 hisidx
[histype
] = -1; /* mark history as cleared */
5494 hisnum
[histype
] = 0; /* reset identifier counter */
5501 * Remove all entries matching {str} from a history.
5502 * "histype" may be one of the HIST_ values.
5505 del_history_entry(histype
, str
)
5509 regmatch_T regmatch
;
5510 histentry_T
*hisptr
;
5516 regmatch
.regprog
= NULL
;
5517 regmatch
.rm_ic
= FALSE
; /* always match case */
5520 && histype
< HIST_COUNT
5522 && (idx
= hisidx
[histype
]) >= 0
5523 && (regmatch
.regprog
= vim_regcomp(str
, RE_MAGIC
+ RE_STRING
))
5529 hisptr
= &history
[histype
][i
];
5530 if (hisptr
->hisstr
== NULL
)
5532 if (vim_regexec(®match
, hisptr
->hisstr
, (colnr_T
)0))
5535 vim_free(hisptr
->hisstr
);
5536 hisptr
->hisstr
= NULL
;
5543 history
[histype
][last
] = *hisptr
;
5544 hisptr
->hisstr
= NULL
;
5553 if (history
[histype
][idx
].hisstr
== NULL
)
5554 hisidx
[histype
] = -1;
5556 vim_free(regmatch
.regprog
);
5561 * Remove an indexed entry from a history.
5562 * "histype" may be one of the HIST_ values.
5565 del_history_idx(histype
, idx
)
5571 i
= calc_hist_idx(histype
, idx
);
5574 idx
= hisidx
[histype
];
5575 vim_free(history
[histype
][i
].hisstr
);
5577 /* When deleting the last added search string in a mapping, reset
5578 * last_maptick, so that the last added search string isn't deleted again.
5580 if (histype
== HIST_SEARCH
&& maptick
== last_maptick
&& i
== idx
)
5585 j
= (i
+ 1) % hislen
;
5586 history
[histype
][i
] = history
[histype
][j
];
5589 history
[histype
][i
].hisstr
= NULL
;
5590 history
[histype
][i
].hisnum
= 0;
5593 hisidx
[histype
] = i
;
5597 #endif /* FEAT_EVAL */
5599 #if defined(FEAT_CRYPT) || defined(PROTO)
5601 * Very specific function to remove the value in ":set key=val" from the
5605 remove_key_from_history()
5610 i
= hisidx
[HIST_CMD
];
5613 p
= history
[HIST_CMD
][i
].hisstr
;
5616 if (STRNCMP(p
, "key", 3) == 0 && !isalpha(p
[3]))
5618 p
= vim_strchr(p
+ 3, '=');
5622 for (i
= 0; p
[i
] && !vim_iswhite(p
[i
]); ++i
)
5623 if (p
[i
] == '\\' && p
[i
+ 1])
5631 #endif /* FEAT_CMDHIST */
5633 #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5635 * Get indices "num1,num2" that specify a range within a list (not a range of
5636 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5637 * Returns OK if parsed successfully, otherwise FAIL.
5640 get_list_range(str
, num1
, num2
)
5649 *str
= skipwhite(*str
);
5650 if (**str
== '-' || vim_isdigit(**str
)) /* parse "from" part of range */
5652 vim_str2nr(*str
, NULL
, &len
, FALSE
, FALSE
, &num
, NULL
);
5657 *str
= skipwhite(*str
);
5658 if (**str
== ',') /* parse "to" part of range */
5660 *str
= skipwhite(*str
+ 1);
5661 vim_str2nr(*str
, NULL
, &len
, FALSE
, FALSE
, &num
, NULL
);
5665 *str
= skipwhite(*str
+ len
);
5667 else if (!first
) /* no number given at all */
5670 else if (first
) /* only one number given */
5676 #if defined(FEAT_CMDHIST) || defined(PROTO)
5678 * :history command - print a history
5685 int histype1
= HIST_CMD
;
5686 int histype2
= HIST_CMD
;
5692 char_u
*arg
= eap
->arg
;
5696 MSG(_("'history' option is zero"));
5700 if (!(VIM_ISDIGIT(*arg
) || *arg
== '-' || *arg
== ','))
5703 while (ASCII_ISALPHA(*end
)
5704 || vim_strchr((char_u
*)":=@>/?", *end
) != NULL
)
5708 histype1
= get_histtype(arg
);
5711 if (STRNICMP(arg
, "all", STRLEN(arg
)) == 0)
5714 histype2
= HIST_COUNT
-1;
5719 EMSG(_(e_trailing
));
5724 histype2
= histype1
;
5729 if (!get_list_range(&end
, &hisidx1
, &hisidx2
) || *end
!= NUL
)
5731 EMSG(_(e_trailing
));
5735 for (; !got_int
&& histype1
<= histype2
; ++histype1
)
5737 STRCPY(IObuff
, "\n # ");
5738 STRCAT(STRCAT(IObuff
, history_names
[histype1
]), " history");
5739 MSG_PUTS_TITLE(IObuff
);
5740 idx
= hisidx
[histype1
];
5741 hist
= history
[histype1
];
5745 j
= (-j
> hislen
) ? 0 : hist
[(hislen
+j
+idx
+1) % hislen
].hisnum
;
5747 k
= (-k
> hislen
) ? 0 : hist
[(hislen
+k
+idx
+1) % hislen
].hisnum
;
5748 if (idx
>= 0 && j
<= k
)
5749 for (i
= idx
+ 1; !got_int
; ++i
)
5753 if (hist
[i
].hisstr
!= NULL
5754 && hist
[i
].hisnum
>= j
&& hist
[i
].hisnum
<= k
)
5757 sprintf((char *)IObuff
, "%c%6d ", i
== idx
? '>' : ' ',
5759 if (vim_strsize(hist
[i
].hisstr
) > (int)Columns
- 10)
5760 trunc_string(hist
[i
].hisstr
, IObuff
+ STRLEN(IObuff
),
5763 STRCAT(IObuff
, hist
[i
].hisstr
);
5764 msg_outtrans(IObuff
);
5774 #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5775 static char_u
**viminfo_history
[HIST_COUNT
] = {NULL
, NULL
, NULL
, NULL
};
5776 static int viminfo_hisidx
[HIST_COUNT
] = {0, 0, 0, 0};
5777 static int viminfo_hislen
[HIST_COUNT
] = {0, 0, 0, 0};
5778 static int viminfo_add_at_front
= FALSE
;
5780 static int hist_type2char
__ARGS((int type
, int use_question
));
5783 * Translate a history type number to the associated character.
5786 hist_type2char(type
, use_question
)
5788 int use_question
; /* use '?' instead of '/' */
5790 if (type
== HIST_CMD
)
5792 if (type
== HIST_SEARCH
)
5799 if (type
== HIST_EXPR
)
5805 * Prepare for reading the history from the viminfo file.
5806 * This allocates history arrays to store the read history lines.
5809 prepare_viminfo_history(asklen
)
5818 viminfo_add_at_front
= (asklen
!= 0);
5819 if (asklen
> hislen
)
5822 for (type
= 0; type
< HIST_COUNT
; ++type
)
5825 * Count the number of empty spaces in the history list. If there are
5826 * more spaces available than we request, then fill them up.
5828 for (i
= 0, num
= 0; i
< hislen
; i
++)
5829 if (history
[type
][i
].hisstr
== NULL
)
5835 viminfo_history
[type
] = NULL
;
5837 viminfo_history
[type
] =
5838 (char_u
**)lalloc((long_u
)(len
* sizeof(char_u
*)), FALSE
);
5839 if (viminfo_history
[type
] == NULL
)
5841 viminfo_hislen
[type
] = len
;
5842 viminfo_hisidx
[type
] = 0;
5847 * Accept a line from the viminfo, store it in the history array when it's
5851 read_viminfo_history(virp
)
5859 type
= hist_char2type(virp
->vir_line
[0]);
5860 if (viminfo_hisidx
[type
] < viminfo_hislen
[type
])
5862 val
= viminfo_readstring(virp
, 1, TRUE
);
5863 if (val
!= NULL
&& *val
!= NUL
)
5865 if (!in_history(type
, val
+ (type
== HIST_SEARCH
),
5866 viminfo_add_at_front
))
5868 /* Need to re-allocate to append the separator byte. */
5870 p
= lalloc(len
+ 2, TRUE
);
5873 if (type
== HIST_SEARCH
)
5875 /* Search entry: Move the separator from the first
5876 * column to after the NUL. */
5877 mch_memmove(p
, val
+ 1, (size_t)len
);
5878 p
[len
] = (*val
== ' ' ? NUL
: *val
);
5882 /* Not a search entry: No separator in the viminfo
5883 * file, add a NUL separator. */
5884 mch_memmove(p
, val
, (size_t)len
+ 1);
5887 viminfo_history
[type
][viminfo_hisidx
[type
]++] = p
;
5893 return viminfo_readline(virp
);
5897 finish_viminfo_history()
5903 for (type
= 0; type
< HIST_COUNT
; ++type
)
5905 if (history
[type
] == NULL
)
5907 idx
= hisidx
[type
] + viminfo_hisidx
[type
];
5912 if (viminfo_add_at_front
)
5916 if (hisidx
[type
] == -1)
5917 hisidx
[type
] = hislen
- 1;
5920 if (history
[type
][idx
].hisstr
!= NULL
)
5922 if (++idx
== hislen
)
5924 } while (idx
!= hisidx
[type
]);
5925 if (idx
!= hisidx
[type
] && --idx
< 0)
5928 for (i
= 0; i
< viminfo_hisidx
[type
]; i
++)
5930 vim_free(history
[type
][idx
].hisstr
);
5931 history
[type
][idx
].hisstr
= viminfo_history
[type
][i
];
5937 for (i
= 0; i
< viminfo_hisidx
[type
]; i
++)
5939 history
[type
][idx
++].hisnum
= ++hisnum
[type
];
5942 vim_free(viminfo_history
[type
]);
5943 viminfo_history
[type
] = NULL
;
5948 write_viminfo_history(fp
)
5960 for (type
= 0; type
< HIST_COUNT
; ++type
)
5962 num_saved
= get_viminfo_parameter(hist_type2char(type
, FALSE
));
5965 if (num_saved
< 0) /* Use default */
5967 fprintf(fp
, _("\n# %s History (newest to oldest):\n"),
5968 type
== HIST_CMD
? _("Command Line") :
5969 type
== HIST_SEARCH
? _("Search String") :
5970 type
== HIST_EXPR
? _("Expression") :
5972 if (num_saved
> hislen
)
5978 p
= history
[type
][i
].hisstr
;
5981 fputc(hist_type2char(type
, TRUE
), fp
);
5982 /* For the search history: put the separator in the second
5983 * column; use a space if there isn't one. */
5984 if (type
== HIST_SEARCH
)
5986 c
= p
[STRLEN(p
) + 1];
5987 putc(c
== NUL
? ' ' : c
, fp
);
5989 viminfo_writestring(fp
, p
);
5996 #endif /* FEAT_VIMINFO */
5998 #if defined(FEAT_FKMAP) || defined(PROTO)
6000 * Write a character at the current cursor+offset position.
6001 * It is directly written into the command buffer block.
6004 cmd_pchar(c
, offset
)
6007 if (ccline
.cmdpos
+ offset
>= ccline
.cmdlen
|| ccline
.cmdpos
+ offset
< 0)
6009 EMSG(_("E198: cmd_pchar beyond the command length"));
6012 ccline
.cmdbuff
[ccline
.cmdpos
+ offset
] = (char_u
)c
;
6013 ccline
.cmdbuff
[ccline
.cmdlen
] = NUL
;
6020 if (ccline
.cmdpos
+ offset
>= ccline
.cmdlen
|| ccline
.cmdpos
+ offset
< 0)
6022 /* EMSG(_("cmd_gchar beyond the command length")); */
6025 return (int)ccline
.cmdbuff
[ccline
.cmdpos
+ offset
];
6029 #if defined(FEAT_CMDWIN) || defined(PROTO)
6031 * Open a window on the current command line and history. Allow editing in
6032 * the window. Returns when the window is closed.
6034 * CR if the command is to be executed
6035 * Ctrl_C if it is to be abandoned
6036 * K_IGNORE if editing continues
6041 struct cmdline_info save_ccline
;
6042 buf_T
*old_curbuf
= curbuf
;
6043 win_T
*old_curwin
= curwin
;
6053 int save_restart_edit
= restart_edit
;
6054 int save_State
= State
;
6055 int save_exmode
= exmode_active
;
6056 #ifdef FEAT_RIGHTLEFT
6057 int save_cmdmsg_rl
= cmdmsg_rl
;
6060 /* Can't do this recursively. Can't do it when typing a password. */
6061 if (cmdwin_type
!= 0
6062 # if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6071 /* Save current window sizes. */
6072 win_size_save(&winsizes
);
6074 # ifdef FEAT_AUTOCMD
6075 /* Don't execute autocommands while creating the window. */
6078 /* don't use a new tab page */
6081 /* Create a window for the command-line buffer. */
6082 if (win_split((int)p_cwh
, WSP_BOT
) == FAIL
)
6085 # ifdef FEAT_AUTOCMD
6090 cmdwin_type
= get_cmdline_type();
6092 /* Create the command-line buffer empty. */
6093 (void)do_ecmd(0, NULL
, NULL
, NULL
, ECMD_ONE
, ECMD_HIDE
, NULL
);
6094 (void)setfname(curbuf
, (char_u
*)"[Command Line]", NULL
, TRUE
);
6095 set_option_value((char_u
*)"bt", 0L, (char_u
*)"nofile", OPT_LOCAL
);
6096 set_option_value((char_u
*)"swf", 0L, NULL
, OPT_LOCAL
);
6097 curbuf
->b_p_ma
= TRUE
;
6099 curwin
->w_p_fen
= FALSE
;
6101 # ifdef FEAT_RIGHTLEFT
6102 curwin
->w_p_rl
= cmdmsg_rl
;
6105 # ifdef FEAT_SCROLLBIND
6106 curwin
->w_p_scb
= FALSE
;
6109 # ifdef FEAT_AUTOCMD
6110 /* Do execute autocommands for setting the filetype (load syntax). */
6114 /* Showing the prompt may have set need_wait_return, reset it. */
6115 need_wait_return
= FALSE
;
6117 histtype
= hist_char2type(cmdwin_type
);
6118 if (histtype
== HIST_CMD
|| histtype
== HIST_DEBUG
)
6122 add_map((char_u
*)"<buffer> <Tab> <C-X><C-V>", INSERT
);
6123 add_map((char_u
*)"<buffer> <Tab> a<C-X><C-V>", NORMAL
);
6125 set_option_value((char_u
*)"ft", 0L, (char_u
*)"vim", OPT_LOCAL
);
6128 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6129 * sets 'textwidth' to 78). */
6132 /* Fill the buffer with the history. */
6136 i
= hisidx
[histtype
];
6144 if (history
[histtype
][i
].hisstr
!= NULL
)
6145 ml_append(lnum
++, history
[histtype
][i
].hisstr
,
6148 while (i
!= hisidx
[histtype
]);
6152 /* Replace the empty last line with the current command-line and put the
6154 ml_replace(curbuf
->b_ml
.ml_line_count
, ccline
.cmdbuff
, TRUE
);
6155 curwin
->w_cursor
.lnum
= curbuf
->b_ml
.ml_line_count
;
6156 curwin
->w_cursor
.col
= ccline
.cmdpos
;
6157 changed_line_abv_curs();
6158 invalidate_botline();
6159 redraw_later(SOME_VALID
);
6161 /* Save the command line info, can be used recursively. */
6162 save_ccline
= ccline
;
6163 ccline
.cmdbuff
= NULL
;
6164 ccline
.cmdprompt
= NULL
;
6166 /* No Ex mode here! */
6174 # ifdef FEAT_AUTOCMD
6175 /* Trigger CmdwinEnter autocommands. */
6176 typestr
[0] = cmdwin_type
;
6178 apply_autocmds(EVENT_CMDWINENTER
, typestr
, typestr
, FALSE
, curbuf
);
6179 if (restart_edit
!= 0) /* autocmd with ":startinsert" */
6180 stuffcharReadbuff(K_NOP
);
6183 i
= RedrawingDisabled
;
6184 RedrawingDisabled
= 0;
6187 * Call the main loop until <CR> or CTRL-C is typed.
6190 main_loop(TRUE
, FALSE
);
6192 RedrawingDisabled
= i
;
6194 # ifdef FEAT_AUTOCMD
6195 /* Trigger CmdwinLeave autocommands. */
6196 apply_autocmds(EVENT_CMDWINLEAVE
, typestr
, typestr
, FALSE
, curbuf
);
6199 /* Restore the command line info. */
6200 ccline
= save_ccline
;
6203 exmode_active
= save_exmode
;
6205 /* Safety check: The old window or buffer was deleted: It's a bug when
6207 if (!win_valid(old_curwin
) || !buf_valid(old_curbuf
))
6209 cmdwin_result
= Ctrl_C
;
6210 EMSG(_("E199: Active window or buffer deleted"));
6214 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6215 /* autocmds may abort script processing */
6216 if (aborting() && cmdwin_result
!= K_IGNORE
)
6217 cmdwin_result
= Ctrl_C
;
6219 /* Set the new command line from the cmdline buffer. */
6220 vim_free(ccline
.cmdbuff
);
6221 if (cmdwin_result
== K_XF1
|| cmdwin_result
== K_XF2
) /* :qa[!] typed */
6223 char *p
= (cmdwin_result
== K_XF2
) ? "qa" : "qa!";
6225 if (histtype
== HIST_CMD
)
6227 /* Execute the command directly. */
6228 ccline
.cmdbuff
= vim_strsave((char_u
*)p
);
6229 cmdwin_result
= CAR
;
6233 /* First need to cancel what we were doing. */
6234 ccline
.cmdbuff
= NULL
;
6235 stuffcharReadbuff(':');
6236 stuffReadbuff((char_u
*)p
);
6237 stuffcharReadbuff(CAR
);
6240 else if (cmdwin_result
== K_XF2
) /* :qa typed */
6242 ccline
.cmdbuff
= vim_strsave((char_u
*)"qa");
6243 cmdwin_result
= CAR
;
6246 ccline
.cmdbuff
= vim_strsave(ml_get_curline());
6247 if (ccline
.cmdbuff
== NULL
)
6248 cmdwin_result
= Ctrl_C
;
6251 ccline
.cmdlen
= (int)STRLEN(ccline
.cmdbuff
);
6252 ccline
.cmdbufflen
= ccline
.cmdlen
+ 1;
6253 ccline
.cmdpos
= curwin
->w_cursor
.col
;
6254 if (ccline
.cmdpos
> ccline
.cmdlen
)
6255 ccline
.cmdpos
= ccline
.cmdlen
;
6256 if (cmdwin_result
== K_IGNORE
)
6258 set_cmdspos_cursor();
6263 # ifdef FEAT_AUTOCMD
6264 /* Don't execute autocommands while deleting the window. */
6269 win_goto(old_curwin
);
6270 win_close(wp
, TRUE
);
6271 close_buffer(NULL
, bp
, DOBUF_WIPE
);
6273 /* Restore window sizes. */
6274 win_size_restore(&winsizes
);
6276 # ifdef FEAT_AUTOCMD
6281 ga_clear(&winsizes
);
6282 restart_edit
= save_restart_edit
;
6283 # ifdef FEAT_RIGHTLEFT
6284 cmdmsg_rl
= save_cmdmsg_rl
;
6292 return cmdwin_result
;
6294 #endif /* FEAT_CMDWIN */
6297 * Used for commands that either take a simple command string argument, or:
6301 * Returns a pointer to allocated memory with {script} or NULL.
6304 script_get(eap
, cmd
)
6309 char *end_pattern
= NULL
;
6313 if (cmd
[0] != '<' || cmd
[1] != '<' || eap
->getline
== NULL
)
6316 ga_init2(&ga
, 1, 0x400);
6319 end_pattern
= (char *)skipwhite(cmd
+ 2);
6325 theline
= eap
->getline(
6327 eap
->cstack
->cs_looplevel
> 0 ? -1 :
6329 NUL
, eap
->cookie
, 0);
6331 if (theline
== NULL
|| STRCMP(end_pattern
, theline
) == 0)
6337 ga_concat(&ga
, theline
);
6338 ga_append(&ga
, '\n');
6341 ga_append(&ga
, NUL
);
6343 return (char_u
*)ga
.ga_data
;