vim72-20100325-kaoriya-w64j.zip
[MacVim/KaoriYa.git] / src / ex_getln.c
blob06abe39bbef7b831d1a690b52ace6292a45e079c
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.
8 */
11 * ex_getln.c: Functions for entering and editing an Ex command line.
14 #include "vim.h"
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
19 * structure.
21 struct cmdline_info
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 */
37 # ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
39 int input_fn; /* when TRUE Invoked for input() function */
40 # endif
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 ? */
51 #ifdef FEAT_EVAL
52 static int new_cmdpos; /* position set by set_cmdline_pos() */
53 #endif
55 #ifdef FEAT_CMDHIST
56 typedef struct hist_entry
58 int hisnum; /* identifying number */
59 char_u *hisstr; /* actual entry, separator char after the NUL */
60 } histentry_T;
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));
71 # ifdef FEAT_EVAL
72 static int calc_hist_idx __ARGS((int histype, int num));
73 # endif
74 #endif
76 #ifdef FEAT_RIGHTLEFT
77 static int cmd_hkmap = 0; /* Hebrew mapping during command line */
78 #endif
80 #ifdef FEAT_FKMAP
81 static int cmd_fkmap = 0; /* Farsi mapping during command line */
82 #endif
84 static int cmdline_charsize __ARGS((int idx));
85 static void set_cmdspos __ARGS((void));
86 static void set_cmdspos_cursor __ARGS((void));
87 #ifdef FEAT_MBYTE
88 static void correct_cmdspos __ARGS((int idx, int cells));
89 #endif
90 static void alloc_cmdbuff __ARGS((int len));
91 static int realloc_cmdbuff __ARGS((int len));
92 static void draw_cmdline __ARGS((int start, int len));
93 static void save_cmdline __ARGS((struct cmdline_info *ccp));
94 static void restore_cmdline __ARGS((struct cmdline_info *ccp));
95 static int cmdline_paste __ARGS((int regname, int literally, int remcr));
96 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
97 static void redrawcmd_preedit __ARGS((void));
98 #endif
99 #ifdef FEAT_WILDMENU
100 static void cmdline_del __ARGS((int from));
101 #endif
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));
117 # endif
118 #endif
120 #ifdef FEAT_CMDWIN
121 static int ex_window __ARGS((void));
122 #endif
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
136 * command line.
138 * Careful: getcmdline() can be called recursively!
140 * Return pointer to allocated string if there is a commandline, NULL
141 * otherwise.
143 char_u *
144 getcmdline(firstc, count, indent)
145 int firstc;
146 long count UNUSED; /* only used for incremental search */
147 int indent; /* indent for inside conditionals */
149 int c;
150 int i;
151 int j;
152 int gotesc = FALSE; /* TRUE when <ESC> just typed */
153 int do_abbr; /* when TRUE check for abbr. */
154 #ifdef FEAT_CMDHIST
155 char_u *lookfor = NULL; /* string to match */
156 int hiscnt; /* current history line in use */
157 int histype; /* history type to be used */
158 #endif
159 #ifdef FEAT_SEARCH_EXTRA
160 pos_T old_cursor;
161 colnr_T old_curswant;
162 colnr_T old_leftcol;
163 linenr_T old_topline;
164 # ifdef FEAT_DIFF
165 int old_topfill;
166 # endif
167 linenr_T old_botline;
168 int did_incsearch = FALSE;
169 int incsearch_postponed = FALSE;
170 #endif
171 int did_wild_list = FALSE; /* did wild_list() recently */
172 int wim_index = 0; /* index in wim_flags[] */
173 int res;
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 */
177 #ifdef FEAT_MOUSE
178 /* mouse drag and release events are ignored, unless they are
179 * preceded with a mouse down event */
180 int ignore_drag_release = TRUE;
181 #endif
182 #ifdef FEAT_EVAL
183 int break_ctrl_c = FALSE;
184 #endif
185 expand_T xpc;
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;
192 #endif
193 #ifdef USE_MIGEMO
194 int migemo_enabled = 0;
195 #endif
197 #ifdef USE_MIGEMO
198 if (count < 0)
200 migemo_enabled = 1;
201 count = -count;
203 #endif
204 #ifdef FEAT_SNIFF
205 want_sniff_request = 0;
206 #endif
207 #ifdef FEAT_EVAL
208 if (firstc == -1)
210 firstc = NUL;
211 break_ctrl_c = TRUE;
213 #endif
214 #ifdef FEAT_RIGHTLEFT
215 /* start without Hebrew mapping for a command line */
216 if (firstc == ':' || firstc == '=' || firstc == '>')
217 cmd_hkmap = 0;
218 #endif
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;
226 # ifdef FEAT_DIFF
227 old_topfill = curwin->w_topfill;
228 # endif
229 old_botline = curwin->w_botline;
230 #endif
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 */
246 if (firstc <= 0)
248 copy_spaces(ccline.cmdbuff, indent);
249 ccline.cmdbuff[indent] = NUL;
250 ccline.cmdpos = indent;
251 ccline.cmdspos = indent;
252 ccline.cmdlen = indent;
255 ExpandInit(&xpc);
256 ccline.xpc = &xpc;
258 #ifdef FEAT_RIGHTLEFT
259 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
260 && (firstc == '/' || firstc == '?'))
261 cmdmsg_rl = TRUE;
262 else
263 cmdmsg_rl = FALSE;
264 #endif
266 redir_off = TRUE; /* don't redirect the typed command */
267 if (!cmd_silent)
269 i = msg_scrolled;
270 msg_scrolled = 0; /* avoid wait_return message */
271 gotocmdline(TRUE);
272 msg_scrolled += i;
273 redrawcmdprompt(); /* draw prompt or indent */
274 set_cmdspos();
276 xpc.xp_context = EXPAND_NOTHING;
277 xpc.xp_backslash = XP_BS_NONE;
278 #ifndef BACKSLASH_IN_FILENAME
279 xpc.xp_shell = FALSE;
280 #endif
282 #if defined(FEAT_EVAL)
283 if (ccline.input_fn)
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;
289 # endif
291 #endif
294 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
295 * doing ":@0" when register 0 doesn't contain a CR.
297 msg_scroll = FALSE;
299 State = CMDLINE;
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;
306 else
307 b_im_ptr = &curbuf->b_p_imsearch;
308 if (*b_im_ptr == B_IMODE_LMAP)
309 State |= LANGMAP;
310 #ifdef USE_IM_CONTROL
311 im_set_active(*b_im_ptr == B_IMODE_IM);
312 #endif
314 #ifdef USE_IM_CONTROL
315 else if (p_imcmdline)
316 im_set_active(TRUE);
317 #endif
319 #ifdef FEAT_MOUSE
320 setmouse();
321 #endif
322 #ifdef CURSOR_SHAPE
323 ui_cursor_shape(); /* may show different cursor shape */
324 #endif
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. */
328 settmode(TMODE_RAW);
330 #ifdef FEAT_CMDHIST
331 init_history();
332 hiscnt = hislen; /* set hiscnt to impossible history value */
333 histype = hist_char2type(firstc);
334 #endif
336 #ifdef FEAT_DIGRAPHS
337 do_digraph(-1); /* init digraph typeahead */
338 #endif
341 * Collect the command string, handling editing keys.
343 for (;;)
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 */
350 #endif
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. */
359 c = safe_vgetc();
360 } while (c == K_IGNORE);
362 if (KeyTyped)
364 some_key_typed = TRUE;
365 #ifdef FEAT_RIGHTLEFT
366 if (cmd_hkmap)
367 c = hkmap(c);
368 # ifdef FEAT_FKMAP
369 if (cmd_fkmap)
370 c = cmdl_fkmap(c);
371 # endif
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
376 * mapping. */
377 switch (c)
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;
387 #endif
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.
396 if ((c == Ctrl_C
397 #ifdef UNIX
398 || c == intr_char
399 #endif
401 #if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
402 && firstc != '@'
403 #endif
404 #ifdef FEAT_EVAL
405 && !break_ctrl_c
406 #endif
407 && !global_busy)
408 got_int = FALSE;
410 #ifdef FEAT_CMDHIST
411 /* free old command line when finished moving around in the history
412 * list */
413 if (lookfor != NULL
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)))
421 vim_free(lookfor);
422 lookfor = NULL;
424 #endif
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)
431 c = Ctrl_P;
433 #ifdef FEAT_WILDMENU
434 /* Special translations for 'wildmenu' */
435 if (did_wild_list && p_wmnu)
437 if (c == K_LEFT)
438 c = Ctrl_P;
439 else if (c == K_RIGHT)
440 c = Ctrl_N;
442 /* Hitting CR after "emenu Name.": complete submenu */
443 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
444 && ccline.cmdpos > 1
445 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
446 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
447 && (c == '\n' || c == '\r' || c == K_KENTER))
448 c = K_DOWN;
449 #endif
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
455 && c != Ctrl_L)
457 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
458 did_wild_list = FALSE;
459 #ifdef FEAT_WILDMENU
460 if (!p_wmnu || (c != K_UP && c != K_DOWN))
461 #endif
462 xpc.xp_context = EXPAND_NOTHING;
463 wim_index = 0;
464 #ifdef FEAT_WILDMENU
465 if (p_wmnu && wild_menu_showing != 0)
467 int skt = KeyTyped;
468 int old_RedrawingDisabled = RedrawingDisabled;
470 if (ccline.input_fn)
471 RedrawingDisabled = 0;
473 if (wild_menu_showing == WM_SCROLLED)
475 /* Entered command line, move it up */
476 cmdline_row--;
477 redrawcmd();
479 else if (save_p_ls != -1)
481 /* restore 'laststatus' and 'winminheight' */
482 p_ls = save_p_ls;
483 p_wmh = save_p_wmh;
484 last_status(FALSE);
485 save_cmdline(&save_ccline);
486 update_screen(VALID); /* redraw the screen NOW */
487 restore_cmdline(&save_ccline);
488 redrawcmd();
489 save_p_ls = -1;
491 else
493 # ifdef FEAT_VERTSPLIT
494 win_redraw_last_status(topframe);
495 # else
496 lastwin->w_redr_status = TRUE;
497 # endif
498 redraw_statuslines();
500 KeyTyped = skt;
501 wild_menu_showing = 0;
502 if (ccline.input_fn)
503 RedrawingDisabled = old_RedrawingDisabled;
505 #endif
508 #ifdef FEAT_WILDMENU
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] == '.')
515 c = p_wc;
516 else if (c == K_UP)
518 /* Hitting <Up>: Remove one submenu name in front of the
519 * cursor */
520 int found = FALSE;
522 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
523 i = 0;
524 while (--j > 0)
526 /* check for start of menu name */
527 if (ccline.cmdbuff[j] == ' '
528 && ccline.cmdbuff[j - 1] != '\\')
530 i = j + 1;
531 break;
533 /* check for start of submenu name */
534 if (ccline.cmdbuff[j] == '.'
535 && ccline.cmdbuff[j - 1] != '\\')
537 if (found)
539 i = j + 1;
540 break;
542 else
543 found = TRUE;
546 if (i > 0)
547 cmdline_del(i);
548 c = p_wc;
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)
556 char_u upseg[5];
558 upseg[0] = PATHSEP;
559 upseg[1] = '.';
560 upseg[2] = '.';
561 upseg[3] = PATHSEP;
562 upseg[4] = NUL;
564 if (c == K_DOWN
565 && ccline.cmdpos > 0
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 */
572 c = p_wc;
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 */
577 int found = FALSE;
579 j = ccline.cmdpos;
580 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
581 while (--j > i)
583 #ifdef FEAT_MBYTE
584 if (has_mbyte)
585 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
586 #endif
587 if (vim_ispathsep(ccline.cmdbuff[j]))
589 found = TRUE;
590 break;
593 if (found
594 && ccline.cmdbuff[j - 1] == '.'
595 && ccline.cmdbuff[j - 2] == '.'
596 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
598 cmdline_del(j - 2);
599 c = p_wc;
602 else if (c == K_UP)
604 /* go up a directory */
605 int found = FALSE;
607 j = ccline.cmdpos - 1;
608 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
609 while (--j > i)
611 #ifdef FEAT_MBYTE
612 if (has_mbyte)
613 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
614 #endif
615 if (vim_ispathsep(ccline.cmdbuff[j])
616 #ifdef BACKSLASH_IN_FILENAME
617 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
618 == NULL
619 #endif
622 if (found)
624 i = j + 1;
625 break;
627 else
628 found = TRUE;
632 if (!found)
633 j = i;
634 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
635 j += 4;
636 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
637 && j == i)
638 j += 3;
639 else
640 j = 0;
641 if (j > 0)
643 /* TODO this is only for DOS/UNIX systems - need to put in
644 * machine-specific stuff here and in upseg init */
645 cmdline_del(j);
646 put_on_cmdline(upseg + 1, 3, FALSE);
648 else if (ccline.cmdpos > i)
649 cmdline_del(i);
650 c = p_wc;
653 #if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
654 if (p_wmnu
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;
659 #endif
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. */
665 if (c == Ctrl_BSL)
667 ++no_mapping;
668 ++allow_keys;
669 c = plain_vgetc();
670 --no_mapping;
671 --allow_keys;
672 /* CTRL-\ e doesn't work when obtaining an expression. */
673 if (c != Ctrl_N && c != Ctrl_G
674 && (c != 'e' || ccline.cmdfirstc == '='))
676 vungetc(c);
677 c = Ctrl_BSL;
679 #ifdef FEAT_EVAL
680 else if (c == 'e')
682 char_u *p = NULL;
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 */
691 else
692 new_cmdpos = ccline.cmdpos;
694 save_cmdline(&save_ccline);
695 c = get_expr_register();
696 restore_cmdline(&save_ccline);
697 if (c == '=')
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);
703 ++textlock;
704 p = get_expr_line();
705 --textlock;
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);
712 vim_free(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;
718 else
719 ccline.cmdpos = new_cmdpos;
721 KeyTyped = FALSE; /* Don't do p_wc completion. */
722 redrawcmd();
723 goto cmdline_changed;
726 beep_flush();
727 c = ESC;
729 #endif
730 else
732 if (c == Ctrl_G && p_im && restart_edit == 0)
733 restart_edit = 'a';
734 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
735 in history */
736 goto returncmd; /* back to Normal mode */
740 #ifdef FEAT_CMDWIN
741 if (c == cedit_key || c == K_CMDWIN)
744 * Open a window to edit the command line (and history).
746 c = ex_window();
747 some_key_typed = TRUE;
749 # ifdef FEAT_DIGRAPHS
750 else
751 # endif
752 #endif
753 #ifdef FEAT_DIGRAPHS
754 c = do_digraph(c);
755 #endif
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. */
761 if (exmode_active
762 && c != ESC
763 && ccline.cmdpos == ccline.cmdlen
764 && ccline.cmdpos > 0
765 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
767 if (c == K_KENTER)
768 c = '\n';
770 else
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;
776 if (!cmd_silent)
778 windgoto(msg_row, 0);
779 out_flush();
781 break;
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
797 && !did_wild_list
798 && (wim_flags[wim_index] & WIM_LIST))
800 (void)showmatches(&xpc, FALSE);
801 redrawcmd();
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);
808 else
809 res = OK; /* don't insert 'wildchar' now */
811 else /* typed p_wc first time */
813 wim_index = 0;
814 j = ccline.cmdpos;
815 /* if 'wildmode' first contains "longest", get longest
816 * common part */
817 if (wim_flags[0] & WIM_LONGEST)
818 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
819 else
820 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
822 /* if interrupted while completing, behave like it failed */
823 if (got_int)
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);
828 #ifdef FEAT_WILDMENU
829 xpc.xp_context = EXPAND_NOTHING;
830 #endif
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
840 * "list:longest") */
841 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
842 wim_index = 1;
843 if ((wim_flags[wim_index] & WIM_LIST)
844 #ifdef FEAT_WILDMENU
845 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
846 #endif
849 if (!(wim_flags[0] & WIM_LONGEST))
851 #ifdef FEAT_WILDMENU
852 int p_wmnu_save = p_wmnu;
853 p_wmnu = 0;
854 #endif
855 nextwild(&xpc, WILD_PREV, 0); /* remove match */
856 #ifdef FEAT_WILDMENU
857 p_wmnu = p_wmnu_save;
858 #endif
860 #ifdef FEAT_WILDMENU
861 (void)showmatches(&xpc, p_wmnu
862 && ((wim_flags[wim_index] & WIM_LIST) == 0));
863 #else
864 (void)showmatches(&xpc, FALSE);
865 #endif
866 redrawcmd();
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);
873 else
874 vim_beep();
876 #ifdef FEAT_WILDMENU
877 else if (xpc.xp_numfiles == -1)
878 xpc.xp_context = EXPAND_NOTHING;
879 #endif
881 if (wim_index < 3)
882 ++wim_index;
883 if (c == ESC)
884 gotesc = TRUE;
885 if (res == OK)
886 goto cmdline_changed;
889 gotesc = FALSE;
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 */
901 c = NL;
903 do_abbr = TRUE; /* default: check for abbreviation */
906 * Big switch for a typed command line character.
908 switch (c)
910 case K_BS:
911 case Ctrl_H:
912 case K_DEL:
913 case K_KDEL:
914 case Ctrl_W:
915 #ifdef FEAT_FKMAP
916 if (cmd_fkmap && c == K_BS)
917 c = K_DEL;
918 #endif
919 if (c == K_KDEL)
920 c = K_DEL;
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)
927 ++ccline.cmdpos;
928 #ifdef FEAT_MBYTE
929 if (has_mbyte && c == K_DEL)
930 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
931 ccline.cmdbuff + ccline.cmdpos);
932 #endif
933 if (ccline.cmdpos > 0)
935 char_u *p;
937 j = ccline.cmdpos;
938 p = ccline.cmdbuff + j;
939 #ifdef FEAT_MBYTE
940 if (has_mbyte)
942 p = mb_prevptr(ccline.cmdbuff, p);
943 if (c == Ctrl_W)
945 while (p > ccline.cmdbuff && vim_isspace(*p))
946 p = mb_prevptr(ccline.cmdbuff, p);
947 i = mb_get_class(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);
954 else
955 #endif
956 if (c == Ctrl_W)
958 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
959 --p;
960 i = vim_iswordc(p[-1]);
961 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
962 && vim_iswordc(p[-1]) == i)
963 --p;
965 else
966 --p;
967 ccline.cmdpos = (int)(p - ccline.cmdbuff);
968 ccline.cmdlen -= j - ccline.cmdpos;
969 i = 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;
975 redrawcmd();
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. */
981 if (exmode_active
982 #ifdef FEAT_EVAL
983 || ccline.cmdfirstc == '>'
984 #endif
986 goto cmdline_not_changed;
988 vim_free(ccline.cmdbuff); /* no commandline to return */
989 ccline.cmdbuff = NULL;
990 if (!cmd_silent)
992 #ifdef FEAT_RIGHTLEFT
993 if (cmdmsg_rl)
994 msg_col = Columns;
995 else
996 #endif
997 msg_col = 0;
998 msg_putchar(' '); /* delete ':' */
1000 redraw_cmdline = TRUE;
1001 goto returncmd; /* back to cmd mode */
1003 goto cmdline_changed;
1005 case K_INS:
1006 case K_KINS:
1007 #ifdef FEAT_FKMAP
1008 /* if Farsi mode set, we are in reverse insert mode -
1009 Do not change the mode */
1010 if (cmd_fkmap)
1011 beep_flush();
1012 else
1013 #endif
1014 ccline.overstrike = !ccline.overstrike;
1015 #ifdef CURSOR_SHAPE
1016 ui_cursor_shape(); /* may show different cursor shape */
1017 #endif
1018 goto cmdline_not_changed;
1020 case Ctrl_HAT:
1021 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
1023 /* ":lmap" mappings exists, toggle use of mappings. */
1024 State ^= LANGMAP;
1025 #ifdef USE_IM_CONTROL
1026 im_set_active(FALSE); /* Disable input method */
1027 #endif
1028 if (b_im_ptr != NULL)
1030 if (State & LANGMAP)
1031 *b_im_ptr = B_IMODE_LMAP;
1032 else
1033 *b_im_ptr = B_IMODE_NONE;
1036 #ifdef USE_IM_CONTROL
1037 else
1039 /* There are no ":lmap" mappings, toggle IM. When
1040 * 'imdisable' is set don't try getting the status, it's
1041 * always off. */
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;
1049 else
1051 im_set_active(TRUE); /* Enable input method */
1052 if (b_im_ptr != NULL)
1053 *b_im_ptr = B_IMODE_IM;
1056 #endif
1057 if (b_im_ptr != NULL)
1059 if (b_im_ptr == &curbuf->b_p_iminsert)
1060 set_iminsert_global();
1061 else
1062 set_imsearch_global();
1064 #ifdef CURSOR_SHAPE
1065 ui_cursor_shape(); /* may show different cursor shape */
1066 #endif
1067 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1068 /* Show/unshow value of 'keymap' in status lines later. */
1069 status_redraw_curbuf();
1070 #endif
1071 goto cmdline_not_changed;
1073 /* case '@': only in very old vi */
1074 case Ctrl_U:
1075 /* delete all characters left of the cursor */
1076 j = ccline.cmdpos;
1077 ccline.cmdlen -= j;
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;
1083 redrawcmd();
1084 goto cmdline_changed;
1086 #ifdef FEAT_CLIPBOARD
1087 case Ctrl_Y:
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;
1095 break;
1096 #endif
1098 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1099 case Ctrl_C:
1100 /* In exmode it doesn't make sense to return. Except when
1101 * ":normal" runs out of characters. */
1102 if (exmode_active
1103 #ifdef FEAT_EX_EXTRA
1104 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1105 #endif
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 */
1116 #endif
1117 putcmdline('"', TRUE);
1118 ++no_mapping;
1119 i = c = plain_vgetc(); /* CTRL-R <char> */
1120 if (i == Ctrl_O)
1121 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1122 if (i == Ctrl_R)
1123 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
1124 --no_mapping;
1125 #ifdef FEAT_EVAL
1127 * Insert the result of an expression.
1128 * Need to save the current command line, to be able to enter
1129 * a new one...
1131 new_cmdpos = -1;
1132 if (c == '=')
1134 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1136 beep_flush();
1137 c = ESC;
1139 else
1141 save_cmdline(&save_ccline);
1142 c = get_expr_register();
1143 restore_cmdline(&save_ccline);
1146 #endif
1147 if (c != ESC) /* use ESC to cancel inserting register */
1149 cmdline_paste(c, i == Ctrl_R, FALSE);
1151 #ifdef FEAT_EVAL
1152 /* When there was a serious error abort getting the
1153 * command line. */
1154 if (aborting())
1156 gotesc = TRUE; /* will free ccline.cmdbuff after
1157 putting it in history */
1158 goto returncmd; /* back to cmd mode */
1160 #endif
1161 KeyTyped = FALSE; /* Don't do p_wc completion. */
1162 #ifdef FEAT_EVAL
1163 if (new_cmdpos >= 0)
1165 /* set_cmdline_pos() was used */
1166 if (new_cmdpos > ccline.cmdlen)
1167 ccline.cmdpos = ccline.cmdlen;
1168 else
1169 ccline.cmdpos = new_cmdpos;
1171 #endif
1173 redrawcmd();
1174 goto cmdline_changed;
1176 case Ctrl_D:
1177 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1178 break; /* Use ^D as normal char instead */
1180 redrawcmd();
1181 continue; /* don't do incremental search now */
1183 case K_RIGHT:
1184 case K_S_RIGHT:
1185 case K_C_RIGHT:
1188 if (ccline.cmdpos >= ccline.cmdlen)
1189 break;
1190 i = cmdline_charsize(ccline.cmdpos);
1191 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1192 break;
1193 ccline.cmdspos += i;
1194 #ifdef FEAT_MBYTE
1195 if (has_mbyte)
1196 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
1197 + ccline.cmdpos);
1198 else
1199 #endif
1200 ++ccline.cmdpos;
1202 while ((c == K_S_RIGHT || c == K_C_RIGHT
1203 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
1204 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1205 #ifdef FEAT_MBYTE
1206 if (has_mbyte)
1207 set_cmdspos_cursor();
1208 #endif
1209 goto cmdline_not_changed;
1211 case K_LEFT:
1212 case K_S_LEFT:
1213 case K_C_LEFT:
1214 if (ccline.cmdpos == 0)
1215 goto cmdline_not_changed;
1218 --ccline.cmdpos;
1219 #ifdef FEAT_MBYTE
1220 if (has_mbyte) /* move to first byte of char */
1221 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1222 ccline.cmdbuff + ccline.cmdpos);
1223 #endif
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] != ' ');
1230 #ifdef FEAT_MBYTE
1231 if (has_mbyte)
1232 set_cmdspos_cursor();
1233 #endif
1234 goto cmdline_not_changed;
1236 case K_IGNORE:
1237 /* Ignore mouse event or ex_window() result. */
1238 goto cmdline_not_changed;
1240 #ifdef FEAT_GUI_W32
1241 /* On Win32 ignore <M-F4>, we get it when closing the window was
1242 * cancelled. */
1243 case K_F4:
1244 if (mod_mask == MOD_MASK_ALT)
1246 redrawcmd(); /* somehow the cmdline is cleared */
1247 goto cmdline_not_changed;
1249 break;
1250 #endif
1252 #ifdef FEAT_MOUSE
1253 case K_MIDDLEDRAG:
1254 case K_MIDDLERELEASE:
1255 goto cmdline_not_changed; /* Ignore mouse */
1257 case K_MIDDLEMOUSE:
1258 # ifdef FEAT_GUI
1259 /* When GUI is active, also paste when 'mouse' is empty */
1260 if (!gui.in_use)
1261 # endif
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);
1267 else
1268 # endif
1269 cmdline_paste(0, TRUE, TRUE);
1270 redrawcmd();
1271 goto cmdline_changed;
1273 # ifdef FEAT_DND
1274 case K_DROP:
1275 cmdline_paste('~', TRUE, FALSE);
1276 redrawcmd();
1277 goto cmdline_changed;
1278 # endif
1280 case K_LEFTDRAG:
1281 case K_LEFTRELEASE:
1282 case K_RIGHTDRAG:
1283 case K_RIGHTRELEASE:
1284 /* Ignore drag and release events when the button-down wasn't
1285 * seen before. */
1286 if (ignore_drag_release)
1287 goto cmdline_not_changed;
1288 /* FALLTHROUGH */
1289 case K_LEFTMOUSE:
1290 case K_RIGHTMOUSE:
1291 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1292 ignore_drag_release = TRUE;
1293 else
1294 ignore_drag_release = FALSE;
1295 # ifdef FEAT_GUI
1296 /* When GUI is active, also move when 'mouse' is empty */
1297 if (!gui.in_use)
1298 # endif
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;
1321 # endif
1323 set_cmdspos();
1324 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1325 ++ccline.cmdpos)
1327 i = cmdline_charsize(ccline.cmdpos);
1328 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1329 && mouse_col < ccline.cmdspos % Columns + i)
1330 break;
1331 # ifdef FEAT_MBYTE
1332 if (has_mbyte)
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;
1339 # endif
1340 ccline.cmdspos += i;
1342 goto cmdline_not_changed;
1344 /* Mouse scroll wheel: ignored here */
1345 case K_MOUSEDOWN:
1346 case K_MOUSEUP:
1347 /* Alternate buttons ignored here */
1348 case K_X1MOUSE:
1349 case K_X1DRAG:
1350 case K_X1RELEASE:
1351 case K_X2MOUSE:
1352 case K_X2DRAG:
1353 case K_X2RELEASE:
1354 goto cmdline_not_changed;
1356 #endif /* FEAT_MOUSE */
1358 #ifdef FEAT_GUI
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)
1366 gui_do_scroll();
1367 redrawcmd();
1369 goto cmdline_not_changed;
1371 case K_HOR_SCROLLBAR:
1372 if (msg_scrolled == 0)
1374 gui_do_horiz_scroll();
1375 redrawcmd();
1377 goto cmdline_not_changed;
1378 #endif
1379 #ifdef FEAT_GUI_TABLINE
1380 case K_TABLINE:
1381 case K_TABMENU:
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;
1387 #endif
1389 case K_SELECT: /* end of Select mode mapping - ignore */
1390 goto cmdline_not_changed;
1392 case Ctrl_B: /* begin of command line */
1393 case K_HOME:
1394 case K_KHOME:
1395 case K_S_HOME:
1396 case K_C_HOME:
1397 ccline.cmdpos = 0;
1398 set_cmdspos();
1399 goto cmdline_not_changed;
1401 case Ctrl_E: /* end of command line */
1402 case K_END:
1403 case K_KEND:
1404 case K_S_END:
1405 case K_C_END:
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)
1412 break;
1413 goto cmdline_changed;
1415 case Ctrl_L:
1416 #ifdef FEAT_SEARCH_EXTRA
1417 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1419 /* Add a character from under the cursor for 'incsearch' */
1420 if (did_incsearch
1421 && !equalpos(curwin->w_cursor, old_cursor))
1423 c = gchar_cursor();
1424 if (c != NUL)
1426 if (c == firstc || vim_strchr((char_u *)(
1427 p_magic ? "\\^$.*[" : "\\^$"), c)
1428 != NULL)
1430 /* put a backslash before special characters */
1431 stuffcharReadbuff(c);
1432 c = '\\';
1434 break;
1437 goto cmdline_not_changed;
1439 #endif
1441 /* completion: longest common part */
1442 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1443 break;
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)
1451 == FAIL)
1452 break;
1453 goto cmdline_changed;
1456 #ifdef FEAT_CMDHIST
1457 case K_UP:
1458 case K_DOWN:
1459 case K_S_UP:
1460 case K_S_DOWN:
1461 case K_PAGEUP:
1462 case K_KPAGEUP:
1463 case K_PAGEDOWN:
1464 case K_KPAGEDOWN:
1465 if (hislen == 0 || firstc == NUL) /* no history */
1466 goto cmdline_not_changed;
1468 i = hiscnt;
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);
1479 for (;;)
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)
1490 --hiscnt;
1491 else /* at top of list */
1493 hiscnt = i;
1494 break;
1497 else /* one step forwards */
1499 /* on last entry, clear the line */
1500 if (hiscnt == hisidx[histype])
1502 hiscnt = hislen;
1503 break;
1506 /* not on a history line, nothing to do */
1507 if (hiscnt == hislen)
1508 break;
1509 if (hiscnt == hislen - 1) /* wrap around */
1510 hiscnt = 0;
1511 else
1512 ++hiscnt;
1514 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1516 hiscnt = i;
1517 break;
1519 if ((c != K_UP && c != K_DOWN)
1520 || hiscnt == i
1521 || STRNCMP(history[histype][hiscnt].hisstr,
1522 lookfor, (size_t)j) == 0)
1523 break;
1526 if (hiscnt != i) /* jumped to other entry */
1528 char_u *p;
1529 int len;
1530 int old_firstc;
1532 vim_free(ccline.cmdbuff);
1533 xpc.xp_context = EXPAND_NOTHING;
1534 if (hiscnt == hislen)
1535 p = lookfor; /* back to the old one */
1536 else
1537 p = history[histype][hiscnt].hisstr;
1539 if (histype == HIST_SEARCH
1540 && p != lookfor
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)
1549 len = 0;
1550 for (j = 0; p[j] != NUL; ++j)
1552 /* Replace old sep with new sep, unless it is
1553 * escaped. */
1554 if (p[j] == old_firstc
1555 && (j == 0 || p[j - 1] != '\\'))
1557 if (i > 0)
1558 ccline.cmdbuff[len] = firstc;
1560 else
1562 /* Escape new sep, unless it is already
1563 * escaped. */
1564 if (p[j] == firstc
1565 && (j == 0 || p[j - 1] != '\\'))
1567 if (i > 0)
1568 ccline.cmdbuff[len] = '\\';
1569 ++len;
1571 if (i > 0)
1572 ccline.cmdbuff[len] = p[j];
1574 ++len;
1576 if (i == 0)
1578 alloc_cmdbuff(len);
1579 if (ccline.cmdbuff == NULL)
1580 goto returncmd;
1583 ccline.cmdbuff[len] = NUL;
1585 else
1587 alloc_cmdbuff((int)STRLEN(p));
1588 if (ccline.cmdbuff == NULL)
1589 goto returncmd;
1590 STRCPY(ccline.cmdbuff, p);
1593 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1594 redrawcmd();
1595 goto cmdline_changed;
1597 beep_flush();
1598 goto cmdline_not_changed;
1599 #endif
1601 case Ctrl_V:
1602 case Ctrl_Q:
1603 #ifdef FEAT_MOUSE
1604 ignore_drag_release = TRUE;
1605 #endif
1606 putcmdline('^', TRUE);
1607 c = get_literal(); /* get next (two) character(s) */
1608 do_abbr = FALSE; /* don't do abbreviation now */
1609 #ifdef FEAT_MBYTE
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);
1614 msg_putchar(' ');
1615 cursorcmd();
1617 #endif
1618 break;
1620 #ifdef FEAT_DIGRAPHS
1621 case Ctrl_K:
1622 #ifdef FEAT_MOUSE
1623 ignore_drag_release = TRUE;
1624 #endif
1625 putcmdline('?', TRUE);
1626 #ifdef USE_ON_FLY_SCROLL
1627 dont_scroll = TRUE; /* disallow scrolling here */
1628 #endif
1629 c = get_digraph(TRUE);
1630 if (c != NUL)
1631 break;
1633 redrawcmd();
1634 goto cmdline_not_changed;
1635 #endif /* FEAT_DIGRAPHS */
1637 #ifdef FEAT_RIGHTLEFT
1638 case Ctrl__: /* CTRL-_: switch language mode */
1639 if (!p_ari)
1640 break;
1641 #ifdef FEAT_FKMAP
1642 if (p_altkeymap)
1644 cmd_fkmap = !cmd_fkmap;
1645 if (cmd_fkmap) /* in Farsi always in Insert mode */
1646 ccline.overstrike = FALSE;
1648 else /* Hebrew is default */
1649 #endif
1650 cmd_hkmap = !cmd_hkmap;
1651 goto cmdline_not_changed;
1652 #endif
1654 default:
1655 #ifdef UNIX
1656 if (c == intr_char)
1658 gotesc = TRUE; /* will free ccline.cmdbuff after
1659 putting it in history */
1660 goto returncmd; /* back to Normal mode */
1662 #endif
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.
1668 if (!IS_SPECIAL(c))
1669 mod_mask = 0x0;
1670 break;
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(
1678 #ifdef FEAT_MBYTE
1679 /* Add ABBR_OFF for characters above 0x100, this is
1680 * what check_abbr() expects. */
1681 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1682 #endif
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);
1691 else
1693 #ifdef FEAT_MBYTE
1694 if (has_mbyte)
1696 j = (*mb_char2bytes)(c, IObuff);
1697 IObuff[j] = NUL; /* exclude composing chars */
1698 put_on_cmdline(IObuff, j, TRUE);
1700 else
1701 #endif
1703 IObuff[0] = c;
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
1713 * the past.
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)
1720 continue;
1721 #endif
1723 cmdline_changed:
1724 #ifdef FEAT_SEARCH_EXTRA
1726 * 'incsearch' highlighting.
1728 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1730 pos_T end_pos;
1731 #ifdef FEAT_RELTIME
1732 proftime_T tm;
1733 #endif
1735 /* if there is a character waiting, search and redraw later */
1736 if (char_avail())
1738 incsearch_postponed = TRUE;
1739 continue;
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)
1746 i = 0;
1747 else
1749 int search_options = (SEARCH_KEEP + SEARCH_OPT
1750 + SEARCH_NOOF + SEARCH_PEEK);
1752 cursor_off(); /* so the user knows we're busy */
1753 out_flush();
1754 ++emsg_off; /* So it doesn't beep if bad expr */
1755 #ifdef USE_MIGEMO
1756 if (migemo_enabled)
1757 search_options |= SEARCH_MIGEMO;
1758 #endif
1759 #ifdef FEAT_RELTIME
1760 /* Set the time limit to half a second. */
1761 profile_setlimit(500L, &tm);
1762 #endif
1763 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1764 search_options,
1765 #ifdef FEAT_RELTIME
1767 #else
1768 NULL
1769 #endif
1771 --emsg_off;
1772 /* if interrupted while searching, behave like it failed */
1773 if (got_int)
1775 (void)vpeekc(); /* remove <C-C> from input stream */
1776 got_int = FALSE; /* don't abandon the command line */
1777 i = 0;
1779 else if (char_avail())
1780 /* cancelled searching because a char was typed */
1781 incsearch_postponed = TRUE;
1783 if (i != 0)
1784 highlight_match = TRUE; /* highlight position */
1785 else
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;
1792 # ifdef FEAT_DIFF
1793 curwin->w_topfill = old_topfill;
1794 # endif
1795 curwin->w_botline = old_botline;
1796 changed_cline_bef_curs();
1797 update_topline();
1799 if (i != 0)
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);
1814 validate_cursor();
1815 end_pos = curwin->w_cursor;
1816 curwin->w_cursor = save_pos;
1818 else
1819 end_pos = curwin->w_cursor; /* shutup gcc 4 */
1821 validate_cursor();
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;
1826 # endif
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. */
1833 if (i != 0)
1834 curwin->w_cursor = end_pos;
1836 msg_starthere();
1837 redrawcmdline();
1838 did_incsearch = TRUE;
1840 #else /* FEAT_SEARCH_EXTRA */
1842 #endif
1844 #ifdef FEAT_RIGHTLEFT
1845 if (cmdmsg_rl
1846 # ifdef FEAT_ARABIC
1847 || (p_arshape && !p_tbidi && enc_utf8)
1848 # endif
1850 /* Always redraw the whole command line to fix shaping and
1851 * right-left typing. Not efficient, but it works. */
1852 redrawcmd();
1853 #endif
1856 returncmd:
1858 #ifdef FEAT_RIGHTLEFT
1859 cmdmsg_rl = FALSE;
1860 #endif
1862 #ifdef FEAT_FKMAP
1863 cmd_fkmap = 0;
1864 #endif
1866 ExpandCleanup(&xpc);
1867 ccline.xpc = NULL;
1869 #ifdef FEAT_SEARCH_EXTRA
1870 if (did_incsearch)
1872 curwin->w_cursor = old_cursor;
1873 curwin->w_curswant = old_curswant;
1874 curwin->w_leftcol = old_leftcol;
1875 curwin->w_topline = old_topline;
1876 # ifdef FEAT_DIFF
1877 curwin->w_topfill = old_topfill;
1878 # endif
1879 curwin->w_botline = old_botline;
1880 highlight_match = FALSE;
1881 validate_cursor(); /* needed for TAB */
1882 redraw_later(SOME_VALID);
1884 #endif
1886 if (ccline.cmdbuff != NULL)
1889 * Put line in history buffer (":" and "=" only when it was typed).
1891 #ifdef FEAT_CMDHIST
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);
1897 if (firstc == ':')
1899 vim_free(new_last_cmdline);
1900 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1903 #endif
1905 if (gotesc) /* abandon command line */
1907 vim_free(ccline.cmdbuff);
1908 ccline.cmdbuff = NULL;
1909 if (msg_scrolled == 0)
1910 compute_cmdrow();
1911 MSG("");
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.
1921 msg_check();
1922 msg_scroll = save_msg_scroll;
1923 redir_off = FALSE;
1925 /* When the command line was typed, no need for a wait-return prompt. */
1926 if (some_key_typed)
1927 need_wait_return = FALSE;
1929 State = save_State;
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);
1934 #endif
1935 #ifdef FEAT_MOUSE
1936 setmouse();
1937 #endif
1938 #ifdef CURSOR_SHAPE
1939 ui_cursor_shape(); /* may show different cursor shape */
1940 #endif
1943 char_u *p = ccline.cmdbuff;
1945 /* Make ccline empty, getcmdline() may try to use it. */
1946 ccline.cmdbuff = NULL;
1947 return p;
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.
1958 char_u *
1959 getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
1960 int firstc;
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 */
1966 char_u *s;
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;
1973 # ifdef FEAT_EVAL
1974 ccline.xp_context = xp_context;
1975 ccline.xp_arg = xp_arg;
1976 ccline.input_fn = (firstc == '@');
1977 # endif
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;
1983 return s;
1985 #endif
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.
1993 text_locked()
1995 #ifdef FEAT_CMDWIN
1996 if (cmdwin_type != 0)
1997 return TRUE;
1998 #endif
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.
2006 void
2007 text_locked_msg()
2009 #ifdef FEAT_CMDWIN
2010 if (cmdwin_type != 0)
2011 EMSG(_(e_cmdwin));
2012 else
2013 #endif
2014 EMSG(_(e_secure));
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.
2023 curbuf_locked()
2025 if (curbuf_lock > 0)
2027 EMSG(_("E788: Not allowed to edit another buffer now"));
2028 return TRUE;
2030 return allbuf_locked();
2034 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2035 * message.
2038 allbuf_locked()
2040 if (allbuf_lock > 0)
2042 EMSG(_("E811: Not allowed to change buffer information now"));
2043 return TRUE;
2045 return FALSE;
2047 #endif
2049 static int
2050 cmdline_charsize(idx)
2051 int idx;
2053 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2054 if (cmdline_star > 0) /* showing '*', always 1 position */
2055 return 1;
2056 #endif
2057 return ptr2cells(ccline.cmdbuff + idx);
2061 * Compute the offset of the cursor on the command line for the prompt and
2062 * indent.
2064 static void
2065 set_cmdspos()
2067 if (ccline.cmdfirstc != NUL)
2068 ccline.cmdspos = 1 + ccline.cmdindent;
2069 else
2070 ccline.cmdspos = 0 + ccline.cmdindent;
2074 * Compute the screen position for the cursor on the command line.
2076 static void
2077 set_cmdspos_cursor()
2079 int i, m, c;
2081 set_cmdspos();
2082 if (KeyTyped)
2084 m = Columns * Rows;
2085 if (m < 0) /* overflow, Columns or Rows at weird value */
2086 m = MAXCOL;
2088 else
2089 m = MAXCOL;
2090 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2092 c = cmdline_charsize(i);
2093 #ifdef FEAT_MBYTE
2094 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2095 if (has_mbyte)
2096 correct_cmdspos(i, c);
2097 #endif
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;
2103 break;
2105 #ifdef FEAT_MBYTE
2106 if (has_mbyte)
2107 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
2108 #endif
2112 #ifdef FEAT_MBYTE
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.
2117 static void
2118 correct_cmdspos(idx, cells)
2119 int idx;
2120 int cells;
2122 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
2123 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2124 && ccline.cmdspos % Columns + cells > Columns)
2125 ccline.cmdspos++;
2127 #endif
2130 * Get an Ex command line for the ":" command.
2132 char_u *
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() == ':')
2140 (void)vgetc();
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.
2150 char_u *
2151 getexmodeline(promptc, cookie, indent)
2152 int promptc; /* normally ':', NUL for ":append" and '?' for
2153 :s prompt */
2154 void *cookie UNUSED;
2155 int indent; /* indent for inside conditionals */
2157 garray_T line_ga;
2158 char_u *pend;
2159 int startcol = 0;
2160 int c1 = 0;
2161 int escaped = FALSE; /* CTRL-V typed */
2162 int vcol = 0;
2163 char_u *p;
2164 int prev_char;
2166 /* Switch cursor on now. This avoids that it happens after the "\n", which
2167 * confuses the system function that computes tabstops. */
2168 cursor_on();
2170 /* always start in column 0; write a newline if necessary */
2171 compute_cmdrow();
2172 if ((msg_col || msg_didout) && promptc != '?')
2173 msg_putchar('\n');
2174 if (promptc == ':')
2176 /* indent that is only displayed, not in the line itself */
2177 if (p_prompt)
2178 msg_putchar(':');
2179 while (indent-- > 0)
2180 msg_putchar(' ');
2181 startcol = msg_col;
2184 ga_init2(&line_ga, 1, 30);
2186 /* autoindent for :insert and :append is in the line itself */
2187 if (promptc <= 0)
2189 vcol = indent;
2190 while (indent >= 8)
2192 ga_append(&line_ga, TAB);
2193 msg_puts((char_u *)" ");
2194 indent -= 8;
2196 while (indent-- > 0)
2198 ga_append(&line_ga, ' ');
2199 msg_putchar(' ');
2202 ++no_mapping;
2203 ++allow_keys;
2206 * Get the line, one character at a time.
2208 got_int = FALSE;
2209 while (!got_int)
2211 if (ga_grow(&line_ga, 40) == FAIL)
2212 break;
2214 /* Get one character at a time. Don't use inchar(), it can't handle
2215 * special characters. */
2216 prev_char = c1;
2217 c1 = vgetc();
2220 * Handle line editing.
2221 * Previously this was left to the system, putting the terminal in
2222 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
2224 if (got_int)
2226 msg_putchar('\n');
2227 break;
2230 if (!escaped)
2232 /* CR typed means "enter", which is NL */
2233 if (c1 == '\r')
2234 c1 = '\n';
2236 if (c1 == BS || c1 == K_BS
2237 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
2239 if (line_ga.ga_len > 0)
2241 --line_ga.ga_len;
2242 goto redraw;
2244 continue;
2247 if (c1 == Ctrl_U)
2249 msg_col = startcol;
2250 msg_clr_eos();
2251 line_ga.ga_len = 0;
2252 continue;
2255 if (c1 == Ctrl_T)
2257 p = (char_u *)line_ga.ga_data;
2258 p[line_ga.ga_len] = NUL;
2259 indent = get_indent_str(p, 8);
2260 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2261 add_indent:
2262 while (get_indent_str(p, 8) < indent)
2264 char_u *s = skipwhite(p);
2266 ga_grow(&line_ga, 1);
2267 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2268 *s = ' ';
2269 ++line_ga.ga_len;
2271 redraw:
2272 /* redraw the line */
2273 msg_col = startcol;
2274 vcol = 0;
2275 for (p = (char_u *)line_ga.ga_data;
2276 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
2278 if (*p == TAB)
2282 msg_putchar(' ');
2283 } while (++vcol % 8);
2285 else
2287 msg_outtrans_len(p, 1);
2288 vcol += char2cells(*p);
2291 msg_clr_eos();
2292 windgoto(msg_row, msg_col);
2293 continue;
2296 if (c1 == Ctrl_D)
2298 /* Delete one shiftwidth. */
2299 p = (char_u *)line_ga.ga_data;
2300 if (prev_char == '0' || prev_char == '^')
2302 if (prev_char == '^')
2303 ex_keep_indent = TRUE;
2304 indent = 0;
2305 p[--line_ga.ga_len] = NUL;
2307 else
2309 p[line_ga.ga_len] = NUL;
2310 indent = get_indent_str(p, 8);
2311 --indent;
2312 indent -= indent % curbuf->b_p_sw;
2314 while (get_indent_str(p, 8) > indent)
2316 char_u *s = skipwhite(p);
2318 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2319 --line_ga.ga_len;
2321 goto add_indent;
2324 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2326 escaped = TRUE;
2327 continue;
2330 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2331 if (IS_SPECIAL(c1))
2332 continue;
2335 if (IS_SPECIAL(c1))
2336 c1 = '?';
2337 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2338 if (c1 == '\n')
2339 msg_putchar('\n');
2340 else if (c1 == TAB)
2342 /* Don't use chartabsize(), 'ts' can be different */
2345 msg_putchar(' ');
2346 } while (++vcol % 8);
2348 else
2350 msg_outtrans_len(
2351 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2352 vcol += char2cells(c1);
2354 ++line_ga.ga_len;
2355 escaped = FALSE;
2357 windgoto(msg_row, msg_col);
2358 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
2360 /* we are done when a NL is entered, but not when it comes after a
2361 * backslash */
2362 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2363 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
2365 --line_ga.ga_len;
2366 --pend;
2367 *pend = NUL;
2368 break;
2372 --no_mapping;
2373 --allow_keys;
2375 /* make following messages go to the next line */
2376 msg_didout = FALSE;
2377 msg_col = 0;
2378 if (msg_row < Rows - 1)
2379 ++msg_row;
2380 emsg_on_display = FALSE; /* don't want ui_delay() */
2382 if (got_int)
2383 ga_clear(&line_ga);
2385 return (char_u *)line_ga.ga_data;
2388 # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2389 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
2391 * Return TRUE if ccline.overstrike is on.
2394 cmdline_overstrike()
2396 return ccline.overstrike;
2400 * Return TRUE if the cursor is at the end of the cmdline.
2403 cmdline_at_end()
2405 return (ccline.cmdpos >= ccline.cmdlen);
2407 #endif
2409 #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
2411 * Return the virtual column number at the current cursor position.
2412 * This is used by the IM code to obtain the start of the preedit string.
2414 colnr_T
2415 cmdline_getvcol_cursor()
2417 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2418 return MAXCOL;
2420 # ifdef FEAT_MBYTE
2421 if (has_mbyte)
2423 colnr_T col;
2424 int i = 0;
2426 for (col = 0; i < ccline.cmdpos; ++col)
2427 i += (*mb_ptr2len)(ccline.cmdbuff + i);
2429 return col;
2431 else
2432 # endif
2433 return ccline.cmdpos;
2435 #endif
2437 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2439 * If part of the command line is an IM preedit string, redraw it with
2440 * IM feedback attributes. The cursor position is restored after drawing.
2442 static void
2443 redrawcmd_preedit()
2445 if ((State & CMDLINE)
2446 && xic != NULL
2447 /* && im_get_status() doesn't work when using SCIM */
2448 && !p_imdisable
2449 && im_is_preediting())
2451 int cmdpos = 0;
2452 int cmdspos;
2453 int old_row;
2454 int old_col;
2455 colnr_T col;
2457 old_row = msg_row;
2458 old_col = msg_col;
2459 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
2461 # ifdef FEAT_MBYTE
2462 if (has_mbyte)
2464 for (col = 0; col < preedit_start_col
2465 && cmdpos < ccline.cmdlen; ++col)
2467 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
2468 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
2471 else
2472 # endif
2474 cmdspos += preedit_start_col;
2475 cmdpos += preedit_start_col;
2478 msg_row = cmdline_row + (cmdspos / (int)Columns);
2479 msg_col = cmdspos % (int)Columns;
2480 if (msg_row >= Rows)
2481 msg_row = Rows - 1;
2483 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2485 int char_len;
2486 int char_attr;
2488 char_attr = im_get_feedback_attr(col);
2489 if (char_attr < 0)
2490 break; /* end of preedit string */
2492 # ifdef FEAT_MBYTE
2493 if (has_mbyte)
2494 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
2495 else
2496 # endif
2497 char_len = 1;
2499 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2500 cmdpos += char_len;
2503 msg_row = old_row;
2504 msg_col = old_col;
2507 #endif /* FEAT_XIM && FEAT_GUI_GTK */
2510 * Allocate a new command line buffer.
2511 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2512 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2514 static void
2515 alloc_cmdbuff(len)
2516 int len;
2519 * give some extra space to avoid having to allocate all the time
2521 if (len < 80)
2522 len = 100;
2523 else
2524 len += 20;
2526 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2527 ccline.cmdbufflen = len;
2531 * Re-allocate the command line to length len + something extra.
2532 * return FAIL for failure, OK otherwise
2534 static int
2535 realloc_cmdbuff(len)
2536 int len;
2538 char_u *p;
2540 p = ccline.cmdbuff;
2541 alloc_cmdbuff(len); /* will get some more */
2542 if (ccline.cmdbuff == NULL) /* out of memory */
2544 ccline.cmdbuff = p; /* keep the old one */
2545 return FAIL;
2547 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2548 vim_free(p);
2550 if (ccline.xpc != NULL
2551 && ccline.xpc->xp_pattern != NULL
2552 && ccline.xpc->xp_context != EXPAND_NOTHING
2553 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2555 int i = (int)(ccline.xpc->xp_pattern - p);
2557 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2558 * to point into the newly allocated memory. */
2559 if (i >= 0 && i <= ccline.cmdlen)
2560 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2563 return OK;
2566 #if defined(FEAT_ARABIC) || defined(PROTO)
2567 static char_u *arshape_buf = NULL;
2569 # if defined(EXITFREE) || defined(PROTO)
2570 void
2571 free_cmdline_buf()
2573 vim_free(arshape_buf);
2575 # endif
2576 #endif
2579 * Draw part of the cmdline at the current cursor position. But draw stars
2580 * when cmdline_star is TRUE.
2582 static void
2583 draw_cmdline(start, len)
2584 int start;
2585 int len;
2587 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2588 int i;
2590 if (cmdline_star > 0)
2591 for (i = 0; i < len; ++i)
2593 msg_putchar('*');
2594 # ifdef FEAT_MBYTE
2595 if (has_mbyte)
2596 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
2597 # endif
2599 else
2600 #endif
2601 #ifdef FEAT_ARABIC
2602 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2604 static int buflen = 0;
2605 char_u *p;
2606 int j;
2607 int newlen = 0;
2608 int mb_l;
2609 int pc, pc1 = 0;
2610 int prev_c = 0;
2611 int prev_c1 = 0;
2612 int u8c;
2613 int u8cc[MAX_MCO];
2614 int nc = 0;
2617 * Do arabic shaping into a temporary buffer. This is very
2618 * inefficient!
2620 if (len * 2 + 2 > buflen)
2622 /* Re-allocate the buffer. We keep it around to avoid a lot of
2623 * alloc()/free() calls. */
2624 vim_free(arshape_buf);
2625 buflen = len * 2 + 2;
2626 arshape_buf = alloc(buflen);
2627 if (arshape_buf == NULL)
2628 return; /* out of memory */
2631 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2633 /* Prepend a space to draw the leading composing char on. */
2634 arshape_buf[0] = ' ';
2635 newlen = 1;
2638 for (j = start; j < start + len; j += mb_l)
2640 p = ccline.cmdbuff + j;
2641 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
2642 mb_l = utfc_ptr2len_len(p, start + len - j);
2643 if (ARABIC_CHAR(u8c))
2645 /* Do Arabic shaping. */
2646 if (cmdmsg_rl)
2648 /* displaying from right to left */
2649 pc = prev_c;
2650 pc1 = prev_c1;
2651 prev_c1 = u8cc[0];
2652 if (j + mb_l >= start + len)
2653 nc = NUL;
2654 else
2655 nc = utf_ptr2char(p + mb_l);
2657 else
2659 /* displaying from left to right */
2660 if (j + mb_l >= start + len)
2661 pc = NUL;
2662 else
2664 int pcc[MAX_MCO];
2666 pc = utfc_ptr2char_len(p + mb_l, pcc,
2667 start + len - j - mb_l);
2668 pc1 = pcc[0];
2670 nc = prev_c;
2672 prev_c = u8c;
2674 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
2676 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
2677 if (u8cc[0] != 0)
2679 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2680 if (u8cc[1] != 0)
2681 newlen += (*mb_char2bytes)(u8cc[1],
2682 arshape_buf + newlen);
2685 else
2687 prev_c = u8c;
2688 mch_memmove(arshape_buf + newlen, p, mb_l);
2689 newlen += mb_l;
2693 msg_outtrans_len(arshape_buf, newlen);
2695 else
2696 #endif
2697 msg_outtrans_len(ccline.cmdbuff + start, len);
2701 * Put a character on the command line. Shifts the following text to the
2702 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2703 * "c" must be printable (fit in one display cell)!
2705 void
2706 putcmdline(c, shift)
2707 int c;
2708 int shift;
2710 if (cmd_silent)
2711 return;
2712 msg_no_more = TRUE;
2713 msg_putchar(c);
2714 if (shift)
2715 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2716 msg_no_more = FALSE;
2717 cursorcmd();
2721 * Undo a putcmdline(c, FALSE).
2723 void
2724 unputcmdline()
2726 if (cmd_silent)
2727 return;
2728 msg_no_more = TRUE;
2729 if (ccline.cmdlen == ccline.cmdpos)
2730 msg_putchar(' ');
2731 else
2732 draw_cmdline(ccline.cmdpos, 1);
2733 msg_no_more = FALSE;
2734 cursorcmd();
2738 * Put the given string, of the given length, onto the command line.
2739 * If len is -1, then STRLEN() is used to calculate the length.
2740 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2741 * part will be redrawn, otherwise it will not. If this function is called
2742 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2743 * called afterwards.
2746 put_on_cmdline(str, len, redraw)
2747 char_u *str;
2748 int len;
2749 int redraw;
2751 int retval;
2752 int i;
2753 int m;
2754 int c;
2756 if (len < 0)
2757 len = (int)STRLEN(str);
2759 /* Check if ccline.cmdbuff needs to be longer */
2760 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2761 retval = realloc_cmdbuff(ccline.cmdlen + len);
2762 else
2763 retval = OK;
2764 if (retval == OK)
2766 if (!ccline.overstrike)
2768 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2769 ccline.cmdbuff + ccline.cmdpos,
2770 (size_t)(ccline.cmdlen - ccline.cmdpos));
2771 ccline.cmdlen += len;
2773 else
2775 #ifdef FEAT_MBYTE
2776 if (has_mbyte)
2778 /* Count nr of characters in the new string. */
2779 m = 0;
2780 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
2781 ++m;
2782 /* Count nr of bytes in cmdline that are overwritten by these
2783 * characters. */
2784 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
2785 i += (*mb_ptr2len)(ccline.cmdbuff + i))
2786 --m;
2787 if (i < ccline.cmdlen)
2789 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2790 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2791 ccline.cmdlen += ccline.cmdpos + len - i;
2793 else
2794 ccline.cmdlen = ccline.cmdpos + len;
2796 else
2797 #endif
2798 if (ccline.cmdpos + len > ccline.cmdlen)
2799 ccline.cmdlen = ccline.cmdpos + len;
2801 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2802 ccline.cmdbuff[ccline.cmdlen] = NUL;
2804 #ifdef FEAT_MBYTE
2805 if (enc_utf8)
2807 /* When the inserted text starts with a composing character,
2808 * backup to the character before it. There could be two of them.
2810 i = 0;
2811 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2812 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2814 i = (*mb_head_off)(ccline.cmdbuff,
2815 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2816 ccline.cmdpos -= i;
2817 len += i;
2818 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2820 # ifdef FEAT_ARABIC
2821 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2823 /* Check the previous character for Arabic combining pair. */
2824 i = (*mb_head_off)(ccline.cmdbuff,
2825 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2826 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2827 + ccline.cmdpos - i), c))
2829 ccline.cmdpos -= i;
2830 len += i;
2832 else
2833 i = 0;
2835 # endif
2836 if (i != 0)
2838 /* Also backup the cursor position. */
2839 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2840 ccline.cmdspos -= i;
2841 msg_col -= i;
2842 if (msg_col < 0)
2844 msg_col += Columns;
2845 --msg_row;
2849 #endif
2851 if (redraw && !cmd_silent)
2853 msg_no_more = TRUE;
2854 i = cmdline_row;
2855 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2856 /* Avoid clearing the rest of the line too often. */
2857 if (cmdline_row != i || ccline.overstrike)
2858 msg_clr_eos();
2859 msg_no_more = FALSE;
2861 #ifdef FEAT_FKMAP
2863 * If we are in Farsi command mode, the character input must be in
2864 * Insert mode. So do not advance the cmdpos.
2866 if (!cmd_fkmap)
2867 #endif
2869 if (KeyTyped)
2871 m = Columns * Rows;
2872 if (m < 0) /* overflow, Columns or Rows at weird value */
2873 m = MAXCOL;
2875 else
2876 m = MAXCOL;
2877 for (i = 0; i < len; ++i)
2879 c = cmdline_charsize(ccline.cmdpos);
2880 #ifdef FEAT_MBYTE
2881 /* count ">" for a double-wide char that doesn't fit. */
2882 if (has_mbyte)
2883 correct_cmdspos(ccline.cmdpos, c);
2884 #endif
2885 /* Stop cursor at the end of the screen, but do increment the
2886 * insert position, so that entering a very long command
2887 * works, even though you can't see it. */
2888 if (ccline.cmdspos + c < m)
2889 ccline.cmdspos += c;
2890 #ifdef FEAT_MBYTE
2891 if (has_mbyte)
2893 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
2894 if (c > len - i - 1)
2895 c = len - i - 1;
2896 ccline.cmdpos += c;
2897 i += c;
2899 #endif
2900 ++ccline.cmdpos;
2904 if (redraw)
2905 msg_check();
2906 return retval;
2909 static struct cmdline_info prev_ccline;
2910 static int prev_ccline_used = FALSE;
2913 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2914 * and overwrite it. But get_cmdline_str() may need it, thus make it
2915 * available globally in prev_ccline.
2917 static void
2918 save_cmdline(ccp)
2919 struct cmdline_info *ccp;
2921 if (!prev_ccline_used)
2923 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2924 prev_ccline_used = TRUE;
2926 *ccp = prev_ccline;
2927 prev_ccline = ccline;
2928 ccline.cmdbuff = NULL;
2929 ccline.cmdprompt = NULL;
2930 ccline.xpc = NULL;
2934 * Restore ccline after it has been saved with save_cmdline().
2936 static void
2937 restore_cmdline(ccp)
2938 struct cmdline_info *ccp;
2940 ccline = prev_ccline;
2941 prev_ccline = *ccp;
2944 #if defined(FEAT_EVAL) || defined(PROTO)
2946 * Save the command line into allocated memory. Returns a pointer to be
2947 * passed to restore_cmdline_alloc() later.
2948 * Returns NULL when failed.
2950 char_u *
2951 save_cmdline_alloc()
2953 struct cmdline_info *p;
2955 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2956 if (p != NULL)
2957 save_cmdline(p);
2958 return (char_u *)p;
2962 * Restore the command line from the return value of save_cmdline_alloc().
2964 void
2965 restore_cmdline_alloc(p)
2966 char_u *p;
2968 if (p != NULL)
2970 restore_cmdline((struct cmdline_info *)p);
2971 vim_free(p);
2974 #endif
2977 * paste a yank register into the command line.
2978 * used by CTRL-R command in command-line mode
2979 * insert_reg() can't be used here, because special characters from the
2980 * register contents will be interpreted as commands.
2982 * return FAIL for failure, OK otherwise
2984 static int
2985 cmdline_paste(regname, literally, remcr)
2986 int regname;
2987 int literally; /* Insert text literally instead of "as typed" */
2988 int remcr; /* remove trailing CR */
2990 long i;
2991 char_u *arg;
2992 char_u *p;
2993 int allocated;
2994 struct cmdline_info save_ccline;
2996 /* check for valid regname; also accept special characters for CTRL-R in
2997 * the command line */
2998 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2999 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3000 return FAIL;
3002 /* A register containing CTRL-R can cause an endless loop. Allow using
3003 * CTRL-C to break the loop. */
3004 line_breakcheck();
3005 if (got_int)
3006 return FAIL;
3008 #ifdef FEAT_CLIPBOARD
3009 regname = may_get_selection(regname);
3010 #endif
3012 /* Need to save and restore ccline. And set "textlock" to avoid nasty
3013 * things like going to another buffer when evaluating an expression. */
3014 save_cmdline(&save_ccline);
3015 ++textlock;
3016 i = get_spec_reg(regname, &arg, &allocated, TRUE);
3017 --textlock;
3018 restore_cmdline(&save_ccline);
3020 if (i)
3022 /* Got the value of a special register in "arg". */
3023 if (arg == NULL)
3024 return FAIL;
3026 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3027 * part of the word. */
3028 p = arg;
3029 if (p_is && regname == Ctrl_W)
3031 char_u *w;
3032 int len;
3034 /* Locate start of last word in the cmd buffer. */
3035 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
3037 #ifdef FEAT_MBYTE
3038 if (has_mbyte)
3040 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3041 if (!vim_iswordc(mb_ptr2char(w - len)))
3042 break;
3043 w -= len;
3045 else
3046 #endif
3048 if (!vim_iswordc(w[-1]))
3049 break;
3050 --w;
3053 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
3054 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3055 p += len;
3058 cmdline_paste_str(p, literally);
3059 if (allocated)
3060 vim_free(arg);
3061 return OK;
3064 return cmdline_paste_reg(regname, literally, remcr);
3068 * Put a string on the command line.
3069 * When "literally" is TRUE, insert literally.
3070 * When "literally" is FALSE, insert as typed, but don't leave the command
3071 * line.
3073 void
3074 cmdline_paste_str(s, literally)
3075 char_u *s;
3076 int literally;
3078 int c, cv;
3080 if (literally)
3081 put_on_cmdline(s, -1, TRUE);
3082 else
3083 while (*s != NUL)
3085 cv = *s;
3086 if (cv == Ctrl_V && s[1])
3087 ++s;
3088 #ifdef FEAT_MBYTE
3089 if (has_mbyte)
3090 c = mb_cptr2char_adv(&s);
3091 else
3092 #endif
3093 c = *s++;
3094 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3095 #ifdef UNIX
3096 || c == intr_char
3097 #endif
3098 || (c == Ctrl_BSL && *s == Ctrl_N))
3099 stuffcharReadbuff(Ctrl_V);
3100 stuffcharReadbuff(c);
3104 #ifdef FEAT_WILDMENU
3106 * Delete characters on the command line, from "from" to the current
3107 * position.
3109 static void
3110 cmdline_del(from)
3111 int from;
3113 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3114 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3115 ccline.cmdlen -= ccline.cmdpos - from;
3116 ccline.cmdpos = from;
3118 #endif
3121 * this function is called when the screen size changes and with incremental
3122 * search
3124 void
3125 redrawcmdline()
3127 if (cmd_silent)
3128 return;
3129 need_wait_return = FALSE;
3130 compute_cmdrow();
3131 redrawcmd();
3132 cursorcmd();
3135 static void
3136 redrawcmdprompt()
3138 int i;
3140 if (cmd_silent)
3141 return;
3142 if (ccline.cmdfirstc != NUL)
3143 msg_putchar(ccline.cmdfirstc);
3144 if (ccline.cmdprompt != NULL)
3146 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3147 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3148 /* do the reverse of set_cmdspos() */
3149 if (ccline.cmdfirstc != NUL)
3150 --ccline.cmdindent;
3152 else
3153 for (i = ccline.cmdindent; i > 0; --i)
3154 msg_putchar(' ');
3158 * Redraw what is currently on the command line.
3160 void
3161 redrawcmd()
3163 if (cmd_silent)
3164 return;
3166 /* when 'incsearch' is set there may be no command line while redrawing */
3167 if (ccline.cmdbuff == NULL)
3169 windgoto(cmdline_row, 0);
3170 msg_clr_eos();
3171 return;
3174 msg_start();
3175 redrawcmdprompt();
3177 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3178 msg_no_more = TRUE;
3179 draw_cmdline(0, ccline.cmdlen);
3180 msg_clr_eos();
3181 msg_no_more = FALSE;
3183 set_cmdspos_cursor();
3186 * An emsg() before may have set msg_scroll. This is used in normal mode,
3187 * in cmdline mode we can reset them now.
3189 msg_scroll = FALSE; /* next message overwrites cmdline */
3191 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3192 * in cmdline mode */
3193 skip_redraw = FALSE;
3196 void
3197 compute_cmdrow()
3199 if (exmode_active || msg_scrolled != 0)
3200 cmdline_row = Rows - 1;
3201 else
3202 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3203 + W_STATUS_HEIGHT(lastwin);
3206 static void
3207 cursorcmd()
3209 if (cmd_silent)
3210 return;
3212 #ifdef FEAT_RIGHTLEFT
3213 if (cmdmsg_rl)
3215 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3216 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3217 if (msg_row <= 0)
3218 msg_row = Rows - 1;
3220 else
3221 #endif
3223 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3224 msg_col = ccline.cmdspos % (int)Columns;
3225 if (msg_row >= Rows)
3226 msg_row = Rows - 1;
3229 windgoto(msg_row, msg_col);
3230 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3231 redrawcmd_preedit();
3232 #endif
3233 #ifdef MCH_CURSOR_SHAPE
3234 mch_update_cursor();
3235 #endif
3238 void
3239 gotocmdline(clr)
3240 int clr;
3242 msg_start();
3243 #ifdef FEAT_RIGHTLEFT
3244 if (cmdmsg_rl)
3245 msg_col = Columns - 1;
3246 else
3247 #endif
3248 msg_col = 0; /* always start in column 0 */
3249 if (clr) /* clear the bottom line(s) */
3250 msg_clr_eos(); /* will reset clear_cmdline */
3251 windgoto(cmdline_row, 0);
3255 * Check the word in front of the cursor for an abbreviation.
3256 * Called when the non-id character "c" has been entered.
3257 * When an abbreviation is recognized it is removed from the text with
3258 * backspaces and the replacement string is inserted, followed by "c".
3260 static int
3261 ccheck_abbr(c)
3262 int c;
3264 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3265 return FALSE;
3267 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3271 * Return FAIL if this is not an appropriate context in which to do
3272 * completion of anything, return OK if it is (even if there are no matches).
3273 * For the caller, this means that the character is just passed through like a
3274 * normal character (instead of being expanded). This allows :s/^I^D etc.
3276 static int
3277 nextwild(xp, type, options)
3278 expand_T *xp;
3279 int type;
3280 int options; /* extra options for ExpandOne() */
3282 int i, j;
3283 char_u *p1;
3284 char_u *p2;
3285 int difflen;
3286 int v;
3288 if (xp->xp_numfiles == -1)
3290 set_expand_context(xp);
3291 cmd_showtail = expand_showtail(xp);
3294 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3296 beep_flush();
3297 return OK; /* Something illegal on command line */
3299 if (xp->xp_context == EXPAND_NOTHING)
3301 /* Caller can use the character as a normal char instead */
3302 return FAIL;
3305 MSG_PUTS("..."); /* show that we are busy */
3306 out_flush();
3308 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3309 xp->xp_pattern_len = ccline.cmdpos - i;
3311 if (type == WILD_NEXT || type == WILD_PREV)
3314 * Get next/previous match for a previous expanded pattern.
3316 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3318 else
3321 * Translate string into pattern and expand it.
3323 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3324 xp->xp_context)) == NULL)
3325 p2 = NULL;
3326 else
3328 p2 = ExpandOne(xp, p1,
3329 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
3330 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3331 |options, type);
3332 vim_free(p1);
3333 /* longest match: make sure it is not shorter, happens with :help */
3334 if (p2 != NULL && type == WILD_LONGEST)
3336 for (j = 0; j < xp->xp_pattern_len; ++j)
3337 if (ccline.cmdbuff[i + j] == '*'
3338 || ccline.cmdbuff[i + j] == '?')
3339 break;
3340 if ((int)STRLEN(p2) < j)
3342 vim_free(p2);
3343 p2 = NULL;
3349 if (p2 != NULL && !got_int)
3351 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
3352 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3354 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3355 xp->xp_pattern = ccline.cmdbuff + i;
3357 else
3358 v = OK;
3359 if (v == OK)
3361 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3362 &ccline.cmdbuff[ccline.cmdpos],
3363 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3364 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
3365 ccline.cmdlen += difflen;
3366 ccline.cmdpos += difflen;
3369 vim_free(p2);
3371 redrawcmd();
3372 cursorcmd();
3374 /* When expanding a ":map" command and no matches are found, assume that
3375 * the key is supposed to be inserted literally */
3376 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3377 return FAIL;
3379 if (xp->xp_numfiles <= 0 && p2 == NULL)
3380 beep_flush();
3381 else if (xp->xp_numfiles == 1)
3382 /* free expanded pattern */
3383 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3385 return OK;
3389 * Do wildcard expansion on the string 'str'.
3390 * Chars that should not be expanded must be preceded with a backslash.
3391 * Return a pointer to allocated memory containing the new string.
3392 * Return NULL for failure.
3394 * "orig" is the originally expanded string, copied to allocated memory. It
3395 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3396 * WILD_PREV "orig" should be NULL.
3398 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3399 * is WILD_EXPAND_FREE or WILD_ALL.
3401 * mode = WILD_FREE: just free previously expanded matches
3402 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3403 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3404 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3405 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3406 * mode = WILD_ALL: return all matches concatenated
3407 * mode = WILD_LONGEST: return longest matched part
3409 * options = WILD_LIST_NOTFOUND: list entries without a match
3410 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3411 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3412 * options = WILD_NO_BEEP: Don't beep for multiple matches
3413 * options = WILD_ADD_SLASH: add a slash after directory names
3414 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3415 * options = WILD_SILENT: don't print warning messages
3416 * options = WILD_ESCAPE: put backslash before special chars
3418 * The variables xp->xp_context and xp->xp_backslash must have been set!
3420 char_u *
3421 ExpandOne(xp, str, orig, options, mode)
3422 expand_T *xp;
3423 char_u *str;
3424 char_u *orig; /* allocated copy of original of expanded string */
3425 int options;
3426 int mode;
3428 char_u *ss = NULL;
3429 static int findex;
3430 static char_u *orig_save = NULL; /* kept value of orig */
3431 int orig_saved = FALSE;
3432 int i;
3433 long_u len;
3434 int non_suf_match; /* number without matching suffix */
3437 * first handle the case of using an old match
3439 if (mode == WILD_NEXT || mode == WILD_PREV)
3441 if (xp->xp_numfiles > 0)
3443 if (mode == WILD_PREV)
3445 if (findex == -1)
3446 findex = xp->xp_numfiles;
3447 --findex;
3449 else /* mode == WILD_NEXT */
3450 ++findex;
3453 * When wrapping around, return the original string, set findex to
3454 * -1.
3456 if (findex < 0)
3458 if (orig_save == NULL)
3459 findex = xp->xp_numfiles - 1;
3460 else
3461 findex = -1;
3463 if (findex >= xp->xp_numfiles)
3465 if (orig_save == NULL)
3466 findex = 0;
3467 else
3468 findex = -1;
3470 #ifdef FEAT_WILDMENU
3471 if (p_wmnu)
3472 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3473 findex, cmd_showtail);
3474 #endif
3475 if (findex == -1)
3476 return vim_strsave(orig_save);
3477 return vim_strsave(xp->xp_files[findex]);
3479 else
3480 return NULL;
3483 /* free old names */
3484 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3486 FreeWild(xp->xp_numfiles, xp->xp_files);
3487 xp->xp_numfiles = -1;
3488 vim_free(orig_save);
3489 orig_save = NULL;
3491 findex = 0;
3493 if (mode == WILD_FREE) /* only release file name */
3494 return NULL;
3496 if (xp->xp_numfiles == -1)
3498 vim_free(orig_save);
3499 orig_save = orig;
3500 orig_saved = TRUE;
3503 * Do the expansion.
3505 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3506 options) == FAIL)
3508 #ifdef FNAME_ILLEGAL
3509 /* Illegal file name has been silently skipped. But when there
3510 * are wildcards, the real problem is that there was no match,
3511 * causing the pattern to be added, which has illegal characters.
3513 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3514 EMSG2(_(e_nomatch2), str);
3515 #endif
3517 else if (xp->xp_numfiles == 0)
3519 if (!(options & WILD_SILENT))
3520 EMSG2(_(e_nomatch2), str);
3522 else
3524 /* Escape the matches for use on the command line. */
3525 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3528 * Check for matching suffixes in file names.
3530 if (mode != WILD_ALL && mode != WILD_LONGEST)
3532 if (xp->xp_numfiles)
3533 non_suf_match = xp->xp_numfiles;
3534 else
3535 non_suf_match = 1;
3536 if ((xp->xp_context == EXPAND_FILES
3537 || xp->xp_context == EXPAND_DIRECTORIES)
3538 && xp->xp_numfiles > 1)
3541 * More than one match; check suffix.
3542 * The files will have been sorted on matching suffix in
3543 * expand_wildcards, only need to check the first two.
3545 non_suf_match = 0;
3546 for (i = 0; i < 2; ++i)
3547 if (match_suffix(xp->xp_files[i]))
3548 ++non_suf_match;
3550 if (non_suf_match != 1)
3552 /* Can we ever get here unless it's while expanding
3553 * interactively? If not, we can get rid of this all
3554 * together. Don't really want to wait for this message
3555 * (and possibly have to hit return to continue!).
3557 if (!(options & WILD_SILENT))
3558 EMSG(_(e_toomany));
3559 else if (!(options & WILD_NO_BEEP))
3560 beep_flush();
3562 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3563 ss = vim_strsave(xp->xp_files[0]);
3568 /* Find longest common part */
3569 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3571 for (len = 0; xp->xp_files[0][len]; ++len)
3573 for (i = 0; i < xp->xp_numfiles; ++i)
3575 #ifdef CASE_INSENSITIVE_FILENAME
3576 if (xp->xp_context == EXPAND_DIRECTORIES
3577 || xp->xp_context == EXPAND_FILES
3578 || xp->xp_context == EXPAND_SHELLCMD
3579 || xp->xp_context == EXPAND_BUFFERS)
3581 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3582 TOLOWER_LOC(xp->xp_files[0][len]))
3583 break;
3585 else
3586 #endif
3587 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3588 break;
3590 if (i < xp->xp_numfiles)
3592 if (!(options & WILD_NO_BEEP))
3593 vim_beep();
3594 break;
3597 ss = alloc((unsigned)len + 1);
3598 if (ss)
3599 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
3600 findex = -1; /* next p_wc gets first one */
3603 /* Concatenate all matching names */
3604 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3606 len = 0;
3607 for (i = 0; i < xp->xp_numfiles; ++i)
3608 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3609 ss = lalloc(len, TRUE);
3610 if (ss != NULL)
3612 *ss = NUL;
3613 for (i = 0; i < xp->xp_numfiles; ++i)
3615 STRCAT(ss, xp->xp_files[i]);
3616 if (i != xp->xp_numfiles - 1)
3617 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3622 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3623 ExpandCleanup(xp);
3625 /* Free "orig" if it wasn't stored in "orig_save". */
3626 if (!orig_saved)
3627 vim_free(orig);
3629 return ss;
3633 * Prepare an expand structure for use.
3635 void
3636 ExpandInit(xp)
3637 expand_T *xp;
3639 xp->xp_pattern = NULL;
3640 xp->xp_pattern_len = 0;
3641 xp->xp_backslash = XP_BS_NONE;
3642 #ifndef BACKSLASH_IN_FILENAME
3643 xp->xp_shell = FALSE;
3644 #endif
3645 xp->xp_numfiles = -1;
3646 xp->xp_files = NULL;
3647 #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3648 xp->xp_arg = NULL;
3649 #endif
3653 * Cleanup an expand structure after use.
3655 void
3656 ExpandCleanup(xp)
3657 expand_T *xp;
3659 if (xp->xp_numfiles >= 0)
3661 FreeWild(xp->xp_numfiles, xp->xp_files);
3662 xp->xp_numfiles = -1;
3666 void
3667 ExpandEscape(xp, str, numfiles, files, options)
3668 expand_T *xp;
3669 char_u *str;
3670 int numfiles;
3671 char_u **files;
3672 int options;
3674 int i;
3675 char_u *p;
3678 * May change home directory back to "~"
3680 if (options & WILD_HOME_REPLACE)
3681 tilde_replace(str, numfiles, files);
3683 if (options & WILD_ESCAPE)
3685 if (xp->xp_context == EXPAND_FILES
3686 || xp->xp_context == EXPAND_SHELLCMD
3687 || xp->xp_context == EXPAND_BUFFERS
3688 || xp->xp_context == EXPAND_DIRECTORIES)
3691 * Insert a backslash into a file name before a space, \, %, #
3692 * and wildmatch characters, except '~'.
3694 for (i = 0; i < numfiles; ++i)
3696 /* for ":set path=" we need to escape spaces twice */
3697 if (xp->xp_backslash == XP_BS_THREE)
3699 p = vim_strsave_escaped(files[i], (char_u *)" ");
3700 if (p != NULL)
3702 vim_free(files[i]);
3703 files[i] = p;
3704 #if defined(BACKSLASH_IN_FILENAME)
3705 p = vim_strsave_escaped(files[i], (char_u *)" ");
3706 if (p != NULL)
3708 vim_free(files[i]);
3709 files[i] = p;
3711 #endif
3714 #ifdef BACKSLASH_IN_FILENAME
3715 p = vim_strsave_fnameescape(files[i], FALSE);
3716 #else
3717 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
3718 #endif
3719 if (p != NULL)
3721 vim_free(files[i]);
3722 files[i] = p;
3725 /* If 'str' starts with "\~", replace "~" at start of
3726 * files[i] with "\~". */
3727 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
3728 escape_fname(&files[i]);
3730 xp->xp_backslash = XP_BS_NONE;
3732 /* If the first file starts with a '+' escape it. Otherwise it
3733 * could be seen as "+cmd". */
3734 if (*files[0] == '+')
3735 escape_fname(&files[0]);
3737 else if (xp->xp_context == EXPAND_TAGS)
3740 * Insert a backslash before characters in a tag name that
3741 * would terminate the ":tag" command.
3743 for (i = 0; i < numfiles; ++i)
3745 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3746 if (p != NULL)
3748 vim_free(files[i]);
3749 files[i] = p;
3757 * Escape special characters in "fname" for when used as a file name argument
3758 * after a Vim command, or, when "shell" is non-zero, a shell command.
3759 * Returns the result in allocated memory.
3761 char_u *
3762 vim_strsave_fnameescape(fname, shell)
3763 char_u *fname;
3764 int shell;
3766 char_u *p;
3767 #ifdef BACKSLASH_IN_FILENAME
3768 char_u buf[20];
3769 int j = 0;
3771 /* Don't escape '[' and '{' if they are in 'isfname'. */
3772 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3773 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3774 buf[j++] = *p;
3775 buf[j] = NUL;
3776 p = vim_strsave_escaped(fname, buf);
3777 #else
3778 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3779 if (shell && csh_like_shell() && p != NULL)
3781 char_u *s;
3783 /* For csh and similar shells need to put two backslashes before '!'.
3784 * One is taken by Vim, one by the shell. */
3785 s = vim_strsave_escaped(p, (char_u *)"!");
3786 vim_free(p);
3787 p = s;
3789 #endif
3791 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3792 * ":write". "cd -" has a special meaning. */
3793 if (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))
3794 escape_fname(&p);
3796 return p;
3800 * Put a backslash before the file name in "pp", which is in allocated memory.
3802 static void
3803 escape_fname(pp)
3804 char_u **pp;
3806 char_u *p;
3808 p = alloc((unsigned)(STRLEN(*pp) + 2));
3809 if (p != NULL)
3811 p[0] = '\\';
3812 STRCPY(p + 1, *pp);
3813 vim_free(*pp);
3814 *pp = p;
3819 * For each file name in files[num_files]:
3820 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3822 void
3823 tilde_replace(orig_pat, num_files, files)
3824 char_u *orig_pat;
3825 int num_files;
3826 char_u **files;
3828 int i;
3829 char_u *p;
3831 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3833 for (i = 0; i < num_files; ++i)
3835 p = home_replace_save(NULL, files[i]);
3836 if (p != NULL)
3838 vim_free(files[i]);
3839 files[i] = p;
3846 * Show all matches for completion on the command line.
3847 * Returns EXPAND_NOTHING when the character that triggered expansion should
3848 * be inserted like a normal character.
3850 static int
3851 showmatches(xp, wildmenu)
3852 expand_T *xp;
3853 int wildmenu UNUSED;
3855 #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3856 int num_files;
3857 char_u **files_found;
3858 int i, j, k;
3859 int maxlen;
3860 int lines;
3861 int columns;
3862 char_u *p;
3863 int lastlen;
3864 int attr;
3865 int showtail;
3867 if (xp->xp_numfiles == -1)
3869 set_expand_context(xp);
3870 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3871 &num_files, &files_found);
3872 showtail = expand_showtail(xp);
3873 if (i != EXPAND_OK)
3874 return i;
3877 else
3879 num_files = xp->xp_numfiles;
3880 files_found = xp->xp_files;
3881 showtail = cmd_showtail;
3884 #ifdef FEAT_WILDMENU
3885 if (!wildmenu)
3887 #endif
3888 msg_didany = FALSE; /* lines_left will be set */
3889 msg_start(); /* prepare for paging */
3890 msg_putchar('\n');
3891 out_flush();
3892 cmdline_row = msg_row;
3893 msg_didany = FALSE; /* lines_left will be set again */
3894 msg_start(); /* prepare for paging */
3895 #ifdef FEAT_WILDMENU
3897 #endif
3899 if (got_int)
3900 got_int = FALSE; /* only int. the completion, not the cmd line */
3901 #ifdef FEAT_WILDMENU
3902 else if (wildmenu)
3903 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3904 #endif
3905 else
3907 /* find the length of the longest file name */
3908 maxlen = 0;
3909 for (i = 0; i < num_files; ++i)
3911 if (!showtail && (xp->xp_context == EXPAND_FILES
3912 || xp->xp_context == EXPAND_SHELLCMD
3913 || xp->xp_context == EXPAND_BUFFERS))
3915 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3916 j = vim_strsize(NameBuff);
3918 else
3919 j = vim_strsize(L_SHOWFILE(i));
3920 if (j > maxlen)
3921 maxlen = j;
3924 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3925 lines = num_files;
3926 else
3928 /* compute the number of columns and lines for the listing */
3929 maxlen += 2; /* two spaces between file names */
3930 columns = ((int)Columns + 2) / maxlen;
3931 if (columns < 1)
3932 columns = 1;
3933 lines = (num_files + columns - 1) / columns;
3936 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3938 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3940 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3941 msg_clr_eos();
3942 msg_advance(maxlen - 3);
3943 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3946 /* list the files line by line */
3947 for (i = 0; i < lines; ++i)
3949 lastlen = 999;
3950 for (k = i; k < num_files; k += lines)
3952 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3954 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3955 p = files_found[k] + STRLEN(files_found[k]) + 1;
3956 msg_advance(maxlen + 1);
3957 msg_puts(p);
3958 msg_advance(maxlen + 3);
3959 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3960 break;
3962 for (j = maxlen - lastlen; --j >= 0; )
3963 msg_putchar(' ');
3964 if (xp->xp_context == EXPAND_FILES
3965 || xp->xp_context == EXPAND_SHELLCMD
3966 || xp->xp_context == EXPAND_BUFFERS)
3968 /* highlight directories */
3969 if (xp->xp_numfiles != -1)
3971 char_u *halved_slash;
3972 char_u *exp_path;
3974 /* Expansion was done before and special characters
3975 * were escaped, need to halve backslashes. Also
3976 * $HOME has been replaced with ~/. */
3977 exp_path = expand_env_save_opt(files_found[k], TRUE);
3978 halved_slash = backslash_halve_save(
3979 exp_path != NULL ? exp_path : files_found[k]);
3980 j = mch_isdir(halved_slash != NULL ? halved_slash
3981 : files_found[k]);
3982 vim_free(exp_path);
3983 vim_free(halved_slash);
3985 else
3986 /* Expansion was done here, file names are literal. */
3987 j = mch_isdir(files_found[k]);
3988 if (showtail)
3989 p = L_SHOWFILE(k);
3990 else
3992 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3993 TRUE);
3994 p = NameBuff;
3997 else
3999 j = FALSE;
4000 p = L_SHOWFILE(k);
4002 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4004 if (msg_col > 0) /* when not wrapped around */
4006 msg_clr_eos();
4007 msg_putchar('\n');
4009 out_flush(); /* show one line at a time */
4010 if (got_int)
4012 got_int = FALSE;
4013 break;
4018 * we redraw the command below the lines that we have just listed
4019 * This is a bit tricky, but it saves a lot of screen updating.
4021 cmdline_row = msg_row; /* will put it back later */
4024 if (xp->xp_numfiles == -1)
4025 FreeWild(num_files, files_found);
4027 return EXPAND_OK;
4031 * Private gettail for showmatches() (and win_redr_status_matches()):
4032 * Find tail of file name path, but ignore trailing "/".
4034 char_u *
4035 sm_gettail(s)
4036 char_u *s;
4038 char_u *p;
4039 char_u *t = s;
4040 int had_sep = FALSE;
4042 for (p = s; *p != NUL; )
4044 if (vim_ispathsep(*p)
4045 #ifdef BACKSLASH_IN_FILENAME
4046 && !rem_backslash(p)
4047 #endif
4049 had_sep = TRUE;
4050 else if (had_sep)
4052 t = p;
4053 had_sep = FALSE;
4055 mb_ptr_adv(p);
4057 return t;
4061 * Return TRUE if we only need to show the tail of completion matches.
4062 * When not completing file names or there is a wildcard in the path FALSE is
4063 * returned.
4065 static int
4066 expand_showtail(xp)
4067 expand_T *xp;
4069 char_u *s;
4070 char_u *end;
4072 /* When not completing file names a "/" may mean something different. */
4073 if (xp->xp_context != EXPAND_FILES
4074 && xp->xp_context != EXPAND_SHELLCMD
4075 && xp->xp_context != EXPAND_DIRECTORIES)
4076 return FALSE;
4078 end = gettail(xp->xp_pattern);
4079 if (end == xp->xp_pattern) /* there is no path separator */
4080 return FALSE;
4082 for (s = xp->xp_pattern; s < end; s++)
4084 /* Skip escaped wildcards. Only when the backslash is not a path
4085 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4086 if (rem_backslash(s))
4087 ++s;
4088 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4089 return FALSE;
4091 return TRUE;
4095 * Prepare a string for expansion.
4096 * When expanding file names: The string will be used with expand_wildcards().
4097 * Copy the file name into allocated memory and add a '*' at the end.
4098 * When expanding other names: The string will be used with regcomp(). Copy
4099 * the name into allocated memory and prepend "^".
4101 char_u *
4102 addstar(fname, len, context)
4103 char_u *fname;
4104 int len;
4105 int context; /* EXPAND_FILES etc. */
4107 char_u *retval;
4108 int i, j;
4109 int new_len;
4110 char_u *tail;
4112 if (context != EXPAND_FILES
4113 && context != EXPAND_SHELLCMD
4114 && context != EXPAND_DIRECTORIES)
4117 * Matching will be done internally (on something other than files).
4118 * So we convert the file-matching-type wildcards into our kind for
4119 * use with vim_regcomp(). First work out how long it will be:
4122 /* For help tags the translation is done in find_help_tags().
4123 * For a tag pattern starting with "/" no translation is needed. */
4124 if (context == EXPAND_HELP
4125 || context == EXPAND_COLORS
4126 || context == EXPAND_COMPILER
4127 || (context == EXPAND_TAGS && fname[0] == '/'))
4128 retval = vim_strnsave(fname, len);
4129 else
4131 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4132 for (i = 0; i < len; i++)
4134 if (fname[i] == '*' || fname[i] == '~')
4135 new_len++; /* '*' needs to be replaced by ".*"
4136 '~' needs to be replaced by "\~" */
4138 /* Buffer names are like file names. "." should be literal */
4139 if (context == EXPAND_BUFFERS && fname[i] == '.')
4140 new_len++; /* "." becomes "\." */
4142 /* Custom expansion takes care of special things, match
4143 * backslashes literally (perhaps also for other types?) */
4144 if ((context == EXPAND_USER_DEFINED
4145 || context == EXPAND_USER_LIST) && fname[i] == '\\')
4146 new_len++; /* '\' becomes "\\" */
4148 retval = alloc(new_len);
4149 if (retval != NULL)
4151 retval[0] = '^';
4152 j = 1;
4153 for (i = 0; i < len; i++, j++)
4155 /* Skip backslash. But why? At least keep it for custom
4156 * expansion. */
4157 if (context != EXPAND_USER_DEFINED
4158 && context != EXPAND_USER_LIST
4159 && fname[i] == '\\'
4160 && ++i == len)
4161 break;
4163 switch (fname[i])
4165 case '*': retval[j++] = '.';
4166 break;
4167 case '~': retval[j++] = '\\';
4168 break;
4169 case '?': retval[j] = '.';
4170 continue;
4171 case '.': if (context == EXPAND_BUFFERS)
4172 retval[j++] = '\\';
4173 break;
4174 case '\\': if (context == EXPAND_USER_DEFINED
4175 || context == EXPAND_USER_LIST)
4176 retval[j++] = '\\';
4177 break;
4179 retval[j] = fname[i];
4181 retval[j] = NUL;
4185 else
4187 retval = alloc(len + 4);
4188 if (retval != NULL)
4190 vim_strncpy(retval, fname, len);
4193 * Don't add a star to *, ~, ~user, $var or `cmd`.
4194 * * would become **, which walks the whole tree.
4195 * ~ would be at the start of the file name, but not the tail.
4196 * $ could be anywhere in the tail.
4197 * ` could be anywhere in the file name.
4198 * When the name ends in '$' don't add a star, remove the '$'.
4200 tail = gettail(retval);
4201 if ((*retval != '~' || tail != retval)
4202 && (len == 0 || retval[len - 1] != '*')
4203 && vim_strchr(tail, '$') == NULL
4204 && vim_strchr(retval, '`') == NULL)
4205 retval[len++] = '*';
4206 else if (len > 0 && retval[len - 1] == '$')
4207 --len;
4208 retval[len] = NUL;
4211 return retval;
4215 * Must parse the command line so far to work out what context we are in.
4216 * Completion can then be done based on that context.
4217 * This routine sets the variables:
4218 * xp->xp_pattern The start of the pattern to be expanded within
4219 * the command line (ends at the cursor).
4220 * xp->xp_context The type of thing to expand. Will be one of:
4222 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4223 * the command line, like an unknown command. Caller
4224 * should beep.
4225 * EXPAND_NOTHING Unrecognised context for completion, use char like
4226 * a normal char, rather than for completion. eg
4227 * :s/^I/
4228 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4229 * it.
4230 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4231 * EXPAND_FILES After command with XFILE set, or after setting
4232 * with P_EXPAND set. eg :e ^I, :w>>^I
4233 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4234 * when we know only directories are of interest. eg
4235 * :set dir=^I
4236 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
4237 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4238 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4239 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4240 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4241 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4242 * EXPAND_EVENTS Complete event names
4243 * EXPAND_SYNTAX Complete :syntax command arguments
4244 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4245 * EXPAND_AUGROUP Complete autocommand group names
4246 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4247 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4248 * eg :unmap a^I , :cunab x^I
4249 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4250 * eg :call sub^I
4251 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4252 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4253 * names in expressions, eg :while s^I
4254 * EXPAND_ENV_VARS Complete environment variable names
4256 static void
4257 set_expand_context(xp)
4258 expand_T *xp;
4260 /* only expansion for ':', '>' and '=' command-lines */
4261 if (ccline.cmdfirstc != ':'
4262 #ifdef FEAT_EVAL
4263 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
4264 && !ccline.input_fn
4265 #endif
4268 xp->xp_context = EXPAND_NOTHING;
4269 return;
4271 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4274 void
4275 set_cmd_context(xp, str, len, col)
4276 expand_T *xp;
4277 char_u *str; /* start of command line */
4278 int len; /* length of command line (excl. NUL) */
4279 int col; /* position of cursor */
4281 int old_char = NUL;
4282 char_u *nextcomm;
4285 * Avoid a UMR warning from Purify, only save the character if it has been
4286 * written before.
4288 if (col < len)
4289 old_char = str[col];
4290 str[col] = NUL;
4291 nextcomm = str;
4293 #ifdef FEAT_EVAL
4294 if (ccline.cmdfirstc == '=')
4296 # ifdef FEAT_CMDL_COMPL
4297 /* pass CMD_SIZE because there is no real command */
4298 set_context_for_expression(xp, str, CMD_SIZE);
4299 # endif
4301 else if (ccline.input_fn)
4303 xp->xp_context = ccline.xp_context;
4304 xp->xp_pattern = ccline.cmdbuff;
4305 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
4306 xp->xp_arg = ccline.xp_arg;
4307 # endif
4309 else
4310 #endif
4311 while (nextcomm != NULL)
4312 nextcomm = set_one_cmd_context(xp, nextcomm);
4314 str[col] = old_char;
4318 * Expand the command line "str" from context "xp".
4319 * "xp" must have been set by set_cmd_context().
4320 * xp->xp_pattern points into "str", to where the text that is to be expanded
4321 * starts.
4322 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4323 * cursor.
4324 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4325 * key that triggered expansion literally.
4326 * Returns EXPAND_OK otherwise.
4329 expand_cmdline(xp, str, col, matchcount, matches)
4330 expand_T *xp;
4331 char_u *str; /* start of command line */
4332 int col; /* position of cursor */
4333 int *matchcount; /* return: nr of matches */
4334 char_u ***matches; /* return: array of pointers to matches */
4336 char_u *file_str = NULL;
4338 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4340 beep_flush();
4341 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4343 if (xp->xp_context == EXPAND_NOTHING)
4345 /* Caller can use the character as a normal char instead */
4346 return EXPAND_NOTHING;
4349 /* add star to file name, or convert to regexp if not exp. files. */
4350 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4351 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
4352 if (file_str == NULL)
4353 return EXPAND_UNSUCCESSFUL;
4355 /* find all files that match the description */
4356 if (ExpandFromContext(xp, file_str, matchcount, matches,
4357 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4359 *matchcount = 0;
4360 *matches = NULL;
4362 vim_free(file_str);
4364 return EXPAND_OK;
4367 #ifdef FEAT_MULTI_LANG
4369 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4371 static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4373 static void
4374 cleanup_help_tags(num_file, file)
4375 int num_file;
4376 char_u **file;
4378 int i, j;
4379 int len;
4381 for (i = 0; i < num_file; ++i)
4383 len = (int)STRLEN(file[i]) - 3;
4384 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4386 /* Sorting on priority means the same item in another language may
4387 * be anywhere. Search all items for a match up to the "@en". */
4388 for (j = 0; j < num_file; ++j)
4389 if (j != i
4390 && (int)STRLEN(file[j]) == len + 3
4391 && STRNCMP(file[i], file[j], len + 1) == 0)
4392 break;
4393 if (j == num_file)
4394 file[i][len] = NUL;
4398 #endif
4401 * Do the expansion based on xp->xp_context and "pat".
4403 static int
4404 ExpandFromContext(xp, pat, num_file, file, options)
4405 expand_T *xp;
4406 char_u *pat;
4407 int *num_file;
4408 char_u ***file;
4409 int options;
4411 #ifdef FEAT_CMDL_COMPL
4412 regmatch_T regmatch;
4413 #endif
4414 int ret;
4415 int flags;
4417 flags = EW_DIR; /* include directories */
4418 if (options & WILD_LIST_NOTFOUND)
4419 flags |= EW_NOTFOUND;
4420 if (options & WILD_ADD_SLASH)
4421 flags |= EW_ADDSLASH;
4422 if (options & WILD_KEEP_ALL)
4423 flags |= EW_KEEPALL;
4424 if (options & WILD_SILENT)
4425 flags |= EW_SILENT;
4427 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4430 * Expand file or directory names.
4432 int free_pat = FALSE;
4433 int i;
4435 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4436 * space */
4437 if (xp->xp_backslash != XP_BS_NONE)
4439 free_pat = TRUE;
4440 pat = vim_strsave(pat);
4441 for (i = 0; pat[i]; ++i)
4442 if (pat[i] == '\\')
4444 if (xp->xp_backslash == XP_BS_THREE
4445 && pat[i + 1] == '\\'
4446 && pat[i + 2] == '\\'
4447 && pat[i + 3] == ' ')
4448 STRMOVE(pat + i, pat + i + 3);
4449 if (xp->xp_backslash == XP_BS_ONE
4450 && pat[i + 1] == ' ')
4451 STRMOVE(pat + i, pat + i + 1);
4455 if (xp->xp_context == EXPAND_FILES)
4456 flags |= EW_FILE;
4457 else
4458 flags = (flags | EW_DIR) & ~EW_FILE;
4459 /* Expand wildcards, supporting %:h and the like. */
4460 ret = expand_wildcards_eval(&pat, num_file, file, flags);
4461 if (free_pat)
4462 vim_free(pat);
4463 return ret;
4466 *file = (char_u **)"";
4467 *num_file = 0;
4468 if (xp->xp_context == EXPAND_HELP)
4470 /* With an empty argument we would get all the help tags, which is
4471 * very slow. Get matches for "help" instead. */
4472 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4473 num_file, file, FALSE) == OK)
4475 #ifdef FEAT_MULTI_LANG
4476 cleanup_help_tags(*num_file, *file);
4477 #endif
4478 return OK;
4480 return FAIL;
4483 #ifndef FEAT_CMDL_COMPL
4484 return FAIL;
4485 #else
4486 if (xp->xp_context == EXPAND_SHELLCMD)
4487 return expand_shellcmd(pat, num_file, file, flags);
4488 if (xp->xp_context == EXPAND_OLD_SETTING)
4489 return ExpandOldSetting(num_file, file);
4490 if (xp->xp_context == EXPAND_BUFFERS)
4491 return ExpandBufnames(pat, num_file, file, options);
4492 if (xp->xp_context == EXPAND_TAGS
4493 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4494 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4495 if (xp->xp_context == EXPAND_COLORS)
4496 return ExpandRTDir(pat, num_file, file, "colors");
4497 if (xp->xp_context == EXPAND_COMPILER)
4498 return ExpandRTDir(pat, num_file, file, "compiler");
4499 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4500 if (xp->xp_context == EXPAND_USER_LIST)
4501 return ExpandUserList(xp, num_file, file);
4502 # endif
4504 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4505 if (regmatch.regprog == NULL)
4506 return FAIL;
4508 /* set ignore-case according to p_ic, p_scs and pat */
4509 regmatch.rm_ic = ignorecase(pat);
4511 if (xp->xp_context == EXPAND_SETTINGS
4512 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4513 ret = ExpandSettings(xp, &regmatch, num_file, file);
4514 else if (xp->xp_context == EXPAND_MAPPINGS)
4515 ret = ExpandMappings(&regmatch, num_file, file);
4516 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4517 else if (xp->xp_context == EXPAND_USER_DEFINED)
4518 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4519 # endif
4520 else
4522 static struct expgen
4524 int context;
4525 char_u *((*func)__ARGS((expand_T *, int)));
4526 int ic;
4527 } tab[] =
4529 {EXPAND_COMMANDS, get_command_name, FALSE},
4530 {EXPAND_BEHAVE, get_behave_arg, TRUE},
4531 #ifdef FEAT_USR_CMDS
4532 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4533 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4534 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4535 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4536 #endif
4537 #ifdef FEAT_EVAL
4538 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4539 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4540 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4541 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4542 #endif
4543 #ifdef FEAT_MENU
4544 {EXPAND_MENUS, get_menu_name, FALSE},
4545 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4546 #endif
4547 #ifdef FEAT_SYN_HL
4548 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4549 #endif
4550 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4551 #ifdef FEAT_AUTOCMD
4552 {EXPAND_EVENTS, get_event_name, TRUE},
4553 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4554 #endif
4555 #ifdef FEAT_CSCOPE
4556 {EXPAND_CSCOPE, get_cscope_name, TRUE},
4557 #endif
4558 #ifdef FEAT_SIGNS
4559 {EXPAND_SIGN, get_sign_name, TRUE},
4560 #endif
4561 #ifdef FEAT_PROFILE
4562 {EXPAND_PROFILE, get_profile_name, TRUE},
4563 #endif
4564 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4565 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4566 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4567 #endif
4568 {EXPAND_ENV_VARS, get_env_name, TRUE},
4570 int i;
4573 * Find a context in the table and call the ExpandGeneric() with the
4574 * right function to do the expansion.
4576 ret = FAIL;
4577 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
4578 if (xp->xp_context == tab[i].context)
4580 if (tab[i].ic)
4581 regmatch.rm_ic = TRUE;
4582 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4583 break;
4587 vim_free(regmatch.regprog);
4589 return ret;
4590 #endif /* FEAT_CMDL_COMPL */
4593 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4595 * Expand a list of names.
4597 * Generic function for command line completion. It calls a function to
4598 * obtain strings, one by one. The strings are matched against a regexp
4599 * program. Matching strings are copied into an array, which is returned.
4601 * Returns OK when no problems encountered, FAIL for error (out of memory).
4604 ExpandGeneric(xp, regmatch, num_file, file, func)
4605 expand_T *xp;
4606 regmatch_T *regmatch;
4607 int *num_file;
4608 char_u ***file;
4609 char_u *((*func)__ARGS((expand_T *, int)));
4610 /* returns a string from the list */
4612 int i;
4613 int count = 0;
4614 int round;
4615 char_u *str;
4617 /* do this loop twice:
4618 * round == 0: count the number of matching names
4619 * round == 1: copy the matching names into allocated memory
4621 for (round = 0; round <= 1; ++round)
4623 for (i = 0; ; ++i)
4625 str = (*func)(xp, i);
4626 if (str == NULL) /* end of list */
4627 break;
4628 if (*str == NUL) /* skip empty strings */
4629 continue;
4631 if (vim_regexec(regmatch, str, (colnr_T)0))
4633 if (round)
4635 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4636 (*file)[count] = str;
4637 #ifdef FEAT_MENU
4638 if (func == get_menu_names && str != NULL)
4640 /* test for separator added by get_menu_names() */
4641 str += STRLEN(str) - 1;
4642 if (*str == '\001')
4643 *str = '.';
4645 #endif
4647 ++count;
4650 if (round == 0)
4652 if (count == 0)
4653 return OK;
4654 *num_file = count;
4655 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4656 if (*file == NULL)
4658 *file = (char_u **)"";
4659 return FAIL;
4661 count = 0;
4665 /* Sort the results. Keep menu's in the specified order. */
4666 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4667 sort_strings(*file, *num_file);
4669 #ifdef FEAT_CMDL_COMPL
4670 /* Reset the variables used for special highlight names expansion, so that
4671 * they don't show up when getting normal highlight names by ID. */
4672 reset_expand_highlight();
4673 #endif
4675 return OK;
4679 * Complete a shell command.
4680 * Returns FAIL or OK;
4682 static int
4683 expand_shellcmd(filepat, num_file, file, flagsarg)
4684 char_u *filepat; /* pattern to match with command names */
4685 int *num_file; /* return: number of matches */
4686 char_u ***file; /* return: array with matches */
4687 int flagsarg; /* EW_ flags */
4689 char_u *pat;
4690 int i;
4691 char_u *path;
4692 int mustfree = FALSE;
4693 garray_T ga;
4694 char_u *buf = alloc(MAXPATHL);
4695 size_t l;
4696 char_u *s, *e;
4697 int flags = flagsarg;
4698 int ret;
4700 if (buf == NULL)
4701 return FAIL;
4703 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4704 * space */
4705 pat = vim_strsave(filepat);
4706 for (i = 0; pat[i]; ++i)
4707 if (pat[i] == '\\' && pat[i + 1] == ' ')
4708 STRMOVE(pat + i, pat + i + 1);
4710 flags |= EW_FILE | EW_EXEC;
4712 /* For an absolute name we don't use $PATH. */
4713 if (mch_isFullName(pat))
4714 path = (char_u *)" ";
4715 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
4716 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4717 path = (char_u *)".";
4718 else
4719 path = vim_getenv((char_u *)"PATH", &mustfree);
4722 * Go over all directories in $PATH. Expand matches in that directory and
4723 * collect them in "ga".
4725 ga_init2(&ga, (int)sizeof(char *), 10);
4726 for (s = path; *s != NUL; s = e)
4728 if (*s == ' ')
4729 ++s; /* Skip space used for absolute path name. */
4731 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4732 e = vim_strchr(s, ';');
4733 #else
4734 e = vim_strchr(s, ':');
4735 #endif
4736 if (e == NULL)
4737 e = s + STRLEN(s);
4739 l = e - s;
4740 if (l > MAXPATHL - 5)
4741 break;
4742 vim_strncpy(buf, s, l);
4743 add_pathsep(buf);
4744 l = STRLEN(buf);
4745 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4747 /* Expand matches in one directory of $PATH. */
4748 ret = expand_wildcards(1, &buf, num_file, file, flags);
4749 if (ret == OK)
4751 if (ga_grow(&ga, *num_file) == FAIL)
4752 FreeWild(*num_file, *file);
4753 else
4755 for (i = 0; i < *num_file; ++i)
4757 s = (*file)[i];
4758 if (STRLEN(s) > l)
4760 /* Remove the path again. */
4761 STRMOVE(s, s + l);
4762 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4764 else
4765 vim_free(s);
4767 vim_free(*file);
4770 if (*e != NUL)
4771 ++e;
4773 *file = ga.ga_data;
4774 *num_file = ga.ga_len;
4776 vim_free(buf);
4777 vim_free(pat);
4778 if (mustfree)
4779 vim_free(path);
4780 return OK;
4784 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4785 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));
4788 * Call "user_expand_func()" to invoke a user defined VimL function and return
4789 * the result (either a string or a List).
4791 static void *
4792 call_user_expand_func(user_expand_func, xp, num_file, file)
4793 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
4794 expand_T *xp;
4795 int *num_file;
4796 char_u ***file;
4798 char_u keep;
4799 char_u num[50];
4800 char_u *args[3];
4801 int save_current_SID = current_SID;
4802 void *ret;
4803 struct cmdline_info save_ccline;
4805 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
4806 return NULL;
4807 *num_file = 0;
4808 *file = NULL;
4810 if (ccline.cmdbuff == NULL)
4812 /* Completion from Insert mode, pass fake arguments. */
4813 keep = 0;
4814 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
4815 args[1] = xp->xp_pattern;
4817 else
4819 /* Completion on the command line, pass real arguments. */
4820 keep = ccline.cmdbuff[ccline.cmdlen];
4821 ccline.cmdbuff[ccline.cmdlen] = 0;
4822 sprintf((char *)num, "%d", ccline.cmdpos);
4823 args[1] = ccline.cmdbuff;
4825 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
4826 args[2] = num;
4828 /* Save the cmdline, we don't know what the function may do. */
4829 save_ccline = ccline;
4830 ccline.cmdbuff = NULL;
4831 ccline.cmdprompt = NULL;
4832 current_SID = xp->xp_scriptID;
4834 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
4836 ccline = save_ccline;
4837 current_SID = save_current_SID;
4838 if (ccline.cmdbuff != NULL)
4839 ccline.cmdbuff[ccline.cmdlen] = keep;
4841 vim_free(args[0]);
4842 return ret;
4846 * Expand names with a function defined by the user.
4848 static int
4849 ExpandUserDefined(xp, regmatch, num_file, file)
4850 expand_T *xp;
4851 regmatch_T *regmatch;
4852 int *num_file;
4853 char_u ***file;
4855 char_u *retstr;
4856 char_u *s;
4857 char_u *e;
4858 char_u keep;
4859 garray_T ga;
4861 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4862 if (retstr == NULL)
4863 return FAIL;
4865 ga_init2(&ga, (int)sizeof(char *), 3);
4866 for (s = retstr; *s != NUL; s = e)
4868 e = vim_strchr(s, '\n');
4869 if (e == NULL)
4870 e = s + STRLEN(s);
4871 keep = *e;
4872 *e = 0;
4874 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4876 *e = keep;
4877 if (*e != NUL)
4878 ++e;
4879 continue;
4882 if (ga_grow(&ga, 1) == FAIL)
4883 break;
4885 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4886 ++ga.ga_len;
4888 *e = keep;
4889 if (*e != NUL)
4890 ++e;
4892 vim_free(retstr);
4893 *file = ga.ga_data;
4894 *num_file = ga.ga_len;
4895 return OK;
4899 * Expand names with a list returned by a function defined by the user.
4901 static int
4902 ExpandUserList(xp, num_file, file)
4903 expand_T *xp;
4904 int *num_file;
4905 char_u ***file;
4907 list_T *retlist;
4908 listitem_T *li;
4909 garray_T ga;
4911 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4912 if (retlist == NULL)
4913 return FAIL;
4915 ga_init2(&ga, (int)sizeof(char *), 3);
4916 /* Loop over the items in the list. */
4917 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4919 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
4920 continue; /* Skip non-string items and empty strings */
4922 if (ga_grow(&ga, 1) == FAIL)
4923 break;
4925 ((char_u **)ga.ga_data)[ga.ga_len] =
4926 vim_strsave(li->li_tv.vval.v_string);
4927 ++ga.ga_len;
4929 list_unref(retlist);
4931 *file = ga.ga_data;
4932 *num_file = ga.ga_len;
4933 return OK;
4935 #endif
4938 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4939 * or compiler names.
4941 static int
4942 ExpandRTDir(pat, num_file, file, dirname)
4943 char_u *pat;
4944 int *num_file;
4945 char_u ***file;
4946 char *dirname; /* "colors" or "compiler" */
4948 char_u *all;
4949 char_u *s;
4950 char_u *e;
4951 garray_T ga;
4953 *num_file = 0;
4954 *file = NULL;
4955 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4956 if (s == NULL)
4957 return FAIL;
4958 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4959 all = globpath(p_rtp, s, 0);
4960 vim_free(s);
4961 if (all == NULL)
4962 return FAIL;
4964 ga_init2(&ga, (int)sizeof(char *), 3);
4965 for (s = all; *s != NUL; s = e)
4967 e = vim_strchr(s, '\n');
4968 if (e == NULL)
4969 e = s + STRLEN(s);
4970 if (ga_grow(&ga, 1) == FAIL)
4971 break;
4972 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4974 for (s = e - 4; s > all; mb_ptr_back(all, s))
4975 if (*s == '\n' || vim_ispathsep(*s))
4976 break;
4977 ++s;
4978 ((char_u **)ga.ga_data)[ga.ga_len] =
4979 vim_strnsave(s, (int)(e - s - 4));
4980 ++ga.ga_len;
4982 if (*e != NUL)
4983 ++e;
4985 vim_free(all);
4986 *file = ga.ga_data;
4987 *num_file = ga.ga_len;
4988 return OK;
4991 #endif
4993 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4995 * Expand "file" for all comma-separated directories in "path".
4996 * Returns an allocated string with all matches concatenated, separated by
4997 * newlines. Returns NULL for an error or no matches.
4999 char_u *
5000 globpath(path, file, expand_options)
5001 char_u *path;
5002 char_u *file;
5003 int expand_options;
5005 expand_T xpc;
5006 char_u *buf;
5007 garray_T ga;
5008 int i;
5009 int len;
5010 int num_p;
5011 char_u **p;
5012 char_u *cur = NULL;
5014 buf = alloc(MAXPATHL);
5015 if (buf == NULL)
5016 return NULL;
5018 ExpandInit(&xpc);
5019 xpc.xp_context = EXPAND_FILES;
5021 ga_init2(&ga, 1, 100);
5023 /* Loop over all entries in {path}. */
5024 while (*path != NUL)
5026 /* Copy one item of the path to buf[] and concatenate the file name. */
5027 copy_option_part(&path, buf, MAXPATHL, ",");
5028 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5030 add_pathsep(buf);
5031 STRCAT(buf, file);
5032 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5033 WILD_SILENT|expand_options) != FAIL && num_p > 0)
5035 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
5036 for (len = 0, i = 0; i < num_p; ++i)
5037 len += (int)STRLEN(p[i]) + 1;
5039 /* Concatenate new results to previous ones. */
5040 if (ga_grow(&ga, len) == OK)
5042 cur = (char_u *)ga.ga_data + ga.ga_len;
5043 for (i = 0; i < num_p; ++i)
5045 STRCPY(cur, p[i]);
5046 cur += STRLEN(p[i]);
5047 *cur++ = '\n';
5049 ga.ga_len += len;
5051 FreeWild(num_p, p);
5055 if (cur != NULL)
5056 *--cur = 0; /* Replace trailing newline with NUL */
5058 vim_free(buf);
5059 return (char_u *)ga.ga_data;
5062 #endif
5064 #if defined(FEAT_CMDHIST) || defined(PROTO)
5066 /*********************************
5067 * Command line history stuff *
5068 *********************************/
5071 * Translate a history character to the associated type number.
5073 static int
5074 hist_char2type(c)
5075 int c;
5077 if (c == ':')
5078 return HIST_CMD;
5079 if (c == '=')
5080 return HIST_EXPR;
5081 if (c == '@')
5082 return HIST_INPUT;
5083 if (c == '>')
5084 return HIST_DEBUG;
5085 return HIST_SEARCH; /* must be '?' or '/' */
5089 * Table of history names.
5090 * These names are used in :history and various hist...() functions.
5091 * It is sufficient to give the significant prefix of a history name.
5094 static char *(history_names[]) =
5096 "cmd",
5097 "search",
5098 "expr",
5099 "input",
5100 "debug",
5101 NULL
5105 * init_history() - Initialize the command line history.
5106 * Also used to re-allocate the history when the size changes.
5108 void
5109 init_history()
5111 int newlen; /* new length of history table */
5112 histentry_T *temp;
5113 int i;
5114 int j;
5115 int type;
5118 * If size of history table changed, reallocate it
5120 newlen = (int)p_hi;
5121 if (newlen != hislen) /* history length changed */
5123 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5125 if (newlen)
5127 temp = (histentry_T *)lalloc(
5128 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5129 if (temp == NULL) /* out of memory! */
5131 if (type == 0) /* first one: just keep the old length */
5133 newlen = hislen;
5134 break;
5136 /* Already changed one table, now we can only have zero
5137 * length for all tables. */
5138 newlen = 0;
5139 type = -1;
5140 continue;
5143 else
5144 temp = NULL;
5145 if (newlen == 0 || temp != NULL)
5147 if (hisidx[type] < 0) /* there are no entries yet */
5149 for (i = 0; i < newlen; ++i)
5151 temp[i].hisnum = 0;
5152 temp[i].hisstr = NULL;
5155 else if (newlen > hislen) /* array becomes bigger */
5157 for (i = 0; i <= hisidx[type]; ++i)
5158 temp[i] = history[type][i];
5159 j = i;
5160 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5162 temp[i].hisnum = 0;
5163 temp[i].hisstr = NULL;
5165 for ( ; j < hislen; ++i, ++j)
5166 temp[i] = history[type][j];
5168 else /* array becomes smaller or 0 */
5170 j = hisidx[type];
5171 for (i = newlen - 1; ; --i)
5173 if (i >= 0) /* copy newest entries */
5174 temp[i] = history[type][j];
5175 else /* remove older entries */
5176 vim_free(history[type][j].hisstr);
5177 if (--j < 0)
5178 j = hislen - 1;
5179 if (j == hisidx[type])
5180 break;
5182 hisidx[type] = newlen - 1;
5184 vim_free(history[type]);
5185 history[type] = temp;
5188 hislen = newlen;
5193 * Check if command line 'str' is already in history.
5194 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5196 static int
5197 in_history(type, str, move_to_front)
5198 int type;
5199 char_u *str;
5200 int move_to_front; /* Move the entry to the front if it exists */
5202 int i;
5203 int last_i = -1;
5205 if (hisidx[type] < 0)
5206 return FALSE;
5207 i = hisidx[type];
5210 if (history[type][i].hisstr == NULL)
5211 return FALSE;
5212 if (STRCMP(str, history[type][i].hisstr) == 0)
5214 if (!move_to_front)
5215 return TRUE;
5216 last_i = i;
5217 break;
5219 if (--i < 0)
5220 i = hislen - 1;
5221 } while (i != hisidx[type]);
5223 if (last_i >= 0)
5225 str = history[type][i].hisstr;
5226 while (i != hisidx[type])
5228 if (++i >= hislen)
5229 i = 0;
5230 history[type][last_i] = history[type][i];
5231 last_i = i;
5233 history[type][i].hisstr = str;
5234 history[type][i].hisnum = ++hisnum[type];
5235 return TRUE;
5237 return FALSE;
5241 * Convert history name (from table above) to its HIST_ equivalent.
5242 * When "name" is empty, return "cmd" history.
5243 * Returns -1 for unknown history name.
5246 get_histtype(name)
5247 char_u *name;
5249 int i;
5250 int len = (int)STRLEN(name);
5252 /* No argument: use current history. */
5253 if (len == 0)
5254 return hist_char2type(ccline.cmdfirstc);
5256 for (i = 0; history_names[i] != NULL; ++i)
5257 if (STRNICMP(name, history_names[i], len) == 0)
5258 return i;
5260 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5261 return hist_char2type(name[0]);
5263 return -1;
5266 static int last_maptick = -1; /* last seen maptick */
5269 * Add the given string to the given history. If the string is already in the
5270 * history then it is moved to the front. "histype" may be one of he HIST_
5271 * values.
5273 void
5274 add_to_history(histype, new_entry, in_map, sep)
5275 int histype;
5276 char_u *new_entry;
5277 int in_map; /* consider maptick when inside a mapping */
5278 int sep; /* separator character used (search hist) */
5280 histentry_T *hisptr;
5281 int len;
5283 if (hislen == 0) /* no history */
5284 return;
5287 * Searches inside the same mapping overwrite each other, so that only
5288 * the last line is kept. Be careful not to remove a line that was moved
5289 * down, only lines that were added.
5291 if (histype == HIST_SEARCH && in_map)
5293 if (maptick == last_maptick)
5295 /* Current line is from the same mapping, remove it */
5296 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5297 vim_free(hisptr->hisstr);
5298 hisptr->hisstr = NULL;
5299 hisptr->hisnum = 0;
5300 --hisnum[histype];
5301 if (--hisidx[HIST_SEARCH] < 0)
5302 hisidx[HIST_SEARCH] = hislen - 1;
5304 last_maptick = -1;
5306 if (!in_history(histype, new_entry, TRUE))
5308 if (++hisidx[histype] == hislen)
5309 hisidx[histype] = 0;
5310 hisptr = &history[histype][hisidx[histype]];
5311 vim_free(hisptr->hisstr);
5313 /* Store the separator after the NUL of the string. */
5314 len = (int)STRLEN(new_entry);
5315 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5316 if (hisptr->hisstr != NULL)
5317 hisptr->hisstr[len + 1] = sep;
5319 hisptr->hisnum = ++hisnum[histype];
5320 if (histype == HIST_SEARCH && in_map)
5321 last_maptick = maptick;
5325 #if defined(FEAT_EVAL) || defined(PROTO)
5328 * Get identifier of newest history entry.
5329 * "histype" may be one of the HIST_ values.
5332 get_history_idx(histype)
5333 int histype;
5335 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5336 || hisidx[histype] < 0)
5337 return -1;
5339 return history[histype][hisidx[histype]].hisnum;
5342 static struct cmdline_info *get_ccline_ptr __ARGS((void));
5345 * Get pointer to the command line info to use. cmdline_paste() may clear
5346 * ccline and put the previous value in prev_ccline.
5348 static struct cmdline_info *
5349 get_ccline_ptr()
5351 if ((State & CMDLINE) == 0)
5352 return NULL;
5353 if (ccline.cmdbuff != NULL)
5354 return &ccline;
5355 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5356 return &prev_ccline;
5357 return NULL;
5361 * Get the current command line in allocated memory.
5362 * Only works when the command line is being edited.
5363 * Returns NULL when something is wrong.
5365 char_u *
5366 get_cmdline_str()
5368 struct cmdline_info *p = get_ccline_ptr();
5370 if (p == NULL)
5371 return NULL;
5372 return vim_strnsave(p->cmdbuff, p->cmdlen);
5376 * Get the current command line position, counted in bytes.
5377 * Zero is the first position.
5378 * Only works when the command line is being edited.
5379 * Returns -1 when something is wrong.
5382 get_cmdline_pos()
5384 struct cmdline_info *p = get_ccline_ptr();
5386 if (p == NULL)
5387 return -1;
5388 return p->cmdpos;
5392 * Set the command line byte position to "pos". Zero is the first position.
5393 * Only works when the command line is being edited.
5394 * Returns 1 when failed, 0 when OK.
5397 set_cmdline_pos(pos)
5398 int pos;
5400 struct cmdline_info *p = get_ccline_ptr();
5402 if (p == NULL)
5403 return 1;
5405 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5406 * changed the command line. */
5407 if (pos < 0)
5408 new_cmdpos = 0;
5409 else
5410 new_cmdpos = pos;
5411 return 0;
5415 * Get the current command-line type.
5416 * Returns ':' or '/' or '?' or '@' or '>' or '-'
5417 * Only works when the command line is being edited.
5418 * Returns NUL when something is wrong.
5421 get_cmdline_type()
5423 struct cmdline_info *p = get_ccline_ptr();
5425 if (p == NULL)
5426 return NUL;
5427 if (p->cmdfirstc == NUL)
5428 return (p->input_fn) ? '@' : '-';
5429 return p->cmdfirstc;
5433 * Calculate history index from a number:
5434 * num > 0: seen as identifying number of a history entry
5435 * num < 0: relative position in history wrt newest entry
5436 * "histype" may be one of the HIST_ values.
5438 static int
5439 calc_hist_idx(histype, num)
5440 int histype;
5441 int num;
5443 int i;
5444 histentry_T *hist;
5445 int wrapped = FALSE;
5447 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5448 || (i = hisidx[histype]) < 0 || num == 0)
5449 return -1;
5451 hist = history[histype];
5452 if (num > 0)
5454 while (hist[i].hisnum > num)
5455 if (--i < 0)
5457 if (wrapped)
5458 break;
5459 i += hislen;
5460 wrapped = TRUE;
5462 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5463 return i;
5465 else if (-num <= hislen)
5467 i += num + 1;
5468 if (i < 0)
5469 i += hislen;
5470 if (hist[i].hisstr != NULL)
5471 return i;
5473 return -1;
5477 * Get a history entry by its index.
5478 * "histype" may be one of the HIST_ values.
5480 char_u *
5481 get_history_entry(histype, idx)
5482 int histype;
5483 int idx;
5485 idx = calc_hist_idx(histype, idx);
5486 if (idx >= 0)
5487 return history[histype][idx].hisstr;
5488 else
5489 return (char_u *)"";
5493 * Clear all entries of a history.
5494 * "histype" may be one of the HIST_ values.
5497 clr_history(histype)
5498 int histype;
5500 int i;
5501 histentry_T *hisptr;
5503 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5505 hisptr = history[histype];
5506 for (i = hislen; i--;)
5508 vim_free(hisptr->hisstr);
5509 hisptr->hisnum = 0;
5510 hisptr++->hisstr = NULL;
5512 hisidx[histype] = -1; /* mark history as cleared */
5513 hisnum[histype] = 0; /* reset identifier counter */
5514 return OK;
5516 return FAIL;
5520 * Remove all entries matching {str} from a history.
5521 * "histype" may be one of the HIST_ values.
5524 del_history_entry(histype, str)
5525 int histype;
5526 char_u *str;
5528 regmatch_T regmatch;
5529 histentry_T *hisptr;
5530 int idx;
5531 int i;
5532 int last;
5533 int found = FALSE;
5535 regmatch.regprog = NULL;
5536 regmatch.rm_ic = FALSE; /* always match case */
5537 if (hislen != 0
5538 && histype >= 0
5539 && histype < HIST_COUNT
5540 && *str != NUL
5541 && (idx = hisidx[histype]) >= 0
5542 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5543 != NULL)
5545 i = last = idx;
5548 hisptr = &history[histype][i];
5549 if (hisptr->hisstr == NULL)
5550 break;
5551 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5553 found = TRUE;
5554 vim_free(hisptr->hisstr);
5555 hisptr->hisstr = NULL;
5556 hisptr->hisnum = 0;
5558 else
5560 if (i != last)
5562 history[histype][last] = *hisptr;
5563 hisptr->hisstr = NULL;
5564 hisptr->hisnum = 0;
5566 if (--last < 0)
5567 last += hislen;
5569 if (--i < 0)
5570 i += hislen;
5571 } while (i != idx);
5572 if (history[histype][idx].hisstr == NULL)
5573 hisidx[histype] = -1;
5575 vim_free(regmatch.regprog);
5576 return found;
5580 * Remove an indexed entry from a history.
5581 * "histype" may be one of the HIST_ values.
5584 del_history_idx(histype, idx)
5585 int histype;
5586 int idx;
5588 int i, j;
5590 i = calc_hist_idx(histype, idx);
5591 if (i < 0)
5592 return FALSE;
5593 idx = hisidx[histype];
5594 vim_free(history[histype][i].hisstr);
5596 /* When deleting the last added search string in a mapping, reset
5597 * last_maptick, so that the last added search string isn't deleted again.
5599 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5600 last_maptick = -1;
5602 while (i != idx)
5604 j = (i + 1) % hislen;
5605 history[histype][i] = history[histype][j];
5606 i = j;
5608 history[histype][i].hisstr = NULL;
5609 history[histype][i].hisnum = 0;
5610 if (--i < 0)
5611 i += hislen;
5612 hisidx[histype] = i;
5613 return TRUE;
5616 #endif /* FEAT_EVAL */
5618 #if defined(FEAT_CRYPT) || defined(PROTO)
5620 * Very specific function to remove the value in ":set key=val" from the
5621 * history.
5623 void
5624 remove_key_from_history()
5626 char_u *p;
5627 int i;
5629 i = hisidx[HIST_CMD];
5630 if (i < 0)
5631 return;
5632 p = history[HIST_CMD][i].hisstr;
5633 if (p != NULL)
5634 for ( ; *p; ++p)
5635 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5637 p = vim_strchr(p + 3, '=');
5638 if (p == NULL)
5639 break;
5640 ++p;
5641 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5642 if (p[i] == '\\' && p[i + 1])
5643 ++i;
5644 STRMOVE(p, p + i);
5645 --p;
5648 #endif
5650 #endif /* FEAT_CMDHIST */
5652 #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5654 * Get indices "num1,num2" that specify a range within a list (not a range of
5655 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5656 * Returns OK if parsed successfully, otherwise FAIL.
5659 get_list_range(str, num1, num2)
5660 char_u **str;
5661 int *num1;
5662 int *num2;
5664 int len;
5665 int first = FALSE;
5666 long num;
5668 *str = skipwhite(*str);
5669 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5671 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5672 *str += len;
5673 *num1 = (int)num;
5674 first = TRUE;
5676 *str = skipwhite(*str);
5677 if (**str == ',') /* parse "to" part of range */
5679 *str = skipwhite(*str + 1);
5680 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5681 if (len > 0)
5683 *num2 = (int)num;
5684 *str = skipwhite(*str + len);
5686 else if (!first) /* no number given at all */
5687 return FAIL;
5689 else if (first) /* only one number given */
5690 *num2 = *num1;
5691 return OK;
5693 #endif
5695 #if defined(FEAT_CMDHIST) || defined(PROTO)
5697 * :history command - print a history
5699 void
5700 ex_history(eap)
5701 exarg_T *eap;
5703 histentry_T *hist;
5704 int histype1 = HIST_CMD;
5705 int histype2 = HIST_CMD;
5706 int hisidx1 = 1;
5707 int hisidx2 = -1;
5708 int idx;
5709 int i, j, k;
5710 char_u *end;
5711 char_u *arg = eap->arg;
5713 if (hislen == 0)
5715 MSG(_("'history' option is zero"));
5716 return;
5719 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5721 end = arg;
5722 while (ASCII_ISALPHA(*end)
5723 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5724 end++;
5725 i = *end;
5726 *end = NUL;
5727 histype1 = get_histtype(arg);
5728 if (histype1 == -1)
5730 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
5732 histype1 = 0;
5733 histype2 = HIST_COUNT-1;
5735 else
5737 *end = i;
5738 EMSG(_(e_trailing));
5739 return;
5742 else
5743 histype2 = histype1;
5744 *end = i;
5746 else
5747 end = arg;
5748 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5750 EMSG(_(e_trailing));
5751 return;
5754 for (; !got_int && histype1 <= histype2; ++histype1)
5756 STRCPY(IObuff, "\n # ");
5757 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5758 MSG_PUTS_TITLE(IObuff);
5759 idx = hisidx[histype1];
5760 hist = history[histype1];
5761 j = hisidx1;
5762 k = hisidx2;
5763 if (j < 0)
5764 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5765 if (k < 0)
5766 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5767 if (idx >= 0 && j <= k)
5768 for (i = idx + 1; !got_int; ++i)
5770 if (i == hislen)
5771 i = 0;
5772 if (hist[i].hisstr != NULL
5773 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5775 msg_putchar('\n');
5776 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5777 hist[i].hisnum);
5778 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5779 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5780 (int)Columns - 10);
5781 else
5782 STRCAT(IObuff, hist[i].hisstr);
5783 msg_outtrans(IObuff);
5784 out_flush();
5786 if (i == idx)
5787 break;
5791 #endif
5793 #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5794 static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5795 static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5796 static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5797 static int viminfo_add_at_front = FALSE;
5799 static int hist_type2char __ARGS((int type, int use_question));
5802 * Translate a history type number to the associated character.
5804 static int
5805 hist_type2char(type, use_question)
5806 int type;
5807 int use_question; /* use '?' instead of '/' */
5809 if (type == HIST_CMD)
5810 return ':';
5811 if (type == HIST_SEARCH)
5813 if (use_question)
5814 return '?';
5815 else
5816 return '/';
5818 if (type == HIST_EXPR)
5819 return '=';
5820 return '@';
5824 * Prepare for reading the history from the viminfo file.
5825 * This allocates history arrays to store the read history lines.
5827 void
5828 prepare_viminfo_history(asklen)
5829 int asklen;
5831 int i;
5832 int num;
5833 int type;
5834 int len;
5836 init_history();
5837 viminfo_add_at_front = (asklen != 0);
5838 if (asklen > hislen)
5839 asklen = hislen;
5841 for (type = 0; type < HIST_COUNT; ++type)
5844 * Count the number of empty spaces in the history list. If there are
5845 * more spaces available than we request, then fill them up.
5847 for (i = 0, num = 0; i < hislen; i++)
5848 if (history[type][i].hisstr == NULL)
5849 num++;
5850 len = asklen;
5851 if (num > len)
5852 len = num;
5853 if (len <= 0)
5854 viminfo_history[type] = NULL;
5855 else
5856 viminfo_history[type] =
5857 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5858 if (viminfo_history[type] == NULL)
5859 len = 0;
5860 viminfo_hislen[type] = len;
5861 viminfo_hisidx[type] = 0;
5866 * Accept a line from the viminfo, store it in the history array when it's
5867 * new.
5870 read_viminfo_history(virp)
5871 vir_T *virp;
5873 int type;
5874 long_u len;
5875 char_u *val;
5876 char_u *p;
5878 type = hist_char2type(virp->vir_line[0]);
5879 if (viminfo_hisidx[type] < viminfo_hislen[type])
5881 val = viminfo_readstring(virp, 1, TRUE);
5882 if (val != NULL && *val != NUL)
5884 if (!in_history(type, val + (type == HIST_SEARCH),
5885 viminfo_add_at_front))
5887 /* Need to re-allocate to append the separator byte. */
5888 len = STRLEN(val);
5889 p = lalloc(len + 2, TRUE);
5890 if (p != NULL)
5892 if (type == HIST_SEARCH)
5894 /* Search entry: Move the separator from the first
5895 * column to after the NUL. */
5896 mch_memmove(p, val + 1, (size_t)len);
5897 p[len] = (*val == ' ' ? NUL : *val);
5899 else
5901 /* Not a search entry: No separator in the viminfo
5902 * file, add a NUL separator. */
5903 mch_memmove(p, val, (size_t)len + 1);
5904 p[len + 1] = NUL;
5906 viminfo_history[type][viminfo_hisidx[type]++] = p;
5910 vim_free(val);
5912 return viminfo_readline(virp);
5915 void
5916 finish_viminfo_history()
5918 int idx;
5919 int i;
5920 int type;
5922 for (type = 0; type < HIST_COUNT; ++type)
5924 if (history[type] == NULL)
5925 return;
5926 idx = hisidx[type] + viminfo_hisidx[type];
5927 if (idx >= hislen)
5928 idx -= hislen;
5929 else if (idx < 0)
5930 idx = hislen - 1;
5931 if (viminfo_add_at_front)
5932 hisidx[type] = idx;
5933 else
5935 if (hisidx[type] == -1)
5936 hisidx[type] = hislen - 1;
5939 if (history[type][idx].hisstr != NULL)
5940 break;
5941 if (++idx == hislen)
5942 idx = 0;
5943 } while (idx != hisidx[type]);
5944 if (idx != hisidx[type] && --idx < 0)
5945 idx = hislen - 1;
5947 for (i = 0; i < viminfo_hisidx[type]; i++)
5949 vim_free(history[type][idx].hisstr);
5950 history[type][idx].hisstr = viminfo_history[type][i];
5951 if (--idx < 0)
5952 idx = hislen - 1;
5954 idx += 1;
5955 idx %= hislen;
5956 for (i = 0; i < viminfo_hisidx[type]; i++)
5958 history[type][idx++].hisnum = ++hisnum[type];
5959 idx %= hislen;
5961 vim_free(viminfo_history[type]);
5962 viminfo_history[type] = NULL;
5966 void
5967 write_viminfo_history(fp)
5968 FILE *fp;
5970 int i;
5971 int type;
5972 int num_saved;
5973 char_u *p;
5974 int c;
5976 init_history();
5977 if (hislen == 0)
5978 return;
5979 for (type = 0; type < HIST_COUNT; ++type)
5981 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5982 if (num_saved == 0)
5983 continue;
5984 if (num_saved < 0) /* Use default */
5985 num_saved = hislen;
5986 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5987 type == HIST_CMD ? _("Command Line") :
5988 type == HIST_SEARCH ? _("Search String") :
5989 type == HIST_EXPR ? _("Expression") :
5990 _("Input Line"));
5991 if (num_saved > hislen)
5992 num_saved = hislen;
5993 i = hisidx[type];
5994 if (i >= 0)
5995 while (num_saved--)
5997 p = history[type][i].hisstr;
5998 if (p != NULL)
6000 fputc(hist_type2char(type, TRUE), fp);
6001 /* For the search history: put the separator in the second
6002 * column; use a space if there isn't one. */
6003 if (type == HIST_SEARCH)
6005 c = p[STRLEN(p) + 1];
6006 putc(c == NUL ? ' ' : c, fp);
6008 viminfo_writestring(fp, p);
6010 if (--i < 0)
6011 i = hislen - 1;
6015 #endif /* FEAT_VIMINFO */
6017 #if defined(FEAT_FKMAP) || defined(PROTO)
6019 * Write a character at the current cursor+offset position.
6020 * It is directly written into the command buffer block.
6022 void
6023 cmd_pchar(c, offset)
6024 int c, offset;
6026 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6028 EMSG(_("E198: cmd_pchar beyond the command length"));
6029 return;
6031 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6032 ccline.cmdbuff[ccline.cmdlen] = NUL;
6036 cmd_gchar(offset)
6037 int offset;
6039 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6041 /* EMSG(_("cmd_gchar beyond the command length")); */
6042 return NUL;
6044 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6046 #endif
6048 #if defined(FEAT_CMDWIN) || defined(PROTO)
6050 * Open a window on the current command line and history. Allow editing in
6051 * the window. Returns when the window is closed.
6052 * Returns:
6053 * CR if the command is to be executed
6054 * Ctrl_C if it is to be abandoned
6055 * K_IGNORE if editing continues
6057 static int
6058 ex_window()
6060 struct cmdline_info save_ccline;
6061 buf_T *old_curbuf = curbuf;
6062 win_T *old_curwin = curwin;
6063 buf_T *bp;
6064 win_T *wp;
6065 int i;
6066 linenr_T lnum;
6067 int histtype;
6068 garray_T winsizes;
6069 #ifdef FEAT_AUTOCMD
6070 char_u typestr[2];
6071 #endif
6072 int save_restart_edit = restart_edit;
6073 int save_State = State;
6074 int save_exmode = exmode_active;
6075 #ifdef FEAT_RIGHTLEFT
6076 int save_cmdmsg_rl = cmdmsg_rl;
6077 #endif
6079 /* Can't do this recursively. Can't do it when typing a password. */
6080 if (cmdwin_type != 0
6081 # if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6082 || cmdline_star > 0
6083 # endif
6086 beep_flush();
6087 return K_IGNORE;
6090 /* Save current window sizes. */
6091 win_size_save(&winsizes);
6093 # ifdef FEAT_AUTOCMD
6094 /* Don't execute autocommands while creating the window. */
6095 block_autocmds();
6096 # endif
6097 /* don't use a new tab page */
6098 cmdmod.tab = 0;
6100 /* Create a window for the command-line buffer. */
6101 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6103 beep_flush();
6104 # ifdef FEAT_AUTOCMD
6105 unblock_autocmds();
6106 # endif
6107 return K_IGNORE;
6109 cmdwin_type = get_cmdline_type();
6111 /* Create the command-line buffer empty. */
6112 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
6113 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
6114 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6115 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6116 curbuf->b_p_ma = TRUE;
6117 #ifdef FEAT_FOLDING
6118 curwin->w_p_fen = FALSE;
6119 #endif
6120 # ifdef FEAT_RIGHTLEFT
6121 curwin->w_p_rl = cmdmsg_rl;
6122 cmdmsg_rl = FALSE;
6123 # endif
6124 # ifdef FEAT_SCROLLBIND
6125 curwin->w_p_scb = FALSE;
6126 # endif
6128 # ifdef FEAT_AUTOCMD
6129 /* Do execute autocommands for setting the filetype (load syntax). */
6130 unblock_autocmds();
6131 # endif
6133 /* Showing the prompt may have set need_wait_return, reset it. */
6134 need_wait_return = FALSE;
6136 histtype = hist_char2type(cmdwin_type);
6137 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6139 if (p_wc == TAB)
6141 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6142 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6144 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6147 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6148 * sets 'textwidth' to 78). */
6149 curbuf->b_p_tw = 0;
6151 /* Fill the buffer with the history. */
6152 init_history();
6153 if (hislen > 0)
6155 i = hisidx[histtype];
6156 if (i >= 0)
6158 lnum = 0;
6161 if (++i == hislen)
6162 i = 0;
6163 if (history[histtype][i].hisstr != NULL)
6164 ml_append(lnum++, history[histtype][i].hisstr,
6165 (colnr_T)0, FALSE);
6167 while (i != hisidx[histtype]);
6171 /* Replace the empty last line with the current command-line and put the
6172 * cursor there. */
6173 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6174 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6175 curwin->w_cursor.col = ccline.cmdpos;
6176 changed_line_abv_curs();
6177 invalidate_botline();
6178 redraw_later(SOME_VALID);
6180 /* Save the command line info, can be used recursively. */
6181 save_ccline = ccline;
6182 ccline.cmdbuff = NULL;
6183 ccline.cmdprompt = NULL;
6185 /* No Ex mode here! */
6186 exmode_active = 0;
6188 State = NORMAL;
6189 # ifdef FEAT_MOUSE
6190 setmouse();
6191 # endif
6193 # ifdef FEAT_AUTOCMD
6194 /* Trigger CmdwinEnter autocommands. */
6195 typestr[0] = cmdwin_type;
6196 typestr[1] = NUL;
6197 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
6198 if (restart_edit != 0) /* autocmd with ":startinsert" */
6199 stuffcharReadbuff(K_NOP);
6200 # endif
6202 i = RedrawingDisabled;
6203 RedrawingDisabled = 0;
6206 * Call the main loop until <CR> or CTRL-C is typed.
6208 cmdwin_result = 0;
6209 main_loop(TRUE, FALSE);
6211 RedrawingDisabled = i;
6213 # ifdef FEAT_AUTOCMD
6214 /* Trigger CmdwinLeave autocommands. */
6215 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6216 # endif
6218 /* Restore the command line info. */
6219 ccline = save_ccline;
6220 cmdwin_type = 0;
6222 exmode_active = save_exmode;
6224 /* Safety check: The old window or buffer was deleted: It's a bug when
6225 * this happens! */
6226 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6228 cmdwin_result = Ctrl_C;
6229 EMSG(_("E199: Active window or buffer deleted"));
6231 else
6233 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6234 /* autocmds may abort script processing */
6235 if (aborting() && cmdwin_result != K_IGNORE)
6236 cmdwin_result = Ctrl_C;
6237 # endif
6238 /* Set the new command line from the cmdline buffer. */
6239 vim_free(ccline.cmdbuff);
6240 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
6242 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6244 if (histtype == HIST_CMD)
6246 /* Execute the command directly. */
6247 ccline.cmdbuff = vim_strsave((char_u *)p);
6248 cmdwin_result = CAR;
6250 else
6252 /* First need to cancel what we were doing. */
6253 ccline.cmdbuff = NULL;
6254 stuffcharReadbuff(':');
6255 stuffReadbuff((char_u *)p);
6256 stuffcharReadbuff(CAR);
6259 else if (cmdwin_result == K_XF2) /* :qa typed */
6261 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6262 cmdwin_result = CAR;
6264 else
6265 ccline.cmdbuff = vim_strsave(ml_get_curline());
6266 if (ccline.cmdbuff == NULL)
6267 cmdwin_result = Ctrl_C;
6268 else
6270 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6271 ccline.cmdbufflen = ccline.cmdlen + 1;
6272 ccline.cmdpos = curwin->w_cursor.col;
6273 if (ccline.cmdpos > ccline.cmdlen)
6274 ccline.cmdpos = ccline.cmdlen;
6275 if (cmdwin_result == K_IGNORE)
6277 set_cmdspos_cursor();
6278 redrawcmd();
6282 # ifdef FEAT_AUTOCMD
6283 /* Don't execute autocommands while deleting the window. */
6284 block_autocmds();
6285 # endif
6286 wp = curwin;
6287 bp = curbuf;
6288 win_goto(old_curwin);
6289 win_close(wp, TRUE);
6291 /* win_close() may have already wiped the buffer when 'bh' is
6292 * set to 'wipe' */
6293 if (buf_valid(bp))
6294 close_buffer(NULL, bp, DOBUF_WIPE);
6296 /* Restore window sizes. */
6297 win_size_restore(&winsizes);
6299 # ifdef FEAT_AUTOCMD
6300 unblock_autocmds();
6301 # endif
6304 ga_clear(&winsizes);
6305 restart_edit = save_restart_edit;
6306 # ifdef FEAT_RIGHTLEFT
6307 cmdmsg_rl = save_cmdmsg_rl;
6308 # endif
6310 State = save_State;
6311 # ifdef FEAT_MOUSE
6312 setmouse();
6313 # endif
6315 return cmdwin_result;
6317 #endif /* FEAT_CMDWIN */
6320 * Used for commands that either take a simple command string argument, or:
6321 * cmd << endmarker
6322 * {script}
6323 * endmarker
6324 * Returns a pointer to allocated memory with {script} or NULL.
6326 char_u *
6327 script_get(eap, cmd)
6328 exarg_T *eap;
6329 char_u *cmd;
6331 char_u *theline;
6332 char *end_pattern = NULL;
6333 char dot[] = ".";
6334 garray_T ga;
6336 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6337 return NULL;
6339 ga_init2(&ga, 1, 0x400);
6341 if (cmd[2] != NUL)
6342 end_pattern = (char *)skipwhite(cmd + 2);
6343 else
6344 end_pattern = dot;
6346 for (;;)
6348 theline = eap->getline(
6349 #ifdef FEAT_EVAL
6350 eap->cstack->cs_looplevel > 0 ? -1 :
6351 #endif
6352 NUL, eap->cookie, 0);
6354 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6356 vim_free(theline);
6357 break;
6360 ga_concat(&ga, theline);
6361 ga_append(&ga, '\n');
6362 vim_free(theline);
6364 ga_append(&ga, NUL);
6366 return (char_u *)ga.ga_data;