Merge branch 'master' of git://repo.or.cz/MacVim into KaoriYa
[MacVim/KaoriYa.git] / src / ex_getln.c
blob7591214c8a2f9e99996492e5f34a556077976215
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 j = (mch_isdir(files_found[k]));
3973 if (showtail)
3974 p = L_SHOWFILE(k);
3975 else
3977 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3978 TRUE);
3979 p = NameBuff;
3982 else
3984 j = FALSE;
3985 p = L_SHOWFILE(k);
3987 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3989 if (msg_col > 0) /* when not wrapped around */
3991 msg_clr_eos();
3992 msg_putchar('\n');
3994 out_flush(); /* show one line at a time */
3995 if (got_int)
3997 got_int = FALSE;
3998 break;
4003 * we redraw the command below the lines that we have just listed
4004 * This is a bit tricky, but it saves a lot of screen updating.
4006 cmdline_row = msg_row; /* will put it back later */
4009 if (xp->xp_numfiles == -1)
4010 FreeWild(num_files, files_found);
4012 return EXPAND_OK;
4016 * Private gettail for showmatches() (and win_redr_status_matches()):
4017 * Find tail of file name path, but ignore trailing "/".
4019 char_u *
4020 sm_gettail(s)
4021 char_u *s;
4023 char_u *p;
4024 char_u *t = s;
4025 int had_sep = FALSE;
4027 for (p = s; *p != NUL; )
4029 if (vim_ispathsep(*p)
4030 #ifdef BACKSLASH_IN_FILENAME
4031 && !rem_backslash(p)
4032 #endif
4034 had_sep = TRUE;
4035 else if (had_sep)
4037 t = p;
4038 had_sep = FALSE;
4040 mb_ptr_adv(p);
4042 return t;
4046 * Return TRUE if we only need to show the tail of completion matches.
4047 * When not completing file names or there is a wildcard in the path FALSE is
4048 * returned.
4050 static int
4051 expand_showtail(xp)
4052 expand_T *xp;
4054 char_u *s;
4055 char_u *end;
4057 /* When not completing file names a "/" may mean something different. */
4058 if (xp->xp_context != EXPAND_FILES
4059 && xp->xp_context != EXPAND_SHELLCMD
4060 && xp->xp_context != EXPAND_DIRECTORIES)
4061 return FALSE;
4063 end = gettail(xp->xp_pattern);
4064 if (end == xp->xp_pattern) /* there is no path separator */
4065 return FALSE;
4067 for (s = xp->xp_pattern; s < end; s++)
4069 /* Skip escaped wildcards. Only when the backslash is not a path
4070 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4071 if (rem_backslash(s))
4072 ++s;
4073 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4074 return FALSE;
4076 return TRUE;
4080 * Prepare a string for expansion.
4081 * When expanding file names: The string will be used with expand_wildcards().
4082 * Copy the file name into allocated memory and add a '*' at the end.
4083 * When expanding other names: The string will be used with regcomp(). Copy
4084 * the name into allocated memory and prepend "^".
4086 char_u *
4087 addstar(fname, len, context)
4088 char_u *fname;
4089 int len;
4090 int context; /* EXPAND_FILES etc. */
4092 char_u *retval;
4093 int i, j;
4094 int new_len;
4095 char_u *tail;
4097 if (context != EXPAND_FILES
4098 && context != EXPAND_SHELLCMD
4099 && context != EXPAND_DIRECTORIES)
4102 * Matching will be done internally (on something other than files).
4103 * So we convert the file-matching-type wildcards into our kind for
4104 * use with vim_regcomp(). First work out how long it will be:
4107 /* For help tags the translation is done in find_help_tags().
4108 * For a tag pattern starting with "/" no translation is needed. */
4109 if (context == EXPAND_HELP
4110 || context == EXPAND_COLORS
4111 || context == EXPAND_COMPILER
4112 || (context == EXPAND_TAGS && fname[0] == '/'))
4113 retval = vim_strnsave(fname, len);
4114 else
4116 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4117 for (i = 0; i < len; i++)
4119 if (fname[i] == '*' || fname[i] == '~')
4120 new_len++; /* '*' needs to be replaced by ".*"
4121 '~' needs to be replaced by "\~" */
4123 /* Buffer names are like file names. "." should be literal */
4124 if (context == EXPAND_BUFFERS && fname[i] == '.')
4125 new_len++; /* "." becomes "\." */
4127 /* Custom expansion takes care of special things, match
4128 * backslashes literally (perhaps also for other types?) */
4129 if ((context == EXPAND_USER_DEFINED
4130 || context == EXPAND_USER_LIST) && fname[i] == '\\')
4131 new_len++; /* '\' becomes "\\" */
4133 retval = alloc(new_len);
4134 if (retval != NULL)
4136 retval[0] = '^';
4137 j = 1;
4138 for (i = 0; i < len; i++, j++)
4140 /* Skip backslash. But why? At least keep it for custom
4141 * expansion. */
4142 if (context != EXPAND_USER_DEFINED
4143 && context != EXPAND_USER_LIST
4144 && fname[i] == '\\'
4145 && ++i == len)
4146 break;
4148 switch (fname[i])
4150 case '*': retval[j++] = '.';
4151 break;
4152 case '~': retval[j++] = '\\';
4153 break;
4154 case '?': retval[j] = '.';
4155 continue;
4156 case '.': if (context == EXPAND_BUFFERS)
4157 retval[j++] = '\\';
4158 break;
4159 case '\\': if (context == EXPAND_USER_DEFINED
4160 || context == EXPAND_USER_LIST)
4161 retval[j++] = '\\';
4162 break;
4164 retval[j] = fname[i];
4166 retval[j] = NUL;
4170 else
4172 retval = alloc(len + 4);
4173 if (retval != NULL)
4175 vim_strncpy(retval, fname, len);
4178 * Don't add a star to *, ~, ~user, $var or `cmd`.
4179 * * would become **, which walks the whole tree.
4180 * ~ would be at the start of the file name, but not the tail.
4181 * $ could be anywhere in the tail.
4182 * ` could be anywhere in the file name.
4183 * When the name ends in '$' don't add a star, remove the '$'.
4185 tail = gettail(retval);
4186 if ((*retval != '~' || tail != retval)
4187 && (len == 0 || retval[len - 1] != '*')
4188 && vim_strchr(tail, '$') == NULL
4189 && vim_strchr(retval, '`') == NULL)
4190 retval[len++] = '*';
4191 else if (len > 0 && retval[len - 1] == '$')
4192 --len;
4193 retval[len] = NUL;
4196 return retval;
4200 * Must parse the command line so far to work out what context we are in.
4201 * Completion can then be done based on that context.
4202 * This routine sets the variables:
4203 * xp->xp_pattern The start of the pattern to be expanded within
4204 * the command line (ends at the cursor).
4205 * xp->xp_context The type of thing to expand. Will be one of:
4207 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4208 * the command line, like an unknown command. Caller
4209 * should beep.
4210 * EXPAND_NOTHING Unrecognised context for completion, use char like
4211 * a normal char, rather than for completion. eg
4212 * :s/^I/
4213 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4214 * it.
4215 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4216 * EXPAND_FILES After command with XFILE set, or after setting
4217 * with P_EXPAND set. eg :e ^I, :w>>^I
4218 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4219 * when we know only directories are of interest. eg
4220 * :set dir=^I
4221 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
4222 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4223 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4224 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4225 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4226 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4227 * EXPAND_EVENTS Complete event names
4228 * EXPAND_SYNTAX Complete :syntax command arguments
4229 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4230 * EXPAND_AUGROUP Complete autocommand group names
4231 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4232 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4233 * eg :unmap a^I , :cunab x^I
4234 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4235 * eg :call sub^I
4236 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4237 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4238 * names in expressions, eg :while s^I
4239 * EXPAND_ENV_VARS Complete environment variable names
4241 static void
4242 set_expand_context(xp)
4243 expand_T *xp;
4245 /* only expansion for ':', '>' and '=' command-lines */
4246 if (ccline.cmdfirstc != ':'
4247 #ifdef FEAT_EVAL
4248 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
4249 && !ccline.input_fn
4250 #endif
4253 xp->xp_context = EXPAND_NOTHING;
4254 return;
4256 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4259 void
4260 set_cmd_context(xp, str, len, col)
4261 expand_T *xp;
4262 char_u *str; /* start of command line */
4263 int len; /* length of command line (excl. NUL) */
4264 int col; /* position of cursor */
4266 int old_char = NUL;
4267 char_u *nextcomm;
4270 * Avoid a UMR warning from Purify, only save the character if it has been
4271 * written before.
4273 if (col < len)
4274 old_char = str[col];
4275 str[col] = NUL;
4276 nextcomm = str;
4278 #ifdef FEAT_EVAL
4279 if (ccline.cmdfirstc == '=')
4281 # ifdef FEAT_CMDL_COMPL
4282 /* pass CMD_SIZE because there is no real command */
4283 set_context_for_expression(xp, str, CMD_SIZE);
4284 # endif
4286 else if (ccline.input_fn)
4288 xp->xp_context = ccline.xp_context;
4289 xp->xp_pattern = ccline.cmdbuff;
4290 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
4291 xp->xp_arg = ccline.xp_arg;
4292 # endif
4294 else
4295 #endif
4296 while (nextcomm != NULL)
4297 nextcomm = set_one_cmd_context(xp, nextcomm);
4299 str[col] = old_char;
4303 * Expand the command line "str" from context "xp".
4304 * "xp" must have been set by set_cmd_context().
4305 * xp->xp_pattern points into "str", to where the text that is to be expanded
4306 * starts.
4307 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4308 * cursor.
4309 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4310 * key that triggered expansion literally.
4311 * Returns EXPAND_OK otherwise.
4314 expand_cmdline(xp, str, col, matchcount, matches)
4315 expand_T *xp;
4316 char_u *str; /* start of command line */
4317 int col; /* position of cursor */
4318 int *matchcount; /* return: nr of matches */
4319 char_u ***matches; /* return: array of pointers to matches */
4321 char_u *file_str = NULL;
4323 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4325 beep_flush();
4326 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4328 if (xp->xp_context == EXPAND_NOTHING)
4330 /* Caller can use the character as a normal char instead */
4331 return EXPAND_NOTHING;
4334 /* add star to file name, or convert to regexp if not exp. files. */
4335 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4336 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
4337 if (file_str == NULL)
4338 return EXPAND_UNSUCCESSFUL;
4340 /* find all files that match the description */
4341 if (ExpandFromContext(xp, file_str, matchcount, matches,
4342 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4344 *matchcount = 0;
4345 *matches = NULL;
4347 vim_free(file_str);
4349 return EXPAND_OK;
4352 #ifdef FEAT_MULTI_LANG
4354 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4356 static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4358 static void
4359 cleanup_help_tags(num_file, file)
4360 int num_file;
4361 char_u **file;
4363 int i, j;
4364 int len;
4366 for (i = 0; i < num_file; ++i)
4368 len = (int)STRLEN(file[i]) - 3;
4369 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4371 /* Sorting on priority means the same item in another language may
4372 * be anywhere. Search all items for a match up to the "@en". */
4373 for (j = 0; j < num_file; ++j)
4374 if (j != i
4375 && (int)STRLEN(file[j]) == len + 3
4376 && STRNCMP(file[i], file[j], len + 1) == 0)
4377 break;
4378 if (j == num_file)
4379 file[i][len] = NUL;
4383 #endif
4386 * Do the expansion based on xp->xp_context and "pat".
4388 static int
4389 ExpandFromContext(xp, pat, num_file, file, options)
4390 expand_T *xp;
4391 char_u *pat;
4392 int *num_file;
4393 char_u ***file;
4394 int options;
4396 #ifdef FEAT_CMDL_COMPL
4397 regmatch_T regmatch;
4398 #endif
4399 int ret;
4400 int flags;
4402 flags = EW_DIR; /* include directories */
4403 if (options & WILD_LIST_NOTFOUND)
4404 flags |= EW_NOTFOUND;
4405 if (options & WILD_ADD_SLASH)
4406 flags |= EW_ADDSLASH;
4407 if (options & WILD_KEEP_ALL)
4408 flags |= EW_KEEPALL;
4409 if (options & WILD_SILENT)
4410 flags |= EW_SILENT;
4412 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4415 * Expand file or directory names.
4417 int free_pat = FALSE;
4418 int i;
4420 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4421 * space */
4422 if (xp->xp_backslash != XP_BS_NONE)
4424 free_pat = TRUE;
4425 pat = vim_strsave(pat);
4426 for (i = 0; pat[i]; ++i)
4427 if (pat[i] == '\\')
4429 if (xp->xp_backslash == XP_BS_THREE
4430 && pat[i + 1] == '\\'
4431 && pat[i + 2] == '\\'
4432 && pat[i + 3] == ' ')
4433 STRMOVE(pat + i, pat + i + 3);
4434 if (xp->xp_backslash == XP_BS_ONE
4435 && pat[i + 1] == ' ')
4436 STRMOVE(pat + i, pat + i + 1);
4440 if (xp->xp_context == EXPAND_FILES)
4441 flags |= EW_FILE;
4442 else
4443 flags = (flags | EW_DIR) & ~EW_FILE;
4444 /* Expand wildcards, supporting %:h and the like. */
4445 ret = expand_wildcards_eval(&pat, num_file, file, flags);
4446 if (free_pat)
4447 vim_free(pat);
4448 return ret;
4451 *file = (char_u **)"";
4452 *num_file = 0;
4453 if (xp->xp_context == EXPAND_HELP)
4455 /* With an empty argument we would get all the help tags, which is
4456 * very slow. Get matches for "help" instead. */
4457 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4458 num_file, file, FALSE) == OK)
4460 #ifdef FEAT_MULTI_LANG
4461 cleanup_help_tags(*num_file, *file);
4462 #endif
4463 return OK;
4465 return FAIL;
4468 #ifndef FEAT_CMDL_COMPL
4469 return FAIL;
4470 #else
4471 if (xp->xp_context == EXPAND_SHELLCMD)
4472 return expand_shellcmd(pat, num_file, file, flags);
4473 if (xp->xp_context == EXPAND_OLD_SETTING)
4474 return ExpandOldSetting(num_file, file);
4475 if (xp->xp_context == EXPAND_BUFFERS)
4476 return ExpandBufnames(pat, num_file, file, options);
4477 if (xp->xp_context == EXPAND_TAGS
4478 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4479 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4480 if (xp->xp_context == EXPAND_COLORS)
4481 return ExpandRTDir(pat, num_file, file, "colors");
4482 if (xp->xp_context == EXPAND_COMPILER)
4483 return ExpandRTDir(pat, num_file, file, "compiler");
4484 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4485 if (xp->xp_context == EXPAND_USER_LIST)
4486 return ExpandUserList(xp, num_file, file);
4487 # endif
4489 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4490 if (regmatch.regprog == NULL)
4491 return FAIL;
4493 /* set ignore-case according to p_ic, p_scs and pat */
4494 regmatch.rm_ic = ignorecase(pat);
4496 if (xp->xp_context == EXPAND_SETTINGS
4497 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4498 ret = ExpandSettings(xp, &regmatch, num_file, file);
4499 else if (xp->xp_context == EXPAND_MAPPINGS)
4500 ret = ExpandMappings(&regmatch, num_file, file);
4501 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4502 else if (xp->xp_context == EXPAND_USER_DEFINED)
4503 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4504 # endif
4505 else
4507 static struct expgen
4509 int context;
4510 char_u *((*func)__ARGS((expand_T *, int)));
4511 int ic;
4512 } tab[] =
4514 {EXPAND_COMMANDS, get_command_name, FALSE},
4515 #ifdef FEAT_USR_CMDS
4516 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4517 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4518 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4519 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4520 #endif
4521 #ifdef FEAT_EVAL
4522 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4523 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4524 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4525 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4526 #endif
4527 #ifdef FEAT_MENU
4528 {EXPAND_MENUS, get_menu_name, FALSE},
4529 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4530 #endif
4531 #ifdef FEAT_SYN_HL
4532 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4533 #endif
4534 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4535 #ifdef FEAT_AUTOCMD
4536 {EXPAND_EVENTS, get_event_name, TRUE},
4537 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4538 #endif
4539 #ifdef FEAT_CSCOPE
4540 {EXPAND_CSCOPE, get_cscope_name, TRUE},
4541 #endif
4542 #ifdef FEAT_SIGNS
4543 {EXPAND_SIGN, get_sign_name, TRUE},
4544 #endif
4545 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4546 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4547 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4548 #endif
4549 {EXPAND_ENV_VARS, get_env_name, TRUE},
4550 #ifdef FEAT_GUI_MACVIM
4551 {EXPAND_MACACTION, get_macaction_name, FALSE},
4552 #endif
4554 int i;
4557 * Find a context in the table and call the ExpandGeneric() with the
4558 * right function to do the expansion.
4560 ret = FAIL;
4561 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
4562 if (xp->xp_context == tab[i].context)
4564 if (tab[i].ic)
4565 regmatch.rm_ic = TRUE;
4566 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4567 break;
4571 vim_free(regmatch.regprog);
4573 return ret;
4574 #endif /* FEAT_CMDL_COMPL */
4577 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4579 * Expand a list of names.
4581 * Generic function for command line completion. It calls a function to
4582 * obtain strings, one by one. The strings are matched against a regexp
4583 * program. Matching strings are copied into an array, which is returned.
4585 * Returns OK when no problems encountered, FAIL for error (out of memory).
4588 ExpandGeneric(xp, regmatch, num_file, file, func)
4589 expand_T *xp;
4590 regmatch_T *regmatch;
4591 int *num_file;
4592 char_u ***file;
4593 char_u *((*func)__ARGS((expand_T *, int)));
4594 /* returns a string from the list */
4596 int i;
4597 int count = 0;
4598 int round;
4599 char_u *str;
4601 /* do this loop twice:
4602 * round == 0: count the number of matching names
4603 * round == 1: copy the matching names into allocated memory
4605 for (round = 0; round <= 1; ++round)
4607 for (i = 0; ; ++i)
4609 str = (*func)(xp, i);
4610 if (str == NULL) /* end of list */
4611 break;
4612 if (*str == NUL) /* skip empty strings */
4613 continue;
4615 if (vim_regexec(regmatch, str, (colnr_T)0))
4617 if (round)
4619 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4620 (*file)[count] = str;
4621 #ifdef FEAT_MENU
4622 if (func == get_menu_names && str != NULL)
4624 /* test for separator added by get_menu_names() */
4625 str += STRLEN(str) - 1;
4626 if (*str == '\001')
4627 *str = '.';
4629 #endif
4631 ++count;
4634 if (round == 0)
4636 if (count == 0)
4637 return OK;
4638 *num_file = count;
4639 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4640 if (*file == NULL)
4642 *file = (char_u **)"";
4643 return FAIL;
4645 count = 0;
4649 /* Sort the results. Keep menu's in the specified order. */
4650 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4651 sort_strings(*file, *num_file);
4653 #ifdef FEAT_CMDL_COMPL
4654 /* Reset the variables used for special highlight names expansion, so that
4655 * they don't show up when getting normal highlight names by ID. */
4656 reset_expand_highlight();
4657 #endif
4659 return OK;
4663 * Complete a shell command.
4664 * Returns FAIL or OK;
4666 static int
4667 expand_shellcmd(filepat, num_file, file, flagsarg)
4668 char_u *filepat; /* pattern to match with command names */
4669 int *num_file; /* return: number of matches */
4670 char_u ***file; /* return: array with matches */
4671 int flagsarg; /* EW_ flags */
4673 char_u *pat;
4674 int i;
4675 char_u *path;
4676 int mustfree = FALSE;
4677 garray_T ga;
4678 char_u *buf = alloc(MAXPATHL);
4679 size_t l;
4680 char_u *s, *e;
4681 int flags = flagsarg;
4682 int ret;
4684 if (buf == NULL)
4685 return FAIL;
4687 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4688 * space */
4689 pat = vim_strsave(filepat);
4690 for (i = 0; pat[i]; ++i)
4691 if (pat[i] == '\\' && pat[i + 1] == ' ')
4692 STRMOVE(pat + i, pat + i + 1);
4694 flags |= EW_FILE | EW_EXEC;
4696 /* For an absolute name we don't use $PATH. */
4697 if (mch_isFullName(pat))
4698 path = (char_u *)" ";
4699 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
4700 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4701 path = (char_u *)".";
4702 else
4703 path = vim_getenv((char_u *)"PATH", &mustfree);
4706 * Go over all directories in $PATH. Expand matches in that directory and
4707 * collect them in "ga".
4709 ga_init2(&ga, (int)sizeof(char *), 10);
4710 for (s = path; *s != NUL; s = e)
4712 if (*s == ' ')
4713 ++s; /* Skip space used for absolute path name. */
4715 #if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4716 e = vim_strchr(s, ';');
4717 #else
4718 e = vim_strchr(s, ':');
4719 #endif
4720 if (e == NULL)
4721 e = s + STRLEN(s);
4723 l = e - s;
4724 if (l > MAXPATHL - 5)
4725 break;
4726 vim_strncpy(buf, s, l);
4727 add_pathsep(buf);
4728 l = STRLEN(buf);
4729 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4731 /* Expand matches in one directory of $PATH. */
4732 ret = expand_wildcards(1, &buf, num_file, file, flags);
4733 if (ret == OK)
4735 if (ga_grow(&ga, *num_file) == FAIL)
4736 FreeWild(*num_file, *file);
4737 else
4739 for (i = 0; i < *num_file; ++i)
4741 s = (*file)[i];
4742 if (STRLEN(s) > l)
4744 /* Remove the path again. */
4745 STRMOVE(s, s + l);
4746 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4748 else
4749 vim_free(s);
4751 vim_free(*file);
4754 if (*e != NUL)
4755 ++e;
4757 *file = ga.ga_data;
4758 *num_file = ga.ga_len;
4760 vim_free(buf);
4761 vim_free(pat);
4762 if (mustfree)
4763 vim_free(path);
4764 return OK;
4768 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4769 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));
4772 * Call "user_expand_func()" to invoke a user defined VimL function and return
4773 * the result (either a string or a List).
4775 static void *
4776 call_user_expand_func(user_expand_func, xp, num_file, file)
4777 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
4778 expand_T *xp;
4779 int *num_file;
4780 char_u ***file;
4782 char_u keep;
4783 char_u num[50];
4784 char_u *args[3];
4785 int save_current_SID = current_SID;
4786 void *ret;
4787 struct cmdline_info save_ccline;
4789 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
4790 return NULL;
4791 *num_file = 0;
4792 *file = NULL;
4794 if (ccline.cmdbuff == NULL)
4796 /* Completion from Insert mode, pass fake arguments. */
4797 keep = 0;
4798 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
4799 args[1] = xp->xp_pattern;
4801 else
4803 /* Completion on the command line, pass real arguments. */
4804 keep = ccline.cmdbuff[ccline.cmdlen];
4805 ccline.cmdbuff[ccline.cmdlen] = 0;
4806 sprintf((char *)num, "%d", ccline.cmdpos);
4807 args[1] = ccline.cmdbuff;
4809 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
4810 args[2] = num;
4812 /* Save the cmdline, we don't know what the function may do. */
4813 save_ccline = ccline;
4814 ccline.cmdbuff = NULL;
4815 ccline.cmdprompt = NULL;
4816 current_SID = xp->xp_scriptID;
4818 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
4820 ccline = save_ccline;
4821 current_SID = save_current_SID;
4822 if (ccline.cmdbuff != NULL)
4823 ccline.cmdbuff[ccline.cmdlen] = keep;
4825 vim_free(args[0]);
4826 return ret;
4830 * Expand names with a function defined by the user.
4832 static int
4833 ExpandUserDefined(xp, regmatch, num_file, file)
4834 expand_T *xp;
4835 regmatch_T *regmatch;
4836 int *num_file;
4837 char_u ***file;
4839 char_u *retstr;
4840 char_u *s;
4841 char_u *e;
4842 char_u keep;
4843 garray_T ga;
4845 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4846 if (retstr == NULL)
4847 return FAIL;
4849 ga_init2(&ga, (int)sizeof(char *), 3);
4850 for (s = retstr; *s != NUL; s = e)
4852 e = vim_strchr(s, '\n');
4853 if (e == NULL)
4854 e = s + STRLEN(s);
4855 keep = *e;
4856 *e = 0;
4858 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4860 *e = keep;
4861 if (*e != NUL)
4862 ++e;
4863 continue;
4866 if (ga_grow(&ga, 1) == FAIL)
4867 break;
4869 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4870 ++ga.ga_len;
4872 *e = keep;
4873 if (*e != NUL)
4874 ++e;
4876 vim_free(retstr);
4877 *file = ga.ga_data;
4878 *num_file = ga.ga_len;
4879 return OK;
4883 * Expand names with a list returned by a function defined by the user.
4885 static int
4886 ExpandUserList(xp, num_file, file)
4887 expand_T *xp;
4888 int *num_file;
4889 char_u ***file;
4891 list_T *retlist;
4892 listitem_T *li;
4893 garray_T ga;
4895 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4896 if (retlist == NULL)
4897 return FAIL;
4899 ga_init2(&ga, (int)sizeof(char *), 3);
4900 /* Loop over the items in the list. */
4901 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4903 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
4904 continue; /* Skip non-string items and empty strings */
4906 if (ga_grow(&ga, 1) == FAIL)
4907 break;
4909 ((char_u **)ga.ga_data)[ga.ga_len] =
4910 vim_strsave(li->li_tv.vval.v_string);
4911 ++ga.ga_len;
4913 list_unref(retlist);
4915 *file = ga.ga_data;
4916 *num_file = ga.ga_len;
4917 return OK;
4919 #endif
4922 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4923 * or compiler names.
4925 static int
4926 ExpandRTDir(pat, num_file, file, dirname)
4927 char_u *pat;
4928 int *num_file;
4929 char_u ***file;
4930 char *dirname; /* "colors" or "compiler" */
4932 char_u *all;
4933 char_u *s;
4934 char_u *e;
4935 garray_T ga;
4937 *num_file = 0;
4938 *file = NULL;
4939 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4940 if (s == NULL)
4941 return FAIL;
4942 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4943 all = globpath(p_rtp, s, 0);
4944 vim_free(s);
4945 if (all == NULL)
4946 return FAIL;
4948 ga_init2(&ga, (int)sizeof(char *), 3);
4949 for (s = all; *s != NUL; s = e)
4951 e = vim_strchr(s, '\n');
4952 if (e == NULL)
4953 e = s + STRLEN(s);
4954 if (ga_grow(&ga, 1) == FAIL)
4955 break;
4956 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4958 for (s = e - 4; s > all; mb_ptr_back(all, s))
4959 if (*s == '\n' || vim_ispathsep(*s))
4960 break;
4961 ++s;
4962 ((char_u **)ga.ga_data)[ga.ga_len] =
4963 vim_strnsave(s, (int)(e - s - 4));
4964 ++ga.ga_len;
4966 if (*e != NUL)
4967 ++e;
4969 vim_free(all);
4970 *file = ga.ga_data;
4971 *num_file = ga.ga_len;
4972 return OK;
4975 #endif
4977 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4979 * Expand "file" for all comma-separated directories in "path".
4980 * Returns an allocated string with all matches concatenated, separated by
4981 * newlines. Returns NULL for an error or no matches.
4983 char_u *
4984 globpath(path, file, expand_options)
4985 char_u *path;
4986 char_u *file;
4987 int expand_options;
4989 expand_T xpc;
4990 char_u *buf;
4991 garray_T ga;
4992 int i;
4993 int len;
4994 int num_p;
4995 char_u **p;
4996 char_u *cur = NULL;
4998 buf = alloc(MAXPATHL);
4999 if (buf == NULL)
5000 return NULL;
5002 ExpandInit(&xpc);
5003 xpc.xp_context = EXPAND_FILES;
5005 ga_init2(&ga, 1, 100);
5007 /* Loop over all entries in {path}. */
5008 while (*path != NUL)
5010 /* Copy one item of the path to buf[] and concatenate the file name. */
5011 copy_option_part(&path, buf, MAXPATHL, ",");
5012 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5014 add_pathsep(buf);
5015 STRCAT(buf, file);
5016 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5017 WILD_SILENT|expand_options) != FAIL && num_p > 0)
5019 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
5020 for (len = 0, i = 0; i < num_p; ++i)
5021 len += (int)STRLEN(p[i]) + 1;
5023 /* Concatenate new results to previous ones. */
5024 if (ga_grow(&ga, len) == OK)
5026 cur = (char_u *)ga.ga_data + ga.ga_len;
5027 for (i = 0; i < num_p; ++i)
5029 STRCPY(cur, p[i]);
5030 cur += STRLEN(p[i]);
5031 *cur++ = '\n';
5033 ga.ga_len += len;
5035 FreeWild(num_p, p);
5039 if (cur != NULL)
5040 *--cur = 0; /* Replace trailing newline with NUL */
5042 vim_free(buf);
5043 return (char_u *)ga.ga_data;
5046 #endif
5048 #if defined(FEAT_CMDHIST) || defined(PROTO)
5050 /*********************************
5051 * Command line history stuff *
5052 *********************************/
5055 * Translate a history character to the associated type number.
5057 static int
5058 hist_char2type(c)
5059 int c;
5061 if (c == ':')
5062 return HIST_CMD;
5063 if (c == '=')
5064 return HIST_EXPR;
5065 if (c == '@')
5066 return HIST_INPUT;
5067 if (c == '>')
5068 return HIST_DEBUG;
5069 return HIST_SEARCH; /* must be '?' or '/' */
5073 * Table of history names.
5074 * These names are used in :history and various hist...() functions.
5075 * It is sufficient to give the significant prefix of a history name.
5078 static char *(history_names[]) =
5080 "cmd",
5081 "search",
5082 "expr",
5083 "input",
5084 "debug",
5085 NULL
5089 * init_history() - Initialize the command line history.
5090 * Also used to re-allocate the history when the size changes.
5092 void
5093 init_history()
5095 int newlen; /* new length of history table */
5096 histentry_T *temp;
5097 int i;
5098 int j;
5099 int type;
5102 * If size of history table changed, reallocate it
5104 newlen = (int)p_hi;
5105 if (newlen != hislen) /* history length changed */
5107 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5109 if (newlen)
5111 temp = (histentry_T *)lalloc(
5112 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5113 if (temp == NULL) /* out of memory! */
5115 if (type == 0) /* first one: just keep the old length */
5117 newlen = hislen;
5118 break;
5120 /* Already changed one table, now we can only have zero
5121 * length for all tables. */
5122 newlen = 0;
5123 type = -1;
5124 continue;
5127 else
5128 temp = NULL;
5129 if (newlen == 0 || temp != NULL)
5131 if (hisidx[type] < 0) /* there are no entries yet */
5133 for (i = 0; i < newlen; ++i)
5135 temp[i].hisnum = 0;
5136 temp[i].hisstr = NULL;
5139 else if (newlen > hislen) /* array becomes bigger */
5141 for (i = 0; i <= hisidx[type]; ++i)
5142 temp[i] = history[type][i];
5143 j = i;
5144 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5146 temp[i].hisnum = 0;
5147 temp[i].hisstr = NULL;
5149 for ( ; j < hislen; ++i, ++j)
5150 temp[i] = history[type][j];
5152 else /* array becomes smaller or 0 */
5154 j = hisidx[type];
5155 for (i = newlen - 1; ; --i)
5157 if (i >= 0) /* copy newest entries */
5158 temp[i] = history[type][j];
5159 else /* remove older entries */
5160 vim_free(history[type][j].hisstr);
5161 if (--j < 0)
5162 j = hislen - 1;
5163 if (j == hisidx[type])
5164 break;
5166 hisidx[type] = newlen - 1;
5168 vim_free(history[type]);
5169 history[type] = temp;
5172 hislen = newlen;
5177 * Check if command line 'str' is already in history.
5178 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5180 static int
5181 in_history(type, str, move_to_front)
5182 int type;
5183 char_u *str;
5184 int move_to_front; /* Move the entry to the front if it exists */
5186 int i;
5187 int last_i = -1;
5189 if (hisidx[type] < 0)
5190 return FALSE;
5191 i = hisidx[type];
5194 if (history[type][i].hisstr == NULL)
5195 return FALSE;
5196 if (STRCMP(str, history[type][i].hisstr) == 0)
5198 if (!move_to_front)
5199 return TRUE;
5200 last_i = i;
5201 break;
5203 if (--i < 0)
5204 i = hislen - 1;
5205 } while (i != hisidx[type]);
5207 if (last_i >= 0)
5209 str = history[type][i].hisstr;
5210 while (i != hisidx[type])
5212 if (++i >= hislen)
5213 i = 0;
5214 history[type][last_i] = history[type][i];
5215 last_i = i;
5217 history[type][i].hisstr = str;
5218 history[type][i].hisnum = ++hisnum[type];
5219 return TRUE;
5221 return FALSE;
5225 * Convert history name (from table above) to its HIST_ equivalent.
5226 * When "name" is empty, return "cmd" history.
5227 * Returns -1 for unknown history name.
5230 get_histtype(name)
5231 char_u *name;
5233 int i;
5234 int len = (int)STRLEN(name);
5236 /* No argument: use current history. */
5237 if (len == 0)
5238 return hist_char2type(ccline.cmdfirstc);
5240 for (i = 0; history_names[i] != NULL; ++i)
5241 if (STRNICMP(name, history_names[i], len) == 0)
5242 return i;
5244 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5245 return hist_char2type(name[0]);
5247 return -1;
5250 static int last_maptick = -1; /* last seen maptick */
5253 * Add the given string to the given history. If the string is already in the
5254 * history then it is moved to the front. "histype" may be one of he HIST_
5255 * values.
5257 void
5258 add_to_history(histype, new_entry, in_map, sep)
5259 int histype;
5260 char_u *new_entry;
5261 int in_map; /* consider maptick when inside a mapping */
5262 int sep; /* separator character used (search hist) */
5264 histentry_T *hisptr;
5265 int len;
5267 if (hislen == 0) /* no history */
5268 return;
5271 * Searches inside the same mapping overwrite each other, so that only
5272 * the last line is kept. Be careful not to remove a line that was moved
5273 * down, only lines that were added.
5275 if (histype == HIST_SEARCH && in_map)
5277 if (maptick == last_maptick)
5279 /* Current line is from the same mapping, remove it */
5280 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5281 vim_free(hisptr->hisstr);
5282 hisptr->hisstr = NULL;
5283 hisptr->hisnum = 0;
5284 --hisnum[histype];
5285 if (--hisidx[HIST_SEARCH] < 0)
5286 hisidx[HIST_SEARCH] = hislen - 1;
5288 last_maptick = -1;
5290 if (!in_history(histype, new_entry, TRUE))
5292 if (++hisidx[histype] == hislen)
5293 hisidx[histype] = 0;
5294 hisptr = &history[histype][hisidx[histype]];
5295 vim_free(hisptr->hisstr);
5297 /* Store the separator after the NUL of the string. */
5298 len = (int)STRLEN(new_entry);
5299 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5300 if (hisptr->hisstr != NULL)
5301 hisptr->hisstr[len + 1] = sep;
5303 hisptr->hisnum = ++hisnum[histype];
5304 if (histype == HIST_SEARCH && in_map)
5305 last_maptick = maptick;
5309 #if defined(FEAT_EVAL) || defined(PROTO)
5312 * Get identifier of newest history entry.
5313 * "histype" may be one of the HIST_ values.
5316 get_history_idx(histype)
5317 int histype;
5319 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5320 || hisidx[histype] < 0)
5321 return -1;
5323 return history[histype][hisidx[histype]].hisnum;
5326 static struct cmdline_info *get_ccline_ptr __ARGS((void));
5329 * Get pointer to the command line info to use. cmdline_paste() may clear
5330 * ccline and put the previous value in prev_ccline.
5332 static struct cmdline_info *
5333 get_ccline_ptr()
5335 if ((State & CMDLINE) == 0)
5336 return NULL;
5337 if (ccline.cmdbuff != NULL)
5338 return &ccline;
5339 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5340 return &prev_ccline;
5341 return NULL;
5345 * Get the current command line in allocated memory.
5346 * Only works when the command line is being edited.
5347 * Returns NULL when something is wrong.
5349 char_u *
5350 get_cmdline_str()
5352 struct cmdline_info *p = get_ccline_ptr();
5354 if (p == NULL)
5355 return NULL;
5356 return vim_strnsave(p->cmdbuff, p->cmdlen);
5360 * Get the current command line position, counted in bytes.
5361 * Zero is the first position.
5362 * Only works when the command line is being edited.
5363 * Returns -1 when something is wrong.
5366 get_cmdline_pos()
5368 struct cmdline_info *p = get_ccline_ptr();
5370 if (p == NULL)
5371 return -1;
5372 return p->cmdpos;
5376 * Set the command line byte position to "pos". Zero is the first position.
5377 * Only works when the command line is being edited.
5378 * Returns 1 when failed, 0 when OK.
5381 set_cmdline_pos(pos)
5382 int pos;
5384 struct cmdline_info *p = get_ccline_ptr();
5386 if (p == NULL)
5387 return 1;
5389 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5390 * changed the command line. */
5391 if (pos < 0)
5392 new_cmdpos = 0;
5393 else
5394 new_cmdpos = pos;
5395 return 0;
5399 * Get the current command-line type.
5400 * Returns ':' or '/' or '?' or '@' or '>' or '-'
5401 * Only works when the command line is being edited.
5402 * Returns NUL when something is wrong.
5405 get_cmdline_type()
5407 struct cmdline_info *p = get_ccline_ptr();
5409 if (p == NULL)
5410 return NUL;
5411 if (p->cmdfirstc == NUL)
5412 return (p->input_fn) ? '@' : '-';
5413 return p->cmdfirstc;
5417 * Calculate history index from a number:
5418 * num > 0: seen as identifying number of a history entry
5419 * num < 0: relative position in history wrt newest entry
5420 * "histype" may be one of the HIST_ values.
5422 static int
5423 calc_hist_idx(histype, num)
5424 int histype;
5425 int num;
5427 int i;
5428 histentry_T *hist;
5429 int wrapped = FALSE;
5431 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5432 || (i = hisidx[histype]) < 0 || num == 0)
5433 return -1;
5435 hist = history[histype];
5436 if (num > 0)
5438 while (hist[i].hisnum > num)
5439 if (--i < 0)
5441 if (wrapped)
5442 break;
5443 i += hislen;
5444 wrapped = TRUE;
5446 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5447 return i;
5449 else if (-num <= hislen)
5451 i += num + 1;
5452 if (i < 0)
5453 i += hislen;
5454 if (hist[i].hisstr != NULL)
5455 return i;
5457 return -1;
5461 * Get a history entry by its index.
5462 * "histype" may be one of the HIST_ values.
5464 char_u *
5465 get_history_entry(histype, idx)
5466 int histype;
5467 int idx;
5469 idx = calc_hist_idx(histype, idx);
5470 if (idx >= 0)
5471 return history[histype][idx].hisstr;
5472 else
5473 return (char_u *)"";
5477 * Clear all entries of a history.
5478 * "histype" may be one of the HIST_ values.
5481 clr_history(histype)
5482 int histype;
5484 int i;
5485 histentry_T *hisptr;
5487 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5489 hisptr = history[histype];
5490 for (i = hislen; i--;)
5492 vim_free(hisptr->hisstr);
5493 hisptr->hisnum = 0;
5494 hisptr++->hisstr = NULL;
5496 hisidx[histype] = -1; /* mark history as cleared */
5497 hisnum[histype] = 0; /* reset identifier counter */
5498 return OK;
5500 return FAIL;
5504 * Remove all entries matching {str} from a history.
5505 * "histype" may be one of the HIST_ values.
5508 del_history_entry(histype, str)
5509 int histype;
5510 char_u *str;
5512 regmatch_T regmatch;
5513 histentry_T *hisptr;
5514 int idx;
5515 int i;
5516 int last;
5517 int found = FALSE;
5519 regmatch.regprog = NULL;
5520 regmatch.rm_ic = FALSE; /* always match case */
5521 if (hislen != 0
5522 && histype >= 0
5523 && histype < HIST_COUNT
5524 && *str != NUL
5525 && (idx = hisidx[histype]) >= 0
5526 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5527 != NULL)
5529 i = last = idx;
5532 hisptr = &history[histype][i];
5533 if (hisptr->hisstr == NULL)
5534 break;
5535 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5537 found = TRUE;
5538 vim_free(hisptr->hisstr);
5539 hisptr->hisstr = NULL;
5540 hisptr->hisnum = 0;
5542 else
5544 if (i != last)
5546 history[histype][last] = *hisptr;
5547 hisptr->hisstr = NULL;
5548 hisptr->hisnum = 0;
5550 if (--last < 0)
5551 last += hislen;
5553 if (--i < 0)
5554 i += hislen;
5555 } while (i != idx);
5556 if (history[histype][idx].hisstr == NULL)
5557 hisidx[histype] = -1;
5559 vim_free(regmatch.regprog);
5560 return found;
5564 * Remove an indexed entry from a history.
5565 * "histype" may be one of the HIST_ values.
5568 del_history_idx(histype, idx)
5569 int histype;
5570 int idx;
5572 int i, j;
5574 i = calc_hist_idx(histype, idx);
5575 if (i < 0)
5576 return FALSE;
5577 idx = hisidx[histype];
5578 vim_free(history[histype][i].hisstr);
5580 /* When deleting the last added search string in a mapping, reset
5581 * last_maptick, so that the last added search string isn't deleted again.
5583 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5584 last_maptick = -1;
5586 while (i != idx)
5588 j = (i + 1) % hislen;
5589 history[histype][i] = history[histype][j];
5590 i = j;
5592 history[histype][i].hisstr = NULL;
5593 history[histype][i].hisnum = 0;
5594 if (--i < 0)
5595 i += hislen;
5596 hisidx[histype] = i;
5597 return TRUE;
5600 #endif /* FEAT_EVAL */
5602 #if defined(FEAT_CRYPT) || defined(PROTO)
5604 * Very specific function to remove the value in ":set key=val" from the
5605 * history.
5607 void
5608 remove_key_from_history()
5610 char_u *p;
5611 int i;
5613 i = hisidx[HIST_CMD];
5614 if (i < 0)
5615 return;
5616 p = history[HIST_CMD][i].hisstr;
5617 if (p != NULL)
5618 for ( ; *p; ++p)
5619 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5621 p = vim_strchr(p + 3, '=');
5622 if (p == NULL)
5623 break;
5624 ++p;
5625 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5626 if (p[i] == '\\' && p[i + 1])
5627 ++i;
5628 STRMOVE(p, p + i);
5629 --p;
5632 #endif
5634 #endif /* FEAT_CMDHIST */
5636 #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5638 * Get indices "num1,num2" that specify a range within a list (not a range of
5639 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5640 * Returns OK if parsed successfully, otherwise FAIL.
5643 get_list_range(str, num1, num2)
5644 char_u **str;
5645 int *num1;
5646 int *num2;
5648 int len;
5649 int first = FALSE;
5650 long num;
5652 *str = skipwhite(*str);
5653 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5655 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5656 *str += len;
5657 *num1 = (int)num;
5658 first = TRUE;
5660 *str = skipwhite(*str);
5661 if (**str == ',') /* parse "to" part of range */
5663 *str = skipwhite(*str + 1);
5664 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5665 if (len > 0)
5667 *num2 = (int)num;
5668 *str = skipwhite(*str + len);
5670 else if (!first) /* no number given at all */
5671 return FAIL;
5673 else if (first) /* only one number given */
5674 *num2 = *num1;
5675 return OK;
5677 #endif
5679 #if defined(FEAT_CMDHIST) || defined(PROTO)
5681 * :history command - print a history
5683 void
5684 ex_history(eap)
5685 exarg_T *eap;
5687 histentry_T *hist;
5688 int histype1 = HIST_CMD;
5689 int histype2 = HIST_CMD;
5690 int hisidx1 = 1;
5691 int hisidx2 = -1;
5692 int idx;
5693 int i, j, k;
5694 char_u *end;
5695 char_u *arg = eap->arg;
5697 if (hislen == 0)
5699 MSG(_("'history' option is zero"));
5700 return;
5703 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5705 end = arg;
5706 while (ASCII_ISALPHA(*end)
5707 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5708 end++;
5709 i = *end;
5710 *end = NUL;
5711 histype1 = get_histtype(arg);
5712 if (histype1 == -1)
5714 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
5716 histype1 = 0;
5717 histype2 = HIST_COUNT-1;
5719 else
5721 *end = i;
5722 EMSG(_(e_trailing));
5723 return;
5726 else
5727 histype2 = histype1;
5728 *end = i;
5730 else
5731 end = arg;
5732 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5734 EMSG(_(e_trailing));
5735 return;
5738 for (; !got_int && histype1 <= histype2; ++histype1)
5740 STRCPY(IObuff, "\n # ");
5741 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5742 MSG_PUTS_TITLE(IObuff);
5743 idx = hisidx[histype1];
5744 hist = history[histype1];
5745 j = hisidx1;
5746 k = hisidx2;
5747 if (j < 0)
5748 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5749 if (k < 0)
5750 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5751 if (idx >= 0 && j <= k)
5752 for (i = idx + 1; !got_int; ++i)
5754 if (i == hislen)
5755 i = 0;
5756 if (hist[i].hisstr != NULL
5757 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5759 msg_putchar('\n');
5760 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5761 hist[i].hisnum);
5762 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5763 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5764 (int)Columns - 10);
5765 else
5766 STRCAT(IObuff, hist[i].hisstr);
5767 msg_outtrans(IObuff);
5768 out_flush();
5770 if (i == idx)
5771 break;
5775 #endif
5777 #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5778 static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5779 static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5780 static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5781 static int viminfo_add_at_front = FALSE;
5783 static int hist_type2char __ARGS((int type, int use_question));
5786 * Translate a history type number to the associated character.
5788 static int
5789 hist_type2char(type, use_question)
5790 int type;
5791 int use_question; /* use '?' instead of '/' */
5793 if (type == HIST_CMD)
5794 return ':';
5795 if (type == HIST_SEARCH)
5797 if (use_question)
5798 return '?';
5799 else
5800 return '/';
5802 if (type == HIST_EXPR)
5803 return '=';
5804 return '@';
5808 * Prepare for reading the history from the viminfo file.
5809 * This allocates history arrays to store the read history lines.
5811 void
5812 prepare_viminfo_history(asklen)
5813 int asklen;
5815 int i;
5816 int num;
5817 int type;
5818 int len;
5820 init_history();
5821 viminfo_add_at_front = (asklen != 0);
5822 if (asklen > hislen)
5823 asklen = hislen;
5825 for (type = 0; type < HIST_COUNT; ++type)
5828 * Count the number of empty spaces in the history list. If there are
5829 * more spaces available than we request, then fill them up.
5831 for (i = 0, num = 0; i < hislen; i++)
5832 if (history[type][i].hisstr == NULL)
5833 num++;
5834 len = asklen;
5835 if (num > len)
5836 len = num;
5837 if (len <= 0)
5838 viminfo_history[type] = NULL;
5839 else
5840 viminfo_history[type] =
5841 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5842 if (viminfo_history[type] == NULL)
5843 len = 0;
5844 viminfo_hislen[type] = len;
5845 viminfo_hisidx[type] = 0;
5850 * Accept a line from the viminfo, store it in the history array when it's
5851 * new.
5854 read_viminfo_history(virp)
5855 vir_T *virp;
5857 int type;
5858 long_u len;
5859 char_u *val;
5860 char_u *p;
5862 type = hist_char2type(virp->vir_line[0]);
5863 if (viminfo_hisidx[type] < viminfo_hislen[type])
5865 val = viminfo_readstring(virp, 1, TRUE);
5866 if (val != NULL && *val != NUL)
5868 if (!in_history(type, val + (type == HIST_SEARCH),
5869 viminfo_add_at_front))
5871 /* Need to re-allocate to append the separator byte. */
5872 len = STRLEN(val);
5873 p = lalloc(len + 2, TRUE);
5874 if (p != NULL)
5876 if (type == HIST_SEARCH)
5878 /* Search entry: Move the separator from the first
5879 * column to after the NUL. */
5880 mch_memmove(p, val + 1, (size_t)len);
5881 p[len] = (*val == ' ' ? NUL : *val);
5883 else
5885 /* Not a search entry: No separator in the viminfo
5886 * file, add a NUL separator. */
5887 mch_memmove(p, val, (size_t)len + 1);
5888 p[len + 1] = NUL;
5890 viminfo_history[type][viminfo_hisidx[type]++] = p;
5894 vim_free(val);
5896 return viminfo_readline(virp);
5899 void
5900 finish_viminfo_history()
5902 int idx;
5903 int i;
5904 int type;
5906 for (type = 0; type < HIST_COUNT; ++type)
5908 if (history[type] == NULL)
5909 return;
5910 idx = hisidx[type] + viminfo_hisidx[type];
5911 if (idx >= hislen)
5912 idx -= hislen;
5913 else if (idx < 0)
5914 idx = hislen - 1;
5915 if (viminfo_add_at_front)
5916 hisidx[type] = idx;
5917 else
5919 if (hisidx[type] == -1)
5920 hisidx[type] = hislen - 1;
5923 if (history[type][idx].hisstr != NULL)
5924 break;
5925 if (++idx == hislen)
5926 idx = 0;
5927 } while (idx != hisidx[type]);
5928 if (idx != hisidx[type] && --idx < 0)
5929 idx = hislen - 1;
5931 for (i = 0; i < viminfo_hisidx[type]; i++)
5933 vim_free(history[type][idx].hisstr);
5934 history[type][idx].hisstr = viminfo_history[type][i];
5935 if (--idx < 0)
5936 idx = hislen - 1;
5938 idx += 1;
5939 idx %= hislen;
5940 for (i = 0; i < viminfo_hisidx[type]; i++)
5942 history[type][idx++].hisnum = ++hisnum[type];
5943 idx %= hislen;
5945 vim_free(viminfo_history[type]);
5946 viminfo_history[type] = NULL;
5950 void
5951 write_viminfo_history(fp)
5952 FILE *fp;
5954 int i;
5955 int type;
5956 int num_saved;
5957 char_u *p;
5958 int c;
5960 init_history();
5961 if (hislen == 0)
5962 return;
5963 for (type = 0; type < HIST_COUNT; ++type)
5965 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5966 if (num_saved == 0)
5967 continue;
5968 if (num_saved < 0) /* Use default */
5969 num_saved = hislen;
5970 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5971 type == HIST_CMD ? _("Command Line") :
5972 type == HIST_SEARCH ? _("Search String") :
5973 type == HIST_EXPR ? _("Expression") :
5974 _("Input Line"));
5975 if (num_saved > hislen)
5976 num_saved = hislen;
5977 i = hisidx[type];
5978 if (i >= 0)
5979 while (num_saved--)
5981 p = history[type][i].hisstr;
5982 if (p != NULL)
5984 fputc(hist_type2char(type, TRUE), fp);
5985 /* For the search history: put the separator in the second
5986 * column; use a space if there isn't one. */
5987 if (type == HIST_SEARCH)
5989 c = p[STRLEN(p) + 1];
5990 putc(c == NUL ? ' ' : c, fp);
5992 viminfo_writestring(fp, p);
5994 if (--i < 0)
5995 i = hislen - 1;
5999 #endif /* FEAT_VIMINFO */
6001 #if defined(FEAT_FKMAP) || defined(PROTO)
6003 * Write a character at the current cursor+offset position.
6004 * It is directly written into the command buffer block.
6006 void
6007 cmd_pchar(c, offset)
6008 int c, offset;
6010 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6012 EMSG(_("E198: cmd_pchar beyond the command length"));
6013 return;
6015 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6016 ccline.cmdbuff[ccline.cmdlen] = NUL;
6020 cmd_gchar(offset)
6021 int offset;
6023 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6025 /* EMSG(_("cmd_gchar beyond the command length")); */
6026 return NUL;
6028 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6030 #endif
6032 #if defined(FEAT_CMDWIN) || defined(PROTO)
6034 * Open a window on the current command line and history. Allow editing in
6035 * the window. Returns when the window is closed.
6036 * Returns:
6037 * CR if the command is to be executed
6038 * Ctrl_C if it is to be abandoned
6039 * K_IGNORE if editing continues
6041 static int
6042 ex_window()
6044 struct cmdline_info save_ccline;
6045 buf_T *old_curbuf = curbuf;
6046 win_T *old_curwin = curwin;
6047 buf_T *bp;
6048 win_T *wp;
6049 int i;
6050 linenr_T lnum;
6051 int histtype;
6052 garray_T winsizes;
6053 #ifdef FEAT_AUTOCMD
6054 char_u typestr[2];
6055 #endif
6056 int save_restart_edit = restart_edit;
6057 int save_State = State;
6058 int save_exmode = exmode_active;
6059 #ifdef FEAT_RIGHTLEFT
6060 int save_cmdmsg_rl = cmdmsg_rl;
6061 #endif
6063 /* Can't do this recursively. Can't do it when typing a password. */
6064 if (cmdwin_type != 0
6065 # if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6066 || cmdline_star > 0
6067 # endif
6070 beep_flush();
6071 return K_IGNORE;
6074 /* Save current window sizes. */
6075 win_size_save(&winsizes);
6077 # ifdef FEAT_AUTOCMD
6078 /* Don't execute autocommands while creating the window. */
6079 block_autocmds();
6080 # endif
6081 /* don't use a new tab page */
6082 cmdmod.tab = 0;
6084 /* Create a window for the command-line buffer. */
6085 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6087 beep_flush();
6088 # ifdef FEAT_AUTOCMD
6089 unblock_autocmds();
6090 # endif
6091 return K_IGNORE;
6093 cmdwin_type = get_cmdline_type();
6095 /* Create the command-line buffer empty. */
6096 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
6097 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
6098 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6099 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6100 curbuf->b_p_ma = TRUE;
6101 #ifdef FEAT_FOLDING
6102 curwin->w_p_fen = FALSE;
6103 #endif
6104 # ifdef FEAT_RIGHTLEFT
6105 curwin->w_p_rl = cmdmsg_rl;
6106 cmdmsg_rl = FALSE;
6107 # endif
6108 # ifdef FEAT_SCROLLBIND
6109 curwin->w_p_scb = FALSE;
6110 # endif
6112 # ifdef FEAT_AUTOCMD
6113 /* Do execute autocommands for setting the filetype (load syntax). */
6114 unblock_autocmds();
6115 # endif
6117 /* Showing the prompt may have set need_wait_return, reset it. */
6118 need_wait_return = FALSE;
6120 histtype = hist_char2type(cmdwin_type);
6121 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6123 if (p_wc == TAB)
6125 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6126 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6128 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6131 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6132 * sets 'textwidth' to 78). */
6133 curbuf->b_p_tw = 0;
6135 /* Fill the buffer with the history. */
6136 init_history();
6137 if (hislen > 0)
6139 i = hisidx[histtype];
6140 if (i >= 0)
6142 lnum = 0;
6145 if (++i == hislen)
6146 i = 0;
6147 if (history[histtype][i].hisstr != NULL)
6148 ml_append(lnum++, history[histtype][i].hisstr,
6149 (colnr_T)0, FALSE);
6151 while (i != hisidx[histtype]);
6155 /* Replace the empty last line with the current command-line and put the
6156 * cursor there. */
6157 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6158 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6159 curwin->w_cursor.col = ccline.cmdpos;
6160 changed_line_abv_curs();
6161 invalidate_botline();
6162 redraw_later(SOME_VALID);
6164 /* Save the command line info, can be used recursively. */
6165 save_ccline = ccline;
6166 ccline.cmdbuff = NULL;
6167 ccline.cmdprompt = NULL;
6169 /* No Ex mode here! */
6170 exmode_active = 0;
6172 State = NORMAL;
6173 # ifdef FEAT_MOUSE
6174 setmouse();
6175 # endif
6177 # ifdef FEAT_AUTOCMD
6178 /* Trigger CmdwinEnter autocommands. */
6179 typestr[0] = cmdwin_type;
6180 typestr[1] = NUL;
6181 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
6182 if (restart_edit != 0) /* autocmd with ":startinsert" */
6183 stuffcharReadbuff(K_NOP);
6184 # endif
6186 i = RedrawingDisabled;
6187 RedrawingDisabled = 0;
6190 * Call the main loop until <CR> or CTRL-C is typed.
6192 cmdwin_result = 0;
6193 main_loop(TRUE, FALSE);
6195 RedrawingDisabled = i;
6197 # ifdef FEAT_AUTOCMD
6198 /* Trigger CmdwinLeave autocommands. */
6199 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6200 # endif
6202 /* Restore the command line info. */
6203 ccline = save_ccline;
6204 cmdwin_type = 0;
6206 exmode_active = save_exmode;
6208 /* Safety check: The old window or buffer was deleted: It's a bug when
6209 * this happens! */
6210 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6212 cmdwin_result = Ctrl_C;
6213 EMSG(_("E199: Active window or buffer deleted"));
6215 else
6217 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6218 /* autocmds may abort script processing */
6219 if (aborting() && cmdwin_result != K_IGNORE)
6220 cmdwin_result = Ctrl_C;
6221 # endif
6222 /* Set the new command line from the cmdline buffer. */
6223 vim_free(ccline.cmdbuff);
6224 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
6226 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6228 if (histtype == HIST_CMD)
6230 /* Execute the command directly. */
6231 ccline.cmdbuff = vim_strsave((char_u *)p);
6232 cmdwin_result = CAR;
6234 else
6236 /* First need to cancel what we were doing. */
6237 ccline.cmdbuff = NULL;
6238 stuffcharReadbuff(':');
6239 stuffReadbuff((char_u *)p);
6240 stuffcharReadbuff(CAR);
6243 else if (cmdwin_result == K_XF2) /* :qa typed */
6245 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6246 cmdwin_result = CAR;
6248 else
6249 ccline.cmdbuff = vim_strsave(ml_get_curline());
6250 if (ccline.cmdbuff == NULL)
6251 cmdwin_result = Ctrl_C;
6252 else
6254 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6255 ccline.cmdbufflen = ccline.cmdlen + 1;
6256 ccline.cmdpos = curwin->w_cursor.col;
6257 if (ccline.cmdpos > ccline.cmdlen)
6258 ccline.cmdpos = ccline.cmdlen;
6259 if (cmdwin_result == K_IGNORE)
6261 set_cmdspos_cursor();
6262 redrawcmd();
6266 # ifdef FEAT_AUTOCMD
6267 /* Don't execute autocommands while deleting the window. */
6268 block_autocmds();
6269 # endif
6270 wp = curwin;
6271 bp = curbuf;
6272 win_goto(old_curwin);
6273 win_close(wp, TRUE);
6274 close_buffer(NULL, bp, DOBUF_WIPE);
6276 /* Restore window sizes. */
6277 win_size_restore(&winsizes);
6279 # ifdef FEAT_AUTOCMD
6280 unblock_autocmds();
6281 # endif
6284 ga_clear(&winsizes);
6285 restart_edit = save_restart_edit;
6286 # ifdef FEAT_RIGHTLEFT
6287 cmdmsg_rl = save_cmdmsg_rl;
6288 # endif
6290 State = save_State;
6291 # ifdef FEAT_MOUSE
6292 setmouse();
6293 # endif
6295 return cmdwin_result;
6297 #endif /* FEAT_CMDWIN */
6300 * Used for commands that either take a simple command string argument, or:
6301 * cmd << endmarker
6302 * {script}
6303 * endmarker
6304 * Returns a pointer to allocated memory with {script} or NULL.
6306 char_u *
6307 script_get(eap, cmd)
6308 exarg_T *eap;
6309 char_u *cmd;
6311 char_u *theline;
6312 char *end_pattern = NULL;
6313 char dot[] = ".";
6314 garray_T ga;
6316 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6317 return NULL;
6319 ga_init2(&ga, 1, 0x400);
6321 if (cmd[2] != NUL)
6322 end_pattern = (char *)skipwhite(cmd + 2);
6323 else
6324 end_pattern = dot;
6326 for (;;)
6328 theline = eap->getline(
6329 #ifdef FEAT_EVAL
6330 eap->cstack->cs_looplevel > 0 ? -1 :
6331 #endif
6332 NUL, eap->cookie, 0);
6334 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6336 vim_free(theline);
6337 break;
6340 ga_concat(&ga, theline);
6341 ga_append(&ga, '\n');
6342 vim_free(theline);
6344 ga_append(&ga, NUL);
6346 return (char_u *)ga.ga_data;