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