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