Merge branch 'master' of git://repo.or.cz/MacVim into MacVim
[MacVim/KaoriYa.git] / src / ex_getln.c
blobfff999a43c7659b68f4639c53e596901e126b4e7
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) || defined(FEAT_GUI_MACVIM))
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(FEAT_GUI_MACVIM))) \
2410 || defined(PROTO)
2412 * Return the virtual column number at the current cursor position.
2413 * This is used by the IM code to obtain the start of the preedit string.
2415 colnr_T
2416 cmdline_getvcol_cursor()
2418 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2419 return MAXCOL;
2421 # ifdef FEAT_MBYTE
2422 if (has_mbyte)
2424 colnr_T col;
2425 int i = 0;
2427 for (col = 0; i < ccline.cmdpos; ++col)
2428 i += (*mb_ptr2len)(ccline.cmdbuff + i);
2430 return col;
2432 else
2433 # endif
2434 return ccline.cmdpos;
2436 #endif
2438 #if defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
2440 * If part of the command line is an IM preedit string, redraw it with
2441 * IM feedback attributes. The cursor position is restored after drawing.
2443 static void
2444 redrawcmd_preedit()
2446 if ((State & CMDLINE)
2447 # ifndef FEAT_GUI_MACVIM
2448 && xic != NULL
2449 # endif
2450 /* && im_get_status() doesn't work when using SCIM */
2451 && !p_imdisable
2452 && im_is_preediting())
2454 int cmdpos = 0;
2455 int cmdspos;
2456 int old_row;
2457 int old_col;
2458 colnr_T col;
2460 old_row = msg_row;
2461 old_col = msg_col;
2462 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
2464 # ifdef FEAT_MBYTE
2465 if (has_mbyte)
2467 for (col = 0; col < preedit_start_col
2468 && cmdpos < ccline.cmdlen; ++col)
2470 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
2471 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
2474 else
2475 # endif
2477 cmdspos += preedit_start_col;
2478 cmdpos += preedit_start_col;
2481 msg_row = cmdline_row + (cmdspos / (int)Columns);
2482 msg_col = cmdspos % (int)Columns;
2483 if (msg_row >= Rows)
2484 msg_row = Rows - 1;
2486 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2488 int char_len;
2489 int char_attr;
2491 char_attr = im_get_feedback_attr(col);
2492 if (char_attr < 0)
2493 break; /* end of preedit string */
2495 # ifdef FEAT_MBYTE
2496 if (has_mbyte)
2497 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
2498 else
2499 # endif
2500 char_len = 1;
2502 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2503 cmdpos += char_len;
2506 msg_row = old_row;
2507 msg_col = old_col;
2510 #endif /* FEAT_XIM && (FEAT_GUI_GTK || FEAT_GUI_MACVIM) */
2513 * Allocate a new command line buffer.
2514 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2515 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2517 static void
2518 alloc_cmdbuff(len)
2519 int len;
2522 * give some extra space to avoid having to allocate all the time
2524 if (len < 80)
2525 len = 100;
2526 else
2527 len += 20;
2529 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2530 ccline.cmdbufflen = len;
2534 * Re-allocate the command line to length len + something extra.
2535 * return FAIL for failure, OK otherwise
2537 static int
2538 realloc_cmdbuff(len)
2539 int len;
2541 char_u *p;
2543 p = ccline.cmdbuff;
2544 alloc_cmdbuff(len); /* will get some more */
2545 if (ccline.cmdbuff == NULL) /* out of memory */
2547 ccline.cmdbuff = p; /* keep the old one */
2548 return FAIL;
2550 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2551 vim_free(p);
2553 if (ccline.xpc != NULL
2554 && ccline.xpc->xp_pattern != NULL
2555 && ccline.xpc->xp_context != EXPAND_NOTHING
2556 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2558 int i = (int)(ccline.xpc->xp_pattern - p);
2560 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2561 * to point into the newly allocated memory. */
2562 if (i >= 0 && i <= ccline.cmdlen)
2563 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2566 return OK;
2569 #if defined(FEAT_ARABIC) || defined(PROTO)
2570 static char_u *arshape_buf = NULL;
2572 # if defined(EXITFREE) || defined(PROTO)
2573 void
2574 free_cmdline_buf()
2576 vim_free(arshape_buf);
2578 # endif
2579 #endif
2582 * Draw part of the cmdline at the current cursor position. But draw stars
2583 * when cmdline_star is TRUE.
2585 static void
2586 draw_cmdline(start, len)
2587 int start;
2588 int len;
2590 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2591 int i;
2593 if (cmdline_star > 0)
2594 for (i = 0; i < len; ++i)
2596 msg_putchar('*');
2597 # ifdef FEAT_MBYTE
2598 if (has_mbyte)
2599 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
2600 # endif
2602 else
2603 #endif
2604 #ifdef FEAT_ARABIC
2605 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2607 static int buflen = 0;
2608 char_u *p;
2609 int j;
2610 int newlen = 0;
2611 int mb_l;
2612 int pc, pc1 = 0;
2613 int prev_c = 0;
2614 int prev_c1 = 0;
2615 int u8c;
2616 int u8cc[MAX_MCO];
2617 int nc = 0;
2620 * Do arabic shaping into a temporary buffer. This is very
2621 * inefficient!
2623 if (len * 2 + 2 > buflen)
2625 /* Re-allocate the buffer. We keep it around to avoid a lot of
2626 * alloc()/free() calls. */
2627 vim_free(arshape_buf);
2628 buflen = len * 2 + 2;
2629 arshape_buf = alloc(buflen);
2630 if (arshape_buf == NULL)
2631 return; /* out of memory */
2634 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2636 /* Prepend a space to draw the leading composing char on. */
2637 arshape_buf[0] = ' ';
2638 newlen = 1;
2641 for (j = start; j < start + len; j += mb_l)
2643 p = ccline.cmdbuff + j;
2644 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
2645 mb_l = utfc_ptr2len_len(p, start + len - j);
2646 if (ARABIC_CHAR(u8c))
2648 /* Do Arabic shaping. */
2649 if (cmdmsg_rl)
2651 /* displaying from right to left */
2652 pc = prev_c;
2653 pc1 = prev_c1;
2654 prev_c1 = u8cc[0];
2655 if (j + mb_l >= start + len)
2656 nc = NUL;
2657 else
2658 nc = utf_ptr2char(p + mb_l);
2660 else
2662 /* displaying from left to right */
2663 if (j + mb_l >= start + len)
2664 pc = NUL;
2665 else
2667 int pcc[MAX_MCO];
2669 pc = utfc_ptr2char_len(p + mb_l, pcc,
2670 start + len - j - mb_l);
2671 pc1 = pcc[0];
2673 nc = prev_c;
2675 prev_c = u8c;
2677 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
2679 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
2680 if (u8cc[0] != 0)
2682 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2683 if (u8cc[1] != 0)
2684 newlen += (*mb_char2bytes)(u8cc[1],
2685 arshape_buf + newlen);
2688 else
2690 prev_c = u8c;
2691 mch_memmove(arshape_buf + newlen, p, mb_l);
2692 newlen += mb_l;
2696 msg_outtrans_len(arshape_buf, newlen);
2698 else
2699 #endif
2700 msg_outtrans_len(ccline.cmdbuff + start, len);
2704 * Put a character on the command line. Shifts the following text to the
2705 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2706 * "c" must be printable (fit in one display cell)!
2708 void
2709 putcmdline(c, shift)
2710 int c;
2711 int shift;
2713 if (cmd_silent)
2714 return;
2715 msg_no_more = TRUE;
2716 msg_putchar(c);
2717 if (shift)
2718 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2719 msg_no_more = FALSE;
2720 cursorcmd();
2724 * Undo a putcmdline(c, FALSE).
2726 void
2727 unputcmdline()
2729 if (cmd_silent)
2730 return;
2731 msg_no_more = TRUE;
2732 if (ccline.cmdlen == ccline.cmdpos)
2733 msg_putchar(' ');
2734 else
2735 draw_cmdline(ccline.cmdpos, 1);
2736 msg_no_more = FALSE;
2737 cursorcmd();
2741 * Put the given string, of the given length, onto the command line.
2742 * If len is -1, then STRLEN() is used to calculate the length.
2743 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2744 * part will be redrawn, otherwise it will not. If this function is called
2745 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2746 * called afterwards.
2749 put_on_cmdline(str, len, redraw)
2750 char_u *str;
2751 int len;
2752 int redraw;
2754 int retval;
2755 int i;
2756 int m;
2757 int c;
2759 if (len < 0)
2760 len = (int)STRLEN(str);
2762 /* Check if ccline.cmdbuff needs to be longer */
2763 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2764 retval = realloc_cmdbuff(ccline.cmdlen + len);
2765 else
2766 retval = OK;
2767 if (retval == OK)
2769 if (!ccline.overstrike)
2771 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2772 ccline.cmdbuff + ccline.cmdpos,
2773 (size_t)(ccline.cmdlen - ccline.cmdpos));
2774 ccline.cmdlen += len;
2776 else
2778 #ifdef FEAT_MBYTE
2779 if (has_mbyte)
2781 /* Count nr of characters in the new string. */
2782 m = 0;
2783 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
2784 ++m;
2785 /* Count nr of bytes in cmdline that are overwritten by these
2786 * characters. */
2787 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
2788 i += (*mb_ptr2len)(ccline.cmdbuff + i))
2789 --m;
2790 if (i < ccline.cmdlen)
2792 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2793 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2794 ccline.cmdlen += ccline.cmdpos + len - i;
2796 else
2797 ccline.cmdlen = ccline.cmdpos + len;
2799 else
2800 #endif
2801 if (ccline.cmdpos + len > ccline.cmdlen)
2802 ccline.cmdlen = ccline.cmdpos + len;
2804 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2805 ccline.cmdbuff[ccline.cmdlen] = NUL;
2807 #ifdef FEAT_MBYTE
2808 if (enc_utf8)
2810 /* When the inserted text starts with a composing character,
2811 * backup to the character before it. There could be two of them.
2813 i = 0;
2814 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2815 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2817 i = (*mb_head_off)(ccline.cmdbuff,
2818 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2819 ccline.cmdpos -= i;
2820 len += i;
2821 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2823 # ifdef FEAT_ARABIC
2824 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2826 /* Check the previous character for Arabic combining pair. */
2827 i = (*mb_head_off)(ccline.cmdbuff,
2828 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2829 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2830 + ccline.cmdpos - i), c))
2832 ccline.cmdpos -= i;
2833 len += i;
2835 else
2836 i = 0;
2838 # endif
2839 if (i != 0)
2841 /* Also backup the cursor position. */
2842 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2843 ccline.cmdspos -= i;
2844 msg_col -= i;
2845 if (msg_col < 0)
2847 msg_col += Columns;
2848 --msg_row;
2852 #endif
2854 if (redraw && !cmd_silent)
2856 msg_no_more = TRUE;
2857 i = cmdline_row;
2858 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2859 /* Avoid clearing the rest of the line too often. */
2860 if (cmdline_row != i || ccline.overstrike)
2861 msg_clr_eos();
2862 msg_no_more = FALSE;
2864 #ifdef FEAT_FKMAP
2866 * If we are in Farsi command mode, the character input must be in
2867 * Insert mode. So do not advance the cmdpos.
2869 if (!cmd_fkmap)
2870 #endif
2872 if (KeyTyped)
2874 m = Columns * Rows;
2875 if (m < 0) /* overflow, Columns or Rows at weird value */
2876 m = MAXCOL;
2878 else
2879 m = MAXCOL;
2880 for (i = 0; i < len; ++i)
2882 c = cmdline_charsize(ccline.cmdpos);
2883 #ifdef FEAT_MBYTE
2884 /* count ">" for a double-wide char that doesn't fit. */
2885 if (has_mbyte)
2886 correct_cmdspos(ccline.cmdpos, c);
2887 #endif
2888 /* Stop cursor at the end of the screen, but do increment the
2889 * insert position, so that entering a very long command
2890 * works, even though you can't see it. */
2891 if (ccline.cmdspos + c < m)
2892 ccline.cmdspos += c;
2893 #ifdef FEAT_MBYTE
2894 if (has_mbyte)
2896 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
2897 if (c > len - i - 1)
2898 c = len - i - 1;
2899 ccline.cmdpos += c;
2900 i += c;
2902 #endif
2903 ++ccline.cmdpos;
2907 if (redraw)
2908 msg_check();
2909 return retval;
2912 static struct cmdline_info prev_ccline;
2913 static int prev_ccline_used = FALSE;
2916 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2917 * and overwrite it. But get_cmdline_str() may need it, thus make it
2918 * available globally in prev_ccline.
2920 static void
2921 save_cmdline(ccp)
2922 struct cmdline_info *ccp;
2924 if (!prev_ccline_used)
2926 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2927 prev_ccline_used = TRUE;
2929 *ccp = prev_ccline;
2930 prev_ccline = ccline;
2931 ccline.cmdbuff = NULL;
2932 ccline.cmdprompt = NULL;
2933 ccline.xpc = NULL;
2937 * Restore ccline after it has been saved with save_cmdline().
2939 static void
2940 restore_cmdline(ccp)
2941 struct cmdline_info *ccp;
2943 ccline = prev_ccline;
2944 prev_ccline = *ccp;
2947 #if defined(FEAT_EVAL) || defined(PROTO)
2949 * Save the command line into allocated memory. Returns a pointer to be
2950 * passed to restore_cmdline_alloc() later.
2951 * Returns NULL when failed.
2953 char_u *
2954 save_cmdline_alloc()
2956 struct cmdline_info *p;
2958 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2959 if (p != NULL)
2960 save_cmdline(p);
2961 return (char_u *)p;
2965 * Restore the command line from the return value of save_cmdline_alloc().
2967 void
2968 restore_cmdline_alloc(p)
2969 char_u *p;
2971 if (p != NULL)
2973 restore_cmdline((struct cmdline_info *)p);
2974 vim_free(p);
2977 #endif
2980 * paste a yank register into the command line.
2981 * used by CTRL-R command in command-line mode
2982 * insert_reg() can't be used here, because special characters from the
2983 * register contents will be interpreted as commands.
2985 * return FAIL for failure, OK otherwise
2987 static int
2988 cmdline_paste(regname, literally, remcr)
2989 int regname;
2990 int literally; /* Insert text literally instead of "as typed" */
2991 int remcr; /* remove trailing CR */
2993 long i;
2994 char_u *arg;
2995 char_u *p;
2996 int allocated;
2997 struct cmdline_info save_ccline;
2999 /* check for valid regname; also accept special characters for CTRL-R in
3000 * the command line */
3001 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3002 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3003 return FAIL;
3005 /* A register containing CTRL-R can cause an endless loop. Allow using
3006 * CTRL-C to break the loop. */
3007 line_breakcheck();
3008 if (got_int)
3009 return FAIL;
3011 #ifdef FEAT_CLIPBOARD
3012 regname = may_get_selection(regname);
3013 #endif
3015 /* Need to save and restore ccline. And set "textlock" to avoid nasty
3016 * things like going to another buffer when evaluating an expression. */
3017 save_cmdline(&save_ccline);
3018 ++textlock;
3019 i = get_spec_reg(regname, &arg, &allocated, TRUE);
3020 --textlock;
3021 restore_cmdline(&save_ccline);
3023 if (i)
3025 /* Got the value of a special register in "arg". */
3026 if (arg == NULL)
3027 return FAIL;
3029 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3030 * part of the word. */
3031 p = arg;
3032 if (p_is && regname == Ctrl_W)
3034 char_u *w;
3035 int len;
3037 /* Locate start of last word in the cmd buffer. */
3038 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
3040 #ifdef FEAT_MBYTE
3041 if (has_mbyte)
3043 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3044 if (!vim_iswordc(mb_ptr2char(w - len)))
3045 break;
3046 w -= len;
3048 else
3049 #endif
3051 if (!vim_iswordc(w[-1]))
3052 break;
3053 --w;
3056 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
3057 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3058 p += len;
3061 cmdline_paste_str(p, literally);
3062 if (allocated)
3063 vim_free(arg);
3064 return OK;
3067 return cmdline_paste_reg(regname, literally, remcr);
3071 * Put a string on the command line.
3072 * When "literally" is TRUE, insert literally.
3073 * When "literally" is FALSE, insert as typed, but don't leave the command
3074 * line.
3076 void
3077 cmdline_paste_str(s, literally)
3078 char_u *s;
3079 int literally;
3081 int c, cv;
3083 if (literally)
3084 put_on_cmdline(s, -1, TRUE);
3085 else
3086 while (*s != NUL)
3088 cv = *s;
3089 if (cv == Ctrl_V && s[1])
3090 ++s;
3091 #ifdef FEAT_MBYTE
3092 if (has_mbyte)
3093 c = mb_cptr2char_adv(&s);
3094 else
3095 #endif
3096 c = *s++;
3097 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3098 #ifdef UNIX
3099 || c == intr_char
3100 #endif
3101 || (c == Ctrl_BSL && *s == Ctrl_N))
3102 stuffcharReadbuff(Ctrl_V);
3103 stuffcharReadbuff(c);
3107 #ifdef FEAT_WILDMENU
3109 * Delete characters on the command line, from "from" to the current
3110 * position.
3112 static void
3113 cmdline_del(from)
3114 int from;
3116 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3117 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3118 ccline.cmdlen -= ccline.cmdpos - from;
3119 ccline.cmdpos = from;
3121 #endif
3124 * this function is called when the screen size changes and with incremental
3125 * search
3127 void
3128 redrawcmdline()
3130 if (cmd_silent)
3131 return;
3132 need_wait_return = FALSE;
3133 compute_cmdrow();
3134 redrawcmd();
3135 cursorcmd();
3138 static void
3139 redrawcmdprompt()
3141 int i;
3143 if (cmd_silent)
3144 return;
3145 if (ccline.cmdfirstc != NUL)
3146 msg_putchar(ccline.cmdfirstc);
3147 if (ccline.cmdprompt != NULL)
3149 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3150 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3151 /* do the reverse of set_cmdspos() */
3152 if (ccline.cmdfirstc != NUL)
3153 --ccline.cmdindent;
3155 else
3156 for (i = ccline.cmdindent; i > 0; --i)
3157 msg_putchar(' ');
3161 * Redraw what is currently on the command line.
3163 void
3164 redrawcmd()
3166 if (cmd_silent)
3167 return;
3169 /* when 'incsearch' is set there may be no command line while redrawing */
3170 if (ccline.cmdbuff == NULL)
3172 windgoto(cmdline_row, 0);
3173 msg_clr_eos();
3174 return;
3177 msg_start();
3178 redrawcmdprompt();
3180 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3181 msg_no_more = TRUE;
3182 draw_cmdline(0, ccline.cmdlen);
3183 msg_clr_eos();
3184 msg_no_more = FALSE;
3186 set_cmdspos_cursor();
3189 * An emsg() before may have set msg_scroll. This is used in normal mode,
3190 * in cmdline mode we can reset them now.
3192 msg_scroll = FALSE; /* next message overwrites cmdline */
3194 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3195 * in cmdline mode */
3196 skip_redraw = FALSE;
3199 void
3200 compute_cmdrow()
3202 if (exmode_active || msg_scrolled != 0)
3203 cmdline_row = Rows - 1;
3204 else
3205 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3206 + W_STATUS_HEIGHT(lastwin);
3209 static void
3210 cursorcmd()
3212 if (cmd_silent)
3213 return;
3215 #ifdef FEAT_RIGHTLEFT
3216 if (cmdmsg_rl)
3218 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3219 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3220 if (msg_row <= 0)
3221 msg_row = Rows - 1;
3223 else
3224 #endif
3226 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3227 msg_col = ccline.cmdspos % (int)Columns;
3228 if (msg_row >= Rows)
3229 msg_row = Rows - 1;
3232 windgoto(msg_row, msg_col);
3233 #if defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
3234 redrawcmd_preedit();
3235 #endif
3236 #ifdef MCH_CURSOR_SHAPE
3237 mch_update_cursor();
3238 #endif
3241 void
3242 gotocmdline(clr)
3243 int clr;
3245 msg_start();
3246 #ifdef FEAT_RIGHTLEFT
3247 if (cmdmsg_rl)
3248 msg_col = Columns - 1;
3249 else
3250 #endif
3251 msg_col = 0; /* always start in column 0 */
3252 if (clr) /* clear the bottom line(s) */
3253 msg_clr_eos(); /* will reset clear_cmdline */
3254 windgoto(cmdline_row, 0);
3258 * Check the word in front of the cursor for an abbreviation.
3259 * Called when the non-id character "c" has been entered.
3260 * When an abbreviation is recognized it is removed from the text with
3261 * backspaces and the replacement string is inserted, followed by "c".
3263 static int
3264 ccheck_abbr(c)
3265 int c;
3267 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3268 return FALSE;
3270 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3274 * Return FAIL if this is not an appropriate context in which to do
3275 * completion of anything, return OK if it is (even if there are no matches).
3276 * For the caller, this means that the character is just passed through like a
3277 * normal character (instead of being expanded). This allows :s/^I^D etc.
3279 static int
3280 nextwild(xp, type, options)
3281 expand_T *xp;
3282 int type;
3283 int options; /* extra options for ExpandOne() */
3285 int i, j;
3286 char_u *p1;
3287 char_u *p2;
3288 int difflen;
3289 int v;
3291 if (xp->xp_numfiles == -1)
3293 set_expand_context(xp);
3294 cmd_showtail = expand_showtail(xp);
3297 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3299 beep_flush();
3300 return OK; /* Something illegal on command line */
3302 if (xp->xp_context == EXPAND_NOTHING)
3304 /* Caller can use the character as a normal char instead */
3305 return FAIL;
3308 MSG_PUTS("..."); /* show that we are busy */
3309 out_flush();
3311 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3312 xp->xp_pattern_len = ccline.cmdpos - i;
3314 if (type == WILD_NEXT || type == WILD_PREV)
3317 * Get next/previous match for a previous expanded pattern.
3319 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3321 else
3324 * Translate string into pattern and expand it.
3326 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3327 xp->xp_context)) == NULL)
3328 p2 = NULL;
3329 else
3331 p2 = ExpandOne(xp, p1,
3332 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
3333 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3334 |options, type);
3335 vim_free(p1);
3336 /* longest match: make sure it is not shorter, happens with :help */
3337 if (p2 != NULL && type == WILD_LONGEST)
3339 for (j = 0; j < xp->xp_pattern_len; ++j)
3340 if (ccline.cmdbuff[i + j] == '*'
3341 || ccline.cmdbuff[i + j] == '?')
3342 break;
3343 if ((int)STRLEN(p2) < j)
3345 vim_free(p2);
3346 p2 = NULL;
3352 if (p2 != NULL && !got_int)
3354 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
3355 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3357 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3358 xp->xp_pattern = ccline.cmdbuff + i;
3360 else
3361 v = OK;
3362 if (v == OK)
3364 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3365 &ccline.cmdbuff[ccline.cmdpos],
3366 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3367 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
3368 ccline.cmdlen += difflen;
3369 ccline.cmdpos += difflen;
3372 vim_free(p2);
3374 redrawcmd();
3375 cursorcmd();
3377 /* When expanding a ":map" command and no matches are found, assume that
3378 * the key is supposed to be inserted literally */
3379 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3380 return FAIL;
3382 if (xp->xp_numfiles <= 0 && p2 == NULL)
3383 beep_flush();
3384 else if (xp->xp_numfiles == 1)
3385 /* free expanded pattern */
3386 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3388 return OK;
3392 * Do wildcard expansion on the string 'str'.
3393 * Chars that should not be expanded must be preceded with a backslash.
3394 * Return a pointer to allocated memory containing the new string.
3395 * Return NULL for failure.
3397 * "orig" is the originally expanded string, copied to allocated memory. It
3398 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3399 * WILD_PREV "orig" should be NULL.
3401 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3402 * is WILD_EXPAND_FREE or WILD_ALL.
3404 * mode = WILD_FREE: just free previously expanded matches
3405 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3406 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3407 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3408 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3409 * mode = WILD_ALL: return all matches concatenated
3410 * mode = WILD_LONGEST: return longest matched part
3412 * options = WILD_LIST_NOTFOUND: list entries without a match
3413 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3414 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3415 * options = WILD_NO_BEEP: Don't beep for multiple matches
3416 * options = WILD_ADD_SLASH: add a slash after directory names
3417 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3418 * options = WILD_SILENT: don't print warning messages
3419 * options = WILD_ESCAPE: put backslash before special chars
3421 * The variables xp->xp_context and xp->xp_backslash must have been set!
3423 char_u *
3424 ExpandOne(xp, str, orig, options, mode)
3425 expand_T *xp;
3426 char_u *str;
3427 char_u *orig; /* allocated copy of original of expanded string */
3428 int options;
3429 int mode;
3431 char_u *ss = NULL;
3432 static int findex;
3433 static char_u *orig_save = NULL; /* kept value of orig */
3434 int orig_saved = FALSE;
3435 int i;
3436 long_u len;
3437 int non_suf_match; /* number without matching suffix */
3440 * first handle the case of using an old match
3442 if (mode == WILD_NEXT || mode == WILD_PREV)
3444 if (xp->xp_numfiles > 0)
3446 if (mode == WILD_PREV)
3448 if (findex == -1)
3449 findex = xp->xp_numfiles;
3450 --findex;
3452 else /* mode == WILD_NEXT */
3453 ++findex;
3456 * When wrapping around, return the original string, set findex to
3457 * -1.
3459 if (findex < 0)
3461 if (orig_save == NULL)
3462 findex = xp->xp_numfiles - 1;
3463 else
3464 findex = -1;
3466 if (findex >= xp->xp_numfiles)
3468 if (orig_save == NULL)
3469 findex = 0;
3470 else
3471 findex = -1;
3473 #ifdef FEAT_WILDMENU
3474 if (p_wmnu)
3475 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3476 findex, cmd_showtail);
3477 #endif
3478 if (findex == -1)
3479 return vim_strsave(orig_save);
3480 return vim_strsave(xp->xp_files[findex]);
3482 else
3483 return NULL;
3486 /* free old names */
3487 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3489 FreeWild(xp->xp_numfiles, xp->xp_files);
3490 xp->xp_numfiles = -1;
3491 vim_free(orig_save);
3492 orig_save = NULL;
3494 findex = 0;
3496 if (mode == WILD_FREE) /* only release file name */
3497 return NULL;
3499 if (xp->xp_numfiles == -1)
3501 vim_free(orig_save);
3502 orig_save = orig;
3503 orig_saved = TRUE;
3506 * Do the expansion.
3508 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3509 options) == FAIL)
3511 #ifdef FNAME_ILLEGAL
3512 /* Illegal file name has been silently skipped. But when there
3513 * are wildcards, the real problem is that there was no match,
3514 * causing the pattern to be added, which has illegal characters.
3516 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3517 EMSG2(_(e_nomatch2), str);
3518 #endif
3520 else if (xp->xp_numfiles == 0)
3522 if (!(options & WILD_SILENT))
3523 EMSG2(_(e_nomatch2), str);
3525 else
3527 /* Escape the matches for use on the command line. */
3528 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3531 * Check for matching suffixes in file names.
3533 if (mode != WILD_ALL && mode != WILD_LONGEST)
3535 if (xp->xp_numfiles)
3536 non_suf_match = xp->xp_numfiles;
3537 else
3538 non_suf_match = 1;
3539 if ((xp->xp_context == EXPAND_FILES
3540 || xp->xp_context == EXPAND_DIRECTORIES)
3541 && xp->xp_numfiles > 1)
3544 * More than one match; check suffix.
3545 * The files will have been sorted on matching suffix in
3546 * expand_wildcards, only need to check the first two.
3548 non_suf_match = 0;
3549 for (i = 0; i < 2; ++i)
3550 if (match_suffix(xp->xp_files[i]))
3551 ++non_suf_match;
3553 if (non_suf_match != 1)
3555 /* Can we ever get here unless it's while expanding
3556 * interactively? If not, we can get rid of this all
3557 * together. Don't really want to wait for this message
3558 * (and possibly have to hit return to continue!).
3560 if (!(options & WILD_SILENT))
3561 EMSG(_(e_toomany));
3562 else if (!(options & WILD_NO_BEEP))
3563 beep_flush();
3565 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3566 ss = vim_strsave(xp->xp_files[0]);
3571 /* Find longest common part */
3572 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3574 for (len = 0; xp->xp_files[0][len]; ++len)
3576 for (i = 0; i < xp->xp_numfiles; ++i)
3578 #ifdef CASE_INSENSITIVE_FILENAME
3579 if (xp->xp_context == EXPAND_DIRECTORIES
3580 || xp->xp_context == EXPAND_FILES
3581 || xp->xp_context == EXPAND_SHELLCMD
3582 || xp->xp_context == EXPAND_BUFFERS)
3584 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3585 TOLOWER_LOC(xp->xp_files[0][len]))
3586 break;
3588 else
3589 #endif
3590 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3591 break;
3593 if (i < xp->xp_numfiles)
3595 if (!(options & WILD_NO_BEEP))
3596 vim_beep();
3597 break;
3600 ss = alloc((unsigned)len + 1);
3601 if (ss)
3602 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
3603 findex = -1; /* next p_wc gets first one */
3606 /* Concatenate all matching names */
3607 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3609 len = 0;
3610 for (i = 0; i < xp->xp_numfiles; ++i)
3611 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3612 ss = lalloc(len, TRUE);
3613 if (ss != NULL)
3615 *ss = NUL;
3616 for (i = 0; i < xp->xp_numfiles; ++i)
3618 STRCAT(ss, xp->xp_files[i]);
3619 if (i != xp->xp_numfiles - 1)
3620 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3625 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3626 ExpandCleanup(xp);
3628 /* Free "orig" if it wasn't stored in "orig_save". */
3629 if (!orig_saved)
3630 vim_free(orig);
3632 return ss;
3636 * Prepare an expand structure for use.
3638 void
3639 ExpandInit(xp)
3640 expand_T *xp;
3642 xp->xp_pattern = NULL;
3643 xp->xp_pattern_len = 0;
3644 xp->xp_backslash = XP_BS_NONE;
3645 #ifndef BACKSLASH_IN_FILENAME
3646 xp->xp_shell = FALSE;
3647 #endif
3648 xp->xp_numfiles = -1;
3649 xp->xp_files = NULL;
3650 #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3651 xp->xp_arg = NULL;
3652 #endif
3656 * Cleanup an expand structure after use.
3658 void
3659 ExpandCleanup(xp)
3660 expand_T *xp;
3662 if (xp->xp_numfiles >= 0)
3664 FreeWild(xp->xp_numfiles, xp->xp_files);
3665 xp->xp_numfiles = -1;
3669 void
3670 ExpandEscape(xp, str, numfiles, files, options)
3671 expand_T *xp;
3672 char_u *str;
3673 int numfiles;
3674 char_u **files;
3675 int options;
3677 int i;
3678 char_u *p;
3681 * May change home directory back to "~"
3683 if (options & WILD_HOME_REPLACE)
3684 tilde_replace(str, numfiles, files);
3686 if (options & WILD_ESCAPE)
3688 if (xp->xp_context == EXPAND_FILES
3689 || xp->xp_context == EXPAND_SHELLCMD
3690 || xp->xp_context == EXPAND_BUFFERS
3691 || xp->xp_context == EXPAND_DIRECTORIES)
3694 * Insert a backslash into a file name before a space, \, %, #
3695 * and wildmatch characters, except '~'.
3697 for (i = 0; i < numfiles; ++i)
3699 /* for ":set path=" we need to escape spaces twice */
3700 if (xp->xp_backslash == XP_BS_THREE)
3702 p = vim_strsave_escaped(files[i], (char_u *)" ");
3703 if (p != NULL)
3705 vim_free(files[i]);
3706 files[i] = p;
3707 #if defined(BACKSLASH_IN_FILENAME)
3708 p = vim_strsave_escaped(files[i], (char_u *)" ");
3709 if (p != NULL)
3711 vim_free(files[i]);
3712 files[i] = p;
3714 #endif
3717 #ifdef BACKSLASH_IN_FILENAME
3718 p = vim_strsave_fnameescape(files[i], FALSE);
3719 #else
3720 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
3721 #endif
3722 if (p != NULL)
3724 vim_free(files[i]);
3725 files[i] = p;
3728 /* If 'str' starts with "\~", replace "~" at start of
3729 * files[i] with "\~". */
3730 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
3731 escape_fname(&files[i]);
3733 xp->xp_backslash = XP_BS_NONE;
3735 /* If the first file starts with a '+' escape it. Otherwise it
3736 * could be seen as "+cmd". */
3737 if (*files[0] == '+')
3738 escape_fname(&files[0]);
3740 else if (xp->xp_context == EXPAND_TAGS)
3743 * Insert a backslash before characters in a tag name that
3744 * would terminate the ":tag" command.
3746 for (i = 0; i < numfiles; ++i)
3748 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3749 if (p != NULL)
3751 vim_free(files[i]);
3752 files[i] = p;
3760 * Escape special characters in "fname" for when used as a file name argument
3761 * after a Vim command, or, when "shell" is non-zero, a shell command.
3762 * Returns the result in allocated memory.
3764 char_u *
3765 vim_strsave_fnameescape(fname, shell)
3766 char_u *fname;
3767 int shell;
3769 char_u *p;
3770 #ifdef BACKSLASH_IN_FILENAME
3771 char_u buf[20];
3772 int j = 0;
3774 /* Don't escape '[' and '{' if they are in 'isfname'. */
3775 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3776 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3777 buf[j++] = *p;
3778 buf[j] = NUL;
3779 p = vim_strsave_escaped(fname, buf);
3780 #else
3781 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3782 if (shell && csh_like_shell() && p != NULL)
3784 char_u *s;
3786 /* For csh and similar shells need to put two backslashes before '!'.
3787 * One is taken by Vim, one by the shell. */
3788 s = vim_strsave_escaped(p, (char_u *)"!");
3789 vim_free(p);
3790 p = s;
3792 #endif
3794 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3795 * ":write". "cd -" has a special meaning. */
3796 if (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))
3797 escape_fname(&p);
3799 return p;
3803 * Put a backslash before the file name in "pp", which is in allocated memory.
3805 static void
3806 escape_fname(pp)
3807 char_u **pp;
3809 char_u *p;
3811 p = alloc((unsigned)(STRLEN(*pp) + 2));
3812 if (p != NULL)
3814 p[0] = '\\';
3815 STRCPY(p + 1, *pp);
3816 vim_free(*pp);
3817 *pp = p;
3822 * For each file name in files[num_files]:
3823 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3825 void
3826 tilde_replace(orig_pat, num_files, files)
3827 char_u *orig_pat;
3828 int num_files;
3829 char_u **files;
3831 int i;
3832 char_u *p;
3834 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3836 for (i = 0; i < num_files; ++i)
3838 p = home_replace_save(NULL, files[i]);
3839 if (p != NULL)
3841 vim_free(files[i]);
3842 files[i] = p;
3849 * Show all matches for completion on the command line.
3850 * Returns EXPAND_NOTHING when the character that triggered expansion should
3851 * be inserted like a normal character.
3853 static int
3854 showmatches(xp, wildmenu)
3855 expand_T *xp;
3856 int wildmenu UNUSED;
3858 #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3859 int num_files;
3860 char_u **files_found;
3861 int i, j, k;
3862 int maxlen;
3863 int lines;
3864 int columns;
3865 char_u *p;
3866 int lastlen;
3867 int attr;
3868 int showtail;
3870 if (xp->xp_numfiles == -1)
3872 set_expand_context(xp);
3873 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3874 &num_files, &files_found);
3875 showtail = expand_showtail(xp);
3876 if (i != EXPAND_OK)
3877 return i;
3880 else
3882 num_files = xp->xp_numfiles;
3883 files_found = xp->xp_files;
3884 showtail = cmd_showtail;
3887 #ifdef FEAT_WILDMENU
3888 if (!wildmenu)
3890 #endif
3891 msg_didany = FALSE; /* lines_left will be set */
3892 msg_start(); /* prepare for paging */
3893 msg_putchar('\n');
3894 out_flush();
3895 cmdline_row = msg_row;
3896 msg_didany = FALSE; /* lines_left will be set again */
3897 msg_start(); /* prepare for paging */
3898 #ifdef FEAT_WILDMENU
3900 #endif
3902 if (got_int)
3903 got_int = FALSE; /* only int. the completion, not the cmd line */
3904 #ifdef FEAT_WILDMENU
3905 else if (wildmenu)
3906 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3907 #endif
3908 else
3910 /* find the length of the longest file name */
3911 maxlen = 0;
3912 for (i = 0; i < num_files; ++i)
3914 if (!showtail && (xp->xp_context == EXPAND_FILES
3915 || xp->xp_context == EXPAND_SHELLCMD
3916 || xp->xp_context == EXPAND_BUFFERS))
3918 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3919 j = vim_strsize(NameBuff);
3921 else
3922 j = vim_strsize(L_SHOWFILE(i));
3923 if (j > maxlen)
3924 maxlen = j;
3927 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3928 lines = num_files;
3929 else
3931 /* compute the number of columns and lines for the listing */
3932 maxlen += 2; /* two spaces between file names */
3933 columns = ((int)Columns + 2) / maxlen;
3934 if (columns < 1)
3935 columns = 1;
3936 lines = (num_files + columns - 1) / columns;
3939 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3941 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3943 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3944 msg_clr_eos();
3945 msg_advance(maxlen - 3);
3946 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3949 /* list the files line by line */
3950 for (i = 0; i < lines; ++i)
3952 lastlen = 999;
3953 for (k = i; k < num_files; k += lines)
3955 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3957 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3958 p = files_found[k] + STRLEN(files_found[k]) + 1;
3959 msg_advance(maxlen + 1);
3960 msg_puts(p);
3961 msg_advance(maxlen + 3);
3962 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3963 break;
3965 for (j = maxlen - lastlen; --j >= 0; )
3966 msg_putchar(' ');
3967 if (xp->xp_context == EXPAND_FILES
3968 || xp->xp_context == EXPAND_SHELLCMD
3969 || xp->xp_context == EXPAND_BUFFERS)
3971 /* highlight directories */
3972 if (xp->xp_numfiles != -1)
3974 char_u *halved_slash;
3975 char_u *exp_path;
3977 /* Expansion was done before and special characters
3978 * were escaped, need to halve backslashes. Also
3979 * $HOME has been replaced with ~/. */
3980 exp_path = expand_env_save_opt(files_found[k], TRUE);
3981 halved_slash = backslash_halve_save(
3982 exp_path != NULL ? exp_path : files_found[k]);
3983 j = mch_isdir(halved_slash != NULL ? halved_slash
3984 : files_found[k]);
3985 vim_free(exp_path);
3986 vim_free(halved_slash);
3988 else
3989 /* Expansion was done here, file names are literal. */
3990 j = mch_isdir(files_found[k]);
3991 if (showtail)
3992 p = L_SHOWFILE(k);
3993 else
3995 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3996 TRUE);
3997 p = NameBuff;
4000 else
4002 j = FALSE;
4003 p = L_SHOWFILE(k);
4005 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4007 if (msg_col > 0) /* when not wrapped around */
4009 msg_clr_eos();
4010 msg_putchar('\n');
4012 out_flush(); /* show one line at a time */
4013 if (got_int)
4015 got_int = FALSE;
4016 break;
4021 * we redraw the command below the lines that we have just listed
4022 * This is a bit tricky, but it saves a lot of screen updating.
4024 cmdline_row = msg_row; /* will put it back later */
4027 if (xp->xp_numfiles == -1)
4028 FreeWild(num_files, files_found);
4030 return EXPAND_OK;
4034 * Private gettail for showmatches() (and win_redr_status_matches()):
4035 * Find tail of file name path, but ignore trailing "/".
4037 char_u *
4038 sm_gettail(s)
4039 char_u *s;
4041 char_u *p;
4042 char_u *t = s;
4043 int had_sep = FALSE;
4045 for (p = s; *p != NUL; )
4047 if (vim_ispathsep(*p)
4048 #ifdef BACKSLASH_IN_FILENAME
4049 && !rem_backslash(p)
4050 #endif
4052 had_sep = TRUE;
4053 else if (had_sep)
4055 t = p;
4056 had_sep = FALSE;
4058 mb_ptr_adv(p);
4060 return t;
4064 * Return TRUE if we only need to show the tail of completion matches.
4065 * When not completing file names or there is a wildcard in the path FALSE is
4066 * returned.
4068 static int
4069 expand_showtail(xp)
4070 expand_T *xp;
4072 char_u *s;
4073 char_u *end;
4075 /* When not completing file names a "/" may mean something different. */
4076 if (xp->xp_context != EXPAND_FILES
4077 && xp->xp_context != EXPAND_SHELLCMD
4078 && xp->xp_context != EXPAND_DIRECTORIES)
4079 return FALSE;
4081 end = gettail(xp->xp_pattern);
4082 if (end == xp->xp_pattern) /* there is no path separator */
4083 return FALSE;
4085 for (s = xp->xp_pattern; s < end; s++)
4087 /* Skip escaped wildcards. Only when the backslash is not a path
4088 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4089 if (rem_backslash(s))
4090 ++s;
4091 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4092 return FALSE;
4094 return TRUE;
4098 * Prepare a string for expansion.
4099 * When expanding file names: The string will be used with expand_wildcards().
4100 * Copy the file name into allocated memory and add a '*' at the end.
4101 * When expanding other names: The string will be used with regcomp(). Copy
4102 * the name into allocated memory and prepend "^".
4104 char_u *
4105 addstar(fname, len, context)
4106 char_u *fname;
4107 int len;
4108 int context; /* EXPAND_FILES etc. */
4110 char_u *retval;
4111 int i, j;
4112 int new_len;
4113 char_u *tail;
4115 if (context != EXPAND_FILES
4116 && context != EXPAND_SHELLCMD
4117 && context != EXPAND_DIRECTORIES)
4120 * Matching will be done internally (on something other than files).
4121 * So we convert the file-matching-type wildcards into our kind for
4122 * use with vim_regcomp(). First work out how long it will be:
4125 /* For help tags the translation is done in find_help_tags().
4126 * For a tag pattern starting with "/" no translation is needed. */
4127 if (context == EXPAND_HELP
4128 || context == EXPAND_COLORS
4129 || context == EXPAND_COMPILER
4130 || (context == EXPAND_TAGS && fname[0] == '/'))
4131 retval = vim_strnsave(fname, len);
4132 else
4134 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4135 for (i = 0; i < len; i++)
4137 if (fname[i] == '*' || fname[i] == '~')
4138 new_len++; /* '*' needs to be replaced by ".*"
4139 '~' needs to be replaced by "\~" */
4141 /* Buffer names are like file names. "." should be literal */
4142 if (context == EXPAND_BUFFERS && fname[i] == '.')
4143 new_len++; /* "." becomes "\." */
4145 /* Custom expansion takes care of special things, match
4146 * backslashes literally (perhaps also for other types?) */
4147 if ((context == EXPAND_USER_DEFINED
4148 || context == EXPAND_USER_LIST) && fname[i] == '\\')
4149 new_len++; /* '\' becomes "\\" */
4151 retval = alloc(new_len);
4152 if (retval != NULL)
4154 retval[0] = '^';
4155 j = 1;
4156 for (i = 0; i < len; i++, j++)
4158 /* Skip backslash. But why? At least keep it for custom
4159 * expansion. */
4160 if (context != EXPAND_USER_DEFINED
4161 && context != EXPAND_USER_LIST
4162 && fname[i] == '\\'
4163 && ++i == len)
4164 break;
4166 switch (fname[i])
4168 case '*': retval[j++] = '.';
4169 break;
4170 case '~': retval[j++] = '\\';
4171 break;
4172 case '?': retval[j] = '.';
4173 continue;
4174 case '.': if (context == EXPAND_BUFFERS)
4175 retval[j++] = '\\';
4176 break;
4177 case '\\': if (context == EXPAND_USER_DEFINED
4178 || context == EXPAND_USER_LIST)
4179 retval[j++] = '\\';
4180 break;
4182 retval[j] = fname[i];
4184 retval[j] = NUL;
4188 else
4190 retval = alloc(len + 4);
4191 if (retval != NULL)
4193 vim_strncpy(retval, fname, len);
4196 * Don't add a star to *, ~, ~user, $var or `cmd`.
4197 * * would become **, which walks the whole tree.
4198 * ~ would be at the start of the file name, but not the tail.
4199 * $ could be anywhere in the tail.
4200 * ` could be anywhere in the file name.
4201 * When the name ends in '$' don't add a star, remove the '$'.
4203 tail = gettail(retval);
4204 if ((*retval != '~' || tail != retval)
4205 && (len == 0 || retval[len - 1] != '*')
4206 && vim_strchr(tail, '$') == NULL
4207 && vim_strchr(retval, '`') == NULL)
4208 retval[len++] = '*';
4209 else if (len > 0 && retval[len - 1] == '$')
4210 --len;
4211 retval[len] = NUL;
4214 return retval;
4218 * Must parse the command line so far to work out what context we are in.
4219 * Completion can then be done based on that context.
4220 * This routine sets the variables:
4221 * xp->xp_pattern The start of the pattern to be expanded within
4222 * the command line (ends at the cursor).
4223 * xp->xp_context The type of thing to expand. Will be one of:
4225 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4226 * the command line, like an unknown command. Caller
4227 * should beep.
4228 * EXPAND_NOTHING Unrecognised context for completion, use char like
4229 * a normal char, rather than for completion. eg
4230 * :s/^I/
4231 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4232 * it.
4233 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4234 * EXPAND_FILES After command with XFILE set, or after setting
4235 * with P_EXPAND set. eg :e ^I, :w>>^I
4236 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4237 * when we know only directories are of interest. eg
4238 * :set dir=^I
4239 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
4240 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4241 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4242 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4243 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4244 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4245 * EXPAND_EVENTS Complete event names
4246 * EXPAND_SYNTAX Complete :syntax command arguments
4247 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4248 * EXPAND_AUGROUP Complete autocommand group names
4249 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4250 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4251 * eg :unmap a^I , :cunab x^I
4252 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4253 * eg :call sub^I
4254 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4255 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4256 * names in expressions, eg :while s^I
4257 * EXPAND_ENV_VARS Complete environment variable names
4259 static void
4260 set_expand_context(xp)
4261 expand_T *xp;
4263 /* only expansion for ':', '>' and '=' command-lines */
4264 if (ccline.cmdfirstc != ':'
4265 #ifdef FEAT_EVAL
4266 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
4267 && !ccline.input_fn
4268 #endif
4271 xp->xp_context = EXPAND_NOTHING;
4272 return;
4274 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4277 void
4278 set_cmd_context(xp, str, len, col)
4279 expand_T *xp;
4280 char_u *str; /* start of command line */
4281 int len; /* length of command line (excl. NUL) */
4282 int col; /* position of cursor */
4284 int old_char = NUL;
4285 char_u *nextcomm;
4288 * Avoid a UMR warning from Purify, only save the character if it has been
4289 * written before.
4291 if (col < len)
4292 old_char = str[col];
4293 str[col] = NUL;
4294 nextcomm = str;
4296 #ifdef FEAT_EVAL
4297 if (ccline.cmdfirstc == '=')
4299 # ifdef FEAT_CMDL_COMPL
4300 /* pass CMD_SIZE because there is no real command */
4301 set_context_for_expression(xp, str, CMD_SIZE);
4302 # endif
4304 else if (ccline.input_fn)
4306 xp->xp_context = ccline.xp_context;
4307 xp->xp_pattern = ccline.cmdbuff;
4308 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
4309 xp->xp_arg = ccline.xp_arg;
4310 # endif
4312 else
4313 #endif
4314 while (nextcomm != NULL)
4315 nextcomm = set_one_cmd_context(xp, nextcomm);
4317 str[col] = old_char;
4321 * Expand the command line "str" from context "xp".
4322 * "xp" must have been set by set_cmd_context().
4323 * xp->xp_pattern points into "str", to where the text that is to be expanded
4324 * starts.
4325 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4326 * cursor.
4327 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4328 * key that triggered expansion literally.
4329 * Returns EXPAND_OK otherwise.
4332 expand_cmdline(xp, str, col, matchcount, matches)
4333 expand_T *xp;
4334 char_u *str; /* start of command line */
4335 int col; /* position of cursor */
4336 int *matchcount; /* return: nr of matches */
4337 char_u ***matches; /* return: array of pointers to matches */
4339 char_u *file_str = NULL;
4341 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4343 beep_flush();
4344 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4346 if (xp->xp_context == EXPAND_NOTHING)
4348 /* Caller can use the character as a normal char instead */
4349 return EXPAND_NOTHING;
4352 /* add star to file name, or convert to regexp if not exp. files. */
4353 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4354 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
4355 if (file_str == NULL)
4356 return EXPAND_UNSUCCESSFUL;
4358 /* find all files that match the description */
4359 if (ExpandFromContext(xp, file_str, matchcount, matches,
4360 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4362 *matchcount = 0;
4363 *matches = NULL;
4365 vim_free(file_str);
4367 return EXPAND_OK;
4370 #ifdef FEAT_MULTI_LANG
4372 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4374 static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4376 static void
4377 cleanup_help_tags(num_file, file)
4378 int num_file;
4379 char_u **file;
4381 int i, j;
4382 int len;
4384 for (i = 0; i < num_file; ++i)
4386 len = (int)STRLEN(file[i]) - 3;
4387 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4389 /* Sorting on priority means the same item in another language may
4390 * be anywhere. Search all items for a match up to the "@en". */
4391 for (j = 0; j < num_file; ++j)
4392 if (j != i
4393 && (int)STRLEN(file[j]) == len + 3
4394 && STRNCMP(file[i], file[j], len + 1) == 0)
4395 break;
4396 if (j == num_file)
4397 file[i][len] = NUL;
4401 #endif
4404 * Do the expansion based on xp->xp_context and "pat".
4406 static int
4407 ExpandFromContext(xp, pat, num_file, file, options)
4408 expand_T *xp;
4409 char_u *pat;
4410 int *num_file;
4411 char_u ***file;
4412 int options;
4414 #ifdef FEAT_CMDL_COMPL
4415 regmatch_T regmatch;
4416 #endif
4417 int ret;
4418 int flags;
4420 flags = EW_DIR; /* include directories */
4421 if (options & WILD_LIST_NOTFOUND)
4422 flags |= EW_NOTFOUND;
4423 if (options & WILD_ADD_SLASH)
4424 flags |= EW_ADDSLASH;
4425 if (options & WILD_KEEP_ALL)
4426 flags |= EW_KEEPALL;
4427 if (options & WILD_SILENT)
4428 flags |= EW_SILENT;
4430 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4433 * Expand file or directory names.
4435 int free_pat = FALSE;
4436 int i;
4438 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4439 * space */
4440 if (xp->xp_backslash != XP_BS_NONE)
4442 free_pat = TRUE;
4443 pat = vim_strsave(pat);
4444 for (i = 0; pat[i]; ++i)
4445 if (pat[i] == '\\')
4447 if (xp->xp_backslash == XP_BS_THREE
4448 && pat[i + 1] == '\\'
4449 && pat[i + 2] == '\\'
4450 && pat[i + 3] == ' ')
4451 STRMOVE(pat + i, pat + i + 3);
4452 if (xp->xp_backslash == XP_BS_ONE
4453 && pat[i + 1] == ' ')
4454 STRMOVE(pat + i, pat + i + 1);
4458 if (xp->xp_context == EXPAND_FILES)
4459 flags |= EW_FILE;
4460 else
4461 flags = (flags | EW_DIR) & ~EW_FILE;
4462 /* Expand wildcards, supporting %:h and the like. */
4463 ret = expand_wildcards_eval(&pat, num_file, file, flags);
4464 if (free_pat)
4465 vim_free(pat);
4466 return ret;
4469 *file = (char_u **)"";
4470 *num_file = 0;
4471 if (xp->xp_context == EXPAND_HELP)
4473 /* With an empty argument we would get all the help tags, which is
4474 * very slow. Get matches for "help" instead. */
4475 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4476 num_file, file, FALSE) == OK)
4478 #ifdef FEAT_MULTI_LANG
4479 cleanup_help_tags(*num_file, *file);
4480 #endif
4481 return OK;
4483 return FAIL;
4486 #ifndef FEAT_CMDL_COMPL
4487 return FAIL;
4488 #else
4489 if (xp->xp_context == EXPAND_SHELLCMD)
4490 return expand_shellcmd(pat, num_file, file, flags);
4491 if (xp->xp_context == EXPAND_OLD_SETTING)
4492 return ExpandOldSetting(num_file, file);
4493 if (xp->xp_context == EXPAND_BUFFERS)
4494 return ExpandBufnames(pat, num_file, file, options);
4495 if (xp->xp_context == EXPAND_TAGS
4496 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4497 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4498 if (xp->xp_context == EXPAND_COLORS)
4499 return ExpandRTDir(pat, num_file, file, "colors");
4500 if (xp->xp_context == EXPAND_COMPILER)
4501 return ExpandRTDir(pat, num_file, file, "compiler");
4502 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4503 if (xp->xp_context == EXPAND_USER_LIST)
4504 return ExpandUserList(xp, num_file, file);
4505 # endif
4507 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4508 if (regmatch.regprog == NULL)
4509 return FAIL;
4511 /* set ignore-case according to p_ic, p_scs and pat */
4512 regmatch.rm_ic = ignorecase(pat);
4514 if (xp->xp_context == EXPAND_SETTINGS
4515 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4516 ret = ExpandSettings(xp, &regmatch, num_file, file);
4517 else if (xp->xp_context == EXPAND_MAPPINGS)
4518 ret = ExpandMappings(&regmatch, num_file, file);
4519 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4520 else if (xp->xp_context == EXPAND_USER_DEFINED)
4521 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4522 # endif
4523 else
4525 static struct expgen
4527 int context;
4528 char_u *((*func)__ARGS((expand_T *, int)));
4529 int ic;
4530 } tab[] =
4532 {EXPAND_COMMANDS, get_command_name, FALSE},
4533 {EXPAND_BEHAVE, get_behave_arg, TRUE},
4534 #ifdef FEAT_USR_CMDS
4535 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4536 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4537 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4538 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4539 #endif
4540 #ifdef FEAT_EVAL
4541 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4542 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4543 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4544 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4545 #endif
4546 #ifdef FEAT_MENU
4547 {EXPAND_MENUS, get_menu_name, FALSE},
4548 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4549 #endif
4550 #ifdef FEAT_SYN_HL
4551 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4552 #endif
4553 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4554 #ifdef FEAT_AUTOCMD
4555 {EXPAND_EVENTS, get_event_name, TRUE},
4556 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4557 #endif
4558 #ifdef FEAT_CSCOPE
4559 {EXPAND_CSCOPE, get_cscope_name, TRUE},
4560 #endif
4561 #ifdef FEAT_SIGNS
4562 {EXPAND_SIGN, get_sign_name, TRUE},
4563 #endif
4564 #ifdef FEAT_PROFILE
4565 {EXPAND_PROFILE, get_profile_name, TRUE},
4566 #endif
4567 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4568 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4569 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4570 #endif
4571 {EXPAND_ENV_VARS, get_env_name, TRUE},
4572 #ifdef FEAT_GUI_MACVIM
4573 {EXPAND_MACACTION, get_macaction_name, FALSE},
4574 #endif
4576 int i;
4579 * Find a context in the table and call the ExpandGeneric() with the
4580 * right function to do the expansion.
4582 ret = FAIL;
4583 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
4584 if (xp->xp_context == tab[i].context)
4586 if (tab[i].ic)
4587 regmatch.rm_ic = TRUE;
4588 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4589 break;
4593 vim_free(regmatch.regprog);
4595 return ret;
4596 #endif /* FEAT_CMDL_COMPL */
4599 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4601 * Expand a list of names.
4603 * Generic function for command line completion. It calls a function to
4604 * obtain strings, one by one. The strings are matched against a regexp
4605 * program. Matching strings are copied into an array, which is returned.
4607 * Returns OK when no problems encountered, FAIL for error (out of memory).
4610 ExpandGeneric(xp, regmatch, num_file, file, func)
4611 expand_T *xp;
4612 regmatch_T *regmatch;
4613 int *num_file;
4614 char_u ***file;
4615 char_u *((*func)__ARGS((expand_T *, int)));
4616 /* returns a string from the list */
4618 int i;
4619 int count = 0;
4620 int round;
4621 char_u *str;
4623 /* do this loop twice:
4624 * round == 0: count the number of matching names
4625 * round == 1: copy the matching names into allocated memory
4627 for (round = 0; round <= 1; ++round)
4629 for (i = 0; ; ++i)
4631 str = (*func)(xp, i);
4632 if (str == NULL) /* end of list */
4633 break;
4634 if (*str == NUL) /* skip empty strings */
4635 continue;
4637 if (vim_regexec(regmatch, str, (colnr_T)0))
4639 if (round)
4641 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4642 (*file)[count] = str;
4643 #ifdef FEAT_MENU
4644 if (func == get_menu_names && str != NULL)
4646 /* test for separator added by get_menu_names() */
4647 str += STRLEN(str) - 1;
4648 if (*str == '\001')
4649 *str = '.';
4651 #endif
4653 ++count;
4656 if (round == 0)
4658 if (count == 0)
4659 return OK;
4660 *num_file = count;
4661 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4662 if (*file == NULL)
4664 *file = (char_u **)"";
4665 return FAIL;
4667 count = 0;
4671 /* Sort the results. Keep menu's in the specified order. */
4672 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4673 sort_strings(*file, *num_file);
4675 #ifdef FEAT_CMDL_COMPL
4676 /* Reset the variables used for special highlight names expansion, so that
4677 * they don't show up when getting normal highlight names by ID. */
4678 reset_expand_highlight();
4679 #endif
4681 return OK;
4685 * Complete a shell command.
4686 * Returns FAIL or OK;
4688 static int
4689 expand_shellcmd(filepat, num_file, file, flagsarg)
4690 char_u *filepat; /* pattern to match with command names */
4691 int *num_file; /* return: number of matches */
4692 char_u ***file; /* return: array with matches */
4693 int flagsarg; /* EW_ flags */
4695 char_u *pat;
4696 int i;
4697 char_u *path;
4698 int mustfree = FALSE;
4699 garray_T ga;
4700 char_u *buf = alloc(MAXPATHL);
4701 size_t l;
4702 char_u *s, *e;
4703 int flags = flagsarg;
4704 int ret;
4706 if (buf == NULL)
4707 return FAIL;
4709 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4710 * space */
4711 pat = vim_strsave(filepat);
4712 for (i = 0; pat[i]; ++i)
4713 if (pat[i] == '\\' && pat[i + 1] == ' ')
4714 STRMOVE(pat + i, pat + i + 1);
4716 flags |= EW_FILE | EW_EXEC;
4718 /* For an absolute name we don't use $PATH. */
4719 if (mch_isFullName(pat))
4720 path = (char_u *)" ";
4721 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
4722 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4723 path = (char_u *)".";
4724 else
4725 path = vim_getenv((char_u *)"PATH", &mustfree);
4728 * Go over all directories in $PATH. Expand matches in that directory and
4729 * collect them in "ga".
4731 ga_init2(&ga, (int)sizeof(char *), 10);
4732 for (s = path; *s != NUL; s = e)
4734 if (*s == ' ')
4735 ++s; /* Skip space used for absolute path name. */
4737 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4738 e = vim_strchr(s, ';');
4739 #else
4740 e = vim_strchr(s, ':');
4741 #endif
4742 if (e == NULL)
4743 e = s + STRLEN(s);
4745 l = e - s;
4746 if (l > MAXPATHL - 5)
4747 break;
4748 vim_strncpy(buf, s, l);
4749 add_pathsep(buf);
4750 l = STRLEN(buf);
4751 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4753 /* Expand matches in one directory of $PATH. */
4754 ret = expand_wildcards(1, &buf, num_file, file, flags);
4755 if (ret == OK)
4757 if (ga_grow(&ga, *num_file) == FAIL)
4758 FreeWild(*num_file, *file);
4759 else
4761 for (i = 0; i < *num_file; ++i)
4763 s = (*file)[i];
4764 if (STRLEN(s) > l)
4766 /* Remove the path again. */
4767 STRMOVE(s, s + l);
4768 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4770 else
4771 vim_free(s);
4773 vim_free(*file);
4776 if (*e != NUL)
4777 ++e;
4779 *file = ga.ga_data;
4780 *num_file = ga.ga_len;
4782 vim_free(buf);
4783 vim_free(pat);
4784 if (mustfree)
4785 vim_free(path);
4786 return OK;
4790 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4791 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));
4794 * Call "user_expand_func()" to invoke a user defined VimL function and return
4795 * the result (either a string or a List).
4797 static void *
4798 call_user_expand_func(user_expand_func, xp, num_file, file)
4799 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
4800 expand_T *xp;
4801 int *num_file;
4802 char_u ***file;
4804 char_u keep;
4805 char_u num[50];
4806 char_u *args[3];
4807 int save_current_SID = current_SID;
4808 void *ret;
4809 struct cmdline_info save_ccline;
4811 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
4812 return NULL;
4813 *num_file = 0;
4814 *file = NULL;
4816 if (ccline.cmdbuff == NULL)
4818 /* Completion from Insert mode, pass fake arguments. */
4819 keep = 0;
4820 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
4821 args[1] = xp->xp_pattern;
4823 else
4825 /* Completion on the command line, pass real arguments. */
4826 keep = ccline.cmdbuff[ccline.cmdlen];
4827 ccline.cmdbuff[ccline.cmdlen] = 0;
4828 sprintf((char *)num, "%d", ccline.cmdpos);
4829 args[1] = ccline.cmdbuff;
4831 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
4832 args[2] = num;
4834 /* Save the cmdline, we don't know what the function may do. */
4835 save_ccline = ccline;
4836 ccline.cmdbuff = NULL;
4837 ccline.cmdprompt = NULL;
4838 current_SID = xp->xp_scriptID;
4840 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
4842 ccline = save_ccline;
4843 current_SID = save_current_SID;
4844 if (ccline.cmdbuff != NULL)
4845 ccline.cmdbuff[ccline.cmdlen] = keep;
4847 vim_free(args[0]);
4848 return ret;
4852 * Expand names with a function defined by the user.
4854 static int
4855 ExpandUserDefined(xp, regmatch, num_file, file)
4856 expand_T *xp;
4857 regmatch_T *regmatch;
4858 int *num_file;
4859 char_u ***file;
4861 char_u *retstr;
4862 char_u *s;
4863 char_u *e;
4864 char_u keep;
4865 garray_T ga;
4867 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4868 if (retstr == NULL)
4869 return FAIL;
4871 ga_init2(&ga, (int)sizeof(char *), 3);
4872 for (s = retstr; *s != NUL; s = e)
4874 e = vim_strchr(s, '\n');
4875 if (e == NULL)
4876 e = s + STRLEN(s);
4877 keep = *e;
4878 *e = 0;
4880 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4882 *e = keep;
4883 if (*e != NUL)
4884 ++e;
4885 continue;
4888 if (ga_grow(&ga, 1) == FAIL)
4889 break;
4891 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4892 ++ga.ga_len;
4894 *e = keep;
4895 if (*e != NUL)
4896 ++e;
4898 vim_free(retstr);
4899 *file = ga.ga_data;
4900 *num_file = ga.ga_len;
4901 return OK;
4905 * Expand names with a list returned by a function defined by the user.
4907 static int
4908 ExpandUserList(xp, num_file, file)
4909 expand_T *xp;
4910 int *num_file;
4911 char_u ***file;
4913 list_T *retlist;
4914 listitem_T *li;
4915 garray_T ga;
4917 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4918 if (retlist == NULL)
4919 return FAIL;
4921 ga_init2(&ga, (int)sizeof(char *), 3);
4922 /* Loop over the items in the list. */
4923 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4925 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
4926 continue; /* Skip non-string items and empty strings */
4928 if (ga_grow(&ga, 1) == FAIL)
4929 break;
4931 ((char_u **)ga.ga_data)[ga.ga_len] =
4932 vim_strsave(li->li_tv.vval.v_string);
4933 ++ga.ga_len;
4935 list_unref(retlist);
4937 *file = ga.ga_data;
4938 *num_file = ga.ga_len;
4939 return OK;
4941 #endif
4944 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4945 * or compiler names.
4947 static int
4948 ExpandRTDir(pat, num_file, file, dirname)
4949 char_u *pat;
4950 int *num_file;
4951 char_u ***file;
4952 char *dirname; /* "colors" or "compiler" */
4954 char_u *all;
4955 char_u *s;
4956 char_u *e;
4957 garray_T ga;
4959 *num_file = 0;
4960 *file = NULL;
4961 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4962 if (s == NULL)
4963 return FAIL;
4964 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4965 all = globpath(p_rtp, s, 0);
4966 vim_free(s);
4967 if (all == NULL)
4968 return FAIL;
4970 ga_init2(&ga, (int)sizeof(char *), 3);
4971 for (s = all; *s != NUL; s = e)
4973 e = vim_strchr(s, '\n');
4974 if (e == NULL)
4975 e = s + STRLEN(s);
4976 if (ga_grow(&ga, 1) == FAIL)
4977 break;
4978 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4980 for (s = e - 4; s > all; mb_ptr_back(all, s))
4981 if (*s == '\n' || vim_ispathsep(*s))
4982 break;
4983 ++s;
4984 ((char_u **)ga.ga_data)[ga.ga_len] =
4985 vim_strnsave(s, (int)(e - s - 4));
4986 ++ga.ga_len;
4988 if (*e != NUL)
4989 ++e;
4991 vim_free(all);
4992 *file = ga.ga_data;
4993 *num_file = ga.ga_len;
4994 return OK;
4997 #endif
4999 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5001 * Expand "file" for all comma-separated directories in "path".
5002 * Returns an allocated string with all matches concatenated, separated by
5003 * newlines. Returns NULL for an error or no matches.
5005 char_u *
5006 globpath(path, file, expand_options)
5007 char_u *path;
5008 char_u *file;
5009 int expand_options;
5011 expand_T xpc;
5012 char_u *buf;
5013 garray_T ga;
5014 int i;
5015 int len;
5016 int num_p;
5017 char_u **p;
5018 char_u *cur = NULL;
5020 buf = alloc(MAXPATHL);
5021 if (buf == NULL)
5022 return NULL;
5024 ExpandInit(&xpc);
5025 xpc.xp_context = EXPAND_FILES;
5027 ga_init2(&ga, 1, 100);
5029 /* Loop over all entries in {path}. */
5030 while (*path != NUL)
5032 /* Copy one item of the path to buf[] and concatenate the file name. */
5033 copy_option_part(&path, buf, MAXPATHL, ",");
5034 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5036 add_pathsep(buf);
5037 STRCAT(buf, file);
5038 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5039 WILD_SILENT|expand_options) != FAIL && num_p > 0)
5041 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
5042 for (len = 0, i = 0; i < num_p; ++i)
5043 len += (int)STRLEN(p[i]) + 1;
5045 /* Concatenate new results to previous ones. */
5046 if (ga_grow(&ga, len) == OK)
5048 cur = (char_u *)ga.ga_data + ga.ga_len;
5049 for (i = 0; i < num_p; ++i)
5051 STRCPY(cur, p[i]);
5052 cur += STRLEN(p[i]);
5053 *cur++ = '\n';
5055 ga.ga_len += len;
5057 FreeWild(num_p, p);
5061 if (cur != NULL)
5062 *--cur = 0; /* Replace trailing newline with NUL */
5064 vim_free(buf);
5065 return (char_u *)ga.ga_data;
5068 #endif
5070 #if defined(FEAT_CMDHIST) || defined(PROTO)
5072 /*********************************
5073 * Command line history stuff *
5074 *********************************/
5077 * Translate a history character to the associated type number.
5079 static int
5080 hist_char2type(c)
5081 int c;
5083 if (c == ':')
5084 return HIST_CMD;
5085 if (c == '=')
5086 return HIST_EXPR;
5087 if (c == '@')
5088 return HIST_INPUT;
5089 if (c == '>')
5090 return HIST_DEBUG;
5091 return HIST_SEARCH; /* must be '?' or '/' */
5095 * Table of history names.
5096 * These names are used in :history and various hist...() functions.
5097 * It is sufficient to give the significant prefix of a history name.
5100 static char *(history_names[]) =
5102 "cmd",
5103 "search",
5104 "expr",
5105 "input",
5106 "debug",
5107 NULL
5111 * init_history() - Initialize the command line history.
5112 * Also used to re-allocate the history when the size changes.
5114 void
5115 init_history()
5117 int newlen; /* new length of history table */
5118 histentry_T *temp;
5119 int i;
5120 int j;
5121 int type;
5124 * If size of history table changed, reallocate it
5126 newlen = (int)p_hi;
5127 if (newlen != hislen) /* history length changed */
5129 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5131 if (newlen)
5133 temp = (histentry_T *)lalloc(
5134 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5135 if (temp == NULL) /* out of memory! */
5137 if (type == 0) /* first one: just keep the old length */
5139 newlen = hislen;
5140 break;
5142 /* Already changed one table, now we can only have zero
5143 * length for all tables. */
5144 newlen = 0;
5145 type = -1;
5146 continue;
5149 else
5150 temp = NULL;
5151 if (newlen == 0 || temp != NULL)
5153 if (hisidx[type] < 0) /* there are no entries yet */
5155 for (i = 0; i < newlen; ++i)
5157 temp[i].hisnum = 0;
5158 temp[i].hisstr = NULL;
5161 else if (newlen > hislen) /* array becomes bigger */
5163 for (i = 0; i <= hisidx[type]; ++i)
5164 temp[i] = history[type][i];
5165 j = i;
5166 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5168 temp[i].hisnum = 0;
5169 temp[i].hisstr = NULL;
5171 for ( ; j < hislen; ++i, ++j)
5172 temp[i] = history[type][j];
5174 else /* array becomes smaller or 0 */
5176 j = hisidx[type];
5177 for (i = newlen - 1; ; --i)
5179 if (i >= 0) /* copy newest entries */
5180 temp[i] = history[type][j];
5181 else /* remove older entries */
5182 vim_free(history[type][j].hisstr);
5183 if (--j < 0)
5184 j = hislen - 1;
5185 if (j == hisidx[type])
5186 break;
5188 hisidx[type] = newlen - 1;
5190 vim_free(history[type]);
5191 history[type] = temp;
5194 hislen = newlen;
5199 * Check if command line 'str' is already in history.
5200 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5202 static int
5203 in_history(type, str, move_to_front)
5204 int type;
5205 char_u *str;
5206 int move_to_front; /* Move the entry to the front if it exists */
5208 int i;
5209 int last_i = -1;
5211 if (hisidx[type] < 0)
5212 return FALSE;
5213 i = hisidx[type];
5216 if (history[type][i].hisstr == NULL)
5217 return FALSE;
5218 if (STRCMP(str, history[type][i].hisstr) == 0)
5220 if (!move_to_front)
5221 return TRUE;
5222 last_i = i;
5223 break;
5225 if (--i < 0)
5226 i = hislen - 1;
5227 } while (i != hisidx[type]);
5229 if (last_i >= 0)
5231 str = history[type][i].hisstr;
5232 while (i != hisidx[type])
5234 if (++i >= hislen)
5235 i = 0;
5236 history[type][last_i] = history[type][i];
5237 last_i = i;
5239 history[type][i].hisstr = str;
5240 history[type][i].hisnum = ++hisnum[type];
5241 return TRUE;
5243 return FALSE;
5247 * Convert history name (from table above) to its HIST_ equivalent.
5248 * When "name" is empty, return "cmd" history.
5249 * Returns -1 for unknown history name.
5252 get_histtype(name)
5253 char_u *name;
5255 int i;
5256 int len = (int)STRLEN(name);
5258 /* No argument: use current history. */
5259 if (len == 0)
5260 return hist_char2type(ccline.cmdfirstc);
5262 for (i = 0; history_names[i] != NULL; ++i)
5263 if (STRNICMP(name, history_names[i], len) == 0)
5264 return i;
5266 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5267 return hist_char2type(name[0]);
5269 return -1;
5272 static int last_maptick = -1; /* last seen maptick */
5275 * Add the given string to the given history. If the string is already in the
5276 * history then it is moved to the front. "histype" may be one of he HIST_
5277 * values.
5279 void
5280 add_to_history(histype, new_entry, in_map, sep)
5281 int histype;
5282 char_u *new_entry;
5283 int in_map; /* consider maptick when inside a mapping */
5284 int sep; /* separator character used (search hist) */
5286 histentry_T *hisptr;
5287 int len;
5289 if (hislen == 0) /* no history */
5290 return;
5293 * Searches inside the same mapping overwrite each other, so that only
5294 * the last line is kept. Be careful not to remove a line that was moved
5295 * down, only lines that were added.
5297 if (histype == HIST_SEARCH && in_map)
5299 if (maptick == last_maptick)
5301 /* Current line is from the same mapping, remove it */
5302 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5303 vim_free(hisptr->hisstr);
5304 hisptr->hisstr = NULL;
5305 hisptr->hisnum = 0;
5306 --hisnum[histype];
5307 if (--hisidx[HIST_SEARCH] < 0)
5308 hisidx[HIST_SEARCH] = hislen - 1;
5310 last_maptick = -1;
5312 if (!in_history(histype, new_entry, TRUE))
5314 if (++hisidx[histype] == hislen)
5315 hisidx[histype] = 0;
5316 hisptr = &history[histype][hisidx[histype]];
5317 vim_free(hisptr->hisstr);
5319 /* Store the separator after the NUL of the string. */
5320 len = (int)STRLEN(new_entry);
5321 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5322 if (hisptr->hisstr != NULL)
5323 hisptr->hisstr[len + 1] = sep;
5325 hisptr->hisnum = ++hisnum[histype];
5326 if (histype == HIST_SEARCH && in_map)
5327 last_maptick = maptick;
5331 #if defined(FEAT_EVAL) || defined(PROTO)
5334 * Get identifier of newest history entry.
5335 * "histype" may be one of the HIST_ values.
5338 get_history_idx(histype)
5339 int histype;
5341 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5342 || hisidx[histype] < 0)
5343 return -1;
5345 return history[histype][hisidx[histype]].hisnum;
5348 static struct cmdline_info *get_ccline_ptr __ARGS((void));
5351 * Get pointer to the command line info to use. cmdline_paste() may clear
5352 * ccline and put the previous value in prev_ccline.
5354 static struct cmdline_info *
5355 get_ccline_ptr()
5357 if ((State & CMDLINE) == 0)
5358 return NULL;
5359 if (ccline.cmdbuff != NULL)
5360 return &ccline;
5361 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5362 return &prev_ccline;
5363 return NULL;
5367 * Get the current command line in allocated memory.
5368 * Only works when the command line is being edited.
5369 * Returns NULL when something is wrong.
5371 char_u *
5372 get_cmdline_str()
5374 struct cmdline_info *p = get_ccline_ptr();
5376 if (p == NULL)
5377 return NULL;
5378 return vim_strnsave(p->cmdbuff, p->cmdlen);
5382 * Get the current command line position, counted in bytes.
5383 * Zero is the first position.
5384 * Only works when the command line is being edited.
5385 * Returns -1 when something is wrong.
5388 get_cmdline_pos()
5390 struct cmdline_info *p = get_ccline_ptr();
5392 if (p == NULL)
5393 return -1;
5394 return p->cmdpos;
5398 * Set the command line byte position to "pos". Zero is the first position.
5399 * Only works when the command line is being edited.
5400 * Returns 1 when failed, 0 when OK.
5403 set_cmdline_pos(pos)
5404 int pos;
5406 struct cmdline_info *p = get_ccline_ptr();
5408 if (p == NULL)
5409 return 1;
5411 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5412 * changed the command line. */
5413 if (pos < 0)
5414 new_cmdpos = 0;
5415 else
5416 new_cmdpos = pos;
5417 return 0;
5421 * Get the current command-line type.
5422 * Returns ':' or '/' or '?' or '@' or '>' or '-'
5423 * Only works when the command line is being edited.
5424 * Returns NUL when something is wrong.
5427 get_cmdline_type()
5429 struct cmdline_info *p = get_ccline_ptr();
5431 if (p == NULL)
5432 return NUL;
5433 if (p->cmdfirstc == NUL)
5434 return (p->input_fn) ? '@' : '-';
5435 return p->cmdfirstc;
5439 * Calculate history index from a number:
5440 * num > 0: seen as identifying number of a history entry
5441 * num < 0: relative position in history wrt newest entry
5442 * "histype" may be one of the HIST_ values.
5444 static int
5445 calc_hist_idx(histype, num)
5446 int histype;
5447 int num;
5449 int i;
5450 histentry_T *hist;
5451 int wrapped = FALSE;
5453 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5454 || (i = hisidx[histype]) < 0 || num == 0)
5455 return -1;
5457 hist = history[histype];
5458 if (num > 0)
5460 while (hist[i].hisnum > num)
5461 if (--i < 0)
5463 if (wrapped)
5464 break;
5465 i += hislen;
5466 wrapped = TRUE;
5468 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5469 return i;
5471 else if (-num <= hislen)
5473 i += num + 1;
5474 if (i < 0)
5475 i += hislen;
5476 if (hist[i].hisstr != NULL)
5477 return i;
5479 return -1;
5483 * Get a history entry by its index.
5484 * "histype" may be one of the HIST_ values.
5486 char_u *
5487 get_history_entry(histype, idx)
5488 int histype;
5489 int idx;
5491 idx = calc_hist_idx(histype, idx);
5492 if (idx >= 0)
5493 return history[histype][idx].hisstr;
5494 else
5495 return (char_u *)"";
5499 * Clear all entries of a history.
5500 * "histype" may be one of the HIST_ values.
5503 clr_history(histype)
5504 int histype;
5506 int i;
5507 histentry_T *hisptr;
5509 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5511 hisptr = history[histype];
5512 for (i = hislen; i--;)
5514 vim_free(hisptr->hisstr);
5515 hisptr->hisnum = 0;
5516 hisptr++->hisstr = NULL;
5518 hisidx[histype] = -1; /* mark history as cleared */
5519 hisnum[histype] = 0; /* reset identifier counter */
5520 return OK;
5522 return FAIL;
5526 * Remove all entries matching {str} from a history.
5527 * "histype" may be one of the HIST_ values.
5530 del_history_entry(histype, str)
5531 int histype;
5532 char_u *str;
5534 regmatch_T regmatch;
5535 histentry_T *hisptr;
5536 int idx;
5537 int i;
5538 int last;
5539 int found = FALSE;
5541 regmatch.regprog = NULL;
5542 regmatch.rm_ic = FALSE; /* always match case */
5543 if (hislen != 0
5544 && histype >= 0
5545 && histype < HIST_COUNT
5546 && *str != NUL
5547 && (idx = hisidx[histype]) >= 0
5548 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5549 != NULL)
5551 i = last = idx;
5554 hisptr = &history[histype][i];
5555 if (hisptr->hisstr == NULL)
5556 break;
5557 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5559 found = TRUE;
5560 vim_free(hisptr->hisstr);
5561 hisptr->hisstr = NULL;
5562 hisptr->hisnum = 0;
5564 else
5566 if (i != last)
5568 history[histype][last] = *hisptr;
5569 hisptr->hisstr = NULL;
5570 hisptr->hisnum = 0;
5572 if (--last < 0)
5573 last += hislen;
5575 if (--i < 0)
5576 i += hislen;
5577 } while (i != idx);
5578 if (history[histype][idx].hisstr == NULL)
5579 hisidx[histype] = -1;
5581 vim_free(regmatch.regprog);
5582 return found;
5586 * Remove an indexed entry from a history.
5587 * "histype" may be one of the HIST_ values.
5590 del_history_idx(histype, idx)
5591 int histype;
5592 int idx;
5594 int i, j;
5596 i = calc_hist_idx(histype, idx);
5597 if (i < 0)
5598 return FALSE;
5599 idx = hisidx[histype];
5600 vim_free(history[histype][i].hisstr);
5602 /* When deleting the last added search string in a mapping, reset
5603 * last_maptick, so that the last added search string isn't deleted again.
5605 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5606 last_maptick = -1;
5608 while (i != idx)
5610 j = (i + 1) % hislen;
5611 history[histype][i] = history[histype][j];
5612 i = j;
5614 history[histype][i].hisstr = NULL;
5615 history[histype][i].hisnum = 0;
5616 if (--i < 0)
5617 i += hislen;
5618 hisidx[histype] = i;
5619 return TRUE;
5622 #endif /* FEAT_EVAL */
5624 #if defined(FEAT_CRYPT) || defined(PROTO)
5626 * Very specific function to remove the value in ":set key=val" from the
5627 * history.
5629 void
5630 remove_key_from_history()
5632 char_u *p;
5633 int i;
5635 i = hisidx[HIST_CMD];
5636 if (i < 0)
5637 return;
5638 p = history[HIST_CMD][i].hisstr;
5639 if (p != NULL)
5640 for ( ; *p; ++p)
5641 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5643 p = vim_strchr(p + 3, '=');
5644 if (p == NULL)
5645 break;
5646 ++p;
5647 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5648 if (p[i] == '\\' && p[i + 1])
5649 ++i;
5650 STRMOVE(p, p + i);
5651 --p;
5654 #endif
5656 #endif /* FEAT_CMDHIST */
5658 #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5660 * Get indices "num1,num2" that specify a range within a list (not a range of
5661 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5662 * Returns OK if parsed successfully, otherwise FAIL.
5665 get_list_range(str, num1, num2)
5666 char_u **str;
5667 int *num1;
5668 int *num2;
5670 int len;
5671 int first = FALSE;
5672 long num;
5674 *str = skipwhite(*str);
5675 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5677 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5678 *str += len;
5679 *num1 = (int)num;
5680 first = TRUE;
5682 *str = skipwhite(*str);
5683 if (**str == ',') /* parse "to" part of range */
5685 *str = skipwhite(*str + 1);
5686 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5687 if (len > 0)
5689 *num2 = (int)num;
5690 *str = skipwhite(*str + len);
5692 else if (!first) /* no number given at all */
5693 return FAIL;
5695 else if (first) /* only one number given */
5696 *num2 = *num1;
5697 return OK;
5699 #endif
5701 #if defined(FEAT_CMDHIST) || defined(PROTO)
5703 * :history command - print a history
5705 void
5706 ex_history(eap)
5707 exarg_T *eap;
5709 histentry_T *hist;
5710 int histype1 = HIST_CMD;
5711 int histype2 = HIST_CMD;
5712 int hisidx1 = 1;
5713 int hisidx2 = -1;
5714 int idx;
5715 int i, j, k;
5716 char_u *end;
5717 char_u *arg = eap->arg;
5719 if (hislen == 0)
5721 MSG(_("'history' option is zero"));
5722 return;
5725 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5727 end = arg;
5728 while (ASCII_ISALPHA(*end)
5729 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5730 end++;
5731 i = *end;
5732 *end = NUL;
5733 histype1 = get_histtype(arg);
5734 if (histype1 == -1)
5736 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
5738 histype1 = 0;
5739 histype2 = HIST_COUNT-1;
5741 else
5743 *end = i;
5744 EMSG(_(e_trailing));
5745 return;
5748 else
5749 histype2 = histype1;
5750 *end = i;
5752 else
5753 end = arg;
5754 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5756 EMSG(_(e_trailing));
5757 return;
5760 for (; !got_int && histype1 <= histype2; ++histype1)
5762 STRCPY(IObuff, "\n # ");
5763 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5764 MSG_PUTS_TITLE(IObuff);
5765 idx = hisidx[histype1];
5766 hist = history[histype1];
5767 j = hisidx1;
5768 k = hisidx2;
5769 if (j < 0)
5770 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5771 if (k < 0)
5772 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5773 if (idx >= 0 && j <= k)
5774 for (i = idx + 1; !got_int; ++i)
5776 if (i == hislen)
5777 i = 0;
5778 if (hist[i].hisstr != NULL
5779 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5781 msg_putchar('\n');
5782 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5783 hist[i].hisnum);
5784 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5785 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5786 (int)Columns - 10);
5787 else
5788 STRCAT(IObuff, hist[i].hisstr);
5789 msg_outtrans(IObuff);
5790 out_flush();
5792 if (i == idx)
5793 break;
5797 #endif
5799 #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5800 static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5801 static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5802 static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5803 static int viminfo_add_at_front = FALSE;
5805 static int hist_type2char __ARGS((int type, int use_question));
5808 * Translate a history type number to the associated character.
5810 static int
5811 hist_type2char(type, use_question)
5812 int type;
5813 int use_question; /* use '?' instead of '/' */
5815 if (type == HIST_CMD)
5816 return ':';
5817 if (type == HIST_SEARCH)
5819 if (use_question)
5820 return '?';
5821 else
5822 return '/';
5824 if (type == HIST_EXPR)
5825 return '=';
5826 return '@';
5830 * Prepare for reading the history from the viminfo file.
5831 * This allocates history arrays to store the read history lines.
5833 void
5834 prepare_viminfo_history(asklen)
5835 int asklen;
5837 int i;
5838 int num;
5839 int type;
5840 int len;
5842 init_history();
5843 viminfo_add_at_front = (asklen != 0);
5844 if (asklen > hislen)
5845 asklen = hislen;
5847 for (type = 0; type < HIST_COUNT; ++type)
5850 * Count the number of empty spaces in the history list. If there are
5851 * more spaces available than we request, then fill them up.
5853 for (i = 0, num = 0; i < hislen; i++)
5854 if (history[type][i].hisstr == NULL)
5855 num++;
5856 len = asklen;
5857 if (num > len)
5858 len = num;
5859 if (len <= 0)
5860 viminfo_history[type] = NULL;
5861 else
5862 viminfo_history[type] =
5863 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5864 if (viminfo_history[type] == NULL)
5865 len = 0;
5866 viminfo_hislen[type] = len;
5867 viminfo_hisidx[type] = 0;
5872 * Accept a line from the viminfo, store it in the history array when it's
5873 * new.
5876 read_viminfo_history(virp)
5877 vir_T *virp;
5879 int type;
5880 long_u len;
5881 char_u *val;
5882 char_u *p;
5884 type = hist_char2type(virp->vir_line[0]);
5885 if (viminfo_hisidx[type] < viminfo_hislen[type])
5887 val = viminfo_readstring(virp, 1, TRUE);
5888 if (val != NULL && *val != NUL)
5890 if (!in_history(type, val + (type == HIST_SEARCH),
5891 viminfo_add_at_front))
5893 /* Need to re-allocate to append the separator byte. */
5894 len = STRLEN(val);
5895 p = lalloc(len + 2, TRUE);
5896 if (p != NULL)
5898 if (type == HIST_SEARCH)
5900 /* Search entry: Move the separator from the first
5901 * column to after the NUL. */
5902 mch_memmove(p, val + 1, (size_t)len);
5903 p[len] = (*val == ' ' ? NUL : *val);
5905 else
5907 /* Not a search entry: No separator in the viminfo
5908 * file, add a NUL separator. */
5909 mch_memmove(p, val, (size_t)len + 1);
5910 p[len + 1] = NUL;
5912 viminfo_history[type][viminfo_hisidx[type]++] = p;
5916 vim_free(val);
5918 return viminfo_readline(virp);
5921 void
5922 finish_viminfo_history()
5924 int idx;
5925 int i;
5926 int type;
5928 for (type = 0; type < HIST_COUNT; ++type)
5930 if (history[type] == NULL)
5931 return;
5932 idx = hisidx[type] + viminfo_hisidx[type];
5933 if (idx >= hislen)
5934 idx -= hislen;
5935 else if (idx < 0)
5936 idx = hislen - 1;
5937 if (viminfo_add_at_front)
5938 hisidx[type] = idx;
5939 else
5941 if (hisidx[type] == -1)
5942 hisidx[type] = hislen - 1;
5945 if (history[type][idx].hisstr != NULL)
5946 break;
5947 if (++idx == hislen)
5948 idx = 0;
5949 } while (idx != hisidx[type]);
5950 if (idx != hisidx[type] && --idx < 0)
5951 idx = hislen - 1;
5953 for (i = 0; i < viminfo_hisidx[type]; i++)
5955 vim_free(history[type][idx].hisstr);
5956 history[type][idx].hisstr = viminfo_history[type][i];
5957 if (--idx < 0)
5958 idx = hislen - 1;
5960 idx += 1;
5961 idx %= hislen;
5962 for (i = 0; i < viminfo_hisidx[type]; i++)
5964 history[type][idx++].hisnum = ++hisnum[type];
5965 idx %= hislen;
5967 vim_free(viminfo_history[type]);
5968 viminfo_history[type] = NULL;
5972 void
5973 write_viminfo_history(fp)
5974 FILE *fp;
5976 int i;
5977 int type;
5978 int num_saved;
5979 char_u *p;
5980 int c;
5982 init_history();
5983 if (hislen == 0)
5984 return;
5985 for (type = 0; type < HIST_COUNT; ++type)
5987 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5988 if (num_saved == 0)
5989 continue;
5990 if (num_saved < 0) /* Use default */
5991 num_saved = hislen;
5992 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5993 type == HIST_CMD ? _("Command Line") :
5994 type == HIST_SEARCH ? _("Search String") :
5995 type == HIST_EXPR ? _("Expression") :
5996 _("Input Line"));
5997 if (num_saved > hislen)
5998 num_saved = hislen;
5999 i = hisidx[type];
6000 if (i >= 0)
6001 while (num_saved--)
6003 p = history[type][i].hisstr;
6004 if (p != NULL)
6006 fputc(hist_type2char(type, TRUE), fp);
6007 /* For the search history: put the separator in the second
6008 * column; use a space if there isn't one. */
6009 if (type == HIST_SEARCH)
6011 c = p[STRLEN(p) + 1];
6012 putc(c == NUL ? ' ' : c, fp);
6014 viminfo_writestring(fp, p);
6016 if (--i < 0)
6017 i = hislen - 1;
6021 #endif /* FEAT_VIMINFO */
6023 #if defined(FEAT_FKMAP) || defined(PROTO)
6025 * Write a character at the current cursor+offset position.
6026 * It is directly written into the command buffer block.
6028 void
6029 cmd_pchar(c, offset)
6030 int c, offset;
6032 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6034 EMSG(_("E198: cmd_pchar beyond the command length"));
6035 return;
6037 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6038 ccline.cmdbuff[ccline.cmdlen] = NUL;
6042 cmd_gchar(offset)
6043 int offset;
6045 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6047 /* EMSG(_("cmd_gchar beyond the command length")); */
6048 return NUL;
6050 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6052 #endif
6054 #if defined(FEAT_CMDWIN) || defined(PROTO)
6056 * Open a window on the current command line and history. Allow editing in
6057 * the window. Returns when the window is closed.
6058 * Returns:
6059 * CR if the command is to be executed
6060 * Ctrl_C if it is to be abandoned
6061 * K_IGNORE if editing continues
6063 static int
6064 ex_window()
6066 struct cmdline_info save_ccline;
6067 buf_T *old_curbuf = curbuf;
6068 win_T *old_curwin = curwin;
6069 buf_T *bp;
6070 win_T *wp;
6071 int i;
6072 linenr_T lnum;
6073 int histtype;
6074 garray_T winsizes;
6075 #ifdef FEAT_AUTOCMD
6076 char_u typestr[2];
6077 #endif
6078 int save_restart_edit = restart_edit;
6079 int save_State = State;
6080 int save_exmode = exmode_active;
6081 #ifdef FEAT_RIGHTLEFT
6082 int save_cmdmsg_rl = cmdmsg_rl;
6083 #endif
6085 /* Can't do this recursively. Can't do it when typing a password. */
6086 if (cmdwin_type != 0
6087 # if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6088 || cmdline_star > 0
6089 # endif
6092 beep_flush();
6093 return K_IGNORE;
6096 /* Save current window sizes. */
6097 win_size_save(&winsizes);
6099 # ifdef FEAT_AUTOCMD
6100 /* Don't execute autocommands while creating the window. */
6101 block_autocmds();
6102 # endif
6103 /* don't use a new tab page */
6104 cmdmod.tab = 0;
6106 /* Create a window for the command-line buffer. */
6107 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6109 beep_flush();
6110 # ifdef FEAT_AUTOCMD
6111 unblock_autocmds();
6112 # endif
6113 return K_IGNORE;
6115 cmdwin_type = get_cmdline_type();
6117 /* Create the command-line buffer empty. */
6118 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
6119 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
6120 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6121 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6122 curbuf->b_p_ma = TRUE;
6123 #ifdef FEAT_FOLDING
6124 curwin->w_p_fen = FALSE;
6125 #endif
6126 # ifdef FEAT_RIGHTLEFT
6127 curwin->w_p_rl = cmdmsg_rl;
6128 cmdmsg_rl = FALSE;
6129 # endif
6130 # ifdef FEAT_SCROLLBIND
6131 curwin->w_p_scb = FALSE;
6132 # endif
6134 # ifdef FEAT_AUTOCMD
6135 /* Do execute autocommands for setting the filetype (load syntax). */
6136 unblock_autocmds();
6137 # endif
6139 /* Showing the prompt may have set need_wait_return, reset it. */
6140 need_wait_return = FALSE;
6142 histtype = hist_char2type(cmdwin_type);
6143 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6145 if (p_wc == TAB)
6147 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6148 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6150 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6153 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6154 * sets 'textwidth' to 78). */
6155 curbuf->b_p_tw = 0;
6157 /* Fill the buffer with the history. */
6158 init_history();
6159 if (hislen > 0)
6161 i = hisidx[histtype];
6162 if (i >= 0)
6164 lnum = 0;
6167 if (++i == hislen)
6168 i = 0;
6169 if (history[histtype][i].hisstr != NULL)
6170 ml_append(lnum++, history[histtype][i].hisstr,
6171 (colnr_T)0, FALSE);
6173 while (i != hisidx[histtype]);
6177 /* Replace the empty last line with the current command-line and put the
6178 * cursor there. */
6179 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6180 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6181 curwin->w_cursor.col = ccline.cmdpos;
6182 changed_line_abv_curs();
6183 invalidate_botline();
6184 redraw_later(SOME_VALID);
6186 /* Save the command line info, can be used recursively. */
6187 save_ccline = ccline;
6188 ccline.cmdbuff = NULL;
6189 ccline.cmdprompt = NULL;
6191 /* No Ex mode here! */
6192 exmode_active = 0;
6194 State = NORMAL;
6195 # ifdef FEAT_MOUSE
6196 setmouse();
6197 # endif
6199 # ifdef FEAT_AUTOCMD
6200 /* Trigger CmdwinEnter autocommands. */
6201 typestr[0] = cmdwin_type;
6202 typestr[1] = NUL;
6203 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
6204 if (restart_edit != 0) /* autocmd with ":startinsert" */
6205 stuffcharReadbuff(K_NOP);
6206 # endif
6208 i = RedrawingDisabled;
6209 RedrawingDisabled = 0;
6212 * Call the main loop until <CR> or CTRL-C is typed.
6214 cmdwin_result = 0;
6215 main_loop(TRUE, FALSE);
6217 RedrawingDisabled = i;
6219 # ifdef FEAT_AUTOCMD
6220 /* Trigger CmdwinLeave autocommands. */
6221 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6222 # endif
6224 /* Restore the command line info. */
6225 ccline = save_ccline;
6226 cmdwin_type = 0;
6228 exmode_active = save_exmode;
6230 /* Safety check: The old window or buffer was deleted: It's a bug when
6231 * this happens! */
6232 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6234 cmdwin_result = Ctrl_C;
6235 EMSG(_("E199: Active window or buffer deleted"));
6237 else
6239 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6240 /* autocmds may abort script processing */
6241 if (aborting() && cmdwin_result != K_IGNORE)
6242 cmdwin_result = Ctrl_C;
6243 # endif
6244 /* Set the new command line from the cmdline buffer. */
6245 vim_free(ccline.cmdbuff);
6246 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
6248 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6250 if (histtype == HIST_CMD)
6252 /* Execute the command directly. */
6253 ccline.cmdbuff = vim_strsave((char_u *)p);
6254 cmdwin_result = CAR;
6256 else
6258 /* First need to cancel what we were doing. */
6259 ccline.cmdbuff = NULL;
6260 stuffcharReadbuff(':');
6261 stuffReadbuff((char_u *)p);
6262 stuffcharReadbuff(CAR);
6265 else if (cmdwin_result == K_XF2) /* :qa typed */
6267 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6268 cmdwin_result = CAR;
6270 else
6271 ccline.cmdbuff = vim_strsave(ml_get_curline());
6272 if (ccline.cmdbuff == NULL)
6273 cmdwin_result = Ctrl_C;
6274 else
6276 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6277 ccline.cmdbufflen = ccline.cmdlen + 1;
6278 ccline.cmdpos = curwin->w_cursor.col;
6279 if (ccline.cmdpos > ccline.cmdlen)
6280 ccline.cmdpos = ccline.cmdlen;
6281 if (cmdwin_result == K_IGNORE)
6283 set_cmdspos_cursor();
6284 redrawcmd();
6288 # ifdef FEAT_AUTOCMD
6289 /* Don't execute autocommands while deleting the window. */
6290 block_autocmds();
6291 # endif
6292 wp = curwin;
6293 bp = curbuf;
6294 win_goto(old_curwin);
6295 win_close(wp, TRUE);
6297 /* win_close() may have already wiped the buffer when 'bh' is
6298 * set to 'wipe' */
6299 if (buf_valid(bp))
6300 close_buffer(NULL, bp, DOBUF_WIPE);
6302 /* Restore window sizes. */
6303 win_size_restore(&winsizes);
6305 # ifdef FEAT_AUTOCMD
6306 unblock_autocmds();
6307 # endif
6310 ga_clear(&winsizes);
6311 restart_edit = save_restart_edit;
6312 # ifdef FEAT_RIGHTLEFT
6313 cmdmsg_rl = save_cmdmsg_rl;
6314 # endif
6316 State = save_State;
6317 # ifdef FEAT_MOUSE
6318 setmouse();
6319 # endif
6321 return cmdwin_result;
6323 #endif /* FEAT_CMDWIN */
6326 * Used for commands that either take a simple command string argument, or:
6327 * cmd << endmarker
6328 * {script}
6329 * endmarker
6330 * Returns a pointer to allocated memory with {script} or NULL.
6332 char_u *
6333 script_get(eap, cmd)
6334 exarg_T *eap;
6335 char_u *cmd;
6337 char_u *theline;
6338 char *end_pattern = NULL;
6339 char dot[] = ".";
6340 garray_T ga;
6342 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6343 return NULL;
6345 ga_init2(&ga, 1, 0x400);
6347 if (cmd[2] != NUL)
6348 end_pattern = (char *)skipwhite(cmd + 2);
6349 else
6350 end_pattern = dot;
6352 for (;;)
6354 theline = eap->getline(
6355 #ifdef FEAT_EVAL
6356 eap->cstack->cs_looplevel > 0 ? -1 :
6357 #endif
6358 NUL, eap->cookie, 0);
6360 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6362 vim_free(theline);
6363 break;
6366 ga_concat(&ga, theline);
6367 ga_append(&ga, '\n');
6368 vim_free(theline);
6370 ga_append(&ga, NUL);
6372 return (char_u *)ga.ga_data;