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