blob: 9122ab4918ac8fdae6d36cd9099b91b4409c0e68 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
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 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
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.
20 */
21struct cmdline_info
22{
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};
35
36static struct cmdline_info ccline; /* current cmdline_info */
37
38static int cmd_showtail; /* Only show path tail in lists ? */
39
40#ifdef FEAT_EVAL
41static int new_cmdpos; /* position set by set_cmdline_pos() */
42#endif
43
44#ifdef FEAT_CMDHIST
45typedef struct hist_entry
46{
47 int hisnum; /* identifying number */
48 char_u *hisstr; /* actual entry, separator char after the NUL */
49} histentry_T;
50
51static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
52static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
53static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
54 /* identifying (unique) number of newest history entry */
55static int hislen = 0; /* actual length of history tables */
56
57static int hist_char2type __ARGS((int c));
58static void init_history __ARGS((void));
59
60static int in_history __ARGS((int, char_u *, int));
61# ifdef FEAT_EVAL
62static int calc_hist_idx __ARGS((int histype, int num));
63# endif
64#endif
65
66#ifdef FEAT_RIGHTLEFT
67static int cmd_hkmap = 0; /* Hebrew mapping during command line */
68#endif
69
70#ifdef FEAT_FKMAP
71static int cmd_fkmap = 0; /* Farsi mapping during command line */
72#endif
73
74static int cmdline_charsize __ARGS((int idx));
75static void set_cmdspos __ARGS((void));
76static void set_cmdspos_cursor __ARGS((void));
77#ifdef FEAT_MBYTE
78static void correct_cmdspos __ARGS((int idx, int cells));
79#endif
80static void alloc_cmdbuff __ARGS((int len));
81static int realloc_cmdbuff __ARGS((int len));
82static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar8299df92004-07-10 09:47:34 +000083static int cmdline_paste __ARGS((int regname, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
85static void redrawcmd_preedit __ARGS((void));
86#endif
87#ifdef FEAT_WILDMENU
88static void cmdline_del __ARGS((int from));
89#endif
90static void redrawcmdprompt __ARGS((void));
91static void cursorcmd __ARGS((void));
92static int ccheck_abbr __ARGS((int));
93static int nextwild __ARGS((expand_T *xp, int type, int options));
94static int showmatches __ARGS((expand_T *xp, int wildmenu));
95static void set_expand_context __ARGS((expand_T *xp));
96static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
97static int expand_showtail __ARGS((expand_T *xp));
98#ifdef FEAT_CMDL_COMPL
99static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
100# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
101static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
102# endif
103#endif
104
105#ifdef FEAT_CMDWIN
106static int ex_window __ARGS((void));
107#endif
108
109/*
110 * getcmdline() - accept a command line starting with firstc.
111 *
112 * firstc == ':' get ":" command line.
113 * firstc == '/' or '?' get search pattern
114 * firstc == '=' get expression
115 * firstc == '@' get text for input() function
116 * firstc == '>' get text for debug mode
117 * firstc == NUL get text for :insert command
118 * firstc == -1 like NUL, and break on CTRL-C
119 *
120 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
121 * command line.
122 *
123 * Careful: getcmdline() can be called recursively!
124 *
125 * Return pointer to allocated string if there is a commandline, NULL
126 * otherwise.
127 */
128/*ARGSUSED*/
129 char_u *
130getcmdline(firstc, count, indent)
131 int firstc;
132 long count; /* only used for incremental search */
133 int indent; /* indent for inside conditionals */
134{
135 int c;
136 int i;
137 int j;
138 int gotesc = FALSE; /* TRUE when <ESC> just typed */
139 int do_abbr; /* when TRUE check for abbr. */
140#ifdef FEAT_CMDHIST
141 char_u *lookfor = NULL; /* string to match */
142 int hiscnt; /* current history line in use */
143 int histype; /* history type to be used */
144#endif
145#ifdef FEAT_SEARCH_EXTRA
146 pos_T old_cursor;
147 colnr_T old_curswant;
148 colnr_T old_leftcol;
149 linenr_T old_topline;
150# ifdef FEAT_DIFF
151 int old_topfill;
152# endif
153 linenr_T old_botline;
154 int did_incsearch = FALSE;
155 int incsearch_postponed = FALSE;
156#endif
157 int did_wild_list = FALSE; /* did wild_list() recently */
158 int wim_index = 0; /* index in wim_flags[] */
159 int res;
160 int save_msg_scroll = msg_scroll;
161 int save_State = State; /* remember State when called */
162 int some_key_typed = FALSE; /* one of the keys was typed */
163#ifdef FEAT_MOUSE
164 /* mouse drag and release events are ignored, unless they are
165 * preceded with a mouse down event */
166 int ignore_drag_release = TRUE;
167#endif
168#ifdef FEAT_EVAL
169 int break_ctrl_c = FALSE;
170#endif
171 expand_T xpc;
172 long *b_im_ptr = NULL;
173
174#ifdef FEAT_SNIFF
175 want_sniff_request = 0;
176#endif
177#ifdef FEAT_EVAL
178 if (firstc == -1)
179 {
180 firstc = NUL;
181 break_ctrl_c = TRUE;
182 }
183#endif
184#ifdef FEAT_RIGHTLEFT
185 /* start without Hebrew mapping for a command line */
186 if (firstc == ':' || firstc == '=' || firstc == '>')
187 cmd_hkmap = 0;
188#endif
189
190 ccline.overstrike = FALSE; /* always start in insert mode */
191#ifdef FEAT_SEARCH_EXTRA
192 old_cursor = curwin->w_cursor; /* needs to be restored later */
193 old_curswant = curwin->w_curswant;
194 old_leftcol = curwin->w_leftcol;
195 old_topline = curwin->w_topline;
196# ifdef FEAT_DIFF
197 old_topfill = curwin->w_topfill;
198# endif
199 old_botline = curwin->w_botline;
200#endif
201
202 /*
203 * set some variables for redrawcmd()
204 */
205 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
206 ccline.cmdindent = indent;
207 alloc_cmdbuff(exmode_active ? 250 : 0); /* alloc initial ccline.cmdbuff */
208 if (ccline.cmdbuff == NULL)
209 return NULL; /* out of memory */
210 ccline.cmdlen = ccline.cmdpos = 0;
211 ccline.cmdbuff[0] = NUL;
212
213 ExpandInit(&xpc);
214
215#ifdef FEAT_RIGHTLEFT
216 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
217 && (firstc == '/' || firstc == '?'))
218 cmdmsg_rl = TRUE;
219 else
220 cmdmsg_rl = FALSE;
221#endif
222
223 redir_off = TRUE; /* don't redirect the typed command */
224 if (!cmd_silent)
225 {
226 i = msg_scrolled;
227 msg_scrolled = 0; /* avoid wait_return message */
228 gotocmdline(TRUE);
229 msg_scrolled += i;
230 redrawcmdprompt(); /* draw prompt or indent */
231 set_cmdspos();
232 }
233 xpc.xp_context = EXPAND_NOTHING;
234 xpc.xp_backslash = XP_BS_NONE;
235
236 /*
237 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
238 * doing ":@0" when register 0 doesn't contain a CR.
239 */
240 msg_scroll = FALSE;
241
242 State = CMDLINE;
243
244 if (firstc == '/' || firstc == '?' || firstc == '@')
245 {
246 /* Use ":lmap" mappings for search pattern and input(). */
247 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
248 b_im_ptr = &curbuf->b_p_iminsert;
249 else
250 b_im_ptr = &curbuf->b_p_imsearch;
251 if (*b_im_ptr == B_IMODE_LMAP)
252 State |= LANGMAP;
253#ifdef USE_IM_CONTROL
254 im_set_active(*b_im_ptr == B_IMODE_IM);
255#endif
256 }
257#ifdef USE_IM_CONTROL
258 else if (p_imcmdline)
259 im_set_active(TRUE);
260#endif
261
262#ifdef FEAT_MOUSE
263 setmouse();
264#endif
265#ifdef CURSOR_SHAPE
266 ui_cursor_shape(); /* may show different cursor shape */
267#endif
268
269#ifdef FEAT_CMDHIST
270 init_history();
271 hiscnt = hislen; /* set hiscnt to impossible history value */
272 histype = hist_char2type(firstc);
273#endif
274
275#ifdef FEAT_DIGRAPHS
276 do_digraph(-1); /* init digraph typahead */
277#endif
278
279 /*
280 * Collect the command string, handling editing keys.
281 */
282 for (;;)
283 {
284#ifdef USE_ON_FLY_SCROLL
285 dont_scroll = FALSE; /* allow scrolling here */
286#endif
287 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
288
289 cursorcmd(); /* set the cursor on the right spot */
290 c = safe_vgetc();
291 if (KeyTyped)
292 {
293 some_key_typed = TRUE;
294#ifdef FEAT_RIGHTLEFT
295 if (cmd_hkmap)
296 c = hkmap(c);
297# ifdef FEAT_FKMAP
298 if (cmd_fkmap)
299 c = cmdl_fkmap(c);
300# endif
301 if (cmdmsg_rl && !KeyStuffed)
302 {
303 /* Invert horizontal movements and operations. Only when
304 * typed by the user directly, not when the result of a
305 * mapping. */
306 switch (c)
307 {
308 case K_RIGHT: c = K_LEFT; break;
309 case K_S_RIGHT: c = K_S_LEFT; break;
310 case K_C_RIGHT: c = K_C_LEFT; break;
311 case K_LEFT: c = K_RIGHT; break;
312 case K_S_LEFT: c = K_S_RIGHT; break;
313 case K_C_LEFT: c = K_C_RIGHT; break;
314 }
315 }
316#endif
317 }
318
319 /*
320 * Ignore got_int when CTRL-C was typed here.
321 * Don't ignore it in :global, we really need to break then, e.g., for
322 * ":g/pat/normal /pat" (without the <CR>).
323 * Don't ignore it for the input() function.
324 */
325 if ((c == Ctrl_C
326#ifdef UNIX
327 || c == intr_char
328#endif
329 )
330#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
331 && firstc != '@'
332#endif
333#ifdef FEAT_EVAL
334 && !break_ctrl_c
335#endif
336 && !global_busy)
337 got_int = FALSE;
338
339#ifdef FEAT_CMDHIST
340 /* free old command line when finished moving around in the history
341 * list */
342 if (lookfor != NULL
343 && c != K_S_DOWN && c != K_S_UP && c != K_DOWN && c != K_UP
344 && c != K_PAGEDOWN && c != K_PAGEUP
345 && c != K_KPAGEDOWN && c != K_KPAGEUP
346 && c != K_LEFT && c != K_RIGHT
347 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
348 {
349 vim_free(lookfor);
350 lookfor = NULL;
351 }
352#endif
353
354 /*
355 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
356 */
357 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
358 c = Ctrl_P;
359
360#ifdef FEAT_WILDMENU
361 /* Special translations for 'wildmenu' */
362 if (did_wild_list && p_wmnu)
363 {
364 if (c == K_LEFT)
365 c = Ctrl_P;
366 else if (c == K_RIGHT)
367 c = Ctrl_N;
368 }
369 /* Hitting CR after "emenu Name.": complete submenu */
370 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
371 && ccline.cmdpos > 1
372 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
373 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
374 && (c == '\n' || c == '\r' || c == K_KENTER))
375 c = K_DOWN;
376#endif
377
378 /* free expanded names when finished walking through matches */
379 if (xpc.xp_numfiles != -1
380 && !(c == p_wc && KeyTyped) && c != p_wcm
381 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
382 && c != Ctrl_L)
383 {
384 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
385 did_wild_list = FALSE;
386#ifdef FEAT_WILDMENU
387 if (!p_wmnu || (c != K_UP && c != K_DOWN))
388#endif
389 xpc.xp_context = EXPAND_NOTHING;
390 wim_index = 0;
391#ifdef FEAT_WILDMENU
392 if (p_wmnu && wild_menu_showing != 0)
393 {
394 int skt = KeyTyped;
395
396 if (wild_menu_showing == WM_SCROLLED)
397 {
398 /* Entered command line, move it up */
399 cmdline_row--;
400 redrawcmd();
401 }
402 else if (save_p_ls != -1)
403 {
404 /* restore 'laststatus' and 'winminheight' */
405 p_ls = save_p_ls;
406 p_wmh = save_p_wmh;
407 last_status(FALSE);
408 update_screen(VALID); /* redraw the screen NOW */
409 redrawcmd();
410 save_p_ls = -1;
411 }
412 else
413 {
414# ifdef FEAT_VERTSPLIT
415 win_redraw_last_status(topframe);
416# else
417 lastwin->w_redr_status = TRUE;
418# endif
419 redraw_statuslines();
420 }
421 KeyTyped = skt;
422 wild_menu_showing = 0;
423 }
424#endif
425 }
426
427#ifdef FEAT_WILDMENU
428 /* Special translations for 'wildmenu' */
429 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
430 {
431 /* Hitting <Down> after "emenu Name.": complete submenu */
432 if (ccline.cmdbuff[ccline.cmdpos - 1] == '.' && c == K_DOWN)
433 c = p_wc;
434 else if (c == K_UP)
435 {
436 /* Hitting <Up>: Remove one submenu name in front of the
437 * cursor */
438 int found = FALSE;
439
440 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
441 i = 0;
442 while (--j > 0)
443 {
444 /* check for start of menu name */
445 if (ccline.cmdbuff[j] == ' '
446 && ccline.cmdbuff[j - 1] != '\\')
447 {
448 i = j + 1;
449 break;
450 }
451 /* check for start of submenu name */
452 if (ccline.cmdbuff[j] == '.'
453 && ccline.cmdbuff[j - 1] != '\\')
454 {
455 if (found)
456 {
457 i = j + 1;
458 break;
459 }
460 else
461 found = TRUE;
462 }
463 }
464 if (i > 0)
465 cmdline_del(i);
466 c = p_wc;
467 xpc.xp_context = EXPAND_NOTHING;
468 }
469 }
470 if (xpc.xp_context == EXPAND_FILES && p_wmnu)
471 {
472 char_u upseg[5];
473
474 upseg[0] = PATHSEP;
475 upseg[1] = '.';
476 upseg[2] = '.';
477 upseg[3] = PATHSEP;
478 upseg[4] = NUL;
479
480 if (ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
481 && c == K_DOWN
482 && (ccline.cmdbuff[ccline.cmdpos - 2] != '.'
483 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
484 {
485 /* go down a directory */
486 c = p_wc;
487 }
488 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
489 {
490 /* If in a direct ancestor, strip off one ../ to go down */
491 int found = FALSE;
492
493 j = ccline.cmdpos;
494 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
495 while (--j > i)
496 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000497#ifdef FEAT_MBYTE
498 if (has_mbyte)
499 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 if (vim_ispathsep(ccline.cmdbuff[j]))
502 {
503 found = TRUE;
504 break;
505 }
506 }
507 if (found
508 && ccline.cmdbuff[j - 1] == '.'
509 && ccline.cmdbuff[j - 2] == '.'
510 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
511 {
512 cmdline_del(j - 2);
513 c = p_wc;
514 }
515 }
516 else if (c == K_UP)
517 {
518 /* go up a directory */
519 int found = FALSE;
520
521 j = ccline.cmdpos - 1;
522 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
523 while (--j > i)
524 {
525#ifdef FEAT_MBYTE
526 if (has_mbyte)
527 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
528#endif
529 if (vim_ispathsep(ccline.cmdbuff[j])
530#ifdef BACKSLASH_IN_FILENAME
531 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
532 == NULL
533#endif
534 )
535 {
536 if (found)
537 {
538 i = j + 1;
539 break;
540 }
541 else
542 found = TRUE;
543 }
544 }
545
546 if (!found)
547 j = i;
548 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
549 j += 4;
550 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
551 && j == i)
552 j += 3;
553 else
554 j = 0;
555 if (j > 0)
556 {
557 /* TODO this is only for DOS/UNIX systems - need to put in
558 * machine-specific stuff here and in upseg init */
559 cmdline_del(j);
560 put_on_cmdline(upseg + 1, 3, FALSE);
561 }
562 else if (ccline.cmdpos > i)
563 cmdline_del(i);
564 c = p_wc;
565 }
566 }
567#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
568 if (p_wmnu
569 && (xpc.xp_context == EXPAND_FILES
570 || xpc.xp_context == EXPAND_MENUNAMES)
571 && (c == K_UP || c == K_DOWN))
572 xpc.xp_context = EXPAND_NOTHING;
573#endif
574
575#endif /* FEAT_WILDMENU */
576
577 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
578 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
579 if (c == Ctrl_BSL)
580 {
581 ++no_mapping;
582 ++allow_keys;
583 c = safe_vgetc();
584 --no_mapping;
585 --allow_keys;
586 /* CTRL-\ e doesn't work when obtaining an expression. */
587 if (c != Ctrl_N && c != Ctrl_G
588 && (c != 'e' || ccline.cmdfirstc == '='))
589 {
590 vungetc(c);
591 c = Ctrl_BSL;
592 }
593#ifdef FEAT_EVAL
594 else if (c == 'e')
595 {
596 struct cmdline_info save_ccline;
597 char_u *p;
598
599 /*
600 * Replace the command line with the result of an expression.
601 * Need to save the current command line, to be able to enter
602 * a new one...
603 */
604 if (ccline.cmdpos == ccline.cmdlen)
605 new_cmdpos = 99999; /* keep it at the end */
606 else
607 new_cmdpos = ccline.cmdpos;
608 save_ccline = ccline;
609 ccline.cmdbuff = NULL;
610 ccline.cmdprompt = NULL;
611 c = get_expr_register();
612 ccline = save_ccline;
613 if (c == '=')
614 {
615 p = get_expr_line();
616 if (p != NULL
617 && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
618 {
619 ccline.cmdlen = STRLEN(p);
620 STRCPY(ccline.cmdbuff, p);
621 vim_free(p);
622
623 /* Restore the cursor or use the position set with
624 * set_cmdline_pos(). */
625 if (new_cmdpos > ccline.cmdlen)
626 ccline.cmdpos = ccline.cmdlen;
627 else
628 ccline.cmdpos = new_cmdpos;
629
630 KeyTyped = FALSE; /* Don't do p_wc completion. */
631 redrawcmd();
632 goto cmdline_changed;
633 }
634 }
635 beep_flush();
636 c = ESC;
637 }
638#endif
639 else
640 {
641 if (c == Ctrl_G && p_im && restart_edit == 0)
642 restart_edit = 'a';
643 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
644 in history */
645 goto returncmd; /* back to Normal mode */
646 }
647 }
648
649#ifdef FEAT_CMDWIN
650 if (c == cedit_key || c == K_CMDWIN)
651 {
652 /*
653 * Open a window to edit the command line (and history).
654 */
655 c = ex_window();
656 some_key_typed = TRUE;
657 }
658# ifdef FEAT_DIGRAPHS
659 else
660# endif
661#endif
662#ifdef FEAT_DIGRAPHS
663 c = do_digraph(c);
664#endif
665
666 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
667 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
668 {
669 gotesc = FALSE; /* Might have typed ESC previously, don't
670 truncate the cmdline now. */
671 if (ccheck_abbr(c + ABBR_OFF))
672 goto cmdline_changed;
673 if (!cmd_silent)
674 {
675 windgoto(msg_row, 0);
676 out_flush();
677 }
678 break;
679 }
680
681 /*
682 * Completion for 'wildchar' or 'wildcharm' key.
683 * - hitting <ESC> twice means: abandon command line.
684 * - wildcard expansion is only done when the 'wildchar' key is really
685 * typed, not when it comes from a macro
686 */
687 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
688 {
689 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
690 {
691 /* if 'wildmode' contains "list" may still need to list */
692 if (xpc.xp_numfiles > 1
693 && !did_wild_list
694 && (wim_flags[wim_index] & WIM_LIST))
695 {
696 (void)showmatches(&xpc, FALSE);
697 redrawcmd();
698 did_wild_list = TRUE;
699 }
700 if (wim_flags[wim_index] & WIM_LONGEST)
701 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
702 else if (wim_flags[wim_index] & WIM_FULL)
703 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
704 else
705 res = OK; /* don't insert 'wildchar' now */
706 }
707 else /* typed p_wc first time */
708 {
709 wim_index = 0;
710 j = ccline.cmdpos;
711 /* if 'wildmode' first contains "longest", get longest
712 * common part */
713 if (wim_flags[0] & WIM_LONGEST)
714 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
715 else
716 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
717
718 /* if interrupted while completing, behave like it failed */
719 if (got_int)
720 {
721 (void)vpeekc(); /* remove <C-C> from input stream */
722 got_int = FALSE; /* don't abandon the command line */
723 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
724#ifdef FEAT_WILDMENU
725 xpc.xp_context = EXPAND_NOTHING;
726#endif
727 goto cmdline_changed;
728 }
729
730 /* when more than one match, and 'wildmode' first contains
731 * "list", or no change and 'wildmode' contains "longest,list",
732 * list all matches */
733 if (res == OK && xpc.xp_numfiles > 1)
734 {
735 /* a "longest" that didn't do anything is skipped (but not
736 * "list:longest") */
737 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
738 wim_index = 1;
739 if ((wim_flags[wim_index] & WIM_LIST)
740#ifdef FEAT_WILDMENU
741 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
742#endif
743 )
744 {
745 if (!(wim_flags[0] & WIM_LONGEST))
746 {
747#ifdef FEAT_WILDMENU
748 int p_wmnu_save = p_wmnu;
749 p_wmnu = 0;
750#endif
751 nextwild(&xpc, WILD_PREV, 0); /* remove match */
752#ifdef FEAT_WILDMENU
753 p_wmnu = p_wmnu_save;
754#endif
755 }
756#ifdef FEAT_WILDMENU
757 (void)showmatches(&xpc, p_wmnu
758 && ((wim_flags[wim_index] & WIM_LIST) == 0));
759#else
760 (void)showmatches(&xpc, FALSE);
761#endif
762 redrawcmd();
763 did_wild_list = TRUE;
764 if (wim_flags[wim_index] & WIM_LONGEST)
765 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
766 else if (wim_flags[wim_index] & WIM_FULL)
767 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
768 }
769 else
770 vim_beep();
771 }
772#ifdef FEAT_WILDMENU
773 else if (xpc.xp_numfiles == -1)
774 xpc.xp_context = EXPAND_NOTHING;
775#endif
776 }
777 if (wim_index < 3)
778 ++wim_index;
779 if (c == ESC)
780 gotesc = TRUE;
781 if (res == OK)
782 goto cmdline_changed;
783 }
784
785 gotesc = FALSE;
786
787 /* <S-Tab> goes to last match, in a clumsy way */
788 if (c == K_S_TAB && KeyTyped)
789 {
790 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
791 && nextwild(&xpc, WILD_PREV, 0) == OK
792 && nextwild(&xpc, WILD_PREV, 0) == OK)
793 goto cmdline_changed;
794 }
795
796 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
797 c = NL;
798
799 do_abbr = TRUE; /* default: check for abbreviation */
800
801 /*
802 * Big switch for a typed command line character.
803 */
804 switch (c)
805 {
806 case K_BS:
807 case Ctrl_H:
808 case K_DEL:
809 case K_KDEL:
810 case Ctrl_W:
811#ifdef FEAT_FKMAP
812 if (cmd_fkmap && c == K_BS)
813 c = K_DEL;
814#endif
815 if (c == K_KDEL)
816 c = K_DEL;
817
818 /*
819 * delete current character is the same as backspace on next
820 * character, except at end of line
821 */
822 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
823 ++ccline.cmdpos;
824#ifdef FEAT_MBYTE
825 if (has_mbyte && c == K_DEL)
826 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
827 ccline.cmdbuff + ccline.cmdpos);
828#endif
829 if (ccline.cmdpos > 0)
830 {
831 char_u *p;
832
833 j = ccline.cmdpos;
834 p = ccline.cmdbuff + j;
835#ifdef FEAT_MBYTE
836 if (has_mbyte)
837 {
838 p = mb_prevptr(ccline.cmdbuff, p);
839 if (c == Ctrl_W)
840 {
841 while (p > ccline.cmdbuff && vim_isspace(*p))
842 p = mb_prevptr(ccline.cmdbuff, p);
843 i = mb_get_class(p);
844 while (p > ccline.cmdbuff && mb_get_class(p) == i)
845 p = mb_prevptr(ccline.cmdbuff, p);
846 if (mb_get_class(p) != i)
847 p += (*mb_ptr2len_check)(p);
848 }
849 }
850 else
851#endif
852 if (c == Ctrl_W)
853 {
854 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
855 --p;
856 i = vim_iswordc(p[-1]);
857 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
858 && vim_iswordc(p[-1]) == i)
859 --p;
860 }
861 else
862 --p;
863 ccline.cmdpos = (int)(p - ccline.cmdbuff);
864 ccline.cmdlen -= j - ccline.cmdpos;
865 i = ccline.cmdpos;
866 while (i < ccline.cmdlen)
867 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
868
869 /* Truncate at the end, required for multi-byte chars. */
870 ccline.cmdbuff[ccline.cmdlen] = NUL;
871 redrawcmd();
872 }
873 else if (ccline.cmdlen == 0 && c != Ctrl_W
874 && ccline.cmdprompt == NULL && indent == 0)
875 {
876 /* In ex and debug mode it doesn't make sense to return. */
877 if (exmode_active
878#ifdef FEAT_EVAL
879 || ccline.cmdfirstc == '>'
880#endif
881 )
882 goto cmdline_not_changed;
883
884 vim_free(ccline.cmdbuff); /* no commandline to return */
885 ccline.cmdbuff = NULL;
886 if (!cmd_silent)
887 {
888#ifdef FEAT_RIGHTLEFT
889 if (cmdmsg_rl)
890 msg_col = Columns;
891 else
892#endif
893 msg_col = 0;
894 msg_putchar(' '); /* delete ':' */
895 }
896 redraw_cmdline = TRUE;
897 goto returncmd; /* back to cmd mode */
898 }
899 goto cmdline_changed;
900
901 case K_INS:
902 case K_KINS:
903#ifdef FEAT_FKMAP
904 /* if Farsi mode set, we are in reverse insert mode -
905 Do not change the mode */
906 if (cmd_fkmap)
907 beep_flush();
908 else
909#endif
910 ccline.overstrike = !ccline.overstrike;
911#ifdef CURSOR_SHAPE
912 ui_cursor_shape(); /* may show different cursor shape */
913#endif
914 goto cmdline_not_changed;
915
916 case Ctrl_HAT:
917 if (map_to_exists_mode((char_u *)"", LANGMAP))
918 {
919 /* ":lmap" mappings exists, toggle use of mappings. */
920 State ^= LANGMAP;
921#ifdef USE_IM_CONTROL
922 im_set_active(FALSE); /* Disable input method */
923#endif
924 if (b_im_ptr != NULL)
925 {
926 if (State & LANGMAP)
927 *b_im_ptr = B_IMODE_LMAP;
928 else
929 *b_im_ptr = B_IMODE_NONE;
930 }
931 }
932#ifdef USE_IM_CONTROL
933 else
934 {
935 /* There are no ":lmap" mappings, toggle IM. When
936 * 'imdisable' is set don't try getting the status, it's
937 * always off. */
938 if ((p_imdisable && b_im_ptr != NULL)
939 ? *b_im_ptr == B_IMODE_IM : im_get_status())
940 {
941 im_set_active(FALSE); /* Disable input method */
942 if (b_im_ptr != NULL)
943 *b_im_ptr = B_IMODE_NONE;
944 }
945 else
946 {
947 im_set_active(TRUE); /* Enable input method */
948 if (b_im_ptr != NULL)
949 *b_im_ptr = B_IMODE_IM;
950 }
951 }
952#endif
953 if (b_im_ptr != NULL)
954 {
955 if (b_im_ptr == &curbuf->b_p_iminsert)
956 set_iminsert_global();
957 else
958 set_imsearch_global();
959 }
960#ifdef CURSOR_SHAPE
961 ui_cursor_shape(); /* may show different cursor shape */
962#endif
963#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
964 /* Show/unshow value of 'keymap' in status lines later. */
965 status_redraw_curbuf();
966#endif
967 goto cmdline_not_changed;
968
969/* case '@': only in very old vi */
970 case Ctrl_U:
971 /* delete all characters left of the cursor */
972 j = ccline.cmdpos;
973 ccline.cmdlen -= j;
974 i = ccline.cmdpos = 0;
975 while (i < ccline.cmdlen)
976 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
977 /* Truncate at the end, required for multi-byte chars. */
978 ccline.cmdbuff[ccline.cmdlen] = NUL;
979 redrawcmd();
980 goto cmdline_changed;
981
982#ifdef FEAT_CLIPBOARD
983 case Ctrl_Y:
984 /* Copy the modeless selection, if there is one. */
985 if (clip_star.state != SELECT_CLEARED)
986 {
987 if (clip_star.state == SELECT_DONE)
988 clip_copy_modeless_selection(TRUE);
989 goto cmdline_not_changed;
990 }
991 break;
992#endif
993
994 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
995 case Ctrl_C:
996 /* In exmode it doesn't make sense to return. */
997 if (exmode_active)
998 goto cmdline_not_changed;
999
1000 gotesc = TRUE; /* will free ccline.cmdbuff after
1001 putting it in history */
1002 goto returncmd; /* back to cmd mode */
1003
1004 case Ctrl_R: /* insert register */
1005#ifdef USE_ON_FLY_SCROLL
1006 dont_scroll = TRUE; /* disallow scrolling here */
1007#endif
1008 putcmdline('"', TRUE);
1009 ++no_mapping;
1010 i = c = safe_vgetc(); /* CTRL-R <char> */
1011 if (i == Ctrl_O)
1012 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1013 if (i == Ctrl_R)
1014 c = safe_vgetc(); /* CTRL-R CTRL-R <char> */
1015 --no_mapping;
1016#ifdef FEAT_EVAL
1017 /*
1018 * Insert the result of an expression.
1019 * Need to save the current command line, to be able to enter
1020 * a new one...
1021 */
1022 new_cmdpos = -1;
1023 if (c == '=')
1024 {
1025 struct cmdline_info save_ccline;
1026
1027 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1028 {
1029 beep_flush();
1030 c = ESC;
1031 }
1032 else
1033 {
1034 save_ccline = ccline;
1035 ccline.cmdbuff = NULL;
1036 ccline.cmdprompt = NULL;
1037 c = get_expr_register();
1038 ccline = save_ccline;
1039 }
1040 }
1041#endif
1042 if (c != ESC) /* use ESC to cancel inserting register */
1043 {
1044 cmdline_paste(c, i == Ctrl_R);
1045 KeyTyped = FALSE; /* Don't do p_wc completion. */
1046#ifdef FEAT_EVAL
1047 if (new_cmdpos >= 0)
1048 {
1049 /* set_cmdline_pos() was used */
1050 if (new_cmdpos > ccline.cmdlen)
1051 ccline.cmdpos = ccline.cmdlen;
1052 else
1053 ccline.cmdpos = new_cmdpos;
1054 }
1055#endif
1056 }
1057 redrawcmd();
1058 goto cmdline_changed;
1059
1060 case Ctrl_D:
1061 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1062 break; /* Use ^D as normal char instead */
1063
1064 redrawcmd();
1065 continue; /* don't do incremental search now */
1066
1067 case K_RIGHT:
1068 case K_S_RIGHT:
1069 case K_C_RIGHT:
1070 do
1071 {
1072 if (ccline.cmdpos >= ccline.cmdlen)
1073 break;
1074 i = cmdline_charsize(ccline.cmdpos);
1075 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1076 break;
1077 ccline.cmdspos += i;
1078#ifdef FEAT_MBYTE
1079 if (has_mbyte)
1080 ccline.cmdpos += (*mb_ptr2len_check)(ccline.cmdbuff
1081 + ccline.cmdpos);
1082 else
1083#endif
1084 ++ccline.cmdpos;
1085 }
1086 while ((c == K_S_RIGHT || c == K_C_RIGHT)
1087 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1088#ifdef FEAT_MBYTE
1089 if (has_mbyte)
1090 set_cmdspos_cursor();
1091#endif
1092 goto cmdline_not_changed;
1093
1094 case K_LEFT:
1095 case K_S_LEFT:
1096 case K_C_LEFT:
1097 do
1098 {
1099 if (ccline.cmdpos == 0)
1100 break;
1101 --ccline.cmdpos;
1102#ifdef FEAT_MBYTE
1103 if (has_mbyte) /* move to first byte of char */
1104 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1105 ccline.cmdbuff + ccline.cmdpos);
1106#endif
1107 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1108 }
1109 while ((c == K_S_LEFT || c == K_C_LEFT)
1110 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1111#ifdef FEAT_MBYTE
1112 if (has_mbyte)
1113 set_cmdspos_cursor();
1114#endif
1115 goto cmdline_not_changed;
1116
1117 case K_IGNORE:
1118 goto cmdline_not_changed; /* Ignore mouse */
1119
1120#ifdef FEAT_MOUSE
1121 case K_MIDDLEDRAG:
1122 case K_MIDDLERELEASE:
1123 goto cmdline_not_changed; /* Ignore mouse */
1124
1125 case K_MIDDLEMOUSE:
1126# ifdef FEAT_GUI
1127 /* When GUI is active, also paste when 'mouse' is empty */
1128 if (!gui.in_use)
1129# endif
1130 if (!mouse_has(MOUSE_COMMAND))
1131 goto cmdline_not_changed; /* Ignore mouse */
1132#ifdef FEAT_CLIPBOARD
1133 if (clip_star.available)
1134 cmdline_paste('*', TRUE);
1135 else
1136#endif
1137 cmdline_paste(0, TRUE);
1138 redrawcmd();
1139 goto cmdline_changed;
1140
1141#ifdef FEAT_DND
1142 case K_DROP:
1143 cmdline_paste('~', TRUE);
1144 redrawcmd();
1145 goto cmdline_changed;
1146#endif
1147
1148 case K_LEFTDRAG:
1149 case K_LEFTRELEASE:
1150 case K_RIGHTDRAG:
1151 case K_RIGHTRELEASE:
1152 /* Ignore drag and release events when the button-down wasn't
1153 * seen before. */
1154 if (ignore_drag_release)
1155 goto cmdline_not_changed;
1156 /* FALLTHROUGH */
1157 case K_LEFTMOUSE:
1158 case K_RIGHTMOUSE:
1159 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1160 ignore_drag_release = TRUE;
1161 else
1162 ignore_drag_release = FALSE;
1163# ifdef FEAT_GUI
1164 /* When GUI is active, also move when 'mouse' is empty */
1165 if (!gui.in_use)
1166# endif
1167 if (!mouse_has(MOUSE_COMMAND))
1168 goto cmdline_not_changed; /* Ignore mouse */
1169# ifdef FEAT_CLIPBOARD
1170 if (mouse_row < cmdline_row && clip_star.available)
1171 {
1172 int button, is_click, is_drag;
1173
1174 /*
1175 * Handle modeless selection.
1176 */
1177 button = get_mouse_button(KEY2TERMCAP1(c),
1178 &is_click, &is_drag);
1179 if (mouse_model_popup() && button == MOUSE_LEFT
1180 && (mod_mask & MOD_MASK_SHIFT))
1181 {
1182 /* Translate shift-left to right button. */
1183 button = MOUSE_RIGHT;
1184 mod_mask &= ~MOD_MASK_SHIFT;
1185 }
1186 clip_modeless(button, is_click, is_drag);
1187 goto cmdline_not_changed;
1188 }
1189# endif
1190
1191 set_cmdspos();
1192 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1193 ++ccline.cmdpos)
1194 {
1195 i = cmdline_charsize(ccline.cmdpos);
1196 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1197 && mouse_col < ccline.cmdspos % Columns + i)
1198 break;
1199#ifdef FEAT_MBYTE
1200 if (has_mbyte)
1201 {
1202 /* Count ">" for double-wide char that doesn't fit. */
1203 correct_cmdspos(ccline.cmdpos, i);
1204 ccline.cmdpos += (*mb_ptr2len_check)(ccline.cmdbuff
1205 + ccline.cmdpos) - 1;
1206 }
1207#endif
1208 ccline.cmdspos += i;
1209 }
1210 goto cmdline_not_changed;
1211
1212 /* Mouse scroll wheel: ignored here */
1213 case K_MOUSEDOWN:
1214 case K_MOUSEUP:
1215 /* Alternate buttons ignored here */
1216 case K_X1MOUSE:
1217 case K_X1DRAG:
1218 case K_X1RELEASE:
1219 case K_X2MOUSE:
1220 case K_X2DRAG:
1221 case K_X2RELEASE:
1222 goto cmdline_not_changed;
1223
1224#endif /* FEAT_MOUSE */
1225
1226#ifdef FEAT_GUI
1227 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1228 case K_LEFTRELEASE_NM:
1229 goto cmdline_not_changed;
1230
1231 case K_VER_SCROLLBAR:
1232 if (!msg_scrolled)
1233 {
1234 gui_do_scroll();
1235 redrawcmd();
1236 }
1237 goto cmdline_not_changed;
1238
1239 case K_HOR_SCROLLBAR:
1240 if (!msg_scrolled)
1241 {
1242 gui_do_horiz_scroll();
1243 redrawcmd();
1244 }
1245 goto cmdline_not_changed;
1246#endif
1247 case K_SELECT: /* end of Select mode mapping - ignore */
1248 goto cmdline_not_changed;
1249
1250 case Ctrl_B: /* begin of command line */
1251 case K_HOME:
1252 case K_KHOME:
1253 case K_XHOME:
1254 case K_S_HOME:
1255 case K_C_HOME:
1256 ccline.cmdpos = 0;
1257 set_cmdspos();
1258 goto cmdline_not_changed;
1259
1260 case Ctrl_E: /* end of command line */
1261 case K_END:
1262 case K_KEND:
1263 case K_XEND:
1264 case K_S_END:
1265 case K_C_END:
1266 ccline.cmdpos = ccline.cmdlen;
1267 set_cmdspos_cursor();
1268 goto cmdline_not_changed;
1269
1270 case Ctrl_A: /* all matches */
1271 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1272 break;
1273 goto cmdline_changed;
1274
1275 case Ctrl_L: /* longest common part */
1276 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1277 break;
1278 goto cmdline_changed;
1279
1280 case Ctrl_N: /* next match */
1281 case Ctrl_P: /* previous match */
1282 if (xpc.xp_numfiles > 0)
1283 {
1284 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1285 == FAIL)
1286 break;
1287 goto cmdline_changed;
1288 }
1289
1290#ifdef FEAT_CMDHIST
1291 case K_UP:
1292 case K_DOWN:
1293 case K_S_UP:
1294 case K_S_DOWN:
1295 case K_PAGEUP:
1296 case K_KPAGEUP:
1297 case K_PAGEDOWN:
1298 case K_KPAGEDOWN:
1299 if (hislen == 0 || firstc == NUL) /* no history */
1300 goto cmdline_not_changed;
1301
1302 i = hiscnt;
1303
1304 /* save current command string so it can be restored later */
1305 if (lookfor == NULL)
1306 {
1307 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1308 goto cmdline_not_changed;
1309 lookfor[ccline.cmdpos] = NUL;
1310 }
1311
1312 j = (int)STRLEN(lookfor);
1313 for (;;)
1314 {
1315 /* one step backwards */
1316 if (c == K_UP || c == K_S_UP || c == Ctrl_P ||
1317 c == K_PAGEUP || c == K_KPAGEUP)
1318 {
1319 if (hiscnt == hislen) /* first time */
1320 hiscnt = hisidx[histype];
1321 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1322 hiscnt = hislen - 1;
1323 else if (hiscnt != hisidx[histype] + 1)
1324 --hiscnt;
1325 else /* at top of list */
1326 {
1327 hiscnt = i;
1328 break;
1329 }
1330 }
1331 else /* one step forwards */
1332 {
1333 /* on last entry, clear the line */
1334 if (hiscnt == hisidx[histype])
1335 {
1336 hiscnt = hislen;
1337 break;
1338 }
1339
1340 /* not on a history line, nothing to do */
1341 if (hiscnt == hislen)
1342 break;
1343 if (hiscnt == hislen - 1) /* wrap around */
1344 hiscnt = 0;
1345 else
1346 ++hiscnt;
1347 }
1348 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1349 {
1350 hiscnt = i;
1351 break;
1352 }
1353 if ((c != K_UP && c != K_DOWN) || hiscnt == i
1354 || STRNCMP(history[histype][hiscnt].hisstr,
1355 lookfor, (size_t)j) == 0)
1356 break;
1357 }
1358
1359 if (hiscnt != i) /* jumped to other entry */
1360 {
1361 char_u *p;
1362 int len;
1363 int old_firstc;
1364
1365 vim_free(ccline.cmdbuff);
1366 if (hiscnt == hislen)
1367 p = lookfor; /* back to the old one */
1368 else
1369 p = history[histype][hiscnt].hisstr;
1370
1371 if (histype == HIST_SEARCH
1372 && p != lookfor
1373 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1374 {
1375 /* Correct for the separator character used when
1376 * adding the history entry vs the one used now.
1377 * First loop: count length.
1378 * Second loop: copy the characters. */
1379 for (i = 0; i <= 1; ++i)
1380 {
1381 len = 0;
1382 for (j = 0; p[j] != NUL; ++j)
1383 {
1384 /* Replace old sep with new sep, unless it is
1385 * escaped. */
1386 if (p[j] == old_firstc
1387 && (j == 0 || p[j - 1] != '\\'))
1388 {
1389 if (i > 0)
1390 ccline.cmdbuff[len] = firstc;
1391 }
1392 else
1393 {
1394 /* Escape new sep, unless it is already
1395 * escaped. */
1396 if (p[j] == firstc
1397 && (j == 0 || p[j - 1] != '\\'))
1398 {
1399 if (i > 0)
1400 ccline.cmdbuff[len] = '\\';
1401 ++len;
1402 }
1403 if (i > 0)
1404 ccline.cmdbuff[len] = p[j];
1405 }
1406 ++len;
1407 }
1408 if (i == 0)
1409 {
1410 alloc_cmdbuff(len);
1411 if (ccline.cmdbuff == NULL)
1412 goto returncmd;
1413 }
1414 }
1415 ccline.cmdbuff[len] = NUL;
1416 }
1417 else
1418 {
1419 alloc_cmdbuff((int)STRLEN(p));
1420 if (ccline.cmdbuff == NULL)
1421 goto returncmd;
1422 STRCPY(ccline.cmdbuff, p);
1423 }
1424
1425 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1426 redrawcmd();
1427 goto cmdline_changed;
1428 }
1429 beep_flush();
1430 goto cmdline_not_changed;
1431#endif
1432
1433 case Ctrl_V:
1434 case Ctrl_Q:
1435#ifdef FEAT_MOUSE
1436 ignore_drag_release = TRUE;
1437#endif
1438 putcmdline('^', TRUE);
1439 c = get_literal(); /* get next (two) character(s) */
1440 do_abbr = FALSE; /* don't do abbreviation now */
1441#ifdef FEAT_MBYTE
1442 /* may need to remove ^ when composing char was typed */
1443 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1444 {
1445 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1446 msg_putchar(' ');
1447 cursorcmd();
1448 }
1449#endif
1450 break;
1451
1452#ifdef FEAT_DIGRAPHS
1453 case Ctrl_K:
1454#ifdef FEAT_MOUSE
1455 ignore_drag_release = TRUE;
1456#endif
1457 putcmdline('?', TRUE);
1458#ifdef USE_ON_FLY_SCROLL
1459 dont_scroll = TRUE; /* disallow scrolling here */
1460#endif
1461 c = get_digraph(TRUE);
1462 if (c != NUL)
1463 break;
1464
1465 redrawcmd();
1466 goto cmdline_not_changed;
1467#endif /* FEAT_DIGRAPHS */
1468
1469#ifdef FEAT_RIGHTLEFT
1470 case Ctrl__: /* CTRL-_: switch language mode */
1471 if (!p_ari)
1472 break;
1473#ifdef FEAT_FKMAP
1474 if (p_altkeymap)
1475 {
1476 cmd_fkmap = !cmd_fkmap;
1477 if (cmd_fkmap) /* in Farsi always in Insert mode */
1478 ccline.overstrike = FALSE;
1479 }
1480 else /* Hebrew is default */
1481#endif
1482 cmd_hkmap = !cmd_hkmap;
1483 goto cmdline_not_changed;
1484#endif
1485
1486 default:
1487#ifdef UNIX
1488 if (c == intr_char)
1489 {
1490 gotesc = TRUE; /* will free ccline.cmdbuff after
1491 putting it in history */
1492 goto returncmd; /* back to Normal mode */
1493 }
1494#endif
1495 /*
1496 * Normal character with no special meaning. Just set mod_mask
1497 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1498 * the string <S-Space>. This should only happen after ^V.
1499 */
1500 if (!IS_SPECIAL(c))
1501 mod_mask = 0x0;
1502 break;
1503 }
1504 /*
1505 * End of switch on command line character.
1506 * We come here if we have a normal character.
1507 */
1508
1509 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1510#ifdef FEAT_MBYTE
1511 /* Add ABBR_OFF for characters above 0x100, this is
1512 * what check_abbr() expects. */
1513 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1514#endif
1515 c))
1516 goto cmdline_changed;
1517
1518 /*
1519 * put the character in the command line
1520 */
1521 if (IS_SPECIAL(c) || mod_mask != 0)
1522 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1523 else
1524 {
1525#ifdef FEAT_MBYTE
1526 if (has_mbyte)
1527 {
1528 j = (*mb_char2bytes)(c, IObuff);
1529 IObuff[j] = NUL; /* exclude composing chars */
1530 put_on_cmdline(IObuff, j, TRUE);
1531 }
1532 else
1533#endif
1534 {
1535 IObuff[0] = c;
1536 put_on_cmdline(IObuff, 1, TRUE);
1537 }
1538 }
1539 goto cmdline_changed;
1540
1541/*
1542 * This part implements incremental searches for "/" and "?"
1543 * Jump to cmdline_not_changed when a character has been read but the command
1544 * line did not change. Then we only search and redraw if something changed in
1545 * the past.
1546 * Jump to cmdline_changed when the command line did change.
1547 * (Sorry for the goto's, I know it is ugly).
1548 */
1549cmdline_not_changed:
1550#ifdef FEAT_SEARCH_EXTRA
1551 if (!incsearch_postponed)
1552 continue;
1553#endif
1554
1555cmdline_changed:
1556#ifdef FEAT_SEARCH_EXTRA
1557 /*
1558 * 'incsearch' highlighting.
1559 */
1560 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1561 {
1562 /* if there is a character waiting, search and redraw later */
1563 if (char_avail())
1564 {
1565 incsearch_postponed = TRUE;
1566 continue;
1567 }
1568 incsearch_postponed = FALSE;
1569 curwin->w_cursor = old_cursor; /* start at old position */
1570
1571 /* If there is no command line, don't do anything */
1572 if (ccline.cmdlen == 0)
1573 i = 0;
1574 else
1575 {
1576 cursor_off(); /* so the user knows we're busy */
1577 out_flush();
1578 ++emsg_off; /* So it doesn't beep if bad expr */
1579 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1580 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1581 --emsg_off;
1582 /* if interrupted while searching, behave like it failed */
1583 if (got_int)
1584 {
1585 (void)vpeekc(); /* remove <C-C> from input stream */
1586 got_int = FALSE; /* don't abandon the command line */
1587 i = 0;
1588 }
1589 else if (char_avail())
1590 /* cancelled searching because a char was typed */
1591 incsearch_postponed = TRUE;
1592 }
1593 if (i)
1594 highlight_match = TRUE; /* highlight position */
1595 else
1596 highlight_match = FALSE; /* remove highlight */
1597
1598 /* first restore the old curwin values, so the screen is
1599 * positioned in the same way as the actual search command */
1600 curwin->w_leftcol = old_leftcol;
1601 curwin->w_topline = old_topline;
1602# ifdef FEAT_DIFF
1603 curwin->w_topfill = old_topfill;
1604# endif
1605 curwin->w_botline = old_botline;
1606 changed_cline_bef_curs();
1607 update_topline();
1608
1609 if (i != 0)
1610 {
1611 /*
1612 * First move cursor to end of match, then to start. This
1613 * moves the whole match onto the screen when 'nowrap' is set.
1614 */
1615 i = curwin->w_cursor.col;
1616 curwin->w_cursor.lnum += search_match_lines;
1617 curwin->w_cursor.col = search_match_endcol;
1618 validate_cursor();
1619 curwin->w_cursor.lnum -= search_match_lines;
1620 curwin->w_cursor.col = i;
1621 }
1622 validate_cursor();
1623
1624 update_screen(NOT_VALID);
1625 msg_starthere();
1626 redrawcmdline();
1627 did_incsearch = TRUE;
1628 }
1629#else /* FEAT_SEARCH_EXTRA */
1630 ;
1631#endif
1632
1633#ifdef FEAT_RIGHTLEFT
1634 if (cmdmsg_rl
1635# ifdef FEAT_ARABIC
1636 || p_arshape
1637# endif
1638 )
1639 /* Always redraw the whole command line to fix shaping and
1640 * right-left typing. Not efficient, but it works. */
1641 redrawcmd();
1642#endif
1643 }
1644
1645returncmd:
1646
1647#ifdef FEAT_RIGHTLEFT
1648 cmdmsg_rl = FALSE;
1649#endif
1650
1651#ifdef FEAT_FKMAP
1652 cmd_fkmap = 0;
1653#endif
1654
1655 ExpandCleanup(&xpc);
1656
1657#ifdef FEAT_SEARCH_EXTRA
1658 if (did_incsearch)
1659 {
1660 curwin->w_cursor = old_cursor;
1661 curwin->w_curswant = old_curswant;
1662 curwin->w_leftcol = old_leftcol;
1663 curwin->w_topline = old_topline;
1664# ifdef FEAT_DIFF
1665 curwin->w_topfill = old_topfill;
1666# endif
1667 curwin->w_botline = old_botline;
1668 highlight_match = FALSE;
1669 validate_cursor(); /* needed for TAB */
1670 redraw_later(NOT_VALID);
1671 }
1672#endif
1673
1674 if (ccline.cmdbuff != NULL)
1675 {
1676 /*
1677 * Put line in history buffer (":" and "=" only when it was typed).
1678 */
1679#ifdef FEAT_CMDHIST
1680 if (ccline.cmdlen && firstc != NUL
1681 && (some_key_typed || histype == HIST_SEARCH))
1682 {
1683 add_to_history(histype, ccline.cmdbuff, TRUE,
1684 histype == HIST_SEARCH ? firstc : NUL);
1685 if (firstc == ':')
1686 {
1687 vim_free(new_last_cmdline);
1688 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1689 }
1690 }
1691#endif
1692
1693 if (gotesc) /* abandon command line */
1694 {
1695 vim_free(ccline.cmdbuff);
1696 ccline.cmdbuff = NULL;
1697 if (msg_scrolled == 0)
1698 compute_cmdrow();
1699 MSG("");
1700 redraw_cmdline = TRUE;
1701 }
1702 }
1703
1704 /*
1705 * If the screen was shifted up, redraw the whole screen (later).
1706 * If the line is too long, clear it, so ruler and shown command do
1707 * not get printed in the middle of it.
1708 */
1709 msg_check();
1710 msg_scroll = save_msg_scroll;
1711 redir_off = FALSE;
1712
1713 /* When the command line was typed, no need for a wait-return prompt. */
1714 if (some_key_typed)
1715 need_wait_return = FALSE;
1716
1717 State = save_State;
1718#ifdef USE_IM_CONTROL
1719 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1720 im_save_status(b_im_ptr);
1721 im_set_active(FALSE);
1722#endif
1723#ifdef FEAT_MOUSE
1724 setmouse();
1725#endif
1726#ifdef CURSOR_SHAPE
1727 ui_cursor_shape(); /* may show different cursor shape */
1728#endif
1729
1730 return ccline.cmdbuff;
1731}
1732
1733#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1734/*
1735 * Get a command line with a prompt.
1736 * This is prepared to be called recursively from getcmdline() (e.g. by
1737 * f_input() when evaluating an expression from CTRL-R =).
1738 * Returns the command line in allocated memory, or NULL.
1739 */
1740 char_u *
1741getcmdline_prompt(firstc, prompt, attr)
1742 int firstc;
1743 char_u *prompt; /* command line prompt */
1744 int attr; /* attributes for prompt */
1745{
1746 char_u *s;
1747 struct cmdline_info save_ccline;
1748 int msg_col_save = msg_col;
1749
1750 save_ccline = ccline;
1751 ccline.cmdbuff = NULL;
1752 ccline.cmdprompt = prompt;
1753 ccline.cmdattr = attr;
1754 s = getcmdline(firstc, 1L, 0);
1755 ccline = save_ccline;
1756 /* Restore msg_col, the prompt from input() may have changed it. */
1757 msg_col = msg_col_save;
1758
1759 return s;
1760}
1761#endif
1762
1763 static int
1764cmdline_charsize(idx)
1765 int idx;
1766{
1767#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1768 if (cmdline_star > 0) /* showing '*', always 1 position */
1769 return 1;
1770#endif
1771 return ptr2cells(ccline.cmdbuff + idx);
1772}
1773
1774/*
1775 * Compute the offset of the cursor on the command line for the prompt and
1776 * indent.
1777 */
1778 static void
1779set_cmdspos()
1780{
1781 if (ccline.cmdfirstc)
1782 ccline.cmdspos = 1 + ccline.cmdindent;
1783 else
1784 ccline.cmdspos = 0 + ccline.cmdindent;
1785}
1786
1787/*
1788 * Compute the screen position for the cursor on the command line.
1789 */
1790 static void
1791set_cmdspos_cursor()
1792{
1793 int i, m, c;
1794
1795 set_cmdspos();
1796 if (KeyTyped)
1797 m = Columns * Rows;
1798 else
1799 m = MAXCOL;
1800 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
1801 {
1802 c = cmdline_charsize(i);
1803#ifdef FEAT_MBYTE
1804 /* Count ">" for double-wide multi-byte char that doesn't fit. */
1805 if (has_mbyte)
1806 correct_cmdspos(i, c);
1807#endif
1808 /* If the cmdline doesn't fit, put cursor on last visible char. */
1809 if ((ccline.cmdspos += c) >= m)
1810 {
1811 ccline.cmdpos = i - 1;
1812 ccline.cmdspos -= c;
1813 break;
1814 }
1815#ifdef FEAT_MBYTE
1816 if (has_mbyte)
1817 i += (*mb_ptr2len_check)(ccline.cmdbuff + i) - 1;
1818#endif
1819 }
1820}
1821
1822#ifdef FEAT_MBYTE
1823/*
1824 * Check if the character at "idx", which is "cells" wide, is a multi-byte
1825 * character that doesn't fit, so that a ">" must be displayed.
1826 */
1827 static void
1828correct_cmdspos(idx, cells)
1829 int idx;
1830 int cells;
1831{
1832 if ((*mb_ptr2len_check)(ccline.cmdbuff + idx) > 1
1833 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
1834 && ccline.cmdspos % Columns + cells > Columns)
1835 ccline.cmdspos++;
1836}
1837#endif
1838
1839/*
1840 * Get an Ex command line for the ":" command.
1841 */
1842/* ARGSUSED */
1843 char_u *
1844getexline(c, dummy, indent)
1845 int c; /* normally ':', NUL for ":append" */
1846 void *dummy; /* cookie not used */
1847 int indent; /* indent for inside conditionals */
1848{
1849 /* When executing a register, remove ':' that's in front of each line. */
1850 if (exec_from_reg && vpeekc() == ':')
1851 (void)vgetc();
1852 return getcmdline(c, 1L, indent);
1853}
1854
1855/*
1856 * Get an Ex command line for Ex mode.
1857 * In Ex mode we only use the OS supplied line editing features and no
1858 * mappings or abbreviations.
1859 */
1860/* ARGSUSED */
1861 char_u *
1862getexmodeline(c, dummy, indent)
1863 int c; /* normally ':', NUL for ":append" */
1864 void *dummy; /* cookie not used */
1865 int indent; /* indent for inside conditionals */
1866{
1867 garray_T line_ga;
1868 int len;
1869 int off = 0;
1870 char_u *p;
1871 int finished = FALSE;
1872#if defined(FEAT_GUI) || defined(NO_COOKED_INPUT)
1873 int startcol = 0;
1874 int c1;
1875 int escaped = FALSE; /* CTRL-V typed */
1876 int vcol = 0;
1877#endif
1878
1879 /* Switch cursor on now. This avoids that it happens after the "\n", which
1880 * confuses the system function that computes tabstops. */
1881 cursor_on();
1882
1883 /* always start in column 0; write a newline if necessary */
1884 compute_cmdrow();
1885 if (msg_col)
1886 msg_putchar('\n');
1887 if (c == ':')
1888 {
1889 msg_putchar(':');
1890 while (indent-- > 0)
1891 msg_putchar(' ');
1892#if defined(FEAT_GUI) || defined(NO_COOKED_INPUT)
1893 startcol = msg_col;
1894#endif
1895 }
1896
1897 ga_init2(&line_ga, 1, 30);
1898
1899 /*
1900 * Get the line, one character at a time.
1901 */
1902 got_int = FALSE;
1903 while (!got_int && !finished)
1904 {
1905 if (ga_grow(&line_ga, 40) == FAIL)
1906 break;
1907 p = (char_u *)line_ga.ga_data + line_ga.ga_len;
1908
1909 /* Get one character (inchar gets a third of maxlen characters!) */
1910 len = inchar(p + off, 3, -1L, 0);
1911 if (len < 0)
1912 continue; /* end of input script reached */
1913 /* for a special character, we need at least three characters */
1914 if ((*p == K_SPECIAL || *p == CSI) && off + len < 3)
1915 {
1916 off += len;
1917 continue;
1918 }
1919 len += off;
1920 off = 0;
1921
1922 /*
1923 * When using the GUI, and for systems that don't have cooked input,
1924 * handle line editing here.
1925 */
1926#if defined(FEAT_GUI) || defined(NO_COOKED_INPUT)
1927# ifndef NO_COOKED_INPUT
1928 if (gui.in_use)
1929# endif
1930 {
1931 if (got_int)
1932 {
1933 msg_putchar('\n');
1934 break;
1935 }
1936
1937 while (len > 0)
1938 {
1939 c1 = *p++;
1940 --len;
1941 if ((c1 == K_SPECIAL
1942# if !defined(NO_COOKED_INPUT) || defined(FEAT_GUI)
1943 || c1 == CSI
1944# endif
1945 ) && len >= 2)
1946 {
1947 c1 = TO_SPECIAL(p[0], p[1]);
1948 p += 2;
1949 len -= 2;
1950 }
1951
1952 if (!escaped)
1953 {
1954 /* CR typed means "enter", which is NL */
1955 if (c1 == '\r')
1956 c1 = '\n';
1957
1958 if (c1 == BS || c1 == K_BS
1959 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
1960 {
1961 if (line_ga.ga_len > 0)
1962 {
1963 int i, v;
1964 char_u *q;
1965
1966 --line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 /* compute column that cursor should be in */
1968 v = 0;
1969 q = ((char_u *)line_ga.ga_data);
1970 for (i = 0; i < line_ga.ga_len; ++i)
1971 {
1972 if (*q == TAB)
1973 v += 8 - v % 8;
1974 else
1975 v += ptr2cells(q);
1976 ++q;
1977 }
1978 /* erase characters to position cursor */
1979 while (vcol > v)
1980 {
1981 msg_putchar('\b');
1982 msg_putchar(' ');
1983 msg_putchar('\b');
1984 --vcol;
1985 }
1986 }
1987 continue;
1988 }
1989
1990 if (c1 == Ctrl_U)
1991 {
1992 msg_col = startcol;
1993 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 line_ga.ga_len = 0;
1995 continue;
1996 }
1997
1998 if (c1 == Ctrl_V)
1999 {
2000 escaped = TRUE;
2001 continue;
2002 }
2003 }
2004
2005 if (IS_SPECIAL(c1))
2006 c1 = '?';
2007 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2008 if (c1 == '\n')
2009 msg_putchar('\n');
2010 else if (c1 == TAB)
2011 {
2012 /* Don't use chartabsize(), 'ts' can be different */
2013 do
2014 {
2015 msg_putchar(' ');
2016 } while (++vcol % 8);
2017 }
2018 else
2019 {
2020 msg_outtrans_len(
2021 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2022 vcol += char2cells(c1);
2023 }
2024 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 escaped = FALSE;
2026 }
2027 windgoto(msg_row, msg_col);
2028 }
2029# ifndef NO_COOKED_INPUT
2030 else
2031# endif
2032#endif
2033#ifndef NO_COOKED_INPUT
2034 {
2035 line_ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
2037#endif
2038 p = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
2039 if (line_ga.ga_len && p[-1] == '\n')
2040 {
2041 finished = TRUE;
2042 --line_ga.ga_len;
2043 --p;
2044 *p = NUL;
2045 }
2046 }
2047
2048 /* note that cursor has moved, because of the echoed <CR> */
2049 screen_down();
2050 /* make following messages go to the next line */
2051 msg_didout = FALSE;
2052 msg_col = 0;
2053 if (msg_row < Rows - 1)
2054 ++msg_row;
2055 emsg_on_display = FALSE; /* don't want ui_delay() */
2056
2057 if (got_int)
2058 ga_clear(&line_ga);
2059
2060 return (char_u *)line_ga.ga_data;
2061}
2062
2063#ifdef CURSOR_SHAPE
2064/*
2065 * Return TRUE if ccline.overstrike is on.
2066 */
2067 int
2068cmdline_overstrike()
2069{
2070 return ccline.overstrike;
2071}
2072
2073/*
2074 * Return TRUE if the cursor is at the end of the cmdline.
2075 */
2076 int
2077cmdline_at_end()
2078{
2079 return (ccline.cmdpos >= ccline.cmdlen);
2080}
2081#endif
2082
Bram Moolenaar81695252004-12-29 20:58:21 +00002083#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE))) \
2084 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085/*
2086 * Return the virtual column number at the current cursor position.
2087 * This is used by the IM code to obtain the start of the preedit string.
2088 */
2089 colnr_T
2090cmdline_getvcol_cursor()
2091{
2092 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2093 return MAXCOL;
2094
2095# ifdef FEAT_MBYTE
2096 if (has_mbyte)
2097 {
2098 colnr_T col;
2099 int i = 0;
2100
2101 for (col = 0; i < ccline.cmdpos; ++col)
2102 i += (*mb_ptr2len_check)(ccline.cmdbuff + i);
2103
2104 return col;
2105 }
2106 else
2107# endif
2108 return ccline.cmdpos;
2109}
2110#endif
2111
2112#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2113/*
2114 * If part of the command line is an IM preedit string, redraw it with
2115 * IM feedback attributes. The cursor position is restored after drawing.
2116 */
2117 static void
2118redrawcmd_preedit()
2119{
2120 if ((State & CMDLINE)
2121 && xic != NULL
2122 && im_get_status()
2123 && !p_imdisable
2124 && im_is_preediting())
2125 {
2126 int cmdpos = 0;
2127 int cmdspos;
2128 int old_row;
2129 int old_col;
2130 colnr_T col;
2131
2132 old_row = msg_row;
2133 old_col = msg_col;
2134 cmdspos = ((ccline.cmdfirstc) ? 1 : 0) + ccline.cmdindent;
2135
2136# ifdef FEAT_MBYTE
2137 if (has_mbyte)
2138 {
2139 for (col = 0; col < preedit_start_col
2140 && cmdpos < ccline.cmdlen; ++col)
2141 {
2142 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
2143 cmdpos += (*mb_ptr2len_check)(ccline.cmdbuff + cmdpos);
2144 }
2145 }
2146 else
2147# endif
2148 {
2149 cmdspos += preedit_start_col;
2150 cmdpos += preedit_start_col;
2151 }
2152
2153 msg_row = cmdline_row + (cmdspos / (int)Columns);
2154 msg_col = cmdspos % (int)Columns;
2155 if (msg_row >= Rows)
2156 msg_row = Rows - 1;
2157
2158 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2159 {
2160 int char_len;
2161 int char_attr;
2162
2163 char_attr = im_get_feedback_attr(col);
2164 if (char_attr < 0)
2165 break; /* end of preedit string */
2166
2167# ifdef FEAT_MBYTE
2168 if (has_mbyte)
2169 char_len = (*mb_ptr2len_check)(ccline.cmdbuff + cmdpos);
2170 else
2171# endif
2172 char_len = 1;
2173
2174 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2175 cmdpos += char_len;
2176 }
2177
2178 msg_row = old_row;
2179 msg_col = old_col;
2180 }
2181}
2182#endif /* FEAT_XIM && FEAT_GUI_GTK */
2183
2184/*
2185 * Allocate a new command line buffer.
2186 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2187 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2188 */
2189 static void
2190alloc_cmdbuff(len)
2191 int len;
2192{
2193 /*
2194 * give some extra space to avoid having to allocate all the time
2195 */
2196 if (len < 80)
2197 len = 100;
2198 else
2199 len += 20;
2200
2201 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2202 ccline.cmdbufflen = len;
2203}
2204
2205/*
2206 * Re-allocate the command line to length len + something extra.
2207 * return FAIL for failure, OK otherwise
2208 */
2209 static int
2210realloc_cmdbuff(len)
2211 int len;
2212{
2213 char_u *p;
2214
2215 p = ccline.cmdbuff;
2216 alloc_cmdbuff(len); /* will get some more */
2217 if (ccline.cmdbuff == NULL) /* out of memory */
2218 {
2219 ccline.cmdbuff = p; /* keep the old one */
2220 return FAIL;
2221 }
2222 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2223 vim_free(p);
2224 return OK;
2225}
2226
2227/*
2228 * Draw part of the cmdline at the current cursor position. But draw stars
2229 * when cmdline_star is TRUE.
2230 */
2231 static void
2232draw_cmdline(start, len)
2233 int start;
2234 int len;
2235{
2236#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2237 int i;
2238
2239 if (cmdline_star > 0)
2240 for (i = 0; i < len; ++i)
2241 {
2242 msg_putchar('*');
2243# ifdef FEAT_MBYTE
2244 if (has_mbyte)
2245 i += (*mb_ptr2len_check)(ccline.cmdbuff + start + i) - 1;
2246# endif
2247 }
2248 else
2249#endif
2250#ifdef FEAT_ARABIC
2251 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2252 {
2253 static char_u *buf;
2254 static int buflen = 0;
2255 char_u *p;
2256 int j;
2257 int newlen = 0;
2258 int mb_l;
2259 int pc, pc1;
2260 int prev_c = 0;
2261 int prev_c1 = 0;
2262 int u8c, u8c_c1, u8c_c2;
2263 int nc = 0;
2264 int dummy;
2265
2266 /*
2267 * Do arabic shaping into a temporary buffer. This is very
2268 * inefficient!
2269 */
2270 if (len * 2 > buflen)
2271 {
2272 /* Re-allocate the buffer. We keep it around to avoid a lot of
2273 * alloc()/free() calls. */
2274 vim_free(buf);
2275 buflen = len * 2;
2276 buf = alloc(buflen);
2277 if (buf == NULL)
2278 return; /* out of memory */
2279 }
2280
2281 for (j = start; j < start + len; j += mb_l)
2282 {
2283 p = ccline.cmdbuff + j;
2284 u8c = utfc_ptr2char_len(p, &u8c_c1, &u8c_c2, start + len - j);
2285 mb_l = utfc_ptr2len_check_len(p, start + len - j);
2286 if (ARABIC_CHAR(u8c))
2287 {
2288 /* Do Arabic shaping. */
2289 if (cmdmsg_rl)
2290 {
2291 /* displaying from right to left */
2292 pc = prev_c;
2293 pc1 = prev_c1;
2294 prev_c1 = u8c_c1;
2295 if (j + mb_l >= start + len)
2296 nc = NUL;
2297 else
2298 nc = utf_ptr2char(p + mb_l);
2299 }
2300 else
2301 {
2302 /* displaying from left to right */
2303 if (j + mb_l >= start + len)
2304 pc = NUL;
2305 else
2306 pc = utfc_ptr2char_len(p + mb_l, &pc1, &dummy,
2307 start + len - j - mb_l);
2308 nc = prev_c;
2309 }
2310 prev_c = u8c;
2311
2312 u8c = arabic_shape(u8c, NULL, &u8c_c1, pc, pc1, nc);
2313
2314 newlen += (*mb_char2bytes)(u8c, buf + newlen);
2315 if (u8c_c1 != 0)
2316 {
2317 newlen += (*mb_char2bytes)(u8c_c1, buf + newlen);
2318 if (u8c_c2 != 0)
2319 newlen += (*mb_char2bytes)(u8c_c2, buf + newlen);
2320 }
2321 }
2322 else
2323 {
2324 prev_c = u8c;
2325 mch_memmove(buf + newlen, p, mb_l);
2326 newlen += mb_l;
2327 }
2328 }
2329
2330 msg_outtrans_len(buf, newlen);
2331 }
2332 else
2333#endif
2334 msg_outtrans_len(ccline.cmdbuff + start, len);
2335}
2336
2337/*
2338 * Put a character on the command line. Shifts the following text to the
2339 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2340 * "c" must be printable (fit in one display cell)!
2341 */
2342 void
2343putcmdline(c, shift)
2344 int c;
2345 int shift;
2346{
2347 if (cmd_silent)
2348 return;
2349 msg_no_more = TRUE;
2350 msg_putchar(c);
2351 if (shift)
2352 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2353 msg_no_more = FALSE;
2354 cursorcmd();
2355}
2356
2357/*
2358 * Undo a putcmdline(c, FALSE).
2359 */
2360 void
2361unputcmdline()
2362{
2363 if (cmd_silent)
2364 return;
2365 msg_no_more = TRUE;
2366 if (ccline.cmdlen == ccline.cmdpos)
2367 msg_putchar(' ');
2368 else
2369 draw_cmdline(ccline.cmdpos, 1);
2370 msg_no_more = FALSE;
2371 cursorcmd();
2372}
2373
2374/*
2375 * Put the given string, of the given length, onto the command line.
2376 * If len is -1, then STRLEN() is used to calculate the length.
2377 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2378 * part will be redrawn, otherwise it will not. If this function is called
2379 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2380 * called afterwards.
2381 */
2382 int
2383put_on_cmdline(str, len, redraw)
2384 char_u *str;
2385 int len;
2386 int redraw;
2387{
2388 int retval;
2389 int i;
2390 int m;
2391 int c;
2392
2393 if (len < 0)
2394 len = (int)STRLEN(str);
2395
2396 /* Check if ccline.cmdbuff needs to be longer */
2397 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2398 retval = realloc_cmdbuff(ccline.cmdlen + len);
2399 else
2400 retval = OK;
2401 if (retval == OK)
2402 {
2403 if (!ccline.overstrike)
2404 {
2405 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2406 ccline.cmdbuff + ccline.cmdpos,
2407 (size_t)(ccline.cmdlen - ccline.cmdpos));
2408 ccline.cmdlen += len;
2409 }
2410 else
2411 {
2412#ifdef FEAT_MBYTE
2413 if (has_mbyte)
2414 {
2415 /* Count nr of characters in the new string. */
2416 m = 0;
2417 for (i = 0; i < len; i += (*mb_ptr2len_check)(str + i))
2418 ++m;
2419 /* Count nr of bytes in cmdline that are overwritten by these
2420 * characters. */
2421 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
2422 i += (*mb_ptr2len_check)(ccline.cmdbuff + i))
2423 --m;
2424 if (i < ccline.cmdlen)
2425 {
2426 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2427 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2428 ccline.cmdlen += ccline.cmdpos + len - i;
2429 }
2430 else
2431 ccline.cmdlen = ccline.cmdpos + len;
2432 }
2433 else
2434#endif
2435 if (ccline.cmdpos + len > ccline.cmdlen)
2436 ccline.cmdlen = ccline.cmdpos + len;
2437 }
2438 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2439 ccline.cmdbuff[ccline.cmdlen] = NUL;
2440
2441#ifdef FEAT_MBYTE
2442 if (enc_utf8)
2443 {
2444 /* When the inserted text starts with a composing character,
2445 * backup to the character before it. There could be two of them.
2446 */
2447 i = 0;
2448 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2449 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2450 {
2451 i = (*mb_head_off)(ccline.cmdbuff,
2452 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2453 ccline.cmdpos -= i;
2454 len += i;
2455 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2456 }
2457# ifdef FEAT_ARABIC
2458 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2459 {
2460 /* Check the previous character for Arabic combining pair. */
2461 i = (*mb_head_off)(ccline.cmdbuff,
2462 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2463 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2464 + ccline.cmdpos - i), c))
2465 {
2466 ccline.cmdpos -= i;
2467 len += i;
2468 }
2469 else
2470 i = 0;
2471 }
2472# endif
2473 if (i != 0)
2474 {
2475 /* Also backup the cursor position. */
2476 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2477 ccline.cmdspos -= i;
2478 msg_col -= i;
2479 if (msg_col < 0)
2480 {
2481 msg_col += Columns;
2482 --msg_row;
2483 }
2484 }
2485 }
2486#endif
2487
2488 if (redraw && !cmd_silent)
2489 {
2490 msg_no_more = TRUE;
2491 i = cmdline_row;
2492 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2493 /* Avoid clearing the rest of the line too often. */
2494 if (cmdline_row != i || ccline.overstrike)
2495 msg_clr_eos();
2496 msg_no_more = FALSE;
2497 }
2498#ifdef FEAT_FKMAP
2499 /*
2500 * If we are in Farsi command mode, the character input must be in
2501 * Insert mode. So do not advance the cmdpos.
2502 */
2503 if (!cmd_fkmap)
2504#endif
2505 {
2506 if (KeyTyped)
2507 m = Columns * Rows;
2508 else
2509 m = MAXCOL;
2510 for (i = 0; i < len; ++i)
2511 {
2512 c = cmdline_charsize(ccline.cmdpos);
2513#ifdef FEAT_MBYTE
2514 /* count ">" for a double-wide char that doesn't fit. */
2515 if (has_mbyte)
2516 correct_cmdspos(ccline.cmdpos, c);
2517#endif
2518 /* Stop cursor at the end of the screen */
2519 if (ccline.cmdspos + c >= m)
2520 break;
2521 ccline.cmdspos += c;
2522#ifdef FEAT_MBYTE
2523 if (has_mbyte)
2524 {
2525 c = (*mb_ptr2len_check)(ccline.cmdbuff + ccline.cmdpos) - 1;
2526 if (c > len - i - 1)
2527 c = len - i - 1;
2528 ccline.cmdpos += c;
2529 i += c;
2530 }
2531#endif
2532 ++ccline.cmdpos;
2533 }
2534 }
2535 }
2536 if (redraw)
2537 msg_check();
2538 return retval;
2539}
2540
Bram Moolenaar8299df92004-07-10 09:47:34 +00002541/*
2542 * paste a yank register into the command line.
2543 * used by CTRL-R command in command-line mode
2544 * insert_reg() can't be used here, because special characters from the
2545 * register contents will be interpreted as commands.
2546 *
2547 * return FAIL for failure, OK otherwise
2548 */
2549 static int
2550cmdline_paste(regname, literally)
2551 int regname;
2552 int literally; /* Insert text literally instead of "as typed" */
2553{
2554 long i;
2555 char_u *arg;
2556 int allocated;
2557 struct cmdline_info save_ccline;
2558
2559 /* check for valid regname; also accept special characters for CTRL-R in
2560 * the command line */
2561 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2562 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2563 return FAIL;
2564
2565 /* A register containing CTRL-R can cause an endless loop. Allow using
2566 * CTRL-C to break the loop. */
2567 line_breakcheck();
2568 if (got_int)
2569 return FAIL;
2570
2571#ifdef FEAT_CLIPBOARD
2572 regname = may_get_selection(regname);
2573#endif
2574
2575 /* Need to save and restore ccline, because obtaining the "=" register may
2576 * execute "normal :cmd" and overwrite it. */
2577 save_ccline = ccline;
2578 ccline.cmdbuff = NULL;
2579 ccline.cmdprompt = NULL;
2580 i = get_spec_reg(regname, &arg, &allocated, TRUE);
2581 ccline = save_ccline;
2582
2583 if (i)
2584 {
2585 /* Got the value of a special register in "arg". */
2586 if (arg == NULL)
2587 return FAIL;
2588 cmdline_paste_str(arg, literally);
2589 if (allocated)
2590 vim_free(arg);
2591 return OK;
2592 }
2593
2594 return cmdline_paste_reg(regname, literally);
2595}
2596
2597/*
2598 * Put a string on the command line.
2599 * When "literally" is TRUE, insert literally.
2600 * When "literally" is FALSE, insert as typed, but don't leave the command
2601 * line.
2602 */
2603 void
2604cmdline_paste_str(s, literally)
2605 char_u *s;
2606 int literally;
2607{
2608 int c, cv;
2609
2610 if (literally)
2611 put_on_cmdline(s, -1, TRUE);
2612 else
2613 while (*s != NUL)
2614 {
2615 cv = *s;
2616 if (cv == Ctrl_V && s[1])
2617 ++s;
2618#ifdef FEAT_MBYTE
2619 if (has_mbyte)
2620 {
2621 c = mb_ptr2char(s);
2622 s += mb_char2len(c);
2623 }
2624 else
2625#endif
2626 c = *s++;
2627 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2628#ifdef UNIX
2629 || c == intr_char
2630#endif
2631 || (c == Ctrl_BSL && *s == Ctrl_N))
2632 stuffcharReadbuff(Ctrl_V);
2633 stuffcharReadbuff(c);
2634 }
2635}
2636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637#ifdef FEAT_WILDMENU
2638/*
2639 * Delete characters on the command line, from "from" to the current
2640 * position.
2641 */
2642 static void
2643cmdline_del(from)
2644 int from;
2645{
2646 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2647 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2648 ccline.cmdlen -= ccline.cmdpos - from;
2649 ccline.cmdpos = from;
2650}
2651#endif
2652
2653/*
2654 * this fuction is called when the screen size changes and with incremental
2655 * search
2656 */
2657 void
2658redrawcmdline()
2659{
2660 if (cmd_silent)
2661 return;
2662 need_wait_return = FALSE;
2663 compute_cmdrow();
2664 redrawcmd();
2665 cursorcmd();
2666}
2667
2668 static void
2669redrawcmdprompt()
2670{
2671 int i;
2672
2673 if (cmd_silent)
2674 return;
2675 if (ccline.cmdfirstc)
2676 msg_putchar(ccline.cmdfirstc);
2677 if (ccline.cmdprompt != NULL)
2678 {
2679 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2680 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2681 /* do the reverse of set_cmdspos() */
2682 if (ccline.cmdfirstc)
2683 --ccline.cmdindent;
2684 }
2685 else
2686 for (i = ccline.cmdindent; i > 0; --i)
2687 msg_putchar(' ');
2688}
2689
2690/*
2691 * Redraw what is currently on the command line.
2692 */
2693 void
2694redrawcmd()
2695{
2696 if (cmd_silent)
2697 return;
2698
2699 msg_start();
2700 redrawcmdprompt();
2701
2702 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2703 msg_no_more = TRUE;
2704 draw_cmdline(0, ccline.cmdlen);
2705 msg_clr_eos();
2706 msg_no_more = FALSE;
2707
2708 set_cmdspos_cursor();
2709
2710 /*
2711 * An emsg() before may have set msg_scroll. This is used in normal mode,
2712 * in cmdline mode we can reset them now.
2713 */
2714 msg_scroll = FALSE; /* next message overwrites cmdline */
2715
2716 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2717 * in cmdline mode */
2718 skip_redraw = FALSE;
2719}
2720
2721 void
2722compute_cmdrow()
2723{
2724 if (exmode_active || msg_scrolled)
2725 cmdline_row = Rows - 1;
2726 else
2727 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2728 + W_STATUS_HEIGHT(lastwin);
2729}
2730
2731 static void
2732cursorcmd()
2733{
2734 if (cmd_silent)
2735 return;
2736
2737#ifdef FEAT_RIGHTLEFT
2738 if (cmdmsg_rl)
2739 {
2740 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
2741 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
2742 if (msg_row <= 0)
2743 msg_row = Rows - 1;
2744 }
2745 else
2746#endif
2747 {
2748 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
2749 msg_col = ccline.cmdspos % (int)Columns;
2750 if (msg_row >= Rows)
2751 msg_row = Rows - 1;
2752 }
2753
2754 windgoto(msg_row, msg_col);
2755#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2756 redrawcmd_preedit();
2757#endif
2758#ifdef MCH_CURSOR_SHAPE
2759 mch_update_cursor();
2760#endif
2761}
2762
2763 void
2764gotocmdline(clr)
2765 int clr;
2766{
2767 msg_start();
2768#ifdef FEAT_RIGHTLEFT
2769 if (cmdmsg_rl)
2770 msg_col = Columns - 1;
2771 else
2772#endif
2773 msg_col = 0; /* always start in column 0 */
2774 if (clr) /* clear the bottom line(s) */
2775 msg_clr_eos(); /* will reset clear_cmdline */
2776 windgoto(cmdline_row, 0);
2777}
2778
2779/*
2780 * Check the word in front of the cursor for an abbreviation.
2781 * Called when the non-id character "c" has been entered.
2782 * When an abbreviation is recognized it is removed from the text with
2783 * backspaces and the replacement string is inserted, followed by "c".
2784 */
2785 static int
2786ccheck_abbr(c)
2787 int c;
2788{
2789 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
2790 return FALSE;
2791
2792 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
2793}
2794
2795/*
2796 * Return FAIL if this is not an appropriate context in which to do
2797 * completion of anything, return OK if it is (even if there are no matches).
2798 * For the caller, this means that the character is just passed through like a
2799 * normal character (instead of being expanded). This allows :s/^I^D etc.
2800 */
2801 static int
2802nextwild(xp, type, options)
2803 expand_T *xp;
2804 int type;
2805 int options; /* extra options for ExpandOne() */
2806{
2807 int i, j;
2808 char_u *p1;
2809 char_u *p2;
2810 int oldlen;
2811 int difflen;
2812 int v;
2813
2814 if (xp->xp_numfiles == -1)
2815 {
2816 set_expand_context(xp);
2817 cmd_showtail = expand_showtail(xp);
2818 }
2819
2820 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2821 {
2822 beep_flush();
2823 return OK; /* Something illegal on command line */
2824 }
2825 if (xp->xp_context == EXPAND_NOTHING)
2826 {
2827 /* Caller can use the character as a normal char instead */
2828 return FAIL;
2829 }
2830
2831 MSG_PUTS("..."); /* show that we are busy */
2832 out_flush();
2833
2834 i = (int)(xp->xp_pattern - ccline.cmdbuff);
2835 oldlen = ccline.cmdpos - i;
2836
2837 if (type == WILD_NEXT || type == WILD_PREV)
2838 {
2839 /*
2840 * Get next/previous match for a previous expanded pattern.
2841 */
2842 p2 = ExpandOne(xp, NULL, NULL, 0, type);
2843 }
2844 else
2845 {
2846 /*
2847 * Translate string into pattern and expand it.
2848 */
2849 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
2850 p2 = NULL;
2851 else
2852 {
2853 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
2854 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
2855 |options, type);
2856 vim_free(p1);
2857 /* longest match: make sure it is not shorter (happens with :help */
2858 if (p2 != NULL && type == WILD_LONGEST)
2859 {
2860 for (j = 0; j < oldlen; ++j)
2861 if (ccline.cmdbuff[i + j] == '*'
2862 || ccline.cmdbuff[i + j] == '?')
2863 break;
2864 if ((int)STRLEN(p2) < j)
2865 {
2866 vim_free(p2);
2867 p2 = NULL;
2868 }
2869 }
2870 }
2871 }
2872
2873 if (p2 != NULL && !got_int)
2874 {
2875 difflen = (int)STRLEN(p2) - oldlen;
2876 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
2877 {
2878 v = realloc_cmdbuff(ccline.cmdlen + difflen);
2879 xp->xp_pattern = ccline.cmdbuff + i;
2880 }
2881 else
2882 v = OK;
2883 if (v == OK)
2884 {
2885 vim_strncpy(&ccline.cmdbuff[ccline.cmdpos + difflen],
2886 &ccline.cmdbuff[ccline.cmdpos],
2887 ccline.cmdlen - ccline.cmdpos + 1);
2888 STRNCPY(&ccline.cmdbuff[i], p2, STRLEN(p2));
2889 ccline.cmdlen += difflen;
2890 ccline.cmdpos += difflen;
2891 }
2892 }
2893 vim_free(p2);
2894
2895 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00002896 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898 /* When expanding a ":map" command and no matches are found, assume that
2899 * the key is supposed to be inserted literally */
2900 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
2901 return FAIL;
2902
2903 if (xp->xp_numfiles <= 0 && p2 == NULL)
2904 beep_flush();
2905 else if (xp->xp_numfiles == 1)
2906 /* free expanded pattern */
2907 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
2908
2909 return OK;
2910}
2911
2912/*
2913 * Do wildcard expansion on the string 'str'.
2914 * Chars that should not be expanded must be preceded with a backslash.
2915 * Return a pointer to alloced memory containing the new string.
2916 * Return NULL for failure.
2917 *
2918 * Results are cached in xp->xp_files and xp->xp_numfiles.
2919 *
2920 * mode = WILD_FREE: just free previously expanded matches
2921 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
2922 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
2923 * mode = WILD_NEXT: use next match in multiple match, wrap to first
2924 * mode = WILD_PREV: use previous match in multiple match, wrap to first
2925 * mode = WILD_ALL: return all matches concatenated
2926 * mode = WILD_LONGEST: return longest matched part
2927 *
2928 * options = WILD_LIST_NOTFOUND: list entries without a match
2929 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
2930 * options = WILD_USE_NL: Use '\n' for WILD_ALL
2931 * options = WILD_NO_BEEP: Don't beep for multiple matches
2932 * options = WILD_ADD_SLASH: add a slash after directory names
2933 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
2934 * options = WILD_SILENT: don't print warning messages
2935 * options = WILD_ESCAPE: put backslash before special chars
2936 *
2937 * The variables xp->xp_context and xp->xp_backslash must have been set!
2938 */
2939 char_u *
2940ExpandOne(xp, str, orig, options, mode)
2941 expand_T *xp;
2942 char_u *str;
2943 char_u *orig; /* allocated copy of original of expanded string */
2944 int options;
2945 int mode;
2946{
2947 char_u *ss = NULL;
2948 static int findex;
2949 static char_u *orig_save = NULL; /* kept value of orig */
2950 int i;
2951 long_u len;
2952 int non_suf_match; /* number without matching suffix */
2953
2954 /*
2955 * first handle the case of using an old match
2956 */
2957 if (mode == WILD_NEXT || mode == WILD_PREV)
2958 {
2959 if (xp->xp_numfiles > 0)
2960 {
2961 if (mode == WILD_PREV)
2962 {
2963 if (findex == -1)
2964 findex = xp->xp_numfiles;
2965 --findex;
2966 }
2967 else /* mode == WILD_NEXT */
2968 ++findex;
2969
2970 /*
2971 * When wrapping around, return the original string, set findex to
2972 * -1.
2973 */
2974 if (findex < 0)
2975 {
2976 if (orig_save == NULL)
2977 findex = xp->xp_numfiles - 1;
2978 else
2979 findex = -1;
2980 }
2981 if (findex >= xp->xp_numfiles)
2982 {
2983 if (orig_save == NULL)
2984 findex = 0;
2985 else
2986 findex = -1;
2987 }
2988#ifdef FEAT_WILDMENU
2989 if (p_wmnu)
2990 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
2991 findex, cmd_showtail);
2992#endif
2993 if (findex == -1)
2994 return vim_strsave(orig_save);
2995 return vim_strsave(xp->xp_files[findex]);
2996 }
2997 else
2998 return NULL;
2999 }
3000
3001/* free old names */
3002 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3003 {
3004 FreeWild(xp->xp_numfiles, xp->xp_files);
3005 xp->xp_numfiles = -1;
3006 vim_free(orig_save);
3007 orig_save = NULL;
3008 }
3009 findex = 0;
3010
3011 if (mode == WILD_FREE) /* only release file name */
3012 return NULL;
3013
3014 if (xp->xp_numfiles == -1)
3015 {
3016 vim_free(orig_save);
3017 orig_save = orig;
3018
3019 /*
3020 * Do the expansion.
3021 */
3022 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3023 options) == FAIL)
3024 {
3025#ifdef FNAME_ILLEGAL
3026 /* Illegal file name has been silently skipped. But when there
3027 * are wildcards, the real problem is that there was no match,
3028 * causing the pattern to be added, which has illegal characters.
3029 */
3030 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3031 EMSG2(_(e_nomatch2), str);
3032#endif
3033 }
3034 else if (xp->xp_numfiles == 0)
3035 {
3036 if (!(options & WILD_SILENT))
3037 EMSG2(_(e_nomatch2), str);
3038 }
3039 else
3040 {
3041 /* Escape the matches for use on the command line. */
3042 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3043
3044 /*
3045 * Check for matching suffixes in file names.
3046 */
3047 if (mode != WILD_ALL && mode != WILD_LONGEST)
3048 {
3049 if (xp->xp_numfiles)
3050 non_suf_match = xp->xp_numfiles;
3051 else
3052 non_suf_match = 1;
3053 if ((xp->xp_context == EXPAND_FILES
3054 || xp->xp_context == EXPAND_DIRECTORIES)
3055 && xp->xp_numfiles > 1)
3056 {
3057 /*
3058 * More than one match; check suffix.
3059 * The files will have been sorted on matching suffix in
3060 * expand_wildcards, only need to check the first two.
3061 */
3062 non_suf_match = 0;
3063 for (i = 0; i < 2; ++i)
3064 if (match_suffix(xp->xp_files[i]))
3065 ++non_suf_match;
3066 }
3067 if (non_suf_match != 1)
3068 {
3069 /* Can we ever get here unless it's while expanding
3070 * interactively? If not, we can get rid of this all
3071 * together. Don't really want to wait for this message
3072 * (and possibly have to hit return to continue!).
3073 */
3074 if (!(options & WILD_SILENT))
3075 EMSG(_(e_toomany));
3076 else if (!(options & WILD_NO_BEEP))
3077 beep_flush();
3078 }
3079 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3080 ss = vim_strsave(xp->xp_files[0]);
3081 }
3082 }
3083 }
3084
3085 /* Find longest common part */
3086 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3087 {
3088 for (len = 0; xp->xp_files[0][len]; ++len)
3089 {
3090 for (i = 0; i < xp->xp_numfiles; ++i)
3091 {
3092#ifdef CASE_INSENSITIVE_FILENAME
3093 if (xp->xp_context == EXPAND_DIRECTORIES
3094 || xp->xp_context == EXPAND_FILES
3095 || xp->xp_context == EXPAND_BUFFERS)
3096 {
3097 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3098 TOLOWER_LOC(xp->xp_files[0][len]))
3099 break;
3100 }
3101 else
3102#endif
3103 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3104 break;
3105 }
3106 if (i < xp->xp_numfiles)
3107 {
3108 if (!(options & WILD_NO_BEEP))
3109 vim_beep();
3110 break;
3111 }
3112 }
3113 ss = alloc((unsigned)len + 1);
3114 if (ss)
3115 {
3116 STRNCPY(ss, xp->xp_files[0], len);
3117 ss[len] = NUL;
3118 }
3119 findex = -1; /* next p_wc gets first one */
3120 }
3121
3122 /* Concatenate all matching names */
3123 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3124 {
3125 len = 0;
3126 for (i = 0; i < xp->xp_numfiles; ++i)
3127 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3128 ss = lalloc(len, TRUE);
3129 if (ss != NULL)
3130 {
3131 *ss = NUL;
3132 for (i = 0; i < xp->xp_numfiles; ++i)
3133 {
3134 STRCAT(ss, xp->xp_files[i]);
3135 if (i != xp->xp_numfiles - 1)
3136 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3137 }
3138 }
3139 }
3140
3141 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3142 ExpandCleanup(xp);
3143
3144 return ss;
3145}
3146
3147/*
3148 * Prepare an expand structure for use.
3149 */
3150 void
3151ExpandInit(xp)
3152 expand_T *xp;
3153{
3154 xp->xp_backslash = XP_BS_NONE;
3155 xp->xp_numfiles = -1;
3156 xp->xp_files = NULL;
3157}
3158
3159/*
3160 * Cleanup an expand structure after use.
3161 */
3162 void
3163ExpandCleanup(xp)
3164 expand_T *xp;
3165{
3166 if (xp->xp_numfiles >= 0)
3167 {
3168 FreeWild(xp->xp_numfiles, xp->xp_files);
3169 xp->xp_numfiles = -1;
3170 }
3171}
3172
3173 void
3174ExpandEscape(xp, str, numfiles, files, options)
3175 expand_T *xp;
3176 char_u *str;
3177 int numfiles;
3178 char_u **files;
3179 int options;
3180{
3181 int i;
3182 char_u *p;
3183
3184 /*
3185 * May change home directory back to "~"
3186 */
3187 if (options & WILD_HOME_REPLACE)
3188 tilde_replace(str, numfiles, files);
3189
3190 if (options & WILD_ESCAPE)
3191 {
3192 if (xp->xp_context == EXPAND_FILES
3193 || xp->xp_context == EXPAND_BUFFERS
3194 || xp->xp_context == EXPAND_DIRECTORIES)
3195 {
3196 /*
3197 * Insert a backslash into a file name before a space, \, %, #
3198 * and wildmatch characters, except '~'.
3199 */
3200 for (i = 0; i < numfiles; ++i)
3201 {
3202 /* for ":set path=" we need to escape spaces twice */
3203 if (xp->xp_backslash == XP_BS_THREE)
3204 {
3205 p = vim_strsave_escaped(files[i], (char_u *)" ");
3206 if (p != NULL)
3207 {
3208 vim_free(files[i]);
3209 files[i] = p;
3210#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3211 p = vim_strsave_escaped(files[i], (char_u *)" ");
3212 if (p != NULL)
3213 {
3214 vim_free(files[i]);
3215 files[i] = p;
3216 }
3217#endif
3218 }
3219 }
3220#ifdef BACKSLASH_IN_FILENAME
3221 {
3222 char_u buf[20];
3223 int j = 0;
3224
3225 /* Don't escape '[' and '{' if they are in 'isfname'. */
3226 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3227 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3228 buf[j++] = *p;
3229 buf[j] = NUL;
3230 p = vim_strsave_escaped(files[i], buf);
3231 }
3232#else
3233 p = vim_strsave_escaped(files[i], PATH_ESC_CHARS);
3234#endif
3235 if (p != NULL)
3236 {
3237 vim_free(files[i]);
3238 files[i] = p;
3239 }
3240
3241 /* If 'str' starts with "\~", replace "~" at start of
3242 * files[i] with "\~". */
3243 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
3244 {
3245 p = alloc((unsigned)(STRLEN(files[i]) + 2));
3246 if (p != NULL)
3247 {
3248 p[0] = '\\';
3249 STRCPY(p + 1, files[i]);
3250 vim_free(files[i]);
3251 files[i] = p;
3252 }
3253 }
3254 }
3255 xp->xp_backslash = XP_BS_NONE;
3256 }
3257 else if (xp->xp_context == EXPAND_TAGS)
3258 {
3259 /*
3260 * Insert a backslash before characters in a tag name that
3261 * would terminate the ":tag" command.
3262 */
3263 for (i = 0; i < numfiles; ++i)
3264 {
3265 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3266 if (p != NULL)
3267 {
3268 vim_free(files[i]);
3269 files[i] = p;
3270 }
3271 }
3272 }
3273 }
3274}
3275
3276/*
3277 * For each file name in files[num_files]:
3278 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3279 */
3280 void
3281tilde_replace(orig_pat, num_files, files)
3282 char_u *orig_pat;
3283 int num_files;
3284 char_u **files;
3285{
3286 int i;
3287 char_u *p;
3288
3289 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3290 {
3291 for (i = 0; i < num_files; ++i)
3292 {
3293 p = home_replace_save(NULL, files[i]);
3294 if (p != NULL)
3295 {
3296 vim_free(files[i]);
3297 files[i] = p;
3298 }
3299 }
3300 }
3301}
3302
3303/*
3304 * Show all matches for completion on the command line.
3305 * Returns EXPAND_NOTHING when the character that triggered expansion should
3306 * be inserted like a normal character.
3307 */
3308/*ARGSUSED*/
3309 static int
3310showmatches(xp, wildmenu)
3311 expand_T *xp;
3312 int wildmenu;
3313{
3314#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3315 int num_files;
3316 char_u **files_found;
3317 int i, j, k;
3318 int maxlen;
3319 int lines;
3320 int columns;
3321 char_u *p;
3322 int lastlen;
3323 int attr;
3324 int showtail;
3325
3326 if (xp->xp_numfiles == -1)
3327 {
3328 set_expand_context(xp);
3329 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3330 &num_files, &files_found);
3331 showtail = expand_showtail(xp);
3332 if (i != EXPAND_OK)
3333 return i;
3334
3335 }
3336 else
3337 {
3338 num_files = xp->xp_numfiles;
3339 files_found = xp->xp_files;
3340 showtail = cmd_showtail;
3341 }
3342
3343#ifdef FEAT_WILDMENU
3344 if (!wildmenu)
3345 {
3346#endif
3347 msg_didany = FALSE; /* lines_left will be set */
3348 msg_start(); /* prepare for paging */
3349 msg_putchar('\n');
3350 out_flush();
3351 cmdline_row = msg_row;
3352 msg_didany = FALSE; /* lines_left will be set again */
3353 msg_start(); /* prepare for paging */
3354#ifdef FEAT_WILDMENU
3355 }
3356#endif
3357
3358 if (got_int)
3359 got_int = FALSE; /* only int. the completion, not the cmd line */
3360#ifdef FEAT_WILDMENU
3361 else if (wildmenu)
3362 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3363#endif
3364 else
3365 {
3366 /* find the length of the longest file name */
3367 maxlen = 0;
3368 for (i = 0; i < num_files; ++i)
3369 {
3370 if (!showtail && (xp->xp_context == EXPAND_FILES
3371 || xp->xp_context == EXPAND_BUFFERS))
3372 {
3373 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3374 j = vim_strsize(NameBuff);
3375 }
3376 else
3377 j = vim_strsize(L_SHOWFILE(i));
3378 if (j > maxlen)
3379 maxlen = j;
3380 }
3381
3382 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3383 lines = num_files;
3384 else
3385 {
3386 /* compute the number of columns and lines for the listing */
3387 maxlen += 2; /* two spaces between file names */
3388 columns = ((int)Columns + 2) / maxlen;
3389 if (columns < 1)
3390 columns = 1;
3391 lines = (num_files + columns - 1) / columns;
3392 }
3393
3394 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3395
3396 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3397 {
3398 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3399 msg_clr_eos();
3400 msg_advance(maxlen - 3);
3401 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3402 }
3403
3404 /* list the files line by line */
3405 for (i = 0; i < lines; ++i)
3406 {
3407 lastlen = 999;
3408 for (k = i; k < num_files; k += lines)
3409 {
3410 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3411 {
3412 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3413 p = files_found[k] + STRLEN(files_found[k]) + 1;
3414 msg_advance(maxlen + 1);
3415 msg_puts(p);
3416 msg_advance(maxlen + 3);
3417 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3418 break;
3419 }
3420 for (j = maxlen - lastlen; --j >= 0; )
3421 msg_putchar(' ');
3422 if (xp->xp_context == EXPAND_FILES
3423 || xp->xp_context == EXPAND_BUFFERS)
3424 {
3425 /* highlight directories */
3426 j = (mch_isdir(files_found[k]));
3427 if (showtail)
3428 p = L_SHOWFILE(k);
3429 else
3430 {
3431 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3432 TRUE);
3433 p = NameBuff;
3434 }
3435 }
3436 else
3437 {
3438 j = FALSE;
3439 p = L_SHOWFILE(k);
3440 }
3441 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3442 }
3443 if (msg_col > 0) /* when not wrapped around */
3444 {
3445 msg_clr_eos();
3446 msg_putchar('\n');
3447 }
3448 out_flush(); /* show one line at a time */
3449 if (got_int)
3450 {
3451 got_int = FALSE;
3452 break;
3453 }
3454 }
3455
3456 /*
3457 * we redraw the command below the lines that we have just listed
3458 * This is a bit tricky, but it saves a lot of screen updating.
3459 */
3460 cmdline_row = msg_row; /* will put it back later */
3461 }
3462
3463 if (xp->xp_numfiles == -1)
3464 FreeWild(num_files, files_found);
3465
3466 return EXPAND_OK;
3467}
3468
3469/*
3470 * Private gettail for showmatches() (and win_redr_status_matches()):
3471 * Find tail of file name path, but ignore trailing "/".
3472 */
3473 char_u *
3474sm_gettail(s)
3475 char_u *s;
3476{
3477 char_u *p;
3478 char_u *t = s;
3479 int had_sep = FALSE;
3480
3481 for (p = s; *p != NUL; )
3482 {
3483 if (vim_ispathsep(*p)
3484#ifdef BACKSLASH_IN_FILENAME
3485 && !rem_backslash(p)
3486#endif
3487 )
3488 had_sep = TRUE;
3489 else if (had_sep)
3490 {
3491 t = p;
3492 had_sep = FALSE;
3493 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003494 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496 return t;
3497}
3498
3499/*
3500 * Return TRUE if we only need to show the tail of completion matches.
3501 * When not completing file names or there is a wildcard in the path FALSE is
3502 * returned.
3503 */
3504 static int
3505expand_showtail(xp)
3506 expand_T *xp;
3507{
3508 char_u *s;
3509 char_u *end;
3510
3511 /* When not completing file names a "/" may mean something different. */
3512 if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
3513 return FALSE;
3514
3515 end = gettail(xp->xp_pattern);
3516 if (end == xp->xp_pattern) /* there is no path separator */
3517 return FALSE;
3518
3519 for (s = xp->xp_pattern; s < end; s++)
3520 {
3521 /* Skip escaped wildcards. Only when the backslash is not a path
3522 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3523 if (rem_backslash(s))
3524 ++s;
3525 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3526 return FALSE;
3527 }
3528 return TRUE;
3529}
3530
3531/*
3532 * Prepare a string for expansion.
3533 * When expanding file names: The string will be used with expand_wildcards().
3534 * Copy the file name into allocated memory and add a '*' at the end.
3535 * When expanding other names: The string will be used with regcomp(). Copy
3536 * the name into allocated memory and prepend "^".
3537 */
3538 char_u *
3539addstar(fname, len, context)
3540 char_u *fname;
3541 int len;
3542 int context; /* EXPAND_FILES etc. */
3543{
3544 char_u *retval;
3545 int i, j;
3546 int new_len;
3547 char_u *tail;
3548
3549 if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
3550 {
3551 /*
3552 * Matching will be done internally (on something other than files).
3553 * So we convert the file-matching-type wildcards into our kind for
3554 * use with vim_regcomp(). First work out how long it will be:
3555 */
3556
3557 /* For help tags the translation is done in find_help_tags().
3558 * For a tag pattern starting with "/" no translation is needed. */
3559 if (context == EXPAND_HELP
3560 || context == EXPAND_COLORS
3561 || context == EXPAND_COMPILER
3562 || (context == EXPAND_TAGS && fname[0] == '/'))
3563 retval = vim_strnsave(fname, len);
3564 else
3565 {
3566 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3567 for (i = 0; i < len; i++)
3568 {
3569 if (fname[i] == '*' || fname[i] == '~')
3570 new_len++; /* '*' needs to be replaced by ".*"
3571 '~' needs to be replaced by "\~" */
3572
3573 /* Buffer names are like file names. "." should be literal */
3574 if (context == EXPAND_BUFFERS && fname[i] == '.')
3575 new_len++; /* "." becomes "\." */
3576
3577 /* Custom expansion takes care of special things, match
3578 * backslashes literally (perhaps also for other types?) */
3579 if (context == EXPAND_USER_DEFINED && fname[i] == '\\')
3580 new_len++; /* '\' becomes "\\" */
3581 }
3582 retval = alloc(new_len);
3583 if (retval != NULL)
3584 {
3585 retval[0] = '^';
3586 j = 1;
3587 for (i = 0; i < len; i++, j++)
3588 {
3589 /* Skip backslash. But why? At least keep it for custom
3590 * expansion. */
3591 if (context != EXPAND_USER_DEFINED
3592 && fname[i] == '\\' && ++i == len)
3593 break;
3594
3595 switch (fname[i])
3596 {
3597 case '*': retval[j++] = '.';
3598 break;
3599 case '~': retval[j++] = '\\';
3600 break;
3601 case '?': retval[j] = '.';
3602 continue;
3603 case '.': if (context == EXPAND_BUFFERS)
3604 retval[j++] = '\\';
3605 break;
3606 case '\\': if (context == EXPAND_USER_DEFINED)
3607 retval[j++] = '\\';
3608 break;
3609 }
3610 retval[j] = fname[i];
3611 }
3612 retval[j] = NUL;
3613 }
3614 }
3615 }
3616 else
3617 {
3618 retval = alloc(len + 4);
3619 if (retval != NULL)
3620 {
3621 STRNCPY(retval, fname, len);
3622 retval[len] = NUL;
3623
3624 /*
3625 * Don't add a star to ~, ~user, $var or `cmd`.
3626 * ~ would be at the start of the file name, but not the tail.
3627 * $ could be anywhere in the tail.
3628 * ` could be anywhere in the file name.
3629 */
3630 tail = gettail(retval);
3631 if ((*retval != '~' || tail != retval)
3632 && vim_strchr(tail, '$') == NULL
3633 && vim_strchr(retval, '`') == NULL)
3634 retval[len++] = '*';
3635 retval[len] = NUL;
3636 }
3637 }
3638 return retval;
3639}
3640
3641/*
3642 * Must parse the command line so far to work out what context we are in.
3643 * Completion can then be done based on that context.
3644 * This routine sets the variables:
3645 * xp->xp_pattern The start of the pattern to be expanded within
3646 * the command line (ends at the cursor).
3647 * xp->xp_context The type of thing to expand. Will be one of:
3648 *
3649 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3650 * the command line, like an unknown command. Caller
3651 * should beep.
3652 * EXPAND_NOTHING Unrecognised context for completion, use char like
3653 * a normal char, rather than for completion. eg
3654 * :s/^I/
3655 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3656 * it.
3657 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3658 * EXPAND_FILES After command with XFILE set, or after setting
3659 * with P_EXPAND set. eg :e ^I, :w>>^I
3660 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3661 * when we know only directories are of interest. eg
3662 * :set dir=^I
3663 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3664 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3665 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3666 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3667 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3668 * EXPAND_EVENTS Complete event names
3669 * EXPAND_SYNTAX Complete :syntax command arguments
3670 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3671 * EXPAND_AUGROUP Complete autocommand group names
3672 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3673 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3674 * eg :unmap a^I , :cunab x^I
3675 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3676 * eg :call sub^I
3677 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3678 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3679 * names in expressions, eg :while s^I
3680 * EXPAND_ENV_VARS Complete environment variable names
3681 */
3682 static void
3683set_expand_context(xp)
3684 expand_T *xp;
3685{
3686 /* only expansion for ':' and '>' commands */
3687 if (ccline.cmdfirstc != ':'
3688#ifdef FEAT_EVAL
3689 && ccline.cmdfirstc != '>'
3690#endif
3691 )
3692 {
3693 xp->xp_context = EXPAND_NOTHING;
3694 return;
3695 }
3696 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3697}
3698
3699 void
3700set_cmd_context(xp, str, len, col)
3701 expand_T *xp;
3702 char_u *str; /* start of command line */
3703 int len; /* length of command line (excl. NUL) */
3704 int col; /* position of cursor */
3705{
3706 int old_char = NUL;
3707 char_u *nextcomm;
3708
3709 /*
3710 * Avoid a UMR warning from Purify, only save the character if it has been
3711 * written before.
3712 */
3713 if (col < len)
3714 old_char = str[col];
3715 str[col] = NUL;
3716 nextcomm = str;
3717 while (nextcomm != NULL)
3718 nextcomm = set_one_cmd_context(xp, nextcomm);
3719 str[col] = old_char;
3720}
3721
3722/*
3723 * Expand the command line "str" from context "xp".
3724 * "xp" must have been set by set_cmd_context().
3725 * xp->xp_pattern points into "str", to where the text that is to be expanded
3726 * starts.
3727 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
3728 * cursor.
3729 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
3730 * key that triggered expansion literally.
3731 * Returns EXPAND_OK otherwise.
3732 */
3733 int
3734expand_cmdline(xp, str, col, matchcount, matches)
3735 expand_T *xp;
3736 char_u *str; /* start of command line */
3737 int col; /* position of cursor */
3738 int *matchcount; /* return: nr of matches */
3739 char_u ***matches; /* return: array of pointers to matches */
3740{
3741 char_u *file_str = NULL;
3742
3743 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3744 {
3745 beep_flush();
3746 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
3747 }
3748 if (xp->xp_context == EXPAND_NOTHING)
3749 {
3750 /* Caller can use the character as a normal char instead */
3751 return EXPAND_NOTHING;
3752 }
3753
3754 /* add star to file name, or convert to regexp if not exp. files. */
3755 file_str = addstar(xp->xp_pattern,
3756 (int)(str + col - xp->xp_pattern), xp->xp_context);
3757 if (file_str == NULL)
3758 return EXPAND_UNSUCCESSFUL;
3759
3760 /* find all files that match the description */
3761 if (ExpandFromContext(xp, file_str, matchcount, matches,
3762 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
3763 {
3764 *matchcount = 0;
3765 *matches = NULL;
3766 }
3767 vim_free(file_str);
3768
3769 return EXPAND_OK;
3770}
3771
3772#ifdef FEAT_MULTI_LANG
3773/*
3774 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
3775 */
3776static void cleanup_help_tags __ARGS((int num_file, char_u **file));
3777
3778 static void
3779cleanup_help_tags(num_file, file)
3780 int num_file;
3781 char_u **file;
3782{
3783 int i, j;
3784 int len;
3785
3786 for (i = 0; i < num_file; ++i)
3787 {
3788 len = (int)STRLEN(file[i]) - 3;
3789 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
3790 {
3791 /* Sorting on priority means the same item in another language may
3792 * be anywhere. Search all items for a match up to the "@en". */
3793 for (j = 0; j < num_file; ++j)
3794 if (j != i
3795 && (int)STRLEN(file[j]) == len + 3
3796 && STRNCMP(file[i], file[j], len + 1) == 0)
3797 break;
3798 if (j == num_file)
3799 file[i][len] = NUL;
3800 }
3801 }
3802}
3803#endif
3804
3805/*
3806 * Do the expansion based on xp->xp_context and "pat".
3807 */
3808 static int
3809ExpandFromContext(xp, pat, num_file, file, options)
3810 expand_T *xp;
3811 char_u *pat;
3812 int *num_file;
3813 char_u ***file;
3814 int options;
3815{
3816#ifdef FEAT_CMDL_COMPL
3817 regmatch_T regmatch;
3818#endif
3819 int ret;
3820 int flags;
3821
3822 flags = EW_DIR; /* include directories */
3823 if (options & WILD_LIST_NOTFOUND)
3824 flags |= EW_NOTFOUND;
3825 if (options & WILD_ADD_SLASH)
3826 flags |= EW_ADDSLASH;
3827 if (options & WILD_KEEP_ALL)
3828 flags |= EW_KEEPALL;
3829 if (options & WILD_SILENT)
3830 flags |= EW_SILENT;
3831
3832 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
3833 {
3834 /*
3835 * Expand file or directory names.
3836 */
3837 int free_pat = FALSE;
3838 int i;
3839
3840 /* for ":set path=" and ":set tags=" halve backslashes for escaped
3841 * space */
3842 if (xp->xp_backslash != XP_BS_NONE)
3843 {
3844 free_pat = TRUE;
3845 pat = vim_strsave(pat);
3846 for (i = 0; pat[i]; ++i)
3847 if (pat[i] == '\\')
3848 {
3849 if (xp->xp_backslash == XP_BS_THREE
3850 && pat[i + 1] == '\\'
3851 && pat[i + 2] == '\\'
3852 && pat[i + 3] == ' ')
3853 STRCPY(pat + i, pat + i + 3);
3854 if (xp->xp_backslash == XP_BS_ONE
3855 && pat[i + 1] == ' ')
3856 STRCPY(pat + i, pat + i + 1);
3857 }
3858 }
3859
3860 if (xp->xp_context == EXPAND_FILES)
3861 flags |= EW_FILE;
3862 else
3863 flags = (flags | EW_DIR) & ~EW_FILE;
3864 ret = expand_wildcards(1, &pat, num_file, file, flags);
3865 if (free_pat)
3866 vim_free(pat);
3867 return ret;
3868 }
3869
3870 *file = (char_u **)"";
3871 *num_file = 0;
3872 if (xp->xp_context == EXPAND_HELP)
3873 {
3874 if (find_help_tags(pat, num_file, file, FALSE) == OK)
3875 {
3876#ifdef FEAT_MULTI_LANG
3877 cleanup_help_tags(*num_file, *file);
3878#endif
3879 return OK;
3880 }
3881 return FAIL;
3882 }
3883
3884#ifndef FEAT_CMDL_COMPL
3885 return FAIL;
3886#else
3887 if (xp->xp_context == EXPAND_OLD_SETTING)
3888 return ExpandOldSetting(num_file, file);
3889 if (xp->xp_context == EXPAND_BUFFERS)
3890 return ExpandBufnames(pat, num_file, file, options);
3891 if (xp->xp_context == EXPAND_TAGS
3892 || xp->xp_context == EXPAND_TAGS_LISTFILES)
3893 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
3894 if (xp->xp_context == EXPAND_COLORS)
3895 return ExpandRTDir(pat, num_file, file, "colors");
3896 if (xp->xp_context == EXPAND_COMPILER)
3897 return ExpandRTDir(pat, num_file, file, "compiler");
3898
3899 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
3900 if (regmatch.regprog == NULL)
3901 return FAIL;
3902
3903 /* set ignore-case according to p_ic, p_scs and pat */
3904 regmatch.rm_ic = ignorecase(pat);
3905
3906 if (xp->xp_context == EXPAND_SETTINGS
3907 || xp->xp_context == EXPAND_BOOL_SETTINGS)
3908 ret = ExpandSettings(xp, &regmatch, num_file, file);
3909 else if (xp->xp_context == EXPAND_MAPPINGS)
3910 ret = ExpandMappings(&regmatch, num_file, file);
3911# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
3912 else if (xp->xp_context == EXPAND_USER_DEFINED)
3913 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
3914# endif
3915 else
3916 {
3917 static struct expgen
3918 {
3919 int context;
3920 char_u *((*func)__ARGS((expand_T *, int)));
3921 int ic;
3922 } tab[] =
3923 {
3924 {EXPAND_COMMANDS, get_command_name, FALSE},
3925#ifdef FEAT_USR_CMDS
3926 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
3927 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
3928 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
3929 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
3930#endif
3931#ifdef FEAT_EVAL
3932 {EXPAND_USER_VARS, get_user_var_name, FALSE},
3933 {EXPAND_FUNCTIONS, get_function_name, FALSE},
3934 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
3935 {EXPAND_EXPRESSION, get_expr_name, FALSE},
3936#endif
3937#ifdef FEAT_MENU
3938 {EXPAND_MENUS, get_menu_name, FALSE},
3939 {EXPAND_MENUNAMES, get_menu_names, FALSE},
3940#endif
3941#ifdef FEAT_SYN_HL
3942 {EXPAND_SYNTAX, get_syntax_name, TRUE},
3943#endif
3944 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
3945#ifdef FEAT_AUTOCMD
3946 {EXPAND_EVENTS, get_event_name, TRUE},
3947 {EXPAND_AUGROUP, get_augroup_name, TRUE},
3948#endif
3949#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3950 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3951 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
3952#endif
3953 {EXPAND_ENV_VARS, get_env_name, TRUE},
3954 };
3955 int i;
3956
3957 /*
3958 * Find a context in the table and call the ExpandGeneric() with the
3959 * right function to do the expansion.
3960 */
3961 ret = FAIL;
3962 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
3963 if (xp->xp_context == tab[i].context)
3964 {
3965 if (tab[i].ic)
3966 regmatch.rm_ic = TRUE;
3967 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
3968 break;
3969 }
3970 }
3971
3972 vim_free(regmatch.regprog);
3973
3974 return ret;
3975#endif /* FEAT_CMDL_COMPL */
3976}
3977
3978#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3979/*
3980 * Expand a list of names.
3981 *
3982 * Generic function for command line completion. It calls a function to
3983 * obtain strings, one by one. The strings are matched against a regexp
3984 * program. Matching strings are copied into an array, which is returned.
3985 *
3986 * Returns OK when no problems encountered, FAIL for error (out of memory).
3987 */
3988 int
3989ExpandGeneric(xp, regmatch, num_file, file, func)
3990 expand_T *xp;
3991 regmatch_T *regmatch;
3992 int *num_file;
3993 char_u ***file;
3994 char_u *((*func)__ARGS((expand_T *, int)));
3995 /* returns a string from the list */
3996{
3997 int i;
3998 int count = 0;
3999 int loop;
4000 char_u *str;
4001
4002 /* do this loop twice:
4003 * loop == 0: count the number of matching names
4004 * loop == 1: copy the matching names into allocated memory
4005 */
4006 for (loop = 0; loop <= 1; ++loop)
4007 {
4008 for (i = 0; ; ++i)
4009 {
4010 str = (*func)(xp, i);
4011 if (str == NULL) /* end of list */
4012 break;
4013 if (*str == NUL) /* skip empty strings */
4014 continue;
4015
4016 if (vim_regexec(regmatch, str, (colnr_T)0))
4017 {
4018 if (loop)
4019 {
4020 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4021 (*file)[count] = str;
4022#ifdef FEAT_MENU
4023 if (func == get_menu_names && str != NULL)
4024 {
4025 /* test for separator added by get_menu_names() */
4026 str += STRLEN(str) - 1;
4027 if (*str == '\001')
4028 *str = '.';
4029 }
4030#endif
4031 }
4032 ++count;
4033 }
4034 }
4035 if (loop == 0)
4036 {
4037 if (count == 0)
4038 return OK;
4039 *num_file = count;
4040 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4041 if (*file == NULL)
4042 {
4043 *file = (char_u **)"";
4044 return FAIL;
4045 }
4046 count = 0;
4047 }
4048 }
4049 return OK;
4050}
4051
4052# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4053/*
4054 * Expand names with a function defined by the user.
4055 */
4056 static int
4057ExpandUserDefined(xp, regmatch, num_file, file)
4058 expand_T *xp;
4059 regmatch_T *regmatch;
4060 int *num_file;
4061 char_u ***file;
4062{
4063 char_u *args[3];
4064 char_u *all;
4065 char_u *s;
4066 char_u *e;
4067 char_u keep;
4068 char_u num[50];
4069 garray_T ga;
4070 int save_current_SID = current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004071 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072
4073 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
4074 return FAIL;
4075 *num_file = 0;
4076 *file = NULL;
4077
4078 keep = ccline.cmdbuff[ccline.cmdlen];
4079 ccline.cmdbuff[ccline.cmdlen] = 0;
4080 sprintf((char *)num, "%d", ccline.cmdpos);
4081 args[0] = xp->xp_pattern;
4082 args[1] = ccline.cmdbuff;
4083 args[2] = num;
4084
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004085 /* Save the cmdline, we don't know what the function may do. */
4086 save_ccline = ccline;
4087 ccline.cmdbuff = NULL;
4088 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004090
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 all = call_vim_function(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004092
4093 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004095
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 ccline.cmdbuff[ccline.cmdlen] = keep;
4097 if (all == NULL)
4098 return FAIL;
4099
4100 ga_init2(&ga, (int)sizeof(char *), 3);
4101 for (s = all; *s != NUL; s = e)
4102 {
4103 e = vim_strchr(s, '\n');
4104 if (e == NULL)
4105 e = s + STRLEN(s);
4106 keep = *e;
4107 *e = 0;
4108
4109 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4110 {
4111 *e = keep;
4112 if (*e != NUL)
4113 ++e;
4114 continue;
4115 }
4116
4117 if (ga_grow(&ga, 1) == FAIL)
4118 break;
4119
4120 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4121 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
4123 *e = keep;
4124 if (*e != NUL)
4125 ++e;
4126 }
4127 vim_free(all);
4128 *file = ga.ga_data;
4129 *num_file = ga.ga_len;
4130 return OK;
4131}
4132#endif
4133
4134/*
4135 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4136 * or compiler names.
4137 */
4138 static int
4139ExpandRTDir(pat, num_file, file, dirname)
4140 char_u *pat;
4141 int *num_file;
4142 char_u ***file;
4143 char *dirname; /* "colors" or "compiler" */
4144{
4145 char_u *all;
4146 char_u *s;
4147 char_u *e;
4148 garray_T ga;
4149
4150 *num_file = 0;
4151 *file = NULL;
4152 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4153 if (s == NULL)
4154 return FAIL;
4155 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4156 all = globpath(p_rtp, s);
4157 vim_free(s);
4158 if (all == NULL)
4159 return FAIL;
4160
4161 ga_init2(&ga, (int)sizeof(char *), 3);
4162 for (s = all; *s != NUL; s = e)
4163 {
4164 e = vim_strchr(s, '\n');
4165 if (e == NULL)
4166 e = s + STRLEN(s);
4167 if (ga_grow(&ga, 1) == FAIL)
4168 break;
4169 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4170 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004171 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 if (*s == '\n' || vim_ispathsep(*s))
4173 break;
4174 ++s;
4175 ((char_u **)ga.ga_data)[ga.ga_len] =
4176 vim_strnsave(s, (int)(e - s - 4));
4177 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 if (*e != NUL)
4180 ++e;
4181 }
4182 vim_free(all);
4183 *file = ga.ga_data;
4184 *num_file = ga.ga_len;
4185 return OK;
4186}
4187
4188#endif
4189
4190#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4191/*
4192 * Expand "file" for all comma-separated directories in "path".
4193 * Returns an allocated string with all matches concatenated, separated by
4194 * newlines. Returns NULL for an error or no matches.
4195 */
4196 char_u *
4197globpath(path, file)
4198 char_u *path;
4199 char_u *file;
4200{
4201 expand_T xpc;
4202 char_u *buf;
4203 garray_T ga;
4204 int i;
4205 int len;
4206 int num_p;
4207 char_u **p;
4208 char_u *cur = NULL;
4209
4210 buf = alloc(MAXPATHL);
4211 if (buf == NULL)
4212 return NULL;
4213
4214 xpc.xp_context = EXPAND_FILES;
4215 xpc.xp_backslash = XP_BS_NONE;
4216 ga_init2(&ga, 1, 100);
4217
4218 /* Loop over all entries in {path}. */
4219 while (*path != NUL)
4220 {
4221 /* Copy one item of the path to buf[] and concatenate the file name. */
4222 copy_option_part(&path, buf, MAXPATHL, ",");
4223 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4224 {
4225 add_pathsep(buf);
4226 STRCAT(buf, file);
4227 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4228 && num_p > 0)
4229 {
4230 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4231 for (len = 0, i = 0; i < num_p; ++i)
4232 len += (long_u)STRLEN(p[i]) + 1;
4233
4234 /* Concatenate new results to previous ones. */
4235 if (ga_grow(&ga, len) == OK)
4236 {
4237 cur = (char_u *)ga.ga_data + ga.ga_len;
4238 for (i = 0; i < num_p; ++i)
4239 {
4240 STRCPY(cur, p[i]);
4241 cur += STRLEN(p[i]);
4242 *cur++ = '\n';
4243 }
4244 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 FreeWild(num_p, p);
4247 }
4248 }
4249 }
4250 if (cur != NULL)
4251 *--cur = 0; /* Replace trailing newline with NUL */
4252
4253 vim_free(buf);
4254 return (char_u *)ga.ga_data;
4255}
4256
4257#endif
4258
4259#if defined(FEAT_CMDHIST) || defined(PROTO)
4260
4261/*********************************
4262 * Command line history stuff *
4263 *********************************/
4264
4265/*
4266 * Translate a history character to the associated type number.
4267 */
4268 static int
4269hist_char2type(c)
4270 int c;
4271{
4272 if (c == ':')
4273 return HIST_CMD;
4274 if (c == '=')
4275 return HIST_EXPR;
4276 if (c == '@')
4277 return HIST_INPUT;
4278 if (c == '>')
4279 return HIST_DEBUG;
4280 return HIST_SEARCH; /* must be '?' or '/' */
4281}
4282
4283/*
4284 * Table of history names.
4285 * These names are used in :history and various hist...() functions.
4286 * It is sufficient to give the significant prefix of a history name.
4287 */
4288
4289static char *(history_names[]) =
4290{
4291 "cmd",
4292 "search",
4293 "expr",
4294 "input",
4295 "debug",
4296 NULL
4297};
4298
4299/*
4300 * init_history() - Initialize the command line history.
4301 * Also used to re-allocate the history when the size changes.
4302 */
4303 static void
4304init_history()
4305{
4306 int newlen; /* new length of history table */
4307 histentry_T *temp;
4308 int i;
4309 int j;
4310 int type;
4311
4312 /*
4313 * If size of history table changed, reallocate it
4314 */
4315 newlen = (int)p_hi;
4316 if (newlen != hislen) /* history length changed */
4317 {
4318 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4319 {
4320 if (newlen)
4321 {
4322 temp = (histentry_T *)lalloc(
4323 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4324 if (temp == NULL) /* out of memory! */
4325 {
4326 if (type == 0) /* first one: just keep the old length */
4327 {
4328 newlen = hislen;
4329 break;
4330 }
4331 /* Already changed one table, now we can only have zero
4332 * length for all tables. */
4333 newlen = 0;
4334 type = -1;
4335 continue;
4336 }
4337 }
4338 else
4339 temp = NULL;
4340 if (newlen == 0 || temp != NULL)
4341 {
4342 if (hisidx[type] < 0) /* there are no entries yet */
4343 {
4344 for (i = 0; i < newlen; ++i)
4345 {
4346 temp[i].hisnum = 0;
4347 temp[i].hisstr = NULL;
4348 }
4349 }
4350 else if (newlen > hislen) /* array becomes bigger */
4351 {
4352 for (i = 0; i <= hisidx[type]; ++i)
4353 temp[i] = history[type][i];
4354 j = i;
4355 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4356 {
4357 temp[i].hisnum = 0;
4358 temp[i].hisstr = NULL;
4359 }
4360 for ( ; j < hislen; ++i, ++j)
4361 temp[i] = history[type][j];
4362 }
4363 else /* array becomes smaller or 0 */
4364 {
4365 j = hisidx[type];
4366 for (i = newlen - 1; ; --i)
4367 {
4368 if (i >= 0) /* copy newest entries */
4369 temp[i] = history[type][j];
4370 else /* remove older entries */
4371 vim_free(history[type][j].hisstr);
4372 if (--j < 0)
4373 j = hislen - 1;
4374 if (j == hisidx[type])
4375 break;
4376 }
4377 hisidx[type] = newlen - 1;
4378 }
4379 vim_free(history[type]);
4380 history[type] = temp;
4381 }
4382 }
4383 hislen = newlen;
4384 }
4385}
4386
4387/*
4388 * Check if command line 'str' is already in history.
4389 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4390 */
4391 static int
4392in_history(type, str, move_to_front)
4393 int type;
4394 char_u *str;
4395 int move_to_front; /* Move the entry to the front if it exists */
4396{
4397 int i;
4398 int last_i = -1;
4399
4400 if (hisidx[type] < 0)
4401 return FALSE;
4402 i = hisidx[type];
4403 do
4404 {
4405 if (history[type][i].hisstr == NULL)
4406 return FALSE;
4407 if (STRCMP(str, history[type][i].hisstr) == 0)
4408 {
4409 if (!move_to_front)
4410 return TRUE;
4411 last_i = i;
4412 break;
4413 }
4414 if (--i < 0)
4415 i = hislen - 1;
4416 } while (i != hisidx[type]);
4417
4418 if (last_i >= 0)
4419 {
4420 str = history[type][i].hisstr;
4421 while (i != hisidx[type])
4422 {
4423 if (++i >= hislen)
4424 i = 0;
4425 history[type][last_i] = history[type][i];
4426 last_i = i;
4427 }
4428 history[type][i].hisstr = str;
4429 history[type][i].hisnum = ++hisnum[type];
4430 return TRUE;
4431 }
4432 return FALSE;
4433}
4434
4435/*
4436 * Convert history name (from table above) to its HIST_ equivalent.
4437 * When "name" is empty, return "cmd" history.
4438 * Returns -1 for unknown history name.
4439 */
4440 int
4441get_histtype(name)
4442 char_u *name;
4443{
4444 int i;
4445 int len = (int)STRLEN(name);
4446
4447 /* No argument: use current history. */
4448 if (len == 0)
4449 return hist_char2type(ccline.cmdfirstc);
4450
4451 for (i = 0; history_names[i] != NULL; ++i)
4452 if (STRNICMP(name, history_names[i], len) == 0)
4453 return i;
4454
4455 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4456 return hist_char2type(name[0]);
4457
4458 return -1;
4459}
4460
4461static int last_maptick = -1; /* last seen maptick */
4462
4463/*
4464 * Add the given string to the given history. If the string is already in the
4465 * history then it is moved to the front. "histype" may be one of he HIST_
4466 * values.
4467 */
4468 void
4469add_to_history(histype, new_entry, in_map, sep)
4470 int histype;
4471 char_u *new_entry;
4472 int in_map; /* consider maptick when inside a mapping */
4473 int sep; /* separator character used (search hist) */
4474{
4475 histentry_T *hisptr;
4476 int len;
4477
4478 if (hislen == 0) /* no history */
4479 return;
4480
4481 /*
4482 * Searches inside the same mapping overwrite each other, so that only
4483 * the last line is kept. Be careful not to remove a line that was moved
4484 * down, only lines that were added.
4485 */
4486 if (histype == HIST_SEARCH && in_map)
4487 {
4488 if (maptick == last_maptick)
4489 {
4490 /* Current line is from the same mapping, remove it */
4491 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4492 vim_free(hisptr->hisstr);
4493 hisptr->hisstr = NULL;
4494 hisptr->hisnum = 0;
4495 --hisnum[histype];
4496 if (--hisidx[HIST_SEARCH] < 0)
4497 hisidx[HIST_SEARCH] = hislen - 1;
4498 }
4499 last_maptick = -1;
4500 }
4501 if (!in_history(histype, new_entry, TRUE))
4502 {
4503 if (++hisidx[histype] == hislen)
4504 hisidx[histype] = 0;
4505 hisptr = &history[histype][hisidx[histype]];
4506 vim_free(hisptr->hisstr);
4507
4508 /* Store the separator after the NUL of the string. */
4509 len = STRLEN(new_entry);
4510 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4511 if (hisptr->hisstr != NULL)
4512 hisptr->hisstr[len + 1] = sep;
4513
4514 hisptr->hisnum = ++hisnum[histype];
4515 if (histype == HIST_SEARCH && in_map)
4516 last_maptick = maptick;
4517 }
4518}
4519
4520#if defined(FEAT_EVAL) || defined(PROTO)
4521
4522/*
4523 * Get identifier of newest history entry.
4524 * "histype" may be one of the HIST_ values.
4525 */
4526 int
4527get_history_idx(histype)
4528 int histype;
4529{
4530 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4531 || hisidx[histype] < 0)
4532 return -1;
4533
4534 return history[histype][hisidx[histype]].hisnum;
4535}
4536
4537/*
4538 * Get the current command line in allocated memory.
4539 * Only works when the command line is being edited.
4540 * Returns NULL when something is wrong.
4541 */
4542 char_u *
4543get_cmdline_str()
4544{
4545 if (ccline.cmdbuff == NULL || (State & CMDLINE) == 0)
4546 return NULL;
4547 return vim_strnsave(ccline.cmdbuff, ccline.cmdlen);
4548}
4549
4550/*
4551 * Get the current command line position, counted in bytes.
4552 * Zero is the first position.
4553 * Only works when the command line is being edited.
4554 * Returns -1 when something is wrong.
4555 */
4556 int
4557get_cmdline_pos()
4558{
4559 if (ccline.cmdbuff == NULL || (State & CMDLINE) == 0)
4560 return -1;
4561 return ccline.cmdpos;
4562}
4563
4564/*
4565 * Set the command line byte position to "pos". Zero is the first position.
4566 * Only works when the command line is being edited.
4567 * Returns 1 when failed, 0 when OK.
4568 */
4569 int
4570set_cmdline_pos(pos)
4571 int pos;
4572{
4573 if (ccline.cmdbuff == NULL || (State & CMDLINE) == 0)
4574 return 1;
4575
4576 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
4577 * changed the command line. */
4578 if (pos < 0)
4579 new_cmdpos = 0;
4580 else
4581 new_cmdpos = pos;
4582 return 0;
4583}
4584
4585/*
4586 * Calculate history index from a number:
4587 * num > 0: seen as identifying number of a history entry
4588 * num < 0: relative position in history wrt newest entry
4589 * "histype" may be one of the HIST_ values.
4590 */
4591 static int
4592calc_hist_idx(histype, num)
4593 int histype;
4594 int num;
4595{
4596 int i;
4597 histentry_T *hist;
4598 int wrapped = FALSE;
4599
4600 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4601 || (i = hisidx[histype]) < 0 || num == 0)
4602 return -1;
4603
4604 hist = history[histype];
4605 if (num > 0)
4606 {
4607 while (hist[i].hisnum > num)
4608 if (--i < 0)
4609 {
4610 if (wrapped)
4611 break;
4612 i += hislen;
4613 wrapped = TRUE;
4614 }
4615 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
4616 return i;
4617 }
4618 else if (-num <= hislen)
4619 {
4620 i += num + 1;
4621 if (i < 0)
4622 i += hislen;
4623 if (hist[i].hisstr != NULL)
4624 return i;
4625 }
4626 return -1;
4627}
4628
4629/*
4630 * Get a history entry by its index.
4631 * "histype" may be one of the HIST_ values.
4632 */
4633 char_u *
4634get_history_entry(histype, idx)
4635 int histype;
4636 int idx;
4637{
4638 idx = calc_hist_idx(histype, idx);
4639 if (idx >= 0)
4640 return history[histype][idx].hisstr;
4641 else
4642 return (char_u *)"";
4643}
4644
4645/*
4646 * Clear all entries of a history.
4647 * "histype" may be one of the HIST_ values.
4648 */
4649 int
4650clr_history(histype)
4651 int histype;
4652{
4653 int i;
4654 histentry_T *hisptr;
4655
4656 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
4657 {
4658 hisptr = history[histype];
4659 for (i = hislen; i--;)
4660 {
4661 vim_free(hisptr->hisstr);
4662 hisptr->hisnum = 0;
4663 hisptr++->hisstr = NULL;
4664 }
4665 hisidx[histype] = -1; /* mark history as cleared */
4666 hisnum[histype] = 0; /* reset identifier counter */
4667 return OK;
4668 }
4669 return FAIL;
4670}
4671
4672/*
4673 * Remove all entries matching {str} from a history.
4674 * "histype" may be one of the HIST_ values.
4675 */
4676 int
4677del_history_entry(histype, str)
4678 int histype;
4679 char_u *str;
4680{
4681 regmatch_T regmatch;
4682 histentry_T *hisptr;
4683 int idx;
4684 int i;
4685 int last;
4686 int found = FALSE;
4687
4688 regmatch.regprog = NULL;
4689 regmatch.rm_ic = FALSE; /* always match case */
4690 if (hislen != 0
4691 && histype >= 0
4692 && histype < HIST_COUNT
4693 && *str != NUL
4694 && (idx = hisidx[histype]) >= 0
4695 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
4696 != NULL)
4697 {
4698 i = last = idx;
4699 do
4700 {
4701 hisptr = &history[histype][i];
4702 if (hisptr->hisstr == NULL)
4703 break;
4704 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
4705 {
4706 found = TRUE;
4707 vim_free(hisptr->hisstr);
4708 hisptr->hisstr = NULL;
4709 hisptr->hisnum = 0;
4710 }
4711 else
4712 {
4713 if (i != last)
4714 {
4715 history[histype][last] = *hisptr;
4716 hisptr->hisstr = NULL;
4717 hisptr->hisnum = 0;
4718 }
4719 if (--last < 0)
4720 last += hislen;
4721 }
4722 if (--i < 0)
4723 i += hislen;
4724 } while (i != idx);
4725 if (history[histype][idx].hisstr == NULL)
4726 hisidx[histype] = -1;
4727 }
4728 vim_free(regmatch.regprog);
4729 return found;
4730}
4731
4732/*
4733 * Remove an indexed entry from a history.
4734 * "histype" may be one of the HIST_ values.
4735 */
4736 int
4737del_history_idx(histype, idx)
4738 int histype;
4739 int idx;
4740{
4741 int i, j;
4742
4743 i = calc_hist_idx(histype, idx);
4744 if (i < 0)
4745 return FALSE;
4746 idx = hisidx[histype];
4747 vim_free(history[histype][i].hisstr);
4748
4749 /* When deleting the last added search string in a mapping, reset
4750 * last_maptick, so that the last added search string isn't deleted again.
4751 */
4752 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
4753 last_maptick = -1;
4754
4755 while (i != idx)
4756 {
4757 j = (i + 1) % hislen;
4758 history[histype][i] = history[histype][j];
4759 i = j;
4760 }
4761 history[histype][i].hisstr = NULL;
4762 history[histype][i].hisnum = 0;
4763 if (--i < 0)
4764 i += hislen;
4765 hisidx[histype] = i;
4766 return TRUE;
4767}
4768
4769#endif /* FEAT_EVAL */
4770
4771#if defined(FEAT_CRYPT) || defined(PROTO)
4772/*
4773 * Very specific function to remove the value in ":set key=val" from the
4774 * history.
4775 */
4776 void
4777remove_key_from_history()
4778{
4779 char_u *p;
4780 int i;
4781
4782 i = hisidx[HIST_CMD];
4783 if (i < 0)
4784 return;
4785 p = history[HIST_CMD][i].hisstr;
4786 if (p != NULL)
4787 for ( ; *p; ++p)
4788 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
4789 {
4790 p = vim_strchr(p + 3, '=');
4791 if (p == NULL)
4792 break;
4793 ++p;
4794 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
4795 if (p[i] == '\\' && p[i + 1])
4796 ++i;
4797 mch_memmove(p, p + i, STRLEN(p + i) + 1);
4798 --p;
4799 }
4800}
4801#endif
4802
4803#endif /* FEAT_CMDHIST */
4804
4805#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
4806/*
4807 * Get indices "num1,num2" that specify a range within a list (not a range of
4808 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4809 * Returns OK if parsed successfully, otherwise FAIL.
4810 */
4811 int
4812get_list_range(str, num1, num2)
4813 char_u **str;
4814 int *num1;
4815 int *num2;
4816{
4817 int len;
4818 int first = FALSE;
4819 long num;
4820
4821 *str = skipwhite(*str);
4822 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
4823 {
4824 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
4825 *str += len;
4826 *num1 = (int)num;
4827 first = TRUE;
4828 }
4829 *str = skipwhite(*str);
4830 if (**str == ',') /* parse "to" part of range */
4831 {
4832 *str = skipwhite(*str + 1);
4833 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
4834 if (len > 0)
4835 {
4836 *num2 = (int)num;
4837 *str = skipwhite(*str + len);
4838 }
4839 else if (!first) /* no number given at all */
4840 return FAIL;
4841 }
4842 else if (first) /* only one number given */
4843 *num2 = *num1;
4844 return OK;
4845}
4846#endif
4847
4848#if defined(FEAT_CMDHIST) || defined(PROTO)
4849/*
4850 * :history command - print a history
4851 */
4852 void
4853ex_history(eap)
4854 exarg_T *eap;
4855{
4856 histentry_T *hist;
4857 int histype1 = HIST_CMD;
4858 int histype2 = HIST_CMD;
4859 int hisidx1 = 1;
4860 int hisidx2 = -1;
4861 int idx;
4862 int i, j, k;
4863 char_u *end;
4864 char_u *arg = eap->arg;
4865
4866 if (hislen == 0)
4867 {
4868 MSG(_("'history' option is zero"));
4869 return;
4870 }
4871
4872 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
4873 {
4874 end = arg;
4875 while (ASCII_ISALPHA(*end)
4876 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
4877 end++;
4878 i = *end;
4879 *end = NUL;
4880 histype1 = get_histtype(arg);
4881 if (histype1 == -1)
4882 {
4883 if (STRICMP(arg, "all") == 0)
4884 {
4885 histype1 = 0;
4886 histype2 = HIST_COUNT-1;
4887 }
4888 else
4889 {
4890 *end = i;
4891 EMSG(_(e_trailing));
4892 return;
4893 }
4894 }
4895 else
4896 histype2 = histype1;
4897 *end = i;
4898 }
4899 else
4900 end = arg;
4901 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
4902 {
4903 EMSG(_(e_trailing));
4904 return;
4905 }
4906
4907 for (; !got_int && histype1 <= histype2; ++histype1)
4908 {
4909 STRCPY(IObuff, "\n # ");
4910 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
4911 MSG_PUTS_TITLE(IObuff);
4912 idx = hisidx[histype1];
4913 hist = history[histype1];
4914 j = hisidx1;
4915 k = hisidx2;
4916 if (j < 0)
4917 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
4918 if (k < 0)
4919 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
4920 if (idx >= 0 && j <= k)
4921 for (i = idx + 1; !got_int; ++i)
4922 {
4923 if (i == hislen)
4924 i = 0;
4925 if (hist[i].hisstr != NULL
4926 && hist[i].hisnum >= j && hist[i].hisnum <= k)
4927 {
4928 msg_putchar('\n');
4929 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
4930 hist[i].hisnum);
4931 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
4932 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
4933 (int)Columns - 10);
4934 else
4935 STRCAT(IObuff, hist[i].hisstr);
4936 msg_outtrans(IObuff);
4937 out_flush();
4938 }
4939 if (i == idx)
4940 break;
4941 }
4942 }
4943}
4944#endif
4945
4946#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
4947static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
4948static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
4949static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
4950static int viminfo_add_at_front = FALSE;
4951
4952static int hist_type2char __ARGS((int type, int use_question));
4953
4954/*
4955 * Translate a history type number to the associated character.
4956 */
4957 static int
4958hist_type2char(type, use_question)
4959 int type;
4960 int use_question; /* use '?' instead of '/' */
4961{
4962 if (type == HIST_CMD)
4963 return ':';
4964 if (type == HIST_SEARCH)
4965 {
4966 if (use_question)
4967 return '?';
4968 else
4969 return '/';
4970 }
4971 if (type == HIST_EXPR)
4972 return '=';
4973 return '@';
4974}
4975
4976/*
4977 * Prepare for reading the history from the viminfo file.
4978 * This allocates history arrays to store the read history lines.
4979 */
4980 void
4981prepare_viminfo_history(asklen)
4982 int asklen;
4983{
4984 int i;
4985 int num;
4986 int type;
4987 int len;
4988
4989 init_history();
4990 viminfo_add_at_front = (asklen != 0);
4991 if (asklen > hislen)
4992 asklen = hislen;
4993
4994 for (type = 0; type < HIST_COUNT; ++type)
4995 {
4996 /*
4997 * Count the number of empty spaces in the history list. If there are
4998 * more spaces available than we request, then fill them up.
4999 */
5000 for (i = 0, num = 0; i < hislen; i++)
5001 if (history[type][i].hisstr == NULL)
5002 num++;
5003 len = asklen;
5004 if (num > len)
5005 len = num;
5006 if (len <= 0)
5007 viminfo_history[type] = NULL;
5008 else
5009 viminfo_history[type] =
5010 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5011 if (viminfo_history[type] == NULL)
5012 len = 0;
5013 viminfo_hislen[type] = len;
5014 viminfo_hisidx[type] = 0;
5015 }
5016}
5017
5018/*
5019 * Accept a line from the viminfo, store it in the history array when it's
5020 * new.
5021 */
5022 int
5023read_viminfo_history(virp)
5024 vir_T *virp;
5025{
5026 int type;
5027 long_u len;
5028 char_u *val;
5029 char_u *p;
5030
5031 type = hist_char2type(virp->vir_line[0]);
5032 if (viminfo_hisidx[type] < viminfo_hislen[type])
5033 {
5034 val = viminfo_readstring(virp, 1, TRUE);
5035 if (val != NULL && *val != NUL)
5036 {
5037 if (!in_history(type, val + (type == HIST_SEARCH),
5038 viminfo_add_at_front))
5039 {
5040 /* Need to re-allocate to append the separator byte. */
5041 len = STRLEN(val);
5042 p = lalloc(len + 2, TRUE);
5043 if (p != NULL)
5044 {
5045 if (type == HIST_SEARCH)
5046 {
5047 /* Search entry: Move the separator from the first
5048 * column to after the NUL. */
5049 mch_memmove(p, val + 1, (size_t)len);
5050 p[len] = (*val == ' ' ? NUL : *val);
5051 }
5052 else
5053 {
5054 /* Not a search entry: No separator in the viminfo
5055 * file, add a NUL separator. */
5056 mch_memmove(p, val, (size_t)len + 1);
5057 p[len + 1] = NUL;
5058 }
5059 viminfo_history[type][viminfo_hisidx[type]++] = p;
5060 }
5061 }
5062 }
5063 vim_free(val);
5064 }
5065 return viminfo_readline(virp);
5066}
5067
5068 void
5069finish_viminfo_history()
5070{
5071 int idx;
5072 int i;
5073 int type;
5074
5075 for (type = 0; type < HIST_COUNT; ++type)
5076 {
5077 if (history[type] == NULL)
5078 return;
5079 idx = hisidx[type] + viminfo_hisidx[type];
5080 if (idx >= hislen)
5081 idx -= hislen;
5082 else if (idx < 0)
5083 idx = hislen - 1;
5084 if (viminfo_add_at_front)
5085 hisidx[type] = idx;
5086 else
5087 {
5088 if (hisidx[type] == -1)
5089 hisidx[type] = hislen - 1;
5090 do
5091 {
5092 if (history[type][idx].hisstr != NULL)
5093 break;
5094 if (++idx == hislen)
5095 idx = 0;
5096 } while (idx != hisidx[type]);
5097 if (idx != hisidx[type] && --idx < 0)
5098 idx = hislen - 1;
5099 }
5100 for (i = 0; i < viminfo_hisidx[type]; i++)
5101 {
5102 vim_free(history[type][idx].hisstr);
5103 history[type][idx].hisstr = viminfo_history[type][i];
5104 if (--idx < 0)
5105 idx = hislen - 1;
5106 }
5107 idx += 1;
5108 idx %= hislen;
5109 for (i = 0; i < viminfo_hisidx[type]; i++)
5110 {
5111 history[type][idx++].hisnum = ++hisnum[type];
5112 idx %= hislen;
5113 }
5114 vim_free(viminfo_history[type]);
5115 viminfo_history[type] = NULL;
5116 }
5117}
5118
5119 void
5120write_viminfo_history(fp)
5121 FILE *fp;
5122{
5123 int i;
5124 int type;
5125 int num_saved;
5126 char_u *p;
5127 int c;
5128
5129 init_history();
5130 if (hislen == 0)
5131 return;
5132 for (type = 0; type < HIST_COUNT; ++type)
5133 {
5134 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5135 if (num_saved == 0)
5136 continue;
5137 if (num_saved < 0) /* Use default */
5138 num_saved = hislen;
5139 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5140 type == HIST_CMD ? _("Command Line") :
5141 type == HIST_SEARCH ? _("Search String") :
5142 type == HIST_EXPR ? _("Expression") :
5143 _("Input Line"));
5144 if (num_saved > hislen)
5145 num_saved = hislen;
5146 i = hisidx[type];
5147 if (i >= 0)
5148 while (num_saved--)
5149 {
5150 p = history[type][i].hisstr;
5151 if (p != NULL)
5152 {
5153 putc(hist_type2char(type, TRUE), fp);
5154 /* For the search history: put the separator in the second
5155 * column; use a space if there isn't one. */
5156 if (type == HIST_SEARCH)
5157 {
5158 c = p[STRLEN(p) + 1];
5159 putc(c == NUL ? ' ' : c, fp);
5160 }
5161 viminfo_writestring(fp, p);
5162 }
5163 if (--i < 0)
5164 i = hislen - 1;
5165 }
5166 }
5167}
5168#endif /* FEAT_VIMINFO */
5169
5170#if defined(FEAT_FKMAP) || defined(PROTO)
5171/*
5172 * Write a character at the current cursor+offset position.
5173 * It is directly written into the command buffer block.
5174 */
5175 void
5176cmd_pchar(c, offset)
5177 int c, offset;
5178{
5179 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5180 {
5181 EMSG(_("E198: cmd_pchar beyond the command length"));
5182 return;
5183 }
5184 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5185 ccline.cmdbuff[ccline.cmdlen] = NUL;
5186}
5187
5188 int
5189cmd_gchar(offset)
5190 int offset;
5191{
5192 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5193 {
5194 /* EMSG(_("cmd_gchar beyond the command length")); */
5195 return NUL;
5196 }
5197 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5198}
5199#endif
5200
5201#if defined(FEAT_CMDWIN) || defined(PROTO)
5202/*
5203 * Open a window on the current command line and history. Allow editing in
5204 * the window. Returns when the window is closed.
5205 * Returns:
5206 * CR if the command is to be executed
5207 * Ctrl_C if it is to be abandoned
5208 * K_IGNORE if editing continues
5209 */
5210 static int
5211ex_window()
5212{
5213 struct cmdline_info save_ccline;
5214 buf_T *old_curbuf = curbuf;
5215 win_T *old_curwin = curwin;
5216 buf_T *bp;
5217 win_T *wp;
5218 int i;
5219 linenr_T lnum;
5220 int histtype;
5221 garray_T winsizes;
5222 char_u typestr[2];
5223 int save_restart_edit = restart_edit;
5224 int save_State = State;
5225 int save_exmode = exmode_active;
5226
5227 /* Can't do this recursively. Can't do it when typing a password. */
5228 if (cmdwin_type != 0
5229# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5230 || cmdline_star > 0
5231# endif
5232 )
5233 {
5234 beep_flush();
5235 return K_IGNORE;
5236 }
5237
5238 /* Save current window sizes. */
5239 win_size_save(&winsizes);
5240
5241# ifdef FEAT_AUTOCMD
5242 /* Don't execute autocommands while creating the window. */
5243 ++autocmd_block;
5244# endif
5245 /* Create a window for the command-line buffer. */
5246 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5247 {
5248 beep_flush();
5249 return K_IGNORE;
5250 }
5251 cmdwin_type = ccline.cmdfirstc;
5252 if (cmdwin_type == NUL)
5253 cmdwin_type = '-';
5254
5255 /* Create the command-line buffer empty. */
5256 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5257 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5258 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5259 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5260 curbuf->b_p_ma = TRUE;
5261# ifdef FEAT_RIGHTLEFT
5262 curwin->w_p_rl = FALSE;
5263# endif
5264# ifdef FEAT_SCROLLBIND
5265 curwin->w_p_scb = FALSE;
5266# endif
5267
5268# ifdef FEAT_AUTOCMD
5269 /* Do execute autocommands for setting the filetype (load syntax). */
5270 --autocmd_block;
5271# endif
5272
5273 histtype = hist_char2type(ccline.cmdfirstc);
5274 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5275 {
5276 if (p_wc == TAB)
5277 {
5278 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5279 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5280 }
5281 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5282 }
5283
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005284 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5285 * sets 'textwidth' to 78). */
5286 curbuf->b_p_tw = 0;
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 /* Fill the buffer with the history. */
5289 init_history();
5290 if (hislen > 0)
5291 {
5292 i = hisidx[histtype];
5293 if (i >= 0)
5294 {
5295 lnum = 0;
5296 do
5297 {
5298 if (++i == hislen)
5299 i = 0;
5300 if (history[histtype][i].hisstr != NULL)
5301 ml_append(lnum++, history[histtype][i].hisstr,
5302 (colnr_T)0, FALSE);
5303 }
5304 while (i != hisidx[histtype]);
5305 }
5306 }
5307
5308 /* Replace the empty last line with the current command-line and put the
5309 * cursor there. */
5310 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5311 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5312 curwin->w_cursor.col = ccline.cmdpos;
5313 redraw_later(NOT_VALID);
5314
5315 /* Save the command line info, can be used recursively. */
5316 save_ccline = ccline;
5317 ccline.cmdbuff = NULL;
5318 ccline.cmdprompt = NULL;
5319
5320 /* No Ex mode here! */
5321 exmode_active = 0;
5322
5323 State = NORMAL;
5324# ifdef FEAT_MOUSE
5325 setmouse();
5326# endif
5327
5328# ifdef FEAT_AUTOCMD
5329 /* Trigger CmdwinEnter autocommands. */
5330 typestr[0] = cmdwin_type;
5331 typestr[1] = NUL;
5332 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5333# endif
5334
5335 i = RedrawingDisabled;
5336 RedrawingDisabled = 0;
5337
5338 /*
5339 * Call the main loop until <CR> or CTRL-C is typed.
5340 */
5341 cmdwin_result = 0;
5342 main_loop(TRUE);
5343
5344 RedrawingDisabled = i;
5345
5346# ifdef FEAT_AUTOCMD
5347 /* Trigger CmdwinLeave autocommands. */
5348 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5349# endif
5350
5351 /* Restore the comand line info. */
5352 ccline = save_ccline;
5353 cmdwin_type = 0;
5354
5355 exmode_active = save_exmode;
5356
5357 /* Safety check: The old window or buffer was deleted: It's a a bug when
5358 * this happens! */
5359 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5360 {
5361 cmdwin_result = Ctrl_C;
5362 EMSG(_("E199: Active window or buffer deleted"));
5363 }
5364 else
5365 {
5366# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5367 /* autocmds may abort script processing */
5368 if (aborting() && cmdwin_result != K_IGNORE)
5369 cmdwin_result = Ctrl_C;
5370# endif
5371 /* Set the new command line from the cmdline buffer. */
5372 vim_free(ccline.cmdbuff);
5373 if (cmdwin_result == K_XF1) /* :qa! typed */
5374 {
5375 ccline.cmdbuff = vim_strsave((char_u *)"qa!");
5376 cmdwin_result = CAR;
5377 }
5378 else if (cmdwin_result == K_XF2) /* :qa typed */
5379 {
5380 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5381 cmdwin_result = CAR;
5382 }
5383 else
5384 ccline.cmdbuff = vim_strsave(ml_get_curline());
5385 if (ccline.cmdbuff == NULL)
5386 cmdwin_result = Ctrl_C;
5387 else
5388 {
5389 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5390 ccline.cmdbufflen = ccline.cmdlen + 1;
5391 ccline.cmdpos = curwin->w_cursor.col;
5392 if (ccline.cmdpos > ccline.cmdlen)
5393 ccline.cmdpos = ccline.cmdlen;
5394 if (cmdwin_result == K_IGNORE)
5395 {
5396 set_cmdspos_cursor();
5397 redrawcmd();
5398 }
5399 }
5400
5401# ifdef FEAT_AUTOCMD
5402 /* Don't execute autocommands while deleting the window. */
5403 ++autocmd_block;
5404# endif
5405 wp = curwin;
5406 bp = curbuf;
5407 win_goto(old_curwin);
5408 win_close(wp, TRUE);
5409 close_buffer(NULL, bp, DOBUF_WIPE);
5410
5411 /* Restore window sizes. */
5412 win_size_restore(&winsizes);
5413
5414# ifdef FEAT_AUTOCMD
5415 --autocmd_block;
5416# endif
5417 }
5418
5419 ga_clear(&winsizes);
5420 restart_edit = save_restart_edit;
5421
5422 State = save_State;
5423# ifdef FEAT_MOUSE
5424 setmouse();
5425# endif
5426
5427 return cmdwin_result;
5428}
5429#endif /* FEAT_CMDWIN */
5430
5431/*
5432 * Used for commands that either take a simple command string argument, or:
5433 * cmd << endmarker
5434 * {script}
5435 * endmarker
5436 * Returns a pointer to allocated memory with {script} or NULL.
5437 */
5438 char_u *
5439script_get(eap, cmd)
5440 exarg_T *eap;
5441 char_u *cmd;
5442{
5443 char_u *theline;
5444 char *end_pattern = NULL;
5445 char dot[] = ".";
5446 garray_T ga;
5447
5448 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5449 return NULL;
5450
5451 ga_init2(&ga, 1, 0x400);
5452
5453 if (cmd[2] != NUL)
5454 end_pattern = (char *)skipwhite(cmd + 2);
5455 else
5456 end_pattern = dot;
5457
5458 for (;;)
5459 {
5460 theline = eap->getline(
5461#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00005462 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463#endif
5464 NUL, eap->cookie, 0);
5465
5466 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
5467 break;
5468
5469 ga_concat(&ga, theline);
5470 ga_append(&ga, '\n');
5471 vim_free(theline);
5472 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00005473 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475 return (char_u *)ga.ga_data;
5476}