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