blob: 335f2a411c3047dc7e586053ed71a609f53480a7 [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.
648 * Need to save the current command line, to be able to enter
649 * a new one...
650 */
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 Moolenaar5f2bb9f2005-01-11 21:29:04 +0000661 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 p = get_expr_line();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000663 restore_cmdline(&save_ccline);
664
665 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 {
667 ccline.cmdlen = STRLEN(p);
668 STRCPY(ccline.cmdbuff, p);
669 vim_free(p);
670
671 /* Restore the cursor or use the position set with
672 * set_cmdline_pos(). */
673 if (new_cmdpos > ccline.cmdlen)
674 ccline.cmdpos = ccline.cmdlen;
675 else
676 ccline.cmdpos = new_cmdpos;
677
678 KeyTyped = FALSE; /* Don't do p_wc completion. */
679 redrawcmd();
680 goto cmdline_changed;
681 }
682 }
683 beep_flush();
684 c = ESC;
685 }
686#endif
687 else
688 {
689 if (c == Ctrl_G && p_im && restart_edit == 0)
690 restart_edit = 'a';
691 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
692 in history */
693 goto returncmd; /* back to Normal mode */
694 }
695 }
696
697#ifdef FEAT_CMDWIN
698 if (c == cedit_key || c == K_CMDWIN)
699 {
700 /*
701 * Open a window to edit the command line (and history).
702 */
703 c = ex_window();
704 some_key_typed = TRUE;
705 }
706# ifdef FEAT_DIGRAPHS
707 else
708# endif
709#endif
710#ifdef FEAT_DIGRAPHS
711 c = do_digraph(c);
712#endif
713
714 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
715 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
716 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000717 /* In Ex mode a backslash escapes a newline. */
718 if (exmode_active
719 && c != ESC
720 && ccline.cmdpos > 0
721 && ccline.cmdpos == ccline.cmdlen
722 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000724 if (c == K_KENTER)
725 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000727 else
728 {
729 gotesc = FALSE; /* Might have typed ESC previously, don't
730 truncate the cmdline now. */
731 if (ccheck_abbr(c + ABBR_OFF))
732 goto cmdline_changed;
733 if (!cmd_silent)
734 {
735 windgoto(msg_row, 0);
736 out_flush();
737 }
738 break;
739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 }
741
742 /*
743 * Completion for 'wildchar' or 'wildcharm' key.
744 * - hitting <ESC> twice means: abandon command line.
745 * - wildcard expansion is only done when the 'wildchar' key is really
746 * typed, not when it comes from a macro
747 */
748 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
749 {
750 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
751 {
752 /* if 'wildmode' contains "list" may still need to list */
753 if (xpc.xp_numfiles > 1
754 && !did_wild_list
755 && (wim_flags[wim_index] & WIM_LIST))
756 {
757 (void)showmatches(&xpc, FALSE);
758 redrawcmd();
759 did_wild_list = TRUE;
760 }
761 if (wim_flags[wim_index] & WIM_LONGEST)
762 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
763 else if (wim_flags[wim_index] & WIM_FULL)
764 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
765 else
766 res = OK; /* don't insert 'wildchar' now */
767 }
768 else /* typed p_wc first time */
769 {
770 wim_index = 0;
771 j = ccline.cmdpos;
772 /* if 'wildmode' first contains "longest", get longest
773 * common part */
774 if (wim_flags[0] & WIM_LONGEST)
775 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
776 else
777 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
778
779 /* if interrupted while completing, behave like it failed */
780 if (got_int)
781 {
782 (void)vpeekc(); /* remove <C-C> from input stream */
783 got_int = FALSE; /* don't abandon the command line */
784 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
785#ifdef FEAT_WILDMENU
786 xpc.xp_context = EXPAND_NOTHING;
787#endif
788 goto cmdline_changed;
789 }
790
791 /* when more than one match, and 'wildmode' first contains
792 * "list", or no change and 'wildmode' contains "longest,list",
793 * list all matches */
794 if (res == OK && xpc.xp_numfiles > 1)
795 {
796 /* a "longest" that didn't do anything is skipped (but not
797 * "list:longest") */
798 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
799 wim_index = 1;
800 if ((wim_flags[wim_index] & WIM_LIST)
801#ifdef FEAT_WILDMENU
802 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
803#endif
804 )
805 {
806 if (!(wim_flags[0] & WIM_LONGEST))
807 {
808#ifdef FEAT_WILDMENU
809 int p_wmnu_save = p_wmnu;
810 p_wmnu = 0;
811#endif
812 nextwild(&xpc, WILD_PREV, 0); /* remove match */
813#ifdef FEAT_WILDMENU
814 p_wmnu = p_wmnu_save;
815#endif
816 }
817#ifdef FEAT_WILDMENU
818 (void)showmatches(&xpc, p_wmnu
819 && ((wim_flags[wim_index] & WIM_LIST) == 0));
820#else
821 (void)showmatches(&xpc, FALSE);
822#endif
823 redrawcmd();
824 did_wild_list = TRUE;
825 if (wim_flags[wim_index] & WIM_LONGEST)
826 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
827 else if (wim_flags[wim_index] & WIM_FULL)
828 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
829 }
830 else
831 vim_beep();
832 }
833#ifdef FEAT_WILDMENU
834 else if (xpc.xp_numfiles == -1)
835 xpc.xp_context = EXPAND_NOTHING;
836#endif
837 }
838 if (wim_index < 3)
839 ++wim_index;
840 if (c == ESC)
841 gotesc = TRUE;
842 if (res == OK)
843 goto cmdline_changed;
844 }
845
846 gotesc = FALSE;
847
848 /* <S-Tab> goes to last match, in a clumsy way */
849 if (c == K_S_TAB && KeyTyped)
850 {
851 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
852 && nextwild(&xpc, WILD_PREV, 0) == OK
853 && nextwild(&xpc, WILD_PREV, 0) == OK)
854 goto cmdline_changed;
855 }
856
857 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
858 c = NL;
859
860 do_abbr = TRUE; /* default: check for abbreviation */
861
862 /*
863 * Big switch for a typed command line character.
864 */
865 switch (c)
866 {
867 case K_BS:
868 case Ctrl_H:
869 case K_DEL:
870 case K_KDEL:
871 case Ctrl_W:
872#ifdef FEAT_FKMAP
873 if (cmd_fkmap && c == K_BS)
874 c = K_DEL;
875#endif
876 if (c == K_KDEL)
877 c = K_DEL;
878
879 /*
880 * delete current character is the same as backspace on next
881 * character, except at end of line
882 */
883 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
884 ++ccline.cmdpos;
885#ifdef FEAT_MBYTE
886 if (has_mbyte && c == K_DEL)
887 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
888 ccline.cmdbuff + ccline.cmdpos);
889#endif
890 if (ccline.cmdpos > 0)
891 {
892 char_u *p;
893
894 j = ccline.cmdpos;
895 p = ccline.cmdbuff + j;
896#ifdef FEAT_MBYTE
897 if (has_mbyte)
898 {
899 p = mb_prevptr(ccline.cmdbuff, p);
900 if (c == Ctrl_W)
901 {
902 while (p > ccline.cmdbuff && vim_isspace(*p))
903 p = mb_prevptr(ccline.cmdbuff, p);
904 i = mb_get_class(p);
905 while (p > ccline.cmdbuff && mb_get_class(p) == i)
906 p = mb_prevptr(ccline.cmdbuff, p);
907 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000908 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 }
910 }
911 else
912#endif
913 if (c == Ctrl_W)
914 {
915 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
916 --p;
917 i = vim_iswordc(p[-1]);
918 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
919 && vim_iswordc(p[-1]) == i)
920 --p;
921 }
922 else
923 --p;
924 ccline.cmdpos = (int)(p - ccline.cmdbuff);
925 ccline.cmdlen -= j - ccline.cmdpos;
926 i = ccline.cmdpos;
927 while (i < ccline.cmdlen)
928 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
929
930 /* Truncate at the end, required for multi-byte chars. */
931 ccline.cmdbuff[ccline.cmdlen] = NUL;
932 redrawcmd();
933 }
934 else if (ccline.cmdlen == 0 && c != Ctrl_W
935 && ccline.cmdprompt == NULL && indent == 0)
936 {
937 /* In ex and debug mode it doesn't make sense to return. */
938 if (exmode_active
939#ifdef FEAT_EVAL
940 || ccline.cmdfirstc == '>'
941#endif
942 )
943 goto cmdline_not_changed;
944
945 vim_free(ccline.cmdbuff); /* no commandline to return */
946 ccline.cmdbuff = NULL;
947 if (!cmd_silent)
948 {
949#ifdef FEAT_RIGHTLEFT
950 if (cmdmsg_rl)
951 msg_col = Columns;
952 else
953#endif
954 msg_col = 0;
955 msg_putchar(' '); /* delete ':' */
956 }
957 redraw_cmdline = TRUE;
958 goto returncmd; /* back to cmd mode */
959 }
960 goto cmdline_changed;
961
962 case K_INS:
963 case K_KINS:
964#ifdef FEAT_FKMAP
965 /* if Farsi mode set, we are in reverse insert mode -
966 Do not change the mode */
967 if (cmd_fkmap)
968 beep_flush();
969 else
970#endif
971 ccline.overstrike = !ccline.overstrike;
972#ifdef CURSOR_SHAPE
973 ui_cursor_shape(); /* may show different cursor shape */
974#endif
975 goto cmdline_not_changed;
976
977 case Ctrl_HAT:
978 if (map_to_exists_mode((char_u *)"", LANGMAP))
979 {
980 /* ":lmap" mappings exists, toggle use of mappings. */
981 State ^= LANGMAP;
982#ifdef USE_IM_CONTROL
983 im_set_active(FALSE); /* Disable input method */
984#endif
985 if (b_im_ptr != NULL)
986 {
987 if (State & LANGMAP)
988 *b_im_ptr = B_IMODE_LMAP;
989 else
990 *b_im_ptr = B_IMODE_NONE;
991 }
992 }
993#ifdef USE_IM_CONTROL
994 else
995 {
996 /* There are no ":lmap" mappings, toggle IM. When
997 * 'imdisable' is set don't try getting the status, it's
998 * always off. */
999 if ((p_imdisable && b_im_ptr != NULL)
1000 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1001 {
1002 im_set_active(FALSE); /* Disable input method */
1003 if (b_im_ptr != NULL)
1004 *b_im_ptr = B_IMODE_NONE;
1005 }
1006 else
1007 {
1008 im_set_active(TRUE); /* Enable input method */
1009 if (b_im_ptr != NULL)
1010 *b_im_ptr = B_IMODE_IM;
1011 }
1012 }
1013#endif
1014 if (b_im_ptr != NULL)
1015 {
1016 if (b_im_ptr == &curbuf->b_p_iminsert)
1017 set_iminsert_global();
1018 else
1019 set_imsearch_global();
1020 }
1021#ifdef CURSOR_SHAPE
1022 ui_cursor_shape(); /* may show different cursor shape */
1023#endif
1024#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1025 /* Show/unshow value of 'keymap' in status lines later. */
1026 status_redraw_curbuf();
1027#endif
1028 goto cmdline_not_changed;
1029
1030/* case '@': only in very old vi */
1031 case Ctrl_U:
1032 /* delete all characters left of the cursor */
1033 j = ccline.cmdpos;
1034 ccline.cmdlen -= j;
1035 i = ccline.cmdpos = 0;
1036 while (i < ccline.cmdlen)
1037 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1038 /* Truncate at the end, required for multi-byte chars. */
1039 ccline.cmdbuff[ccline.cmdlen] = NUL;
1040 redrawcmd();
1041 goto cmdline_changed;
1042
1043#ifdef FEAT_CLIPBOARD
1044 case Ctrl_Y:
1045 /* Copy the modeless selection, if there is one. */
1046 if (clip_star.state != SELECT_CLEARED)
1047 {
1048 if (clip_star.state == SELECT_DONE)
1049 clip_copy_modeless_selection(TRUE);
1050 goto cmdline_not_changed;
1051 }
1052 break;
1053#endif
1054
1055 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1056 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001057 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001058 * ":normal" runs out of characters. */
1059 if (exmode_active
1060#ifdef FEAT_EX_EXTRA
1061 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1062#endif
1063 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 goto cmdline_not_changed;
1065
1066 gotesc = TRUE; /* will free ccline.cmdbuff after
1067 putting it in history */
1068 goto returncmd; /* back to cmd mode */
1069
1070 case Ctrl_R: /* insert register */
1071#ifdef USE_ON_FLY_SCROLL
1072 dont_scroll = TRUE; /* disallow scrolling here */
1073#endif
1074 putcmdline('"', TRUE);
1075 ++no_mapping;
1076 i = c = safe_vgetc(); /* CTRL-R <char> */
1077 if (i == Ctrl_O)
1078 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1079 if (i == Ctrl_R)
1080 c = safe_vgetc(); /* CTRL-R CTRL-R <char> */
1081 --no_mapping;
1082#ifdef FEAT_EVAL
1083 /*
1084 * Insert the result of an expression.
1085 * Need to save the current command line, to be able to enter
1086 * a new one...
1087 */
1088 new_cmdpos = -1;
1089 if (c == '=')
1090 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1092 {
1093 beep_flush();
1094 c = ESC;
1095 }
1096 else
1097 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001098 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001100 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 }
1102 }
1103#endif
1104 if (c != ESC) /* use ESC to cancel inserting register */
1105 {
1106 cmdline_paste(c, i == Ctrl_R);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001107
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001108#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001109 /* When there was a serious error abort getting the
1110 * command line. */
1111 if (aborting())
1112 {
1113 gotesc = TRUE; /* will free ccline.cmdbuff after
1114 putting it in history */
1115 goto returncmd; /* back to cmd mode */
1116 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 KeyTyped = FALSE; /* Don't do p_wc completion. */
1119#ifdef FEAT_EVAL
1120 if (new_cmdpos >= 0)
1121 {
1122 /* set_cmdline_pos() was used */
1123 if (new_cmdpos > ccline.cmdlen)
1124 ccline.cmdpos = ccline.cmdlen;
1125 else
1126 ccline.cmdpos = new_cmdpos;
1127 }
1128#endif
1129 }
1130 redrawcmd();
1131 goto cmdline_changed;
1132
1133 case Ctrl_D:
1134 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1135 break; /* Use ^D as normal char instead */
1136
1137 redrawcmd();
1138 continue; /* don't do incremental search now */
1139
1140 case K_RIGHT:
1141 case K_S_RIGHT:
1142 case K_C_RIGHT:
1143 do
1144 {
1145 if (ccline.cmdpos >= ccline.cmdlen)
1146 break;
1147 i = cmdline_charsize(ccline.cmdpos);
1148 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1149 break;
1150 ccline.cmdspos += i;
1151#ifdef FEAT_MBYTE
1152 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001153 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 + ccline.cmdpos);
1155 else
1156#endif
1157 ++ccline.cmdpos;
1158 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001159 while ((c == K_S_RIGHT || c == K_C_RIGHT
1160 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1162#ifdef FEAT_MBYTE
1163 if (has_mbyte)
1164 set_cmdspos_cursor();
1165#endif
1166 goto cmdline_not_changed;
1167
1168 case K_LEFT:
1169 case K_S_LEFT:
1170 case K_C_LEFT:
1171 do
1172 {
1173 if (ccline.cmdpos == 0)
1174 break;
1175 --ccline.cmdpos;
1176#ifdef FEAT_MBYTE
1177 if (has_mbyte) /* move to first byte of char */
1178 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1179 ccline.cmdbuff + ccline.cmdpos);
1180#endif
1181 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1182 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001183 while ((c == K_S_LEFT || c == K_C_LEFT
1184 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1186#ifdef FEAT_MBYTE
1187 if (has_mbyte)
1188 set_cmdspos_cursor();
1189#endif
1190 goto cmdline_not_changed;
1191
1192 case K_IGNORE:
1193 goto cmdline_not_changed; /* Ignore mouse */
1194
1195#ifdef FEAT_MOUSE
1196 case K_MIDDLEDRAG:
1197 case K_MIDDLERELEASE:
1198 goto cmdline_not_changed; /* Ignore mouse */
1199
1200 case K_MIDDLEMOUSE:
1201# ifdef FEAT_GUI
1202 /* When GUI is active, also paste when 'mouse' is empty */
1203 if (!gui.in_use)
1204# endif
1205 if (!mouse_has(MOUSE_COMMAND))
1206 goto cmdline_not_changed; /* Ignore mouse */
1207#ifdef FEAT_CLIPBOARD
1208 if (clip_star.available)
1209 cmdline_paste('*', TRUE);
1210 else
1211#endif
1212 cmdline_paste(0, TRUE);
1213 redrawcmd();
1214 goto cmdline_changed;
1215
1216#ifdef FEAT_DND
1217 case K_DROP:
1218 cmdline_paste('~', TRUE);
1219 redrawcmd();
1220 goto cmdline_changed;
1221#endif
1222
1223 case K_LEFTDRAG:
1224 case K_LEFTRELEASE:
1225 case K_RIGHTDRAG:
1226 case K_RIGHTRELEASE:
1227 /* Ignore drag and release events when the button-down wasn't
1228 * seen before. */
1229 if (ignore_drag_release)
1230 goto cmdline_not_changed;
1231 /* FALLTHROUGH */
1232 case K_LEFTMOUSE:
1233 case K_RIGHTMOUSE:
1234 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1235 ignore_drag_release = TRUE;
1236 else
1237 ignore_drag_release = FALSE;
1238# ifdef FEAT_GUI
1239 /* When GUI is active, also move when 'mouse' is empty */
1240 if (!gui.in_use)
1241# endif
1242 if (!mouse_has(MOUSE_COMMAND))
1243 goto cmdline_not_changed; /* Ignore mouse */
1244# ifdef FEAT_CLIPBOARD
1245 if (mouse_row < cmdline_row && clip_star.available)
1246 {
1247 int button, is_click, is_drag;
1248
1249 /*
1250 * Handle modeless selection.
1251 */
1252 button = get_mouse_button(KEY2TERMCAP1(c),
1253 &is_click, &is_drag);
1254 if (mouse_model_popup() && button == MOUSE_LEFT
1255 && (mod_mask & MOD_MASK_SHIFT))
1256 {
1257 /* Translate shift-left to right button. */
1258 button = MOUSE_RIGHT;
1259 mod_mask &= ~MOD_MASK_SHIFT;
1260 }
1261 clip_modeless(button, is_click, is_drag);
1262 goto cmdline_not_changed;
1263 }
1264# endif
1265
1266 set_cmdspos();
1267 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1268 ++ccline.cmdpos)
1269 {
1270 i = cmdline_charsize(ccline.cmdpos);
1271 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1272 && mouse_col < ccline.cmdspos % Columns + i)
1273 break;
1274#ifdef FEAT_MBYTE
1275 if (has_mbyte)
1276 {
1277 /* Count ">" for double-wide char that doesn't fit. */
1278 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001279 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 + ccline.cmdpos) - 1;
1281 }
1282#endif
1283 ccline.cmdspos += i;
1284 }
1285 goto cmdline_not_changed;
1286
1287 /* Mouse scroll wheel: ignored here */
1288 case K_MOUSEDOWN:
1289 case K_MOUSEUP:
1290 /* Alternate buttons ignored here */
1291 case K_X1MOUSE:
1292 case K_X1DRAG:
1293 case K_X1RELEASE:
1294 case K_X2MOUSE:
1295 case K_X2DRAG:
1296 case K_X2RELEASE:
1297 goto cmdline_not_changed;
1298
1299#endif /* FEAT_MOUSE */
1300
1301#ifdef FEAT_GUI
1302 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1303 case K_LEFTRELEASE_NM:
1304 goto cmdline_not_changed;
1305
1306 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001307 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
1309 gui_do_scroll();
1310 redrawcmd();
1311 }
1312 goto cmdline_not_changed;
1313
1314 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001315 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
1317 gui_do_horiz_scroll();
1318 redrawcmd();
1319 }
1320 goto cmdline_not_changed;
1321#endif
1322 case K_SELECT: /* end of Select mode mapping - ignore */
1323 goto cmdline_not_changed;
1324
1325 case Ctrl_B: /* begin of command line */
1326 case K_HOME:
1327 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 case K_S_HOME:
1329 case K_C_HOME:
1330 ccline.cmdpos = 0;
1331 set_cmdspos();
1332 goto cmdline_not_changed;
1333
1334 case Ctrl_E: /* end of command line */
1335 case K_END:
1336 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 case K_S_END:
1338 case K_C_END:
1339 ccline.cmdpos = ccline.cmdlen;
1340 set_cmdspos_cursor();
1341 goto cmdline_not_changed;
1342
1343 case Ctrl_A: /* all matches */
1344 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1345 break;
1346 goto cmdline_changed;
1347
1348 case Ctrl_L: /* longest common part */
1349 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1350 break;
1351 goto cmdline_changed;
1352
1353 case Ctrl_N: /* next match */
1354 case Ctrl_P: /* previous match */
1355 if (xpc.xp_numfiles > 0)
1356 {
1357 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1358 == FAIL)
1359 break;
1360 goto cmdline_changed;
1361 }
1362
1363#ifdef FEAT_CMDHIST
1364 case K_UP:
1365 case K_DOWN:
1366 case K_S_UP:
1367 case K_S_DOWN:
1368 case K_PAGEUP:
1369 case K_KPAGEUP:
1370 case K_PAGEDOWN:
1371 case K_KPAGEDOWN:
1372 if (hislen == 0 || firstc == NUL) /* no history */
1373 goto cmdline_not_changed;
1374
1375 i = hiscnt;
1376
1377 /* save current command string so it can be restored later */
1378 if (lookfor == NULL)
1379 {
1380 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1381 goto cmdline_not_changed;
1382 lookfor[ccline.cmdpos] = NUL;
1383 }
1384
1385 j = (int)STRLEN(lookfor);
1386 for (;;)
1387 {
1388 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001389 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001390 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
1392 if (hiscnt == hislen) /* first time */
1393 hiscnt = hisidx[histype];
1394 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1395 hiscnt = hislen - 1;
1396 else if (hiscnt != hisidx[histype] + 1)
1397 --hiscnt;
1398 else /* at top of list */
1399 {
1400 hiscnt = i;
1401 break;
1402 }
1403 }
1404 else /* one step forwards */
1405 {
1406 /* on last entry, clear the line */
1407 if (hiscnt == hisidx[histype])
1408 {
1409 hiscnt = hislen;
1410 break;
1411 }
1412
1413 /* not on a history line, nothing to do */
1414 if (hiscnt == hislen)
1415 break;
1416 if (hiscnt == hislen - 1) /* wrap around */
1417 hiscnt = 0;
1418 else
1419 ++hiscnt;
1420 }
1421 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1422 {
1423 hiscnt = i;
1424 break;
1425 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001426 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001427 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 || STRNCMP(history[histype][hiscnt].hisstr,
1429 lookfor, (size_t)j) == 0)
1430 break;
1431 }
1432
1433 if (hiscnt != i) /* jumped to other entry */
1434 {
1435 char_u *p;
1436 int len;
1437 int old_firstc;
1438
1439 vim_free(ccline.cmdbuff);
1440 if (hiscnt == hislen)
1441 p = lookfor; /* back to the old one */
1442 else
1443 p = history[histype][hiscnt].hisstr;
1444
1445 if (histype == HIST_SEARCH
1446 && p != lookfor
1447 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1448 {
1449 /* Correct for the separator character used when
1450 * adding the history entry vs the one used now.
1451 * First loop: count length.
1452 * Second loop: copy the characters. */
1453 for (i = 0; i <= 1; ++i)
1454 {
1455 len = 0;
1456 for (j = 0; p[j] != NUL; ++j)
1457 {
1458 /* Replace old sep with new sep, unless it is
1459 * escaped. */
1460 if (p[j] == old_firstc
1461 && (j == 0 || p[j - 1] != '\\'))
1462 {
1463 if (i > 0)
1464 ccline.cmdbuff[len] = firstc;
1465 }
1466 else
1467 {
1468 /* Escape new sep, unless it is already
1469 * escaped. */
1470 if (p[j] == firstc
1471 && (j == 0 || p[j - 1] != '\\'))
1472 {
1473 if (i > 0)
1474 ccline.cmdbuff[len] = '\\';
1475 ++len;
1476 }
1477 if (i > 0)
1478 ccline.cmdbuff[len] = p[j];
1479 }
1480 ++len;
1481 }
1482 if (i == 0)
1483 {
1484 alloc_cmdbuff(len);
1485 if (ccline.cmdbuff == NULL)
1486 goto returncmd;
1487 }
1488 }
1489 ccline.cmdbuff[len] = NUL;
1490 }
1491 else
1492 {
1493 alloc_cmdbuff((int)STRLEN(p));
1494 if (ccline.cmdbuff == NULL)
1495 goto returncmd;
1496 STRCPY(ccline.cmdbuff, p);
1497 }
1498
1499 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1500 redrawcmd();
1501 goto cmdline_changed;
1502 }
1503 beep_flush();
1504 goto cmdline_not_changed;
1505#endif
1506
1507 case Ctrl_V:
1508 case Ctrl_Q:
1509#ifdef FEAT_MOUSE
1510 ignore_drag_release = TRUE;
1511#endif
1512 putcmdline('^', TRUE);
1513 c = get_literal(); /* get next (two) character(s) */
1514 do_abbr = FALSE; /* don't do abbreviation now */
1515#ifdef FEAT_MBYTE
1516 /* may need to remove ^ when composing char was typed */
1517 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1518 {
1519 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1520 msg_putchar(' ');
1521 cursorcmd();
1522 }
1523#endif
1524 break;
1525
1526#ifdef FEAT_DIGRAPHS
1527 case Ctrl_K:
1528#ifdef FEAT_MOUSE
1529 ignore_drag_release = TRUE;
1530#endif
1531 putcmdline('?', TRUE);
1532#ifdef USE_ON_FLY_SCROLL
1533 dont_scroll = TRUE; /* disallow scrolling here */
1534#endif
1535 c = get_digraph(TRUE);
1536 if (c != NUL)
1537 break;
1538
1539 redrawcmd();
1540 goto cmdline_not_changed;
1541#endif /* FEAT_DIGRAPHS */
1542
1543#ifdef FEAT_RIGHTLEFT
1544 case Ctrl__: /* CTRL-_: switch language mode */
1545 if (!p_ari)
1546 break;
1547#ifdef FEAT_FKMAP
1548 if (p_altkeymap)
1549 {
1550 cmd_fkmap = !cmd_fkmap;
1551 if (cmd_fkmap) /* in Farsi always in Insert mode */
1552 ccline.overstrike = FALSE;
1553 }
1554 else /* Hebrew is default */
1555#endif
1556 cmd_hkmap = !cmd_hkmap;
1557 goto cmdline_not_changed;
1558#endif
1559
1560 default:
1561#ifdef UNIX
1562 if (c == intr_char)
1563 {
1564 gotesc = TRUE; /* will free ccline.cmdbuff after
1565 putting it in history */
1566 goto returncmd; /* back to Normal mode */
1567 }
1568#endif
1569 /*
1570 * Normal character with no special meaning. Just set mod_mask
1571 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1572 * the string <S-Space>. This should only happen after ^V.
1573 */
1574 if (!IS_SPECIAL(c))
1575 mod_mask = 0x0;
1576 break;
1577 }
1578 /*
1579 * End of switch on command line character.
1580 * We come here if we have a normal character.
1581 */
1582
1583 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1584#ifdef FEAT_MBYTE
1585 /* Add ABBR_OFF for characters above 0x100, this is
1586 * what check_abbr() expects. */
1587 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1588#endif
1589 c))
1590 goto cmdline_changed;
1591
1592 /*
1593 * put the character in the command line
1594 */
1595 if (IS_SPECIAL(c) || mod_mask != 0)
1596 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1597 else
1598 {
1599#ifdef FEAT_MBYTE
1600 if (has_mbyte)
1601 {
1602 j = (*mb_char2bytes)(c, IObuff);
1603 IObuff[j] = NUL; /* exclude composing chars */
1604 put_on_cmdline(IObuff, j, TRUE);
1605 }
1606 else
1607#endif
1608 {
1609 IObuff[0] = c;
1610 put_on_cmdline(IObuff, 1, TRUE);
1611 }
1612 }
1613 goto cmdline_changed;
1614
1615/*
1616 * This part implements incremental searches for "/" and "?"
1617 * Jump to cmdline_not_changed when a character has been read but the command
1618 * line did not change. Then we only search and redraw if something changed in
1619 * the past.
1620 * Jump to cmdline_changed when the command line did change.
1621 * (Sorry for the goto's, I know it is ugly).
1622 */
1623cmdline_not_changed:
1624#ifdef FEAT_SEARCH_EXTRA
1625 if (!incsearch_postponed)
1626 continue;
1627#endif
1628
1629cmdline_changed:
1630#ifdef FEAT_SEARCH_EXTRA
1631 /*
1632 * 'incsearch' highlighting.
1633 */
1634 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1635 {
1636 /* if there is a character waiting, search and redraw later */
1637 if (char_avail())
1638 {
1639 incsearch_postponed = TRUE;
1640 continue;
1641 }
1642 incsearch_postponed = FALSE;
1643 curwin->w_cursor = old_cursor; /* start at old position */
1644
1645 /* If there is no command line, don't do anything */
1646 if (ccline.cmdlen == 0)
1647 i = 0;
1648 else
1649 {
1650 cursor_off(); /* so the user knows we're busy */
1651 out_flush();
1652 ++emsg_off; /* So it doesn't beep if bad expr */
1653 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1654 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1655 --emsg_off;
1656 /* if interrupted while searching, behave like it failed */
1657 if (got_int)
1658 {
1659 (void)vpeekc(); /* remove <C-C> from input stream */
1660 got_int = FALSE; /* don't abandon the command line */
1661 i = 0;
1662 }
1663 else if (char_avail())
1664 /* cancelled searching because a char was typed */
1665 incsearch_postponed = TRUE;
1666 }
1667 if (i)
1668 highlight_match = TRUE; /* highlight position */
1669 else
1670 highlight_match = FALSE; /* remove highlight */
1671
1672 /* first restore the old curwin values, so the screen is
1673 * positioned in the same way as the actual search command */
1674 curwin->w_leftcol = old_leftcol;
1675 curwin->w_topline = old_topline;
1676# ifdef FEAT_DIFF
1677 curwin->w_topfill = old_topfill;
1678# endif
1679 curwin->w_botline = old_botline;
1680 changed_cline_bef_curs();
1681 update_topline();
1682
1683 if (i != 0)
1684 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001685 pos_T save_pos = curwin->w_cursor;
1686
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 /*
1688 * First move cursor to end of match, then to start. This
1689 * moves the whole match onto the screen when 'nowrap' is set.
1690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 curwin->w_cursor.lnum += search_match_lines;
1692 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001693 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1694 {
1695 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1696 coladvance((colnr_T)MAXCOL);
1697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 validate_cursor();
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001699 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 }
1701 validate_cursor();
1702
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001703 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 update_screen(NOT_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001705 restore_cmdline(&save_ccline);
1706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 msg_starthere();
1708 redrawcmdline();
1709 did_incsearch = TRUE;
1710 }
1711#else /* FEAT_SEARCH_EXTRA */
1712 ;
1713#endif
1714
1715#ifdef FEAT_RIGHTLEFT
1716 if (cmdmsg_rl
1717# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001718 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719# endif
1720 )
1721 /* Always redraw the whole command line to fix shaping and
1722 * right-left typing. Not efficient, but it works. */
1723 redrawcmd();
1724#endif
1725 }
1726
1727returncmd:
1728
1729#ifdef FEAT_RIGHTLEFT
1730 cmdmsg_rl = FALSE;
1731#endif
1732
1733#ifdef FEAT_FKMAP
1734 cmd_fkmap = 0;
1735#endif
1736
1737 ExpandCleanup(&xpc);
1738
1739#ifdef FEAT_SEARCH_EXTRA
1740 if (did_incsearch)
1741 {
1742 curwin->w_cursor = old_cursor;
1743 curwin->w_curswant = old_curswant;
1744 curwin->w_leftcol = old_leftcol;
1745 curwin->w_topline = old_topline;
1746# ifdef FEAT_DIFF
1747 curwin->w_topfill = old_topfill;
1748# endif
1749 curwin->w_botline = old_botline;
1750 highlight_match = FALSE;
1751 validate_cursor(); /* needed for TAB */
1752 redraw_later(NOT_VALID);
1753 }
1754#endif
1755
1756 if (ccline.cmdbuff != NULL)
1757 {
1758 /*
1759 * Put line in history buffer (":" and "=" only when it was typed).
1760 */
1761#ifdef FEAT_CMDHIST
1762 if (ccline.cmdlen && firstc != NUL
1763 && (some_key_typed || histype == HIST_SEARCH))
1764 {
1765 add_to_history(histype, ccline.cmdbuff, TRUE,
1766 histype == HIST_SEARCH ? firstc : NUL);
1767 if (firstc == ':')
1768 {
1769 vim_free(new_last_cmdline);
1770 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1771 }
1772 }
1773#endif
1774
1775 if (gotesc) /* abandon command line */
1776 {
1777 vim_free(ccline.cmdbuff);
1778 ccline.cmdbuff = NULL;
1779 if (msg_scrolled == 0)
1780 compute_cmdrow();
1781 MSG("");
1782 redraw_cmdline = TRUE;
1783 }
1784 }
1785
1786 /*
1787 * If the screen was shifted up, redraw the whole screen (later).
1788 * If the line is too long, clear it, so ruler and shown command do
1789 * not get printed in the middle of it.
1790 */
1791 msg_check();
1792 msg_scroll = save_msg_scroll;
1793 redir_off = FALSE;
1794
1795 /* When the command line was typed, no need for a wait-return prompt. */
1796 if (some_key_typed)
1797 need_wait_return = FALSE;
1798
1799 State = save_State;
1800#ifdef USE_IM_CONTROL
1801 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1802 im_save_status(b_im_ptr);
1803 im_set_active(FALSE);
1804#endif
1805#ifdef FEAT_MOUSE
1806 setmouse();
1807#endif
1808#ifdef CURSOR_SHAPE
1809 ui_cursor_shape(); /* may show different cursor shape */
1810#endif
1811
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001812 {
1813 char_u *p = ccline.cmdbuff;
1814
1815 /* Make ccline empty, getcmdline() may try to use it. */
1816 ccline.cmdbuff = NULL;
1817 return p;
1818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819}
1820
1821#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1822/*
1823 * Get a command line with a prompt.
1824 * This is prepared to be called recursively from getcmdline() (e.g. by
1825 * f_input() when evaluating an expression from CTRL-R =).
1826 * Returns the command line in allocated memory, or NULL.
1827 */
1828 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001829getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 int firstc;
1831 char_u *prompt; /* command line prompt */
1832 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001833 int xp_context; /* type of expansion */
1834 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
1836 char_u *s;
1837 struct cmdline_info save_ccline;
1838 int msg_col_save = msg_col;
1839
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001840 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 ccline.cmdprompt = prompt;
1842 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001843# ifdef FEAT_EVAL
1844 ccline.xp_context = xp_context;
1845 ccline.xp_arg = xp_arg;
1846 ccline.input_fn = (firstc == '@');
1847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001849 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 /* Restore msg_col, the prompt from input() may have changed it. */
1851 msg_col = msg_col_save;
1852
1853 return s;
1854}
1855#endif
1856
1857 static int
1858cmdline_charsize(idx)
1859 int idx;
1860{
1861#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1862 if (cmdline_star > 0) /* showing '*', always 1 position */
1863 return 1;
1864#endif
1865 return ptr2cells(ccline.cmdbuff + idx);
1866}
1867
1868/*
1869 * Compute the offset of the cursor on the command line for the prompt and
1870 * indent.
1871 */
1872 static void
1873set_cmdspos()
1874{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001875 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 ccline.cmdspos = 1 + ccline.cmdindent;
1877 else
1878 ccline.cmdspos = 0 + ccline.cmdindent;
1879}
1880
1881/*
1882 * Compute the screen position for the cursor on the command line.
1883 */
1884 static void
1885set_cmdspos_cursor()
1886{
1887 int i, m, c;
1888
1889 set_cmdspos();
1890 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001893 if (m < 0) /* overflow, Columns or Rows at weird value */
1894 m = MAXCOL;
1895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 else
1897 m = MAXCOL;
1898 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
1899 {
1900 c = cmdline_charsize(i);
1901#ifdef FEAT_MBYTE
1902 /* Count ">" for double-wide multi-byte char that doesn't fit. */
1903 if (has_mbyte)
1904 correct_cmdspos(i, c);
1905#endif
1906 /* If the cmdline doesn't fit, put cursor on last visible char. */
1907 if ((ccline.cmdspos += c) >= m)
1908 {
1909 ccline.cmdpos = i - 1;
1910 ccline.cmdspos -= c;
1911 break;
1912 }
1913#ifdef FEAT_MBYTE
1914 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001915 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916#endif
1917 }
1918}
1919
1920#ifdef FEAT_MBYTE
1921/*
1922 * Check if the character at "idx", which is "cells" wide, is a multi-byte
1923 * character that doesn't fit, so that a ">" must be displayed.
1924 */
1925 static void
1926correct_cmdspos(idx, cells)
1927 int idx;
1928 int cells;
1929{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001930 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
1932 && ccline.cmdspos % Columns + cells > Columns)
1933 ccline.cmdspos++;
1934}
1935#endif
1936
1937/*
1938 * Get an Ex command line for the ":" command.
1939 */
1940/* ARGSUSED */
1941 char_u *
1942getexline(c, dummy, indent)
1943 int c; /* normally ':', NUL for ":append" */
1944 void *dummy; /* cookie not used */
1945 int indent; /* indent for inside conditionals */
1946{
1947 /* When executing a register, remove ':' that's in front of each line. */
1948 if (exec_from_reg && vpeekc() == ':')
1949 (void)vgetc();
1950 return getcmdline(c, 1L, indent);
1951}
1952
1953/*
1954 * Get an Ex command line for Ex mode.
1955 * In Ex mode we only use the OS supplied line editing features and no
1956 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001957 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 */
1959/* ARGSUSED */
1960 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001961getexmodeline(promptc, dummy, indent)
1962 int promptc; /* normally ':', NUL for ":append" and '?' for
1963 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 void *dummy; /* cookie not used */
1965 int indent; /* indent for inside conditionals */
1966{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001967 garray_T line_ga;
1968 char_u *pend;
1969 int startcol = 0;
1970 int c1;
1971 int escaped = FALSE; /* CTRL-V typed */
1972 int vcol = 0;
1973 char_u *p;
1974 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
1976 /* Switch cursor on now. This avoids that it happens after the "\n", which
1977 * confuses the system function that computes tabstops. */
1978 cursor_on();
1979
1980 /* always start in column 0; write a newline if necessary */
1981 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001982 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001984 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001986 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001987 if (p_prompt)
1988 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 while (indent-- > 0)
1990 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993
1994 ga_init2(&line_ga, 1, 30);
1995
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001996 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001997 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001998 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001999 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002000 while (indent >= 8)
2001 {
2002 ga_append(&line_ga, TAB);
2003 msg_puts((char_u *)" ");
2004 indent -= 8;
2005 }
2006 while (indent-- > 0)
2007 {
2008 ga_append(&line_ga, ' ');
2009 msg_putchar(' ');
2010 }
2011 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002012 ++no_mapping;
2013 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002014
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 /*
2016 * Get the line, one character at a time.
2017 */
2018 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002019 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 {
2021 if (ga_grow(&line_ga, 40) == FAIL)
2022 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002023 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002025 /* Get one character at a time. Don't use inchar(), it can't handle
2026 * special characters. */
2027 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002030 * Handle line editing.
2031 * Previously this was left to the system, putting the terminal in
2032 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002034 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002036 msg_putchar('\n');
2037 break;
2038 }
2039
2040 if (!escaped)
2041 {
2042 /* CR typed means "enter", which is NL */
2043 if (c1 == '\r')
2044 c1 = '\n';
2045
2046 if (c1 == BS || c1 == K_BS
2047 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002049 if (line_ga.ga_len > 0)
2050 {
2051 --line_ga.ga_len;
2052 goto redraw;
2053 }
2054 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002057 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002059 msg_col = startcol;
2060 msg_clr_eos();
2061 line_ga.ga_len = 0;
2062 continue;
2063 }
2064
2065 if (c1 == Ctrl_T)
2066 {
2067 p = (char_u *)line_ga.ga_data;
2068 p[line_ga.ga_len] = NUL;
2069 indent = get_indent_str(p, 8);
2070 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2071add_indent:
2072 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002074 char_u *s = skipwhite(p);
2075
2076 ga_grow(&line_ga, 1);
2077 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2078 *s = ' ';
2079 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002081redraw:
2082 /* redraw the line */
2083 msg_col = startcol;
2084 windgoto(msg_row, msg_col);
2085 vcol = 0;
2086 for (p = (char_u *)line_ga.ga_data;
2087 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002089 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002091 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002093 msg_putchar(' ');
2094 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002096 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002098 msg_outtrans_len(p, 1);
2099 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
2101 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002102 msg_clr_eos();
2103 continue;
2104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002106 if (c1 == Ctrl_D)
2107 {
2108 /* Delete one shiftwidth. */
2109 p = (char_u *)line_ga.ga_data;
2110 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002112 if (prev_char == '^')
2113 ex_keep_indent = TRUE;
2114 indent = 0;
2115 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117 else
2118 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002119 p[line_ga.ga_len] = NUL;
2120 indent = get_indent_str(p, 8);
2121 --indent;
2122 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002124 while (get_indent_str(p, 8) > indent)
2125 {
2126 char_u *s = skipwhite(p);
2127
2128 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2129 --line_ga.ga_len;
2130 }
2131 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002133
2134 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2135 {
2136 escaped = TRUE;
2137 continue;
2138 }
2139
2140 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2141 if (IS_SPECIAL(c1))
2142 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002144
2145 if (IS_SPECIAL(c1))
2146 c1 = '?';
2147 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2148 prev_char = c1;
2149 if (c1 == '\n')
2150 msg_putchar('\n');
2151 else if (c1 == TAB)
2152 {
2153 /* Don't use chartabsize(), 'ts' can be different */
2154 do
2155 {
2156 msg_putchar(' ');
2157 } while (++vcol % 8);
2158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002161 msg_outtrans_len(
2162 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2163 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002165 ++line_ga.ga_len;
2166 escaped = FALSE;
2167
2168 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002169 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002170
2171 /* we are done when a NL is entered, but not when it comes after a
2172 * backslash */
2173 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2174 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002177 --pend;
2178 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002179 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 }
2181 }
2182
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002183 --no_mapping;
2184 --allow_keys;
2185
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 /* make following messages go to the next line */
2187 msg_didout = FALSE;
2188 msg_col = 0;
2189 if (msg_row < Rows - 1)
2190 ++msg_row;
2191 emsg_on_display = FALSE; /* don't want ui_delay() */
2192
2193 if (got_int)
2194 ga_clear(&line_ga);
2195
2196 return (char_u *)line_ga.ga_data;
2197}
2198
Bram Moolenaare344bea2005-09-01 20:46:49 +00002199# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2200 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201/*
2202 * Return TRUE if ccline.overstrike is on.
2203 */
2204 int
2205cmdline_overstrike()
2206{
2207 return ccline.overstrike;
2208}
2209
2210/*
2211 * Return TRUE if the cursor is at the end of the cmdline.
2212 */
2213 int
2214cmdline_at_end()
2215{
2216 return (ccline.cmdpos >= ccline.cmdlen);
2217}
2218#endif
2219
Bram Moolenaar9372a112005-12-06 19:59:18 +00002220#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221/*
2222 * Return the virtual column number at the current cursor position.
2223 * This is used by the IM code to obtain the start of the preedit string.
2224 */
2225 colnr_T
2226cmdline_getvcol_cursor()
2227{
2228 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2229 return MAXCOL;
2230
2231# ifdef FEAT_MBYTE
2232 if (has_mbyte)
2233 {
2234 colnr_T col;
2235 int i = 0;
2236
2237 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002238 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
2240 return col;
2241 }
2242 else
2243# endif
2244 return ccline.cmdpos;
2245}
2246#endif
2247
2248#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2249/*
2250 * If part of the command line is an IM preedit string, redraw it with
2251 * IM feedback attributes. The cursor position is restored after drawing.
2252 */
2253 static void
2254redrawcmd_preedit()
2255{
2256 if ((State & CMDLINE)
2257 && xic != NULL
2258 && im_get_status()
2259 && !p_imdisable
2260 && im_is_preediting())
2261 {
2262 int cmdpos = 0;
2263 int cmdspos;
2264 int old_row;
2265 int old_col;
2266 colnr_T col;
2267
2268 old_row = msg_row;
2269 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002270 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271
2272# ifdef FEAT_MBYTE
2273 if (has_mbyte)
2274 {
2275 for (col = 0; col < preedit_start_col
2276 && cmdpos < ccline.cmdlen; ++col)
2277 {
2278 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002279 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281 }
2282 else
2283# endif
2284 {
2285 cmdspos += preedit_start_col;
2286 cmdpos += preedit_start_col;
2287 }
2288
2289 msg_row = cmdline_row + (cmdspos / (int)Columns);
2290 msg_col = cmdspos % (int)Columns;
2291 if (msg_row >= Rows)
2292 msg_row = Rows - 1;
2293
2294 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2295 {
2296 int char_len;
2297 int char_attr;
2298
2299 char_attr = im_get_feedback_attr(col);
2300 if (char_attr < 0)
2301 break; /* end of preedit string */
2302
2303# ifdef FEAT_MBYTE
2304 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002305 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 else
2307# endif
2308 char_len = 1;
2309
2310 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2311 cmdpos += char_len;
2312 }
2313
2314 msg_row = old_row;
2315 msg_col = old_col;
2316 }
2317}
2318#endif /* FEAT_XIM && FEAT_GUI_GTK */
2319
2320/*
2321 * Allocate a new command line buffer.
2322 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2323 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2324 */
2325 static void
2326alloc_cmdbuff(len)
2327 int len;
2328{
2329 /*
2330 * give some extra space to avoid having to allocate all the time
2331 */
2332 if (len < 80)
2333 len = 100;
2334 else
2335 len += 20;
2336
2337 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2338 ccline.cmdbufflen = len;
2339}
2340
2341/*
2342 * Re-allocate the command line to length len + something extra.
2343 * return FAIL for failure, OK otherwise
2344 */
2345 static int
2346realloc_cmdbuff(len)
2347 int len;
2348{
2349 char_u *p;
2350
2351 p = ccline.cmdbuff;
2352 alloc_cmdbuff(len); /* will get some more */
2353 if (ccline.cmdbuff == NULL) /* out of memory */
2354 {
2355 ccline.cmdbuff = p; /* keep the old one */
2356 return FAIL;
2357 }
2358 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2359 vim_free(p);
2360 return OK;
2361}
2362
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002363#if defined(FEAT_ARABIC) || defined(PROTO)
2364static char_u *arshape_buf = NULL;
2365
2366# if defined(EXITFREE) || defined(PROTO)
2367 void
2368free_cmdline_buf()
2369{
2370 vim_free(arshape_buf);
2371}
2372# endif
2373#endif
2374
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375/*
2376 * Draw part of the cmdline at the current cursor position. But draw stars
2377 * when cmdline_star is TRUE.
2378 */
2379 static void
2380draw_cmdline(start, len)
2381 int start;
2382 int len;
2383{
2384#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2385 int i;
2386
2387 if (cmdline_star > 0)
2388 for (i = 0; i < len; ++i)
2389 {
2390 msg_putchar('*');
2391# ifdef FEAT_MBYTE
2392 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002393 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394# endif
2395 }
2396 else
2397#endif
2398#ifdef FEAT_ARABIC
2399 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 static int buflen = 0;
2402 char_u *p;
2403 int j;
2404 int newlen = 0;
2405 int mb_l;
2406 int pc, pc1;
2407 int prev_c = 0;
2408 int prev_c1 = 0;
2409 int u8c, u8c_c1, u8c_c2;
2410 int nc = 0;
2411 int dummy;
2412
2413 /*
2414 * Do arabic shaping into a temporary buffer. This is very
2415 * inefficient!
2416 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002417 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
2419 /* Re-allocate the buffer. We keep it around to avoid a lot of
2420 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002421 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002422 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002423 arshape_buf = alloc(buflen);
2424 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 return; /* out of memory */
2426 }
2427
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002428 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2429 {
2430 /* Prepend a space to draw the leading composing char on. */
2431 arshape_buf[0] = ' ';
2432 newlen = 1;
2433 }
2434
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 for (j = start; j < start + len; j += mb_l)
2436 {
2437 p = ccline.cmdbuff + j;
2438 u8c = utfc_ptr2char_len(p, &u8c_c1, &u8c_c2, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002439 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 if (ARABIC_CHAR(u8c))
2441 {
2442 /* Do Arabic shaping. */
2443 if (cmdmsg_rl)
2444 {
2445 /* displaying from right to left */
2446 pc = prev_c;
2447 pc1 = prev_c1;
2448 prev_c1 = u8c_c1;
2449 if (j + mb_l >= start + len)
2450 nc = NUL;
2451 else
2452 nc = utf_ptr2char(p + mb_l);
2453 }
2454 else
2455 {
2456 /* displaying from left to right */
2457 if (j + mb_l >= start + len)
2458 pc = NUL;
2459 else
2460 pc = utfc_ptr2char_len(p + mb_l, &pc1, &dummy,
2461 start + len - j - mb_l);
2462 nc = prev_c;
2463 }
2464 prev_c = u8c;
2465
2466 u8c = arabic_shape(u8c, NULL, &u8c_c1, pc, pc1, nc);
2467
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002468 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 if (u8c_c1 != 0)
2470 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002471 newlen += (*mb_char2bytes)(u8c_c1, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (u8c_c2 != 0)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002473 newlen += (*mb_char2bytes)(u8c_c2,
2474 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 }
2476 }
2477 else
2478 {
2479 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002480 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 newlen += mb_l;
2482 }
2483 }
2484
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002485 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
2487 else
2488#endif
2489 msg_outtrans_len(ccline.cmdbuff + start, len);
2490}
2491
2492/*
2493 * Put a character on the command line. Shifts the following text to the
2494 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2495 * "c" must be printable (fit in one display cell)!
2496 */
2497 void
2498putcmdline(c, shift)
2499 int c;
2500 int shift;
2501{
2502 if (cmd_silent)
2503 return;
2504 msg_no_more = TRUE;
2505 msg_putchar(c);
2506 if (shift)
2507 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2508 msg_no_more = FALSE;
2509 cursorcmd();
2510}
2511
2512/*
2513 * Undo a putcmdline(c, FALSE).
2514 */
2515 void
2516unputcmdline()
2517{
2518 if (cmd_silent)
2519 return;
2520 msg_no_more = TRUE;
2521 if (ccline.cmdlen == ccline.cmdpos)
2522 msg_putchar(' ');
2523 else
2524 draw_cmdline(ccline.cmdpos, 1);
2525 msg_no_more = FALSE;
2526 cursorcmd();
2527}
2528
2529/*
2530 * Put the given string, of the given length, onto the command line.
2531 * If len is -1, then STRLEN() is used to calculate the length.
2532 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2533 * part will be redrawn, otherwise it will not. If this function is called
2534 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2535 * called afterwards.
2536 */
2537 int
2538put_on_cmdline(str, len, redraw)
2539 char_u *str;
2540 int len;
2541 int redraw;
2542{
2543 int retval;
2544 int i;
2545 int m;
2546 int c;
2547
2548 if (len < 0)
2549 len = (int)STRLEN(str);
2550
2551 /* Check if ccline.cmdbuff needs to be longer */
2552 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2553 retval = realloc_cmdbuff(ccline.cmdlen + len);
2554 else
2555 retval = OK;
2556 if (retval == OK)
2557 {
2558 if (!ccline.overstrike)
2559 {
2560 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2561 ccline.cmdbuff + ccline.cmdpos,
2562 (size_t)(ccline.cmdlen - ccline.cmdpos));
2563 ccline.cmdlen += len;
2564 }
2565 else
2566 {
2567#ifdef FEAT_MBYTE
2568 if (has_mbyte)
2569 {
2570 /* Count nr of characters in the new string. */
2571 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002572 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 ++m;
2574 /* Count nr of bytes in cmdline that are overwritten by these
2575 * characters. */
2576 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002577 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 --m;
2579 if (i < ccline.cmdlen)
2580 {
2581 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2582 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2583 ccline.cmdlen += ccline.cmdpos + len - i;
2584 }
2585 else
2586 ccline.cmdlen = ccline.cmdpos + len;
2587 }
2588 else
2589#endif
2590 if (ccline.cmdpos + len > ccline.cmdlen)
2591 ccline.cmdlen = ccline.cmdpos + len;
2592 }
2593 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2594 ccline.cmdbuff[ccline.cmdlen] = NUL;
2595
2596#ifdef FEAT_MBYTE
2597 if (enc_utf8)
2598 {
2599 /* When the inserted text starts with a composing character,
2600 * backup to the character before it. There could be two of them.
2601 */
2602 i = 0;
2603 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2604 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2605 {
2606 i = (*mb_head_off)(ccline.cmdbuff,
2607 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2608 ccline.cmdpos -= i;
2609 len += i;
2610 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2611 }
2612# ifdef FEAT_ARABIC
2613 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2614 {
2615 /* Check the previous character for Arabic combining pair. */
2616 i = (*mb_head_off)(ccline.cmdbuff,
2617 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2618 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2619 + ccline.cmdpos - i), c))
2620 {
2621 ccline.cmdpos -= i;
2622 len += i;
2623 }
2624 else
2625 i = 0;
2626 }
2627# endif
2628 if (i != 0)
2629 {
2630 /* Also backup the cursor position. */
2631 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2632 ccline.cmdspos -= i;
2633 msg_col -= i;
2634 if (msg_col < 0)
2635 {
2636 msg_col += Columns;
2637 --msg_row;
2638 }
2639 }
2640 }
2641#endif
2642
2643 if (redraw && !cmd_silent)
2644 {
2645 msg_no_more = TRUE;
2646 i = cmdline_row;
2647 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2648 /* Avoid clearing the rest of the line too often. */
2649 if (cmdline_row != i || ccline.overstrike)
2650 msg_clr_eos();
2651 msg_no_more = FALSE;
2652 }
2653#ifdef FEAT_FKMAP
2654 /*
2655 * If we are in Farsi command mode, the character input must be in
2656 * Insert mode. So do not advance the cmdpos.
2657 */
2658 if (!cmd_fkmap)
2659#endif
2660 {
2661 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002664 if (m < 0) /* overflow, Columns or Rows at weird value */
2665 m = MAXCOL;
2666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 else
2668 m = MAXCOL;
2669 for (i = 0; i < len; ++i)
2670 {
2671 c = cmdline_charsize(ccline.cmdpos);
2672#ifdef FEAT_MBYTE
2673 /* count ">" for a double-wide char that doesn't fit. */
2674 if (has_mbyte)
2675 correct_cmdspos(ccline.cmdpos, c);
2676#endif
2677 /* Stop cursor at the end of the screen */
2678 if (ccline.cmdspos + c >= m)
2679 break;
2680 ccline.cmdspos += c;
2681#ifdef FEAT_MBYTE
2682 if (has_mbyte)
2683 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002684 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 if (c > len - i - 1)
2686 c = len - i - 1;
2687 ccline.cmdpos += c;
2688 i += c;
2689 }
2690#endif
2691 ++ccline.cmdpos;
2692 }
2693 }
2694 }
2695 if (redraw)
2696 msg_check();
2697 return retval;
2698}
2699
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002700static struct cmdline_info prev_ccline;
2701static int prev_ccline_used = FALSE;
2702
2703/*
2704 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2705 * and overwrite it. But get_cmdline_str() may need it, thus make it
2706 * available globally in prev_ccline.
2707 */
2708 static void
2709save_cmdline(ccp)
2710 struct cmdline_info *ccp;
2711{
2712 if (!prev_ccline_used)
2713 {
2714 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2715 prev_ccline_used = TRUE;
2716 }
2717 *ccp = prev_ccline;
2718 prev_ccline = ccline;
2719 ccline.cmdbuff = NULL;
2720 ccline.cmdprompt = NULL;
2721}
2722
2723/*
2724 * Resture ccline after it has been saved with save_cmdline().
2725 */
2726 static void
2727restore_cmdline(ccp)
2728 struct cmdline_info *ccp;
2729{
2730 ccline = prev_ccline;
2731 prev_ccline = *ccp;
2732}
2733
Bram Moolenaar8299df92004-07-10 09:47:34 +00002734/*
2735 * paste a yank register into the command line.
2736 * used by CTRL-R command in command-line mode
2737 * insert_reg() can't be used here, because special characters from the
2738 * register contents will be interpreted as commands.
2739 *
2740 * return FAIL for failure, OK otherwise
2741 */
2742 static int
2743cmdline_paste(regname, literally)
2744 int regname;
2745 int literally; /* Insert text literally instead of "as typed" */
2746{
2747 long i;
2748 char_u *arg;
2749 int allocated;
2750 struct cmdline_info save_ccline;
2751
2752 /* check for valid regname; also accept special characters for CTRL-R in
2753 * the command line */
2754 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2755 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2756 return FAIL;
2757
2758 /* A register containing CTRL-R can cause an endless loop. Allow using
2759 * CTRL-C to break the loop. */
2760 line_breakcheck();
2761 if (got_int)
2762 return FAIL;
2763
2764#ifdef FEAT_CLIPBOARD
2765 regname = may_get_selection(regname);
2766#endif
2767
Bram Moolenaarebefac62005-12-28 22:39:57 +00002768 /* Need to save and restore ccline. And go into the sandbox to avoid
2769 * nasty things like going to another buffer when evaluating an
2770 * expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002771 save_cmdline(&save_ccline);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002772#ifdef HAVE_SANDBOX
2773 ++sandbox;
2774#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +00002775 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarebefac62005-12-28 22:39:57 +00002776#ifdef HAVE_SANDBOX
2777 --sandbox;
2778#endif
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002779 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002780
2781 if (i)
2782 {
2783 /* Got the value of a special register in "arg". */
2784 if (arg == NULL)
2785 return FAIL;
2786 cmdline_paste_str(arg, literally);
2787 if (allocated)
2788 vim_free(arg);
2789 return OK;
2790 }
2791
2792 return cmdline_paste_reg(regname, literally);
2793}
2794
2795/*
2796 * Put a string on the command line.
2797 * When "literally" is TRUE, insert literally.
2798 * When "literally" is FALSE, insert as typed, but don't leave the command
2799 * line.
2800 */
2801 void
2802cmdline_paste_str(s, literally)
2803 char_u *s;
2804 int literally;
2805{
2806 int c, cv;
2807
2808 if (literally)
2809 put_on_cmdline(s, -1, TRUE);
2810 else
2811 while (*s != NUL)
2812 {
2813 cv = *s;
2814 if (cv == Ctrl_V && s[1])
2815 ++s;
2816#ifdef FEAT_MBYTE
2817 if (has_mbyte)
2818 {
2819 c = mb_ptr2char(s);
2820 s += mb_char2len(c);
2821 }
2822 else
2823#endif
2824 c = *s++;
2825 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2826#ifdef UNIX
2827 || c == intr_char
2828#endif
2829 || (c == Ctrl_BSL && *s == Ctrl_N))
2830 stuffcharReadbuff(Ctrl_V);
2831 stuffcharReadbuff(c);
2832 }
2833}
2834
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835#ifdef FEAT_WILDMENU
2836/*
2837 * Delete characters on the command line, from "from" to the current
2838 * position.
2839 */
2840 static void
2841cmdline_del(from)
2842 int from;
2843{
2844 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2845 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2846 ccline.cmdlen -= ccline.cmdpos - from;
2847 ccline.cmdpos = from;
2848}
2849#endif
2850
2851/*
2852 * this fuction is called when the screen size changes and with incremental
2853 * search
2854 */
2855 void
2856redrawcmdline()
2857{
2858 if (cmd_silent)
2859 return;
2860 need_wait_return = FALSE;
2861 compute_cmdrow();
2862 redrawcmd();
2863 cursorcmd();
2864}
2865
2866 static void
2867redrawcmdprompt()
2868{
2869 int i;
2870
2871 if (cmd_silent)
2872 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002873 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 msg_putchar(ccline.cmdfirstc);
2875 if (ccline.cmdprompt != NULL)
2876 {
2877 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2878 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2879 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002880 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 --ccline.cmdindent;
2882 }
2883 else
2884 for (i = ccline.cmdindent; i > 0; --i)
2885 msg_putchar(' ');
2886}
2887
2888/*
2889 * Redraw what is currently on the command line.
2890 */
2891 void
2892redrawcmd()
2893{
2894 if (cmd_silent)
2895 return;
2896
2897 msg_start();
2898 redrawcmdprompt();
2899
2900 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2901 msg_no_more = TRUE;
2902 draw_cmdline(0, ccline.cmdlen);
2903 msg_clr_eos();
2904 msg_no_more = FALSE;
2905
2906 set_cmdspos_cursor();
2907
2908 /*
2909 * An emsg() before may have set msg_scroll. This is used in normal mode,
2910 * in cmdline mode we can reset them now.
2911 */
2912 msg_scroll = FALSE; /* next message overwrites cmdline */
2913
2914 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2915 * in cmdline mode */
2916 skip_redraw = FALSE;
2917}
2918
2919 void
2920compute_cmdrow()
2921{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002922 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 cmdline_row = Rows - 1;
2924 else
2925 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2926 + W_STATUS_HEIGHT(lastwin);
2927}
2928
2929 static void
2930cursorcmd()
2931{
2932 if (cmd_silent)
2933 return;
2934
2935#ifdef FEAT_RIGHTLEFT
2936 if (cmdmsg_rl)
2937 {
2938 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
2939 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
2940 if (msg_row <= 0)
2941 msg_row = Rows - 1;
2942 }
2943 else
2944#endif
2945 {
2946 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
2947 msg_col = ccline.cmdspos % (int)Columns;
2948 if (msg_row >= Rows)
2949 msg_row = Rows - 1;
2950 }
2951
2952 windgoto(msg_row, msg_col);
2953#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2954 redrawcmd_preedit();
2955#endif
2956#ifdef MCH_CURSOR_SHAPE
2957 mch_update_cursor();
2958#endif
2959}
2960
2961 void
2962gotocmdline(clr)
2963 int clr;
2964{
2965 msg_start();
2966#ifdef FEAT_RIGHTLEFT
2967 if (cmdmsg_rl)
2968 msg_col = Columns - 1;
2969 else
2970#endif
2971 msg_col = 0; /* always start in column 0 */
2972 if (clr) /* clear the bottom line(s) */
2973 msg_clr_eos(); /* will reset clear_cmdline */
2974 windgoto(cmdline_row, 0);
2975}
2976
2977/*
2978 * Check the word in front of the cursor for an abbreviation.
2979 * Called when the non-id character "c" has been entered.
2980 * When an abbreviation is recognized it is removed from the text with
2981 * backspaces and the replacement string is inserted, followed by "c".
2982 */
2983 static int
2984ccheck_abbr(c)
2985 int c;
2986{
2987 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
2988 return FALSE;
2989
2990 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
2991}
2992
2993/*
2994 * Return FAIL if this is not an appropriate context in which to do
2995 * completion of anything, return OK if it is (even if there are no matches).
2996 * For the caller, this means that the character is just passed through like a
2997 * normal character (instead of being expanded). This allows :s/^I^D etc.
2998 */
2999 static int
3000nextwild(xp, type, options)
3001 expand_T *xp;
3002 int type;
3003 int options; /* extra options for ExpandOne() */
3004{
3005 int i, j;
3006 char_u *p1;
3007 char_u *p2;
3008 int oldlen;
3009 int difflen;
3010 int v;
3011
3012 if (xp->xp_numfiles == -1)
3013 {
3014 set_expand_context(xp);
3015 cmd_showtail = expand_showtail(xp);
3016 }
3017
3018 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3019 {
3020 beep_flush();
3021 return OK; /* Something illegal on command line */
3022 }
3023 if (xp->xp_context == EXPAND_NOTHING)
3024 {
3025 /* Caller can use the character as a normal char instead */
3026 return FAIL;
3027 }
3028
3029 MSG_PUTS("..."); /* show that we are busy */
3030 out_flush();
3031
3032 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3033 oldlen = ccline.cmdpos - i;
3034
3035 if (type == WILD_NEXT || type == WILD_PREV)
3036 {
3037 /*
3038 * Get next/previous match for a previous expanded pattern.
3039 */
3040 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3041 }
3042 else
3043 {
3044 /*
3045 * Translate string into pattern and expand it.
3046 */
3047 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3048 p2 = NULL;
3049 else
3050 {
3051 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3052 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3053 |options, type);
3054 vim_free(p1);
3055 /* longest match: make sure it is not shorter (happens with :help */
3056 if (p2 != NULL && type == WILD_LONGEST)
3057 {
3058 for (j = 0; j < oldlen; ++j)
3059 if (ccline.cmdbuff[i + j] == '*'
3060 || ccline.cmdbuff[i + j] == '?')
3061 break;
3062 if ((int)STRLEN(p2) < j)
3063 {
3064 vim_free(p2);
3065 p2 = NULL;
3066 }
3067 }
3068 }
3069 }
3070
3071 if (p2 != NULL && !got_int)
3072 {
3073 difflen = (int)STRLEN(p2) - oldlen;
3074 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3075 {
3076 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3077 xp->xp_pattern = ccline.cmdbuff + i;
3078 }
3079 else
3080 v = OK;
3081 if (v == OK)
3082 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003083 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3084 &ccline.cmdbuff[ccline.cmdpos],
3085 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3086 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 ccline.cmdlen += difflen;
3088 ccline.cmdpos += difflen;
3089 }
3090 }
3091 vim_free(p2);
3092
3093 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003094 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096 /* When expanding a ":map" command and no matches are found, assume that
3097 * the key is supposed to be inserted literally */
3098 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3099 return FAIL;
3100
3101 if (xp->xp_numfiles <= 0 && p2 == NULL)
3102 beep_flush();
3103 else if (xp->xp_numfiles == 1)
3104 /* free expanded pattern */
3105 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3106
3107 return OK;
3108}
3109
3110/*
3111 * Do wildcard expansion on the string 'str'.
3112 * Chars that should not be expanded must be preceded with a backslash.
3113 * Return a pointer to alloced memory containing the new string.
3114 * Return NULL for failure.
3115 *
3116 * Results are cached in xp->xp_files and xp->xp_numfiles.
3117 *
3118 * mode = WILD_FREE: just free previously expanded matches
3119 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3120 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3121 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3122 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3123 * mode = WILD_ALL: return all matches concatenated
3124 * mode = WILD_LONGEST: return longest matched part
3125 *
3126 * options = WILD_LIST_NOTFOUND: list entries without a match
3127 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3128 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3129 * options = WILD_NO_BEEP: Don't beep for multiple matches
3130 * options = WILD_ADD_SLASH: add a slash after directory names
3131 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3132 * options = WILD_SILENT: don't print warning messages
3133 * options = WILD_ESCAPE: put backslash before special chars
3134 *
3135 * The variables xp->xp_context and xp->xp_backslash must have been set!
3136 */
3137 char_u *
3138ExpandOne(xp, str, orig, options, mode)
3139 expand_T *xp;
3140 char_u *str;
3141 char_u *orig; /* allocated copy of original of expanded string */
3142 int options;
3143 int mode;
3144{
3145 char_u *ss = NULL;
3146 static int findex;
3147 static char_u *orig_save = NULL; /* kept value of orig */
3148 int i;
3149 long_u len;
3150 int non_suf_match; /* number without matching suffix */
3151
3152 /*
3153 * first handle the case of using an old match
3154 */
3155 if (mode == WILD_NEXT || mode == WILD_PREV)
3156 {
3157 if (xp->xp_numfiles > 0)
3158 {
3159 if (mode == WILD_PREV)
3160 {
3161 if (findex == -1)
3162 findex = xp->xp_numfiles;
3163 --findex;
3164 }
3165 else /* mode == WILD_NEXT */
3166 ++findex;
3167
3168 /*
3169 * When wrapping around, return the original string, set findex to
3170 * -1.
3171 */
3172 if (findex < 0)
3173 {
3174 if (orig_save == NULL)
3175 findex = xp->xp_numfiles - 1;
3176 else
3177 findex = -1;
3178 }
3179 if (findex >= xp->xp_numfiles)
3180 {
3181 if (orig_save == NULL)
3182 findex = 0;
3183 else
3184 findex = -1;
3185 }
3186#ifdef FEAT_WILDMENU
3187 if (p_wmnu)
3188 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3189 findex, cmd_showtail);
3190#endif
3191 if (findex == -1)
3192 return vim_strsave(orig_save);
3193 return vim_strsave(xp->xp_files[findex]);
3194 }
3195 else
3196 return NULL;
3197 }
3198
3199/* free old names */
3200 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3201 {
3202 FreeWild(xp->xp_numfiles, xp->xp_files);
3203 xp->xp_numfiles = -1;
3204 vim_free(orig_save);
3205 orig_save = NULL;
3206 }
3207 findex = 0;
3208
3209 if (mode == WILD_FREE) /* only release file name */
3210 return NULL;
3211
3212 if (xp->xp_numfiles == -1)
3213 {
3214 vim_free(orig_save);
3215 orig_save = orig;
3216
3217 /*
3218 * Do the expansion.
3219 */
3220 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3221 options) == FAIL)
3222 {
3223#ifdef FNAME_ILLEGAL
3224 /* Illegal file name has been silently skipped. But when there
3225 * are wildcards, the real problem is that there was no match,
3226 * causing the pattern to be added, which has illegal characters.
3227 */
3228 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3229 EMSG2(_(e_nomatch2), str);
3230#endif
3231 }
3232 else if (xp->xp_numfiles == 0)
3233 {
3234 if (!(options & WILD_SILENT))
3235 EMSG2(_(e_nomatch2), str);
3236 }
3237 else
3238 {
3239 /* Escape the matches for use on the command line. */
3240 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3241
3242 /*
3243 * Check for matching suffixes in file names.
3244 */
3245 if (mode != WILD_ALL && mode != WILD_LONGEST)
3246 {
3247 if (xp->xp_numfiles)
3248 non_suf_match = xp->xp_numfiles;
3249 else
3250 non_suf_match = 1;
3251 if ((xp->xp_context == EXPAND_FILES
3252 || xp->xp_context == EXPAND_DIRECTORIES)
3253 && xp->xp_numfiles > 1)
3254 {
3255 /*
3256 * More than one match; check suffix.
3257 * The files will have been sorted on matching suffix in
3258 * expand_wildcards, only need to check the first two.
3259 */
3260 non_suf_match = 0;
3261 for (i = 0; i < 2; ++i)
3262 if (match_suffix(xp->xp_files[i]))
3263 ++non_suf_match;
3264 }
3265 if (non_suf_match != 1)
3266 {
3267 /* Can we ever get here unless it's while expanding
3268 * interactively? If not, we can get rid of this all
3269 * together. Don't really want to wait for this message
3270 * (and possibly have to hit return to continue!).
3271 */
3272 if (!(options & WILD_SILENT))
3273 EMSG(_(e_toomany));
3274 else if (!(options & WILD_NO_BEEP))
3275 beep_flush();
3276 }
3277 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3278 ss = vim_strsave(xp->xp_files[0]);
3279 }
3280 }
3281 }
3282
3283 /* Find longest common part */
3284 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3285 {
3286 for (len = 0; xp->xp_files[0][len]; ++len)
3287 {
3288 for (i = 0; i < xp->xp_numfiles; ++i)
3289 {
3290#ifdef CASE_INSENSITIVE_FILENAME
3291 if (xp->xp_context == EXPAND_DIRECTORIES
3292 || xp->xp_context == EXPAND_FILES
3293 || xp->xp_context == EXPAND_BUFFERS)
3294 {
3295 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3296 TOLOWER_LOC(xp->xp_files[0][len]))
3297 break;
3298 }
3299 else
3300#endif
3301 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3302 break;
3303 }
3304 if (i < xp->xp_numfiles)
3305 {
3306 if (!(options & WILD_NO_BEEP))
3307 vim_beep();
3308 break;
3309 }
3310 }
3311 ss = alloc((unsigned)len + 1);
3312 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003313 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 findex = -1; /* next p_wc gets first one */
3315 }
3316
3317 /* Concatenate all matching names */
3318 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3319 {
3320 len = 0;
3321 for (i = 0; i < xp->xp_numfiles; ++i)
3322 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3323 ss = lalloc(len, TRUE);
3324 if (ss != NULL)
3325 {
3326 *ss = NUL;
3327 for (i = 0; i < xp->xp_numfiles; ++i)
3328 {
3329 STRCAT(ss, xp->xp_files[i]);
3330 if (i != xp->xp_numfiles - 1)
3331 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3332 }
3333 }
3334 }
3335
3336 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3337 ExpandCleanup(xp);
3338
3339 return ss;
3340}
3341
3342/*
3343 * Prepare an expand structure for use.
3344 */
3345 void
3346ExpandInit(xp)
3347 expand_T *xp;
3348{
3349 xp->xp_backslash = XP_BS_NONE;
3350 xp->xp_numfiles = -1;
3351 xp->xp_files = NULL;
3352}
3353
3354/*
3355 * Cleanup an expand structure after use.
3356 */
3357 void
3358ExpandCleanup(xp)
3359 expand_T *xp;
3360{
3361 if (xp->xp_numfiles >= 0)
3362 {
3363 FreeWild(xp->xp_numfiles, xp->xp_files);
3364 xp->xp_numfiles = -1;
3365 }
3366}
3367
3368 void
3369ExpandEscape(xp, str, numfiles, files, options)
3370 expand_T *xp;
3371 char_u *str;
3372 int numfiles;
3373 char_u **files;
3374 int options;
3375{
3376 int i;
3377 char_u *p;
3378
3379 /*
3380 * May change home directory back to "~"
3381 */
3382 if (options & WILD_HOME_REPLACE)
3383 tilde_replace(str, numfiles, files);
3384
3385 if (options & WILD_ESCAPE)
3386 {
3387 if (xp->xp_context == EXPAND_FILES
3388 || xp->xp_context == EXPAND_BUFFERS
3389 || xp->xp_context == EXPAND_DIRECTORIES)
3390 {
3391 /*
3392 * Insert a backslash into a file name before a space, \, %, #
3393 * and wildmatch characters, except '~'.
3394 */
3395 for (i = 0; i < numfiles; ++i)
3396 {
3397 /* for ":set path=" we need to escape spaces twice */
3398 if (xp->xp_backslash == XP_BS_THREE)
3399 {
3400 p = vim_strsave_escaped(files[i], (char_u *)" ");
3401 if (p != NULL)
3402 {
3403 vim_free(files[i]);
3404 files[i] = p;
3405#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3406 p = vim_strsave_escaped(files[i], (char_u *)" ");
3407 if (p != NULL)
3408 {
3409 vim_free(files[i]);
3410 files[i] = p;
3411 }
3412#endif
3413 }
3414 }
3415#ifdef BACKSLASH_IN_FILENAME
3416 {
3417 char_u buf[20];
3418 int j = 0;
3419
3420 /* Don't escape '[' and '{' if they are in 'isfname'. */
3421 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3422 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3423 buf[j++] = *p;
3424 buf[j] = NUL;
3425 p = vim_strsave_escaped(files[i], buf);
3426 }
3427#else
3428 p = vim_strsave_escaped(files[i], PATH_ESC_CHARS);
3429#endif
3430 if (p != NULL)
3431 {
3432 vim_free(files[i]);
3433 files[i] = p;
3434 }
3435
3436 /* If 'str' starts with "\~", replace "~" at start of
3437 * files[i] with "\~". */
3438 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003439 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003442
3443 /* If the first file starts with a '+' escape it. Otherwise it
3444 * could be seen as "+cmd". */
3445 if (*files[0] == '+')
3446 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448 else if (xp->xp_context == EXPAND_TAGS)
3449 {
3450 /*
3451 * Insert a backslash before characters in a tag name that
3452 * would terminate the ":tag" command.
3453 */
3454 for (i = 0; i < numfiles; ++i)
3455 {
3456 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3457 if (p != NULL)
3458 {
3459 vim_free(files[i]);
3460 files[i] = p;
3461 }
3462 }
3463 }
3464 }
3465}
3466
3467/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003468 * Put a backslash before the file name in "pp", which is in allocated memory.
3469 */
3470 static void
3471escape_fname(pp)
3472 char_u **pp;
3473{
3474 char_u *p;
3475
3476 p = alloc((unsigned)(STRLEN(*pp) + 2));
3477 if (p != NULL)
3478 {
3479 p[0] = '\\';
3480 STRCPY(p + 1, *pp);
3481 vim_free(*pp);
3482 *pp = p;
3483 }
3484}
3485
3486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 * For each file name in files[num_files]:
3488 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3489 */
3490 void
3491tilde_replace(orig_pat, num_files, files)
3492 char_u *orig_pat;
3493 int num_files;
3494 char_u **files;
3495{
3496 int i;
3497 char_u *p;
3498
3499 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3500 {
3501 for (i = 0; i < num_files; ++i)
3502 {
3503 p = home_replace_save(NULL, files[i]);
3504 if (p != NULL)
3505 {
3506 vim_free(files[i]);
3507 files[i] = p;
3508 }
3509 }
3510 }
3511}
3512
3513/*
3514 * Show all matches for completion on the command line.
3515 * Returns EXPAND_NOTHING when the character that triggered expansion should
3516 * be inserted like a normal character.
3517 */
3518/*ARGSUSED*/
3519 static int
3520showmatches(xp, wildmenu)
3521 expand_T *xp;
3522 int wildmenu;
3523{
3524#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3525 int num_files;
3526 char_u **files_found;
3527 int i, j, k;
3528 int maxlen;
3529 int lines;
3530 int columns;
3531 char_u *p;
3532 int lastlen;
3533 int attr;
3534 int showtail;
3535
3536 if (xp->xp_numfiles == -1)
3537 {
3538 set_expand_context(xp);
3539 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3540 &num_files, &files_found);
3541 showtail = expand_showtail(xp);
3542 if (i != EXPAND_OK)
3543 return i;
3544
3545 }
3546 else
3547 {
3548 num_files = xp->xp_numfiles;
3549 files_found = xp->xp_files;
3550 showtail = cmd_showtail;
3551 }
3552
3553#ifdef FEAT_WILDMENU
3554 if (!wildmenu)
3555 {
3556#endif
3557 msg_didany = FALSE; /* lines_left will be set */
3558 msg_start(); /* prepare for paging */
3559 msg_putchar('\n');
3560 out_flush();
3561 cmdline_row = msg_row;
3562 msg_didany = FALSE; /* lines_left will be set again */
3563 msg_start(); /* prepare for paging */
3564#ifdef FEAT_WILDMENU
3565 }
3566#endif
3567
3568 if (got_int)
3569 got_int = FALSE; /* only int. the completion, not the cmd line */
3570#ifdef FEAT_WILDMENU
3571 else if (wildmenu)
3572 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3573#endif
3574 else
3575 {
3576 /* find the length of the longest file name */
3577 maxlen = 0;
3578 for (i = 0; i < num_files; ++i)
3579 {
3580 if (!showtail && (xp->xp_context == EXPAND_FILES
3581 || xp->xp_context == EXPAND_BUFFERS))
3582 {
3583 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3584 j = vim_strsize(NameBuff);
3585 }
3586 else
3587 j = vim_strsize(L_SHOWFILE(i));
3588 if (j > maxlen)
3589 maxlen = j;
3590 }
3591
3592 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3593 lines = num_files;
3594 else
3595 {
3596 /* compute the number of columns and lines for the listing */
3597 maxlen += 2; /* two spaces between file names */
3598 columns = ((int)Columns + 2) / maxlen;
3599 if (columns < 1)
3600 columns = 1;
3601 lines = (num_files + columns - 1) / columns;
3602 }
3603
3604 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3605
3606 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3607 {
3608 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3609 msg_clr_eos();
3610 msg_advance(maxlen - 3);
3611 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3612 }
3613
3614 /* list the files line by line */
3615 for (i = 0; i < lines; ++i)
3616 {
3617 lastlen = 999;
3618 for (k = i; k < num_files; k += lines)
3619 {
3620 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3621 {
3622 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3623 p = files_found[k] + STRLEN(files_found[k]) + 1;
3624 msg_advance(maxlen + 1);
3625 msg_puts(p);
3626 msg_advance(maxlen + 3);
3627 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3628 break;
3629 }
3630 for (j = maxlen - lastlen; --j >= 0; )
3631 msg_putchar(' ');
3632 if (xp->xp_context == EXPAND_FILES
3633 || xp->xp_context == EXPAND_BUFFERS)
3634 {
3635 /* highlight directories */
3636 j = (mch_isdir(files_found[k]));
3637 if (showtail)
3638 p = L_SHOWFILE(k);
3639 else
3640 {
3641 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3642 TRUE);
3643 p = NameBuff;
3644 }
3645 }
3646 else
3647 {
3648 j = FALSE;
3649 p = L_SHOWFILE(k);
3650 }
3651 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3652 }
3653 if (msg_col > 0) /* when not wrapped around */
3654 {
3655 msg_clr_eos();
3656 msg_putchar('\n');
3657 }
3658 out_flush(); /* show one line at a time */
3659 if (got_int)
3660 {
3661 got_int = FALSE;
3662 break;
3663 }
3664 }
3665
3666 /*
3667 * we redraw the command below the lines that we have just listed
3668 * This is a bit tricky, but it saves a lot of screen updating.
3669 */
3670 cmdline_row = msg_row; /* will put it back later */
3671 }
3672
3673 if (xp->xp_numfiles == -1)
3674 FreeWild(num_files, files_found);
3675
3676 return EXPAND_OK;
3677}
3678
3679/*
3680 * Private gettail for showmatches() (and win_redr_status_matches()):
3681 * Find tail of file name path, but ignore trailing "/".
3682 */
3683 char_u *
3684sm_gettail(s)
3685 char_u *s;
3686{
3687 char_u *p;
3688 char_u *t = s;
3689 int had_sep = FALSE;
3690
3691 for (p = s; *p != NUL; )
3692 {
3693 if (vim_ispathsep(*p)
3694#ifdef BACKSLASH_IN_FILENAME
3695 && !rem_backslash(p)
3696#endif
3697 )
3698 had_sep = TRUE;
3699 else if (had_sep)
3700 {
3701 t = p;
3702 had_sep = FALSE;
3703 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003704 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 }
3706 return t;
3707}
3708
3709/*
3710 * Return TRUE if we only need to show the tail of completion matches.
3711 * When not completing file names or there is a wildcard in the path FALSE is
3712 * returned.
3713 */
3714 static int
3715expand_showtail(xp)
3716 expand_T *xp;
3717{
3718 char_u *s;
3719 char_u *end;
3720
3721 /* When not completing file names a "/" may mean something different. */
3722 if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
3723 return FALSE;
3724
3725 end = gettail(xp->xp_pattern);
3726 if (end == xp->xp_pattern) /* there is no path separator */
3727 return FALSE;
3728
3729 for (s = xp->xp_pattern; s < end; s++)
3730 {
3731 /* Skip escaped wildcards. Only when the backslash is not a path
3732 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3733 if (rem_backslash(s))
3734 ++s;
3735 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3736 return FALSE;
3737 }
3738 return TRUE;
3739}
3740
3741/*
3742 * Prepare a string for expansion.
3743 * When expanding file names: The string will be used with expand_wildcards().
3744 * Copy the file name into allocated memory and add a '*' at the end.
3745 * When expanding other names: The string will be used with regcomp(). Copy
3746 * the name into allocated memory and prepend "^".
3747 */
3748 char_u *
3749addstar(fname, len, context)
3750 char_u *fname;
3751 int len;
3752 int context; /* EXPAND_FILES etc. */
3753{
3754 char_u *retval;
3755 int i, j;
3756 int new_len;
3757 char_u *tail;
3758
3759 if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
3760 {
3761 /*
3762 * Matching will be done internally (on something other than files).
3763 * So we convert the file-matching-type wildcards into our kind for
3764 * use with vim_regcomp(). First work out how long it will be:
3765 */
3766
3767 /* For help tags the translation is done in find_help_tags().
3768 * For a tag pattern starting with "/" no translation is needed. */
3769 if (context == EXPAND_HELP
3770 || context == EXPAND_COLORS
3771 || context == EXPAND_COMPILER
3772 || (context == EXPAND_TAGS && fname[0] == '/'))
3773 retval = vim_strnsave(fname, len);
3774 else
3775 {
3776 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3777 for (i = 0; i < len; i++)
3778 {
3779 if (fname[i] == '*' || fname[i] == '~')
3780 new_len++; /* '*' needs to be replaced by ".*"
3781 '~' needs to be replaced by "\~" */
3782
3783 /* Buffer names are like file names. "." should be literal */
3784 if (context == EXPAND_BUFFERS && fname[i] == '.')
3785 new_len++; /* "." becomes "\." */
3786
3787 /* Custom expansion takes care of special things, match
3788 * backslashes literally (perhaps also for other types?) */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003789 if ((context == EXPAND_USER_DEFINED ||
3790 context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 new_len++; /* '\' becomes "\\" */
3792 }
3793 retval = alloc(new_len);
3794 if (retval != NULL)
3795 {
3796 retval[0] = '^';
3797 j = 1;
3798 for (i = 0; i < len; i++, j++)
3799 {
3800 /* Skip backslash. But why? At least keep it for custom
3801 * expansion. */
3802 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003803 && context != EXPAND_USER_LIST
3804 && fname[i] == '\\'
3805 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 break;
3807
3808 switch (fname[i])
3809 {
3810 case '*': retval[j++] = '.';
3811 break;
3812 case '~': retval[j++] = '\\';
3813 break;
3814 case '?': retval[j] = '.';
3815 continue;
3816 case '.': if (context == EXPAND_BUFFERS)
3817 retval[j++] = '\\';
3818 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003819 case '\\': if (context == EXPAND_USER_DEFINED
3820 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 retval[j++] = '\\';
3822 break;
3823 }
3824 retval[j] = fname[i];
3825 }
3826 retval[j] = NUL;
3827 }
3828 }
3829 }
3830 else
3831 {
3832 retval = alloc(len + 4);
3833 if (retval != NULL)
3834 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003835 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
3837 /*
3838 * Don't add a star to ~, ~user, $var or `cmd`.
3839 * ~ would be at the start of the file name, but not the tail.
3840 * $ could be anywhere in the tail.
3841 * ` could be anywhere in the file name.
3842 */
3843 tail = gettail(retval);
3844 if ((*retval != '~' || tail != retval)
3845 && vim_strchr(tail, '$') == NULL
3846 && vim_strchr(retval, '`') == NULL)
3847 retval[len++] = '*';
3848 retval[len] = NUL;
3849 }
3850 }
3851 return retval;
3852}
3853
3854/*
3855 * Must parse the command line so far to work out what context we are in.
3856 * Completion can then be done based on that context.
3857 * This routine sets the variables:
3858 * xp->xp_pattern The start of the pattern to be expanded within
3859 * the command line (ends at the cursor).
3860 * xp->xp_context The type of thing to expand. Will be one of:
3861 *
3862 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3863 * the command line, like an unknown command. Caller
3864 * should beep.
3865 * EXPAND_NOTHING Unrecognised context for completion, use char like
3866 * a normal char, rather than for completion. eg
3867 * :s/^I/
3868 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3869 * it.
3870 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3871 * EXPAND_FILES After command with XFILE set, or after setting
3872 * with P_EXPAND set. eg :e ^I, :w>>^I
3873 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3874 * when we know only directories are of interest. eg
3875 * :set dir=^I
3876 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3877 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3878 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3879 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3880 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3881 * EXPAND_EVENTS Complete event names
3882 * EXPAND_SYNTAX Complete :syntax command arguments
3883 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3884 * EXPAND_AUGROUP Complete autocommand group names
3885 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3886 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3887 * eg :unmap a^I , :cunab x^I
3888 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3889 * eg :call sub^I
3890 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3891 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3892 * names in expressions, eg :while s^I
3893 * EXPAND_ENV_VARS Complete environment variable names
3894 */
3895 static void
3896set_expand_context(xp)
3897 expand_T *xp;
3898{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003899 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 if (ccline.cmdfirstc != ':'
3901#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003902 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003903 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904#endif
3905 )
3906 {
3907 xp->xp_context = EXPAND_NOTHING;
3908 return;
3909 }
3910 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3911}
3912
3913 void
3914set_cmd_context(xp, str, len, col)
3915 expand_T *xp;
3916 char_u *str; /* start of command line */
3917 int len; /* length of command line (excl. NUL) */
3918 int col; /* position of cursor */
3919{
3920 int old_char = NUL;
3921 char_u *nextcomm;
3922
3923 /*
3924 * Avoid a UMR warning from Purify, only save the character if it has been
3925 * written before.
3926 */
3927 if (col < len)
3928 old_char = str[col];
3929 str[col] = NUL;
3930 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003931
3932#ifdef FEAT_EVAL
3933 if (ccline.cmdfirstc == '=')
3934 /* pass CMD_SIZE because there is no real command */
3935 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003936 else if (ccline.input_fn)
3937 {
3938 xp->xp_context = ccline.xp_context;
3939 xp->xp_pattern = ccline.cmdbuff;
3940 xp->xp_arg = ccline.xp_arg;
3941 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003942 else
3943#endif
3944 while (nextcomm != NULL)
3945 nextcomm = set_one_cmd_context(xp, nextcomm);
3946
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 str[col] = old_char;
3948}
3949
3950/*
3951 * Expand the command line "str" from context "xp".
3952 * "xp" must have been set by set_cmd_context().
3953 * xp->xp_pattern points into "str", to where the text that is to be expanded
3954 * starts.
3955 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
3956 * cursor.
3957 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
3958 * key that triggered expansion literally.
3959 * Returns EXPAND_OK otherwise.
3960 */
3961 int
3962expand_cmdline(xp, str, col, matchcount, matches)
3963 expand_T *xp;
3964 char_u *str; /* start of command line */
3965 int col; /* position of cursor */
3966 int *matchcount; /* return: nr of matches */
3967 char_u ***matches; /* return: array of pointers to matches */
3968{
3969 char_u *file_str = NULL;
3970
3971 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3972 {
3973 beep_flush();
3974 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
3975 }
3976 if (xp->xp_context == EXPAND_NOTHING)
3977 {
3978 /* Caller can use the character as a normal char instead */
3979 return EXPAND_NOTHING;
3980 }
3981
3982 /* add star to file name, or convert to regexp if not exp. files. */
3983 file_str = addstar(xp->xp_pattern,
3984 (int)(str + col - xp->xp_pattern), xp->xp_context);
3985 if (file_str == NULL)
3986 return EXPAND_UNSUCCESSFUL;
3987
3988 /* find all files that match the description */
3989 if (ExpandFromContext(xp, file_str, matchcount, matches,
3990 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
3991 {
3992 *matchcount = 0;
3993 *matches = NULL;
3994 }
3995 vim_free(file_str);
3996
3997 return EXPAND_OK;
3998}
3999
4000#ifdef FEAT_MULTI_LANG
4001/*
4002 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4003 */
4004static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4005
4006 static void
4007cleanup_help_tags(num_file, file)
4008 int num_file;
4009 char_u **file;
4010{
4011 int i, j;
4012 int len;
4013
4014 for (i = 0; i < num_file; ++i)
4015 {
4016 len = (int)STRLEN(file[i]) - 3;
4017 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4018 {
4019 /* Sorting on priority means the same item in another language may
4020 * be anywhere. Search all items for a match up to the "@en". */
4021 for (j = 0; j < num_file; ++j)
4022 if (j != i
4023 && (int)STRLEN(file[j]) == len + 3
4024 && STRNCMP(file[i], file[j], len + 1) == 0)
4025 break;
4026 if (j == num_file)
4027 file[i][len] = NUL;
4028 }
4029 }
4030}
4031#endif
4032
4033/*
4034 * Do the expansion based on xp->xp_context and "pat".
4035 */
4036 static int
4037ExpandFromContext(xp, pat, num_file, file, options)
4038 expand_T *xp;
4039 char_u *pat;
4040 int *num_file;
4041 char_u ***file;
4042 int options;
4043{
4044#ifdef FEAT_CMDL_COMPL
4045 regmatch_T regmatch;
4046#endif
4047 int ret;
4048 int flags;
4049
4050 flags = EW_DIR; /* include directories */
4051 if (options & WILD_LIST_NOTFOUND)
4052 flags |= EW_NOTFOUND;
4053 if (options & WILD_ADD_SLASH)
4054 flags |= EW_ADDSLASH;
4055 if (options & WILD_KEEP_ALL)
4056 flags |= EW_KEEPALL;
4057 if (options & WILD_SILENT)
4058 flags |= EW_SILENT;
4059
4060 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4061 {
4062 /*
4063 * Expand file or directory names.
4064 */
4065 int free_pat = FALSE;
4066 int i;
4067
4068 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4069 * space */
4070 if (xp->xp_backslash != XP_BS_NONE)
4071 {
4072 free_pat = TRUE;
4073 pat = vim_strsave(pat);
4074 for (i = 0; pat[i]; ++i)
4075 if (pat[i] == '\\')
4076 {
4077 if (xp->xp_backslash == XP_BS_THREE
4078 && pat[i + 1] == '\\'
4079 && pat[i + 2] == '\\'
4080 && pat[i + 3] == ' ')
4081 STRCPY(pat + i, pat + i + 3);
4082 if (xp->xp_backslash == XP_BS_ONE
4083 && pat[i + 1] == ' ')
4084 STRCPY(pat + i, pat + i + 1);
4085 }
4086 }
4087
4088 if (xp->xp_context == EXPAND_FILES)
4089 flags |= EW_FILE;
4090 else
4091 flags = (flags | EW_DIR) & ~EW_FILE;
4092 ret = expand_wildcards(1, &pat, num_file, file, flags);
4093 if (free_pat)
4094 vim_free(pat);
4095 return ret;
4096 }
4097
4098 *file = (char_u **)"";
4099 *num_file = 0;
4100 if (xp->xp_context == EXPAND_HELP)
4101 {
4102 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4103 {
4104#ifdef FEAT_MULTI_LANG
4105 cleanup_help_tags(*num_file, *file);
4106#endif
4107 return OK;
4108 }
4109 return FAIL;
4110 }
4111
4112#ifndef FEAT_CMDL_COMPL
4113 return FAIL;
4114#else
4115 if (xp->xp_context == EXPAND_OLD_SETTING)
4116 return ExpandOldSetting(num_file, file);
4117 if (xp->xp_context == EXPAND_BUFFERS)
4118 return ExpandBufnames(pat, num_file, file, options);
4119 if (xp->xp_context == EXPAND_TAGS
4120 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4121 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4122 if (xp->xp_context == EXPAND_COLORS)
4123 return ExpandRTDir(pat, num_file, file, "colors");
4124 if (xp->xp_context == EXPAND_COMPILER)
4125 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004126# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4127 if (xp->xp_context == EXPAND_USER_LIST)
4128 return ExpandUserList(xp, num_file, file);
4129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
4131 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4132 if (regmatch.regprog == NULL)
4133 return FAIL;
4134
4135 /* set ignore-case according to p_ic, p_scs and pat */
4136 regmatch.rm_ic = ignorecase(pat);
4137
4138 if (xp->xp_context == EXPAND_SETTINGS
4139 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4140 ret = ExpandSettings(xp, &regmatch, num_file, file);
4141 else if (xp->xp_context == EXPAND_MAPPINGS)
4142 ret = ExpandMappings(&regmatch, num_file, file);
4143# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4144 else if (xp->xp_context == EXPAND_USER_DEFINED)
4145 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4146# endif
4147 else
4148 {
4149 static struct expgen
4150 {
4151 int context;
4152 char_u *((*func)__ARGS((expand_T *, int)));
4153 int ic;
4154 } tab[] =
4155 {
4156 {EXPAND_COMMANDS, get_command_name, FALSE},
4157#ifdef FEAT_USR_CMDS
4158 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4159 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4160 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4161 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4162#endif
4163#ifdef FEAT_EVAL
4164 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4165 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4166 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4167 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4168#endif
4169#ifdef FEAT_MENU
4170 {EXPAND_MENUS, get_menu_name, FALSE},
4171 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4172#endif
4173#ifdef FEAT_SYN_HL
4174 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4175#endif
4176 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4177#ifdef FEAT_AUTOCMD
4178 {EXPAND_EVENTS, get_event_name, TRUE},
4179 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4180#endif
4181#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4182 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4183 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4184#endif
4185 {EXPAND_ENV_VARS, get_env_name, TRUE},
4186 };
4187 int i;
4188
4189 /*
4190 * Find a context in the table and call the ExpandGeneric() with the
4191 * right function to do the expansion.
4192 */
4193 ret = FAIL;
4194 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4195 if (xp->xp_context == tab[i].context)
4196 {
4197 if (tab[i].ic)
4198 regmatch.rm_ic = TRUE;
4199 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4200 break;
4201 }
4202 }
4203
4204 vim_free(regmatch.regprog);
4205
4206 return ret;
4207#endif /* FEAT_CMDL_COMPL */
4208}
4209
4210#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4211/*
4212 * Expand a list of names.
4213 *
4214 * Generic function for command line completion. It calls a function to
4215 * obtain strings, one by one. The strings are matched against a regexp
4216 * program. Matching strings are copied into an array, which is returned.
4217 *
4218 * Returns OK when no problems encountered, FAIL for error (out of memory).
4219 */
4220 int
4221ExpandGeneric(xp, regmatch, num_file, file, func)
4222 expand_T *xp;
4223 regmatch_T *regmatch;
4224 int *num_file;
4225 char_u ***file;
4226 char_u *((*func)__ARGS((expand_T *, int)));
4227 /* returns a string from the list */
4228{
4229 int i;
4230 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004231 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u *str;
4233
4234 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004235 * round == 0: count the number of matching names
4236 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004238 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 {
4240 for (i = 0; ; ++i)
4241 {
4242 str = (*func)(xp, i);
4243 if (str == NULL) /* end of list */
4244 break;
4245 if (*str == NUL) /* skip empty strings */
4246 continue;
4247
4248 if (vim_regexec(regmatch, str, (colnr_T)0))
4249 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004250 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
4252 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4253 (*file)[count] = str;
4254#ifdef FEAT_MENU
4255 if (func == get_menu_names && str != NULL)
4256 {
4257 /* test for separator added by get_menu_names() */
4258 str += STRLEN(str) - 1;
4259 if (*str == '\001')
4260 *str = '.';
4261 }
4262#endif
4263 }
4264 ++count;
4265 }
4266 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004267 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
4269 if (count == 0)
4270 return OK;
4271 *num_file = count;
4272 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4273 if (*file == NULL)
4274 {
4275 *file = (char_u **)"";
4276 return FAIL;
4277 }
4278 count = 0;
4279 }
4280 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004281
4282 /* Sort the results. */
4283 sort_strings(*file, *num_file);
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return OK;
4286}
4287
4288# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004289static 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));
4290
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004292 * call "user_expand_func()" to invoke a user defined VimL function and return
4293 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004295 static void *
4296call_user_expand_func(user_expand_func, xp, num_file, file)
4297 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 int *num_file;
4300 char_u ***file;
4301{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004302 char_u keep;
4303 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004306 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004307 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
4309 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004310 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 *num_file = 0;
4312 *file = NULL;
4313
4314 keep = ccline.cmdbuff[ccline.cmdlen];
4315 ccline.cmdbuff[ccline.cmdlen] = 0;
4316 sprintf((char *)num, "%d", ccline.cmdpos);
4317 args[0] = xp->xp_pattern;
4318 args[1] = ccline.cmdbuff;
4319 args[2] = num;
4320
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004321 /* Save the cmdline, we don't know what the function may do. */
4322 save_ccline = ccline;
4323 ccline.cmdbuff = NULL;
4324 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004326
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004327 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004328
4329 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004331
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004333
4334 return ret;
4335}
4336
4337/*
4338 * Expand names with a function defined by the user.
4339 */
4340 static int
4341ExpandUserDefined(xp, regmatch, num_file, file)
4342 expand_T *xp;
4343 regmatch_T *regmatch;
4344 int *num_file;
4345 char_u ***file;
4346{
4347 char_u *retstr;
4348 char_u *s;
4349 char_u *e;
4350 char_u keep;
4351 garray_T ga;
4352
4353 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4354 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 return FAIL;
4356
4357 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004358 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 {
4360 e = vim_strchr(s, '\n');
4361 if (e == NULL)
4362 e = s + STRLEN(s);
4363 keep = *e;
4364 *e = 0;
4365
4366 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4367 {
4368 *e = keep;
4369 if (*e != NUL)
4370 ++e;
4371 continue;
4372 }
4373
4374 if (ga_grow(&ga, 1) == FAIL)
4375 break;
4376
4377 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4378 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380 *e = keep;
4381 if (*e != NUL)
4382 ++e;
4383 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004384 vim_free(retstr);
4385 *file = ga.ga_data;
4386 *num_file = ga.ga_len;
4387 return OK;
4388}
4389
4390/*
4391 * Expand names with a list returned by a function defined by the user.
4392 */
4393 static int
4394ExpandUserList(xp, num_file, file)
4395 expand_T *xp;
4396 int *num_file;
4397 char_u ***file;
4398{
4399 list_T *retlist;
4400 listitem_T *li;
4401 garray_T ga;
4402
4403 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4404 if (retlist == NULL)
4405 return FAIL;
4406
4407 ga_init2(&ga, (int)sizeof(char *), 3);
4408 /* Loop over the items in the list. */
4409 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4410 {
4411 if (li->li_tv.v_type != VAR_STRING)
4412 continue; /* Skip non-string items */
4413
4414 if (ga_grow(&ga, 1) == FAIL)
4415 break;
4416
4417 ((char_u **)ga.ga_data)[ga.ga_len] =
4418 vim_strsave(li->li_tv.vval.v_string);
4419 ++ga.ga_len;
4420 }
4421 list_unref(retlist);
4422
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 *file = ga.ga_data;
4424 *num_file = ga.ga_len;
4425 return OK;
4426}
4427#endif
4428
4429/*
4430 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4431 * or compiler names.
4432 */
4433 static int
4434ExpandRTDir(pat, num_file, file, dirname)
4435 char_u *pat;
4436 int *num_file;
4437 char_u ***file;
4438 char *dirname; /* "colors" or "compiler" */
4439{
4440 char_u *all;
4441 char_u *s;
4442 char_u *e;
4443 garray_T ga;
4444
4445 *num_file = 0;
4446 *file = NULL;
4447 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4448 if (s == NULL)
4449 return FAIL;
4450 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4451 all = globpath(p_rtp, s);
4452 vim_free(s);
4453 if (all == NULL)
4454 return FAIL;
4455
4456 ga_init2(&ga, (int)sizeof(char *), 3);
4457 for (s = all; *s != NUL; s = e)
4458 {
4459 e = vim_strchr(s, '\n');
4460 if (e == NULL)
4461 e = s + STRLEN(s);
4462 if (ga_grow(&ga, 1) == FAIL)
4463 break;
4464 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4465 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004466 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 if (*s == '\n' || vim_ispathsep(*s))
4468 break;
4469 ++s;
4470 ((char_u **)ga.ga_data)[ga.ga_len] =
4471 vim_strnsave(s, (int)(e - s - 4));
4472 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474 if (*e != NUL)
4475 ++e;
4476 }
4477 vim_free(all);
4478 *file = ga.ga_data;
4479 *num_file = ga.ga_len;
4480 return OK;
4481}
4482
4483#endif
4484
4485#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4486/*
4487 * Expand "file" for all comma-separated directories in "path".
4488 * Returns an allocated string with all matches concatenated, separated by
4489 * newlines. Returns NULL for an error or no matches.
4490 */
4491 char_u *
4492globpath(path, file)
4493 char_u *path;
4494 char_u *file;
4495{
4496 expand_T xpc;
4497 char_u *buf;
4498 garray_T ga;
4499 int i;
4500 int len;
4501 int num_p;
4502 char_u **p;
4503 char_u *cur = NULL;
4504
4505 buf = alloc(MAXPATHL);
4506 if (buf == NULL)
4507 return NULL;
4508
4509 xpc.xp_context = EXPAND_FILES;
4510 xpc.xp_backslash = XP_BS_NONE;
4511 ga_init2(&ga, 1, 100);
4512
4513 /* Loop over all entries in {path}. */
4514 while (*path != NUL)
4515 {
4516 /* Copy one item of the path to buf[] and concatenate the file name. */
4517 copy_option_part(&path, buf, MAXPATHL, ",");
4518 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4519 {
4520 add_pathsep(buf);
4521 STRCAT(buf, file);
4522 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4523 && num_p > 0)
4524 {
4525 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4526 for (len = 0, i = 0; i < num_p; ++i)
4527 len += (long_u)STRLEN(p[i]) + 1;
4528
4529 /* Concatenate new results to previous ones. */
4530 if (ga_grow(&ga, len) == OK)
4531 {
4532 cur = (char_u *)ga.ga_data + ga.ga_len;
4533 for (i = 0; i < num_p; ++i)
4534 {
4535 STRCPY(cur, p[i]);
4536 cur += STRLEN(p[i]);
4537 *cur++ = '\n';
4538 }
4539 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541 FreeWild(num_p, p);
4542 }
4543 }
4544 }
4545 if (cur != NULL)
4546 *--cur = 0; /* Replace trailing newline with NUL */
4547
4548 vim_free(buf);
4549 return (char_u *)ga.ga_data;
4550}
4551
4552#endif
4553
4554#if defined(FEAT_CMDHIST) || defined(PROTO)
4555
4556/*********************************
4557 * Command line history stuff *
4558 *********************************/
4559
4560/*
4561 * Translate a history character to the associated type number.
4562 */
4563 static int
4564hist_char2type(c)
4565 int c;
4566{
4567 if (c == ':')
4568 return HIST_CMD;
4569 if (c == '=')
4570 return HIST_EXPR;
4571 if (c == '@')
4572 return HIST_INPUT;
4573 if (c == '>')
4574 return HIST_DEBUG;
4575 return HIST_SEARCH; /* must be '?' or '/' */
4576}
4577
4578/*
4579 * Table of history names.
4580 * These names are used in :history and various hist...() functions.
4581 * It is sufficient to give the significant prefix of a history name.
4582 */
4583
4584static char *(history_names[]) =
4585{
4586 "cmd",
4587 "search",
4588 "expr",
4589 "input",
4590 "debug",
4591 NULL
4592};
4593
4594/*
4595 * init_history() - Initialize the command line history.
4596 * Also used to re-allocate the history when the size changes.
4597 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004598 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599init_history()
4600{
4601 int newlen; /* new length of history table */
4602 histentry_T *temp;
4603 int i;
4604 int j;
4605 int type;
4606
4607 /*
4608 * If size of history table changed, reallocate it
4609 */
4610 newlen = (int)p_hi;
4611 if (newlen != hislen) /* history length changed */
4612 {
4613 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4614 {
4615 if (newlen)
4616 {
4617 temp = (histentry_T *)lalloc(
4618 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4619 if (temp == NULL) /* out of memory! */
4620 {
4621 if (type == 0) /* first one: just keep the old length */
4622 {
4623 newlen = hislen;
4624 break;
4625 }
4626 /* Already changed one table, now we can only have zero
4627 * length for all tables. */
4628 newlen = 0;
4629 type = -1;
4630 continue;
4631 }
4632 }
4633 else
4634 temp = NULL;
4635 if (newlen == 0 || temp != NULL)
4636 {
4637 if (hisidx[type] < 0) /* there are no entries yet */
4638 {
4639 for (i = 0; i < newlen; ++i)
4640 {
4641 temp[i].hisnum = 0;
4642 temp[i].hisstr = NULL;
4643 }
4644 }
4645 else if (newlen > hislen) /* array becomes bigger */
4646 {
4647 for (i = 0; i <= hisidx[type]; ++i)
4648 temp[i] = history[type][i];
4649 j = i;
4650 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4651 {
4652 temp[i].hisnum = 0;
4653 temp[i].hisstr = NULL;
4654 }
4655 for ( ; j < hislen; ++i, ++j)
4656 temp[i] = history[type][j];
4657 }
4658 else /* array becomes smaller or 0 */
4659 {
4660 j = hisidx[type];
4661 for (i = newlen - 1; ; --i)
4662 {
4663 if (i >= 0) /* copy newest entries */
4664 temp[i] = history[type][j];
4665 else /* remove older entries */
4666 vim_free(history[type][j].hisstr);
4667 if (--j < 0)
4668 j = hislen - 1;
4669 if (j == hisidx[type])
4670 break;
4671 }
4672 hisidx[type] = newlen - 1;
4673 }
4674 vim_free(history[type]);
4675 history[type] = temp;
4676 }
4677 }
4678 hislen = newlen;
4679 }
4680}
4681
4682/*
4683 * Check if command line 'str' is already in history.
4684 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4685 */
4686 static int
4687in_history(type, str, move_to_front)
4688 int type;
4689 char_u *str;
4690 int move_to_front; /* Move the entry to the front if it exists */
4691{
4692 int i;
4693 int last_i = -1;
4694
4695 if (hisidx[type] < 0)
4696 return FALSE;
4697 i = hisidx[type];
4698 do
4699 {
4700 if (history[type][i].hisstr == NULL)
4701 return FALSE;
4702 if (STRCMP(str, history[type][i].hisstr) == 0)
4703 {
4704 if (!move_to_front)
4705 return TRUE;
4706 last_i = i;
4707 break;
4708 }
4709 if (--i < 0)
4710 i = hislen - 1;
4711 } while (i != hisidx[type]);
4712
4713 if (last_i >= 0)
4714 {
4715 str = history[type][i].hisstr;
4716 while (i != hisidx[type])
4717 {
4718 if (++i >= hislen)
4719 i = 0;
4720 history[type][last_i] = history[type][i];
4721 last_i = i;
4722 }
4723 history[type][i].hisstr = str;
4724 history[type][i].hisnum = ++hisnum[type];
4725 return TRUE;
4726 }
4727 return FALSE;
4728}
4729
4730/*
4731 * Convert history name (from table above) to its HIST_ equivalent.
4732 * When "name" is empty, return "cmd" history.
4733 * Returns -1 for unknown history name.
4734 */
4735 int
4736get_histtype(name)
4737 char_u *name;
4738{
4739 int i;
4740 int len = (int)STRLEN(name);
4741
4742 /* No argument: use current history. */
4743 if (len == 0)
4744 return hist_char2type(ccline.cmdfirstc);
4745
4746 for (i = 0; history_names[i] != NULL; ++i)
4747 if (STRNICMP(name, history_names[i], len) == 0)
4748 return i;
4749
4750 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4751 return hist_char2type(name[0]);
4752
4753 return -1;
4754}
4755
4756static int last_maptick = -1; /* last seen maptick */
4757
4758/*
4759 * Add the given string to the given history. If the string is already in the
4760 * history then it is moved to the front. "histype" may be one of he HIST_
4761 * values.
4762 */
4763 void
4764add_to_history(histype, new_entry, in_map, sep)
4765 int histype;
4766 char_u *new_entry;
4767 int in_map; /* consider maptick when inside a mapping */
4768 int sep; /* separator character used (search hist) */
4769{
4770 histentry_T *hisptr;
4771 int len;
4772
4773 if (hislen == 0) /* no history */
4774 return;
4775
4776 /*
4777 * Searches inside the same mapping overwrite each other, so that only
4778 * the last line is kept. Be careful not to remove a line that was moved
4779 * down, only lines that were added.
4780 */
4781 if (histype == HIST_SEARCH && in_map)
4782 {
4783 if (maptick == last_maptick)
4784 {
4785 /* Current line is from the same mapping, remove it */
4786 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4787 vim_free(hisptr->hisstr);
4788 hisptr->hisstr = NULL;
4789 hisptr->hisnum = 0;
4790 --hisnum[histype];
4791 if (--hisidx[HIST_SEARCH] < 0)
4792 hisidx[HIST_SEARCH] = hislen - 1;
4793 }
4794 last_maptick = -1;
4795 }
4796 if (!in_history(histype, new_entry, TRUE))
4797 {
4798 if (++hisidx[histype] == hislen)
4799 hisidx[histype] = 0;
4800 hisptr = &history[histype][hisidx[histype]];
4801 vim_free(hisptr->hisstr);
4802
4803 /* Store the separator after the NUL of the string. */
4804 len = STRLEN(new_entry);
4805 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4806 if (hisptr->hisstr != NULL)
4807 hisptr->hisstr[len + 1] = sep;
4808
4809 hisptr->hisnum = ++hisnum[histype];
4810 if (histype == HIST_SEARCH && in_map)
4811 last_maptick = maptick;
4812 }
4813}
4814
4815#if defined(FEAT_EVAL) || defined(PROTO)
4816
4817/*
4818 * Get identifier of newest history entry.
4819 * "histype" may be one of the HIST_ values.
4820 */
4821 int
4822get_history_idx(histype)
4823 int histype;
4824{
4825 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4826 || hisidx[histype] < 0)
4827 return -1;
4828
4829 return history[histype][hisidx[histype]].hisnum;
4830}
4831
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004832static struct cmdline_info *get_ccline_ptr __ARGS((void));
4833
4834/*
4835 * Get pointer to the command line info to use. cmdline_paste() may clear
4836 * ccline and put the previous value in prev_ccline.
4837 */
4838 static struct cmdline_info *
4839get_ccline_ptr()
4840{
4841 if ((State & CMDLINE) == 0)
4842 return NULL;
4843 if (ccline.cmdbuff != NULL)
4844 return &ccline;
4845 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
4846 return &prev_ccline;
4847 return NULL;
4848}
4849
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850/*
4851 * Get the current command line in allocated memory.
4852 * Only works when the command line is being edited.
4853 * Returns NULL when something is wrong.
4854 */
4855 char_u *
4856get_cmdline_str()
4857{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004858 struct cmdline_info *p = get_ccline_ptr();
4859
4860 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004862 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863}
4864
4865/*
4866 * Get the current command line position, counted in bytes.
4867 * Zero is the first position.
4868 * Only works when the command line is being edited.
4869 * Returns -1 when something is wrong.
4870 */
4871 int
4872get_cmdline_pos()
4873{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004874 struct cmdline_info *p = get_ccline_ptr();
4875
4876 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004878 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879}
4880
4881/*
4882 * Set the command line byte position to "pos". Zero is the first position.
4883 * Only works when the command line is being edited.
4884 * Returns 1 when failed, 0 when OK.
4885 */
4886 int
4887set_cmdline_pos(pos)
4888 int pos;
4889{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004890 struct cmdline_info *p = get_ccline_ptr();
4891
4892 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 return 1;
4894
4895 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
4896 * changed the command line. */
4897 if (pos < 0)
4898 new_cmdpos = 0;
4899 else
4900 new_cmdpos = pos;
4901 return 0;
4902}
4903
4904/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004905 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00004906 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004907 * Only works when the command line is being edited.
4908 * Returns NUL when something is wrong.
4909 */
4910 int
4911get_cmdline_type()
4912{
4913 struct cmdline_info *p = get_ccline_ptr();
4914
4915 if (p == NULL)
4916 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00004917 if (p->cmdfirstc == NUL)
4918 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004919 return p->cmdfirstc;
4920}
4921
4922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 * Calculate history index from a number:
4924 * num > 0: seen as identifying number of a history entry
4925 * num < 0: relative position in history wrt newest entry
4926 * "histype" may be one of the HIST_ values.
4927 */
4928 static int
4929calc_hist_idx(histype, num)
4930 int histype;
4931 int num;
4932{
4933 int i;
4934 histentry_T *hist;
4935 int wrapped = FALSE;
4936
4937 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4938 || (i = hisidx[histype]) < 0 || num == 0)
4939 return -1;
4940
4941 hist = history[histype];
4942 if (num > 0)
4943 {
4944 while (hist[i].hisnum > num)
4945 if (--i < 0)
4946 {
4947 if (wrapped)
4948 break;
4949 i += hislen;
4950 wrapped = TRUE;
4951 }
4952 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
4953 return i;
4954 }
4955 else if (-num <= hislen)
4956 {
4957 i += num + 1;
4958 if (i < 0)
4959 i += hislen;
4960 if (hist[i].hisstr != NULL)
4961 return i;
4962 }
4963 return -1;
4964}
4965
4966/*
4967 * Get a history entry by its index.
4968 * "histype" may be one of the HIST_ values.
4969 */
4970 char_u *
4971get_history_entry(histype, idx)
4972 int histype;
4973 int idx;
4974{
4975 idx = calc_hist_idx(histype, idx);
4976 if (idx >= 0)
4977 return history[histype][idx].hisstr;
4978 else
4979 return (char_u *)"";
4980}
4981
4982/*
4983 * Clear all entries of a history.
4984 * "histype" may be one of the HIST_ values.
4985 */
4986 int
4987clr_history(histype)
4988 int histype;
4989{
4990 int i;
4991 histentry_T *hisptr;
4992
4993 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
4994 {
4995 hisptr = history[histype];
4996 for (i = hislen; i--;)
4997 {
4998 vim_free(hisptr->hisstr);
4999 hisptr->hisnum = 0;
5000 hisptr++->hisstr = NULL;
5001 }
5002 hisidx[histype] = -1; /* mark history as cleared */
5003 hisnum[histype] = 0; /* reset identifier counter */
5004 return OK;
5005 }
5006 return FAIL;
5007}
5008
5009/*
5010 * Remove all entries matching {str} from a history.
5011 * "histype" may be one of the HIST_ values.
5012 */
5013 int
5014del_history_entry(histype, str)
5015 int histype;
5016 char_u *str;
5017{
5018 regmatch_T regmatch;
5019 histentry_T *hisptr;
5020 int idx;
5021 int i;
5022 int last;
5023 int found = FALSE;
5024
5025 regmatch.regprog = NULL;
5026 regmatch.rm_ic = FALSE; /* always match case */
5027 if (hislen != 0
5028 && histype >= 0
5029 && histype < HIST_COUNT
5030 && *str != NUL
5031 && (idx = hisidx[histype]) >= 0
5032 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5033 != NULL)
5034 {
5035 i = last = idx;
5036 do
5037 {
5038 hisptr = &history[histype][i];
5039 if (hisptr->hisstr == NULL)
5040 break;
5041 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5042 {
5043 found = TRUE;
5044 vim_free(hisptr->hisstr);
5045 hisptr->hisstr = NULL;
5046 hisptr->hisnum = 0;
5047 }
5048 else
5049 {
5050 if (i != last)
5051 {
5052 history[histype][last] = *hisptr;
5053 hisptr->hisstr = NULL;
5054 hisptr->hisnum = 0;
5055 }
5056 if (--last < 0)
5057 last += hislen;
5058 }
5059 if (--i < 0)
5060 i += hislen;
5061 } while (i != idx);
5062 if (history[histype][idx].hisstr == NULL)
5063 hisidx[histype] = -1;
5064 }
5065 vim_free(regmatch.regprog);
5066 return found;
5067}
5068
5069/*
5070 * Remove an indexed entry from a history.
5071 * "histype" may be one of the HIST_ values.
5072 */
5073 int
5074del_history_idx(histype, idx)
5075 int histype;
5076 int idx;
5077{
5078 int i, j;
5079
5080 i = calc_hist_idx(histype, idx);
5081 if (i < 0)
5082 return FALSE;
5083 idx = hisidx[histype];
5084 vim_free(history[histype][i].hisstr);
5085
5086 /* When deleting the last added search string in a mapping, reset
5087 * last_maptick, so that the last added search string isn't deleted again.
5088 */
5089 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5090 last_maptick = -1;
5091
5092 while (i != idx)
5093 {
5094 j = (i + 1) % hislen;
5095 history[histype][i] = history[histype][j];
5096 i = j;
5097 }
5098 history[histype][i].hisstr = NULL;
5099 history[histype][i].hisnum = 0;
5100 if (--i < 0)
5101 i += hislen;
5102 hisidx[histype] = i;
5103 return TRUE;
5104}
5105
5106#endif /* FEAT_EVAL */
5107
5108#if defined(FEAT_CRYPT) || defined(PROTO)
5109/*
5110 * Very specific function to remove the value in ":set key=val" from the
5111 * history.
5112 */
5113 void
5114remove_key_from_history()
5115{
5116 char_u *p;
5117 int i;
5118
5119 i = hisidx[HIST_CMD];
5120 if (i < 0)
5121 return;
5122 p = history[HIST_CMD][i].hisstr;
5123 if (p != NULL)
5124 for ( ; *p; ++p)
5125 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5126 {
5127 p = vim_strchr(p + 3, '=');
5128 if (p == NULL)
5129 break;
5130 ++p;
5131 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5132 if (p[i] == '\\' && p[i + 1])
5133 ++i;
5134 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5135 --p;
5136 }
5137}
5138#endif
5139
5140#endif /* FEAT_CMDHIST */
5141
5142#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5143/*
5144 * Get indices "num1,num2" that specify a range within a list (not a range of
5145 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5146 * Returns OK if parsed successfully, otherwise FAIL.
5147 */
5148 int
5149get_list_range(str, num1, num2)
5150 char_u **str;
5151 int *num1;
5152 int *num2;
5153{
5154 int len;
5155 int first = FALSE;
5156 long num;
5157
5158 *str = skipwhite(*str);
5159 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5160 {
5161 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5162 *str += len;
5163 *num1 = (int)num;
5164 first = TRUE;
5165 }
5166 *str = skipwhite(*str);
5167 if (**str == ',') /* parse "to" part of range */
5168 {
5169 *str = skipwhite(*str + 1);
5170 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5171 if (len > 0)
5172 {
5173 *num2 = (int)num;
5174 *str = skipwhite(*str + len);
5175 }
5176 else if (!first) /* no number given at all */
5177 return FAIL;
5178 }
5179 else if (first) /* only one number given */
5180 *num2 = *num1;
5181 return OK;
5182}
5183#endif
5184
5185#if defined(FEAT_CMDHIST) || defined(PROTO)
5186/*
5187 * :history command - print a history
5188 */
5189 void
5190ex_history(eap)
5191 exarg_T *eap;
5192{
5193 histentry_T *hist;
5194 int histype1 = HIST_CMD;
5195 int histype2 = HIST_CMD;
5196 int hisidx1 = 1;
5197 int hisidx2 = -1;
5198 int idx;
5199 int i, j, k;
5200 char_u *end;
5201 char_u *arg = eap->arg;
5202
5203 if (hislen == 0)
5204 {
5205 MSG(_("'history' option is zero"));
5206 return;
5207 }
5208
5209 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5210 {
5211 end = arg;
5212 while (ASCII_ISALPHA(*end)
5213 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5214 end++;
5215 i = *end;
5216 *end = NUL;
5217 histype1 = get_histtype(arg);
5218 if (histype1 == -1)
5219 {
5220 if (STRICMP(arg, "all") == 0)
5221 {
5222 histype1 = 0;
5223 histype2 = HIST_COUNT-1;
5224 }
5225 else
5226 {
5227 *end = i;
5228 EMSG(_(e_trailing));
5229 return;
5230 }
5231 }
5232 else
5233 histype2 = histype1;
5234 *end = i;
5235 }
5236 else
5237 end = arg;
5238 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5239 {
5240 EMSG(_(e_trailing));
5241 return;
5242 }
5243
5244 for (; !got_int && histype1 <= histype2; ++histype1)
5245 {
5246 STRCPY(IObuff, "\n # ");
5247 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5248 MSG_PUTS_TITLE(IObuff);
5249 idx = hisidx[histype1];
5250 hist = history[histype1];
5251 j = hisidx1;
5252 k = hisidx2;
5253 if (j < 0)
5254 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5255 if (k < 0)
5256 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5257 if (idx >= 0 && j <= k)
5258 for (i = idx + 1; !got_int; ++i)
5259 {
5260 if (i == hislen)
5261 i = 0;
5262 if (hist[i].hisstr != NULL
5263 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5264 {
5265 msg_putchar('\n');
5266 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5267 hist[i].hisnum);
5268 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5269 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5270 (int)Columns - 10);
5271 else
5272 STRCAT(IObuff, hist[i].hisstr);
5273 msg_outtrans(IObuff);
5274 out_flush();
5275 }
5276 if (i == idx)
5277 break;
5278 }
5279 }
5280}
5281#endif
5282
5283#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5284static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5285static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5286static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5287static int viminfo_add_at_front = FALSE;
5288
5289static int hist_type2char __ARGS((int type, int use_question));
5290
5291/*
5292 * Translate a history type number to the associated character.
5293 */
5294 static int
5295hist_type2char(type, use_question)
5296 int type;
5297 int use_question; /* use '?' instead of '/' */
5298{
5299 if (type == HIST_CMD)
5300 return ':';
5301 if (type == HIST_SEARCH)
5302 {
5303 if (use_question)
5304 return '?';
5305 else
5306 return '/';
5307 }
5308 if (type == HIST_EXPR)
5309 return '=';
5310 return '@';
5311}
5312
5313/*
5314 * Prepare for reading the history from the viminfo file.
5315 * This allocates history arrays to store the read history lines.
5316 */
5317 void
5318prepare_viminfo_history(asklen)
5319 int asklen;
5320{
5321 int i;
5322 int num;
5323 int type;
5324 int len;
5325
5326 init_history();
5327 viminfo_add_at_front = (asklen != 0);
5328 if (asklen > hislen)
5329 asklen = hislen;
5330
5331 for (type = 0; type < HIST_COUNT; ++type)
5332 {
5333 /*
5334 * Count the number of empty spaces in the history list. If there are
5335 * more spaces available than we request, then fill them up.
5336 */
5337 for (i = 0, num = 0; i < hislen; i++)
5338 if (history[type][i].hisstr == NULL)
5339 num++;
5340 len = asklen;
5341 if (num > len)
5342 len = num;
5343 if (len <= 0)
5344 viminfo_history[type] = NULL;
5345 else
5346 viminfo_history[type] =
5347 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5348 if (viminfo_history[type] == NULL)
5349 len = 0;
5350 viminfo_hislen[type] = len;
5351 viminfo_hisidx[type] = 0;
5352 }
5353}
5354
5355/*
5356 * Accept a line from the viminfo, store it in the history array when it's
5357 * new.
5358 */
5359 int
5360read_viminfo_history(virp)
5361 vir_T *virp;
5362{
5363 int type;
5364 long_u len;
5365 char_u *val;
5366 char_u *p;
5367
5368 type = hist_char2type(virp->vir_line[0]);
5369 if (viminfo_hisidx[type] < viminfo_hislen[type])
5370 {
5371 val = viminfo_readstring(virp, 1, TRUE);
5372 if (val != NULL && *val != NUL)
5373 {
5374 if (!in_history(type, val + (type == HIST_SEARCH),
5375 viminfo_add_at_front))
5376 {
5377 /* Need to re-allocate to append the separator byte. */
5378 len = STRLEN(val);
5379 p = lalloc(len + 2, TRUE);
5380 if (p != NULL)
5381 {
5382 if (type == HIST_SEARCH)
5383 {
5384 /* Search entry: Move the separator from the first
5385 * column to after the NUL. */
5386 mch_memmove(p, val + 1, (size_t)len);
5387 p[len] = (*val == ' ' ? NUL : *val);
5388 }
5389 else
5390 {
5391 /* Not a search entry: No separator in the viminfo
5392 * file, add a NUL separator. */
5393 mch_memmove(p, val, (size_t)len + 1);
5394 p[len + 1] = NUL;
5395 }
5396 viminfo_history[type][viminfo_hisidx[type]++] = p;
5397 }
5398 }
5399 }
5400 vim_free(val);
5401 }
5402 return viminfo_readline(virp);
5403}
5404
5405 void
5406finish_viminfo_history()
5407{
5408 int idx;
5409 int i;
5410 int type;
5411
5412 for (type = 0; type < HIST_COUNT; ++type)
5413 {
5414 if (history[type] == NULL)
5415 return;
5416 idx = hisidx[type] + viminfo_hisidx[type];
5417 if (idx >= hislen)
5418 idx -= hislen;
5419 else if (idx < 0)
5420 idx = hislen - 1;
5421 if (viminfo_add_at_front)
5422 hisidx[type] = idx;
5423 else
5424 {
5425 if (hisidx[type] == -1)
5426 hisidx[type] = hislen - 1;
5427 do
5428 {
5429 if (history[type][idx].hisstr != NULL)
5430 break;
5431 if (++idx == hislen)
5432 idx = 0;
5433 } while (idx != hisidx[type]);
5434 if (idx != hisidx[type] && --idx < 0)
5435 idx = hislen - 1;
5436 }
5437 for (i = 0; i < viminfo_hisidx[type]; i++)
5438 {
5439 vim_free(history[type][idx].hisstr);
5440 history[type][idx].hisstr = viminfo_history[type][i];
5441 if (--idx < 0)
5442 idx = hislen - 1;
5443 }
5444 idx += 1;
5445 idx %= hislen;
5446 for (i = 0; i < viminfo_hisidx[type]; i++)
5447 {
5448 history[type][idx++].hisnum = ++hisnum[type];
5449 idx %= hislen;
5450 }
5451 vim_free(viminfo_history[type]);
5452 viminfo_history[type] = NULL;
5453 }
5454}
5455
5456 void
5457write_viminfo_history(fp)
5458 FILE *fp;
5459{
5460 int i;
5461 int type;
5462 int num_saved;
5463 char_u *p;
5464 int c;
5465
5466 init_history();
5467 if (hislen == 0)
5468 return;
5469 for (type = 0; type < HIST_COUNT; ++type)
5470 {
5471 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5472 if (num_saved == 0)
5473 continue;
5474 if (num_saved < 0) /* Use default */
5475 num_saved = hislen;
5476 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5477 type == HIST_CMD ? _("Command Line") :
5478 type == HIST_SEARCH ? _("Search String") :
5479 type == HIST_EXPR ? _("Expression") :
5480 _("Input Line"));
5481 if (num_saved > hislen)
5482 num_saved = hislen;
5483 i = hisidx[type];
5484 if (i >= 0)
5485 while (num_saved--)
5486 {
5487 p = history[type][i].hisstr;
5488 if (p != NULL)
5489 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005490 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 /* For the search history: put the separator in the second
5492 * column; use a space if there isn't one. */
5493 if (type == HIST_SEARCH)
5494 {
5495 c = p[STRLEN(p) + 1];
5496 putc(c == NUL ? ' ' : c, fp);
5497 }
5498 viminfo_writestring(fp, p);
5499 }
5500 if (--i < 0)
5501 i = hislen - 1;
5502 }
5503 }
5504}
5505#endif /* FEAT_VIMINFO */
5506
5507#if defined(FEAT_FKMAP) || defined(PROTO)
5508/*
5509 * Write a character at the current cursor+offset position.
5510 * It is directly written into the command buffer block.
5511 */
5512 void
5513cmd_pchar(c, offset)
5514 int c, offset;
5515{
5516 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5517 {
5518 EMSG(_("E198: cmd_pchar beyond the command length"));
5519 return;
5520 }
5521 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5522 ccline.cmdbuff[ccline.cmdlen] = NUL;
5523}
5524
5525 int
5526cmd_gchar(offset)
5527 int offset;
5528{
5529 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5530 {
5531 /* EMSG(_("cmd_gchar beyond the command length")); */
5532 return NUL;
5533 }
5534 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5535}
5536#endif
5537
5538#if defined(FEAT_CMDWIN) || defined(PROTO)
5539/*
5540 * Open a window on the current command line and history. Allow editing in
5541 * the window. Returns when the window is closed.
5542 * Returns:
5543 * CR if the command is to be executed
5544 * Ctrl_C if it is to be abandoned
5545 * K_IGNORE if editing continues
5546 */
5547 static int
5548ex_window()
5549{
5550 struct cmdline_info save_ccline;
5551 buf_T *old_curbuf = curbuf;
5552 win_T *old_curwin = curwin;
5553 buf_T *bp;
5554 win_T *wp;
5555 int i;
5556 linenr_T lnum;
5557 int histtype;
5558 garray_T winsizes;
5559 char_u typestr[2];
5560 int save_restart_edit = restart_edit;
5561 int save_State = State;
5562 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005563#ifdef FEAT_RIGHTLEFT
5564 int save_cmdmsg_rl = cmdmsg_rl;
5565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
5567 /* Can't do this recursively. Can't do it when typing a password. */
5568 if (cmdwin_type != 0
5569# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5570 || cmdline_star > 0
5571# endif
5572 )
5573 {
5574 beep_flush();
5575 return K_IGNORE;
5576 }
5577
5578 /* Save current window sizes. */
5579 win_size_save(&winsizes);
5580
5581# ifdef FEAT_AUTOCMD
5582 /* Don't execute autocommands while creating the window. */
5583 ++autocmd_block;
5584# endif
5585 /* Create a window for the command-line buffer. */
5586 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5587 {
5588 beep_flush();
5589 return K_IGNORE;
5590 }
5591 cmdwin_type = ccline.cmdfirstc;
5592 if (cmdwin_type == NUL)
5593 cmdwin_type = '-';
5594
5595 /* Create the command-line buffer empty. */
5596 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5597 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5598 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5599 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5600 curbuf->b_p_ma = TRUE;
5601# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005602 curwin->w_p_rl = cmdmsg_rl;
5603 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604# endif
5605# ifdef FEAT_SCROLLBIND
5606 curwin->w_p_scb = FALSE;
5607# endif
5608
5609# ifdef FEAT_AUTOCMD
5610 /* Do execute autocommands for setting the filetype (load syntax). */
5611 --autocmd_block;
5612# endif
5613
Bram Moolenaar46152342005-09-07 21:18:43 +00005614 /* Showing the prompt may have set need_wait_return, reset it. */
5615 need_wait_return = FALSE;
5616
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 histtype = hist_char2type(ccline.cmdfirstc);
5618 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5619 {
5620 if (p_wc == TAB)
5621 {
5622 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5623 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5624 }
5625 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5626 }
5627
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005628 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5629 * sets 'textwidth' to 78). */
5630 curbuf->b_p_tw = 0;
5631
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 /* Fill the buffer with the history. */
5633 init_history();
5634 if (hislen > 0)
5635 {
5636 i = hisidx[histtype];
5637 if (i >= 0)
5638 {
5639 lnum = 0;
5640 do
5641 {
5642 if (++i == hislen)
5643 i = 0;
5644 if (history[histtype][i].hisstr != NULL)
5645 ml_append(lnum++, history[histtype][i].hisstr,
5646 (colnr_T)0, FALSE);
5647 }
5648 while (i != hisidx[histtype]);
5649 }
5650 }
5651
5652 /* Replace the empty last line with the current command-line and put the
5653 * cursor there. */
5654 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5655 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5656 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005657 changed_line_abv_curs();
5658 invalidate_botline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 redraw_later(NOT_VALID);
5660
5661 /* Save the command line info, can be used recursively. */
5662 save_ccline = ccline;
5663 ccline.cmdbuff = NULL;
5664 ccline.cmdprompt = NULL;
5665
5666 /* No Ex mode here! */
5667 exmode_active = 0;
5668
5669 State = NORMAL;
5670# ifdef FEAT_MOUSE
5671 setmouse();
5672# endif
5673
5674# ifdef FEAT_AUTOCMD
5675 /* Trigger CmdwinEnter autocommands. */
5676 typestr[0] = cmdwin_type;
5677 typestr[1] = NUL;
5678 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5679# endif
5680
5681 i = RedrawingDisabled;
5682 RedrawingDisabled = 0;
5683
5684 /*
5685 * Call the main loop until <CR> or CTRL-C is typed.
5686 */
5687 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005688 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
5690 RedrawingDisabled = i;
5691
5692# ifdef FEAT_AUTOCMD
5693 /* Trigger CmdwinLeave autocommands. */
5694 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5695# endif
5696
5697 /* Restore the comand line info. */
5698 ccline = save_ccline;
5699 cmdwin_type = 0;
5700
5701 exmode_active = save_exmode;
5702
5703 /* Safety check: The old window or buffer was deleted: It's a a bug when
5704 * this happens! */
5705 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5706 {
5707 cmdwin_result = Ctrl_C;
5708 EMSG(_("E199: Active window or buffer deleted"));
5709 }
5710 else
5711 {
5712# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5713 /* autocmds may abort script processing */
5714 if (aborting() && cmdwin_result != K_IGNORE)
5715 cmdwin_result = Ctrl_C;
5716# endif
5717 /* Set the new command line from the cmdline buffer. */
5718 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00005719 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {
Bram Moolenaar46152342005-09-07 21:18:43 +00005721 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
5722
5723 if (histtype == HIST_CMD)
5724 {
5725 /* Execute the command directly. */
5726 ccline.cmdbuff = vim_strsave((char_u *)p);
5727 cmdwin_result = CAR;
5728 }
5729 else
5730 {
5731 /* First need to cancel what we were doing. */
5732 ccline.cmdbuff = NULL;
5733 stuffcharReadbuff(':');
5734 stuffReadbuff((char_u *)p);
5735 stuffcharReadbuff(CAR);
5736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738 else if (cmdwin_result == K_XF2) /* :qa typed */
5739 {
5740 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5741 cmdwin_result = CAR;
5742 }
5743 else
5744 ccline.cmdbuff = vim_strsave(ml_get_curline());
5745 if (ccline.cmdbuff == NULL)
5746 cmdwin_result = Ctrl_C;
5747 else
5748 {
5749 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5750 ccline.cmdbufflen = ccline.cmdlen + 1;
5751 ccline.cmdpos = curwin->w_cursor.col;
5752 if (ccline.cmdpos > ccline.cmdlen)
5753 ccline.cmdpos = ccline.cmdlen;
5754 if (cmdwin_result == K_IGNORE)
5755 {
5756 set_cmdspos_cursor();
5757 redrawcmd();
5758 }
5759 }
5760
5761# ifdef FEAT_AUTOCMD
5762 /* Don't execute autocommands while deleting the window. */
5763 ++autocmd_block;
5764# endif
5765 wp = curwin;
5766 bp = curbuf;
5767 win_goto(old_curwin);
5768 win_close(wp, TRUE);
5769 close_buffer(NULL, bp, DOBUF_WIPE);
5770
5771 /* Restore window sizes. */
5772 win_size_restore(&winsizes);
5773
5774# ifdef FEAT_AUTOCMD
5775 --autocmd_block;
5776# endif
5777 }
5778
5779 ga_clear(&winsizes);
5780 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00005781# ifdef FEAT_RIGHTLEFT
5782 cmdmsg_rl = save_cmdmsg_rl;
5783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785 State = save_State;
5786# ifdef FEAT_MOUSE
5787 setmouse();
5788# endif
5789
5790 return cmdwin_result;
5791}
5792#endif /* FEAT_CMDWIN */
5793
5794/*
5795 * Used for commands that either take a simple command string argument, or:
5796 * cmd << endmarker
5797 * {script}
5798 * endmarker
5799 * Returns a pointer to allocated memory with {script} or NULL.
5800 */
5801 char_u *
5802script_get(eap, cmd)
5803 exarg_T *eap;
5804 char_u *cmd;
5805{
5806 char_u *theline;
5807 char *end_pattern = NULL;
5808 char dot[] = ".";
5809 garray_T ga;
5810
5811 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5812 return NULL;
5813
5814 ga_init2(&ga, 1, 0x400);
5815
5816 if (cmd[2] != NUL)
5817 end_pattern = (char *)skipwhite(cmd + 2);
5818 else
5819 end_pattern = dot;
5820
5821 for (;;)
5822 {
5823 theline = eap->getline(
5824#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00005825 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#endif
5827 NUL, eap->cookie, 0);
5828
5829 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
5830 break;
5831
5832 ga_concat(&ga, theline);
5833 ga_append(&ga, '\n');
5834 vim_free(theline);
5835 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00005836 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
5838 return (char_u *)ga.ga_data;
5839}