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