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