blob: 1cf67854371a6324ebe4ad6ce401f17741613aab [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 Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
55#ifdef FEAT_CMDHIST
56typedef struct hist_entry
57{
58 int hisnum; /* identifying number */
59 char_u *hisstr; /* actual entry, separator char after the NUL */
60} histentry_T;
61
62static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
63static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
64static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
65 /* identifying (unique) number of newest history entry */
66static int hislen = 0; /* actual length of history tables */
67
68static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
70static int in_history __ARGS((int, char_u *, int));
71# ifdef FEAT_EVAL
72static int calc_hist_idx __ARGS((int histype, int num));
73# endif
74#endif
75
76#ifdef FEAT_RIGHTLEFT
77static int cmd_hkmap = 0; /* Hebrew mapping during command line */
78#endif
79
80#ifdef FEAT_FKMAP
81static int cmd_fkmap = 0; /* Farsi mapping during command line */
82#endif
83
84static int cmdline_charsize __ARGS((int idx));
85static void set_cmdspos __ARGS((void));
86static void set_cmdspos_cursor __ARGS((void));
87#ifdef FEAT_MBYTE
88static void correct_cmdspos __ARGS((int idx, int cells));
89#endif
90static void alloc_cmdbuff __ARGS((int len));
91static int realloc_cmdbuff __ARGS((int len));
92static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000093static void save_cmdline __ARGS((struct cmdline_info *ccp));
94static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar1769d5a2006-10-17 14:25:24 +000095static int cmdline_paste __ARGS((int regname, int literally, int remcr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
97static void redrawcmd_preedit __ARGS((void));
98#endif
99#ifdef FEAT_WILDMENU
100static void cmdline_del __ARGS((int from));
101#endif
102static void redrawcmdprompt __ARGS((void));
103static void cursorcmd __ARGS((void));
104static int ccheck_abbr __ARGS((int));
105static int nextwild __ARGS((expand_T *xp, int type, int options));
Bram Moolenaar45360022005-07-21 21:08:21 +0000106static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static int showmatches __ARGS((expand_T *xp, int wildmenu));
108static void set_expand_context __ARGS((expand_T *xp));
109static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
110static int expand_showtail __ARGS((expand_T *xp));
111#ifdef FEAT_CMDL_COMPL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000112static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
114# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
115static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000116static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117# endif
118#endif
119
120#ifdef FEAT_CMDWIN
121static int ex_window __ARGS((void));
122#endif
123
124/*
125 * getcmdline() - accept a command line starting with firstc.
126 *
127 * firstc == ':' get ":" command line.
128 * firstc == '/' or '?' get search pattern
129 * firstc == '=' get expression
130 * firstc == '@' get text for input() function
131 * firstc == '>' get text for debug mode
132 * firstc == NUL get text for :insert command
133 * firstc == -1 like NUL, and break on CTRL-C
134 *
135 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
136 * command line.
137 *
138 * Careful: getcmdline() can be called recursively!
139 *
140 * Return pointer to allocated string if there is a commandline, NULL
141 * otherwise.
142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 char_u *
144getcmdline(firstc, count, indent)
145 int firstc;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000146 long count UNUSED; /* only used for incremental search */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 int indent; /* indent for inside conditionals */
148{
149 int c;
150 int i;
151 int j;
152 int gotesc = FALSE; /* TRUE when <ESC> just typed */
153 int do_abbr; /* when TRUE check for abbr. */
154#ifdef FEAT_CMDHIST
155 char_u *lookfor = NULL; /* string to match */
156 int hiscnt; /* current history line in use */
157 int histype; /* history type to be used */
158#endif
159#ifdef FEAT_SEARCH_EXTRA
160 pos_T old_cursor;
161 colnr_T old_curswant;
162 colnr_T old_leftcol;
163 linenr_T old_topline;
164# ifdef FEAT_DIFF
165 int old_topfill;
166# endif
167 linenr_T old_botline;
168 int did_incsearch = FALSE;
169 int incsearch_postponed = FALSE;
170#endif
171 int did_wild_list = FALSE; /* did wild_list() recently */
172 int wim_index = 0; /* index in wim_flags[] */
173 int res;
174 int save_msg_scroll = msg_scroll;
175 int save_State = State; /* remember State when called */
176 int some_key_typed = FALSE; /* one of the keys was typed */
177#ifdef FEAT_MOUSE
178 /* mouse drag and release events are ignored, unless they are
179 * preceded with a mouse down event */
180 int ignore_drag_release = TRUE;
181#endif
182#ifdef FEAT_EVAL
183 int break_ctrl_c = FALSE;
184#endif
185 expand_T xpc;
186 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000187#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
188 /* Everything that may work recursively should save and restore the
189 * current command line in save_ccline. That includes update_screen(), a
190 * custom status line may invoke ":normal". */
191 struct cmdline_info save_ccline;
192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194#ifdef FEAT_SNIFF
195 want_sniff_request = 0;
196#endif
197#ifdef FEAT_EVAL
198 if (firstc == -1)
199 {
200 firstc = NUL;
201 break_ctrl_c = TRUE;
202 }
203#endif
204#ifdef FEAT_RIGHTLEFT
205 /* start without Hebrew mapping for a command line */
206 if (firstc == ':' || firstc == '=' || firstc == '>')
207 cmd_hkmap = 0;
208#endif
209
210 ccline.overstrike = FALSE; /* always start in insert mode */
211#ifdef FEAT_SEARCH_EXTRA
212 old_cursor = curwin->w_cursor; /* needs to be restored later */
213 old_curswant = curwin->w_curswant;
214 old_leftcol = curwin->w_leftcol;
215 old_topline = curwin->w_topline;
216# ifdef FEAT_DIFF
217 old_topfill = curwin->w_topfill;
218# endif
219 old_botline = curwin->w_botline;
220#endif
221
222 /*
223 * set some variables for redrawcmd()
224 */
225 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000226 ccline.cmdindent = (firstc > 0 ? indent : 0);
227
228 /* alloc initial ccline.cmdbuff */
229 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 if (ccline.cmdbuff == NULL)
231 return NULL; /* out of memory */
232 ccline.cmdlen = ccline.cmdpos = 0;
233 ccline.cmdbuff[0] = NUL;
234
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000235 /* autoindent for :insert and :append */
236 if (firstc <= 0)
237 {
238 copy_spaces(ccline.cmdbuff, indent);
239 ccline.cmdbuff[indent] = NUL;
240 ccline.cmdpos = indent;
241 ccline.cmdspos = indent;
242 ccline.cmdlen = indent;
243 }
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000246 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247
248#ifdef FEAT_RIGHTLEFT
249 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
250 && (firstc == '/' || firstc == '?'))
251 cmdmsg_rl = TRUE;
252 else
253 cmdmsg_rl = FALSE;
254#endif
255
256 redir_off = TRUE; /* don't redirect the typed command */
257 if (!cmd_silent)
258 {
259 i = msg_scrolled;
260 msg_scrolled = 0; /* avoid wait_return message */
261 gotocmdline(TRUE);
262 msg_scrolled += i;
263 redrawcmdprompt(); /* draw prompt or indent */
264 set_cmdspos();
265 }
266 xpc.xp_context = EXPAND_NOTHING;
267 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000268#ifndef BACKSLASH_IN_FILENAME
269 xpc.xp_shell = FALSE;
270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000272#if defined(FEAT_EVAL)
273 if (ccline.input_fn)
274 {
275 xpc.xp_context = ccline.xp_context;
276 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000277# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000278 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000279# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000280 }
281#endif
282
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 /*
284 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
285 * doing ":@0" when register 0 doesn't contain a CR.
286 */
287 msg_scroll = FALSE;
288
289 State = CMDLINE;
290
291 if (firstc == '/' || firstc == '?' || firstc == '@')
292 {
293 /* Use ":lmap" mappings for search pattern and input(). */
294 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
295 b_im_ptr = &curbuf->b_p_iminsert;
296 else
297 b_im_ptr = &curbuf->b_p_imsearch;
298 if (*b_im_ptr == B_IMODE_LMAP)
299 State |= LANGMAP;
300#ifdef USE_IM_CONTROL
301 im_set_active(*b_im_ptr == B_IMODE_IM);
302#endif
303 }
304#ifdef USE_IM_CONTROL
305 else if (p_imcmdline)
306 im_set_active(TRUE);
307#endif
308
309#ifdef FEAT_MOUSE
310 setmouse();
311#endif
312#ifdef CURSOR_SHAPE
313 ui_cursor_shape(); /* may show different cursor shape */
314#endif
315
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000316 /* When inside an autocommand for writing "exiting" may be set and
317 * terminal mode set to cooked. Need to set raw mode here then. */
318 settmode(TMODE_RAW);
319
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifdef FEAT_CMDHIST
321 init_history();
322 hiscnt = hislen; /* set hiscnt to impossible history value */
323 histype = hist_char2type(firstc);
324#endif
325
326#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000327 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#endif
329
330 /*
331 * Collect the command string, handling editing keys.
332 */
333 for (;;)
334 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000335 redir_off = TRUE; /* Don't redirect the typed command.
336 Repeated, because a ":redir" inside
337 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#ifdef USE_ON_FLY_SCROLL
339 dont_scroll = FALSE; /* allow scrolling here */
340#endif
341 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
342
343 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000344
345 /* Get a character. Ignore K_IGNORE, it should not do anything, such
346 * as stop completion. */
347 do
348 {
349 c = safe_vgetc();
350 } while (c == K_IGNORE);
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 if (KeyTyped)
353 {
354 some_key_typed = TRUE;
355#ifdef FEAT_RIGHTLEFT
356 if (cmd_hkmap)
357 c = hkmap(c);
358# ifdef FEAT_FKMAP
359 if (cmd_fkmap)
360 c = cmdl_fkmap(c);
361# endif
362 if (cmdmsg_rl && !KeyStuffed)
363 {
364 /* Invert horizontal movements and operations. Only when
365 * typed by the user directly, not when the result of a
366 * mapping. */
367 switch (c)
368 {
369 case K_RIGHT: c = K_LEFT; break;
370 case K_S_RIGHT: c = K_S_LEFT; break;
371 case K_C_RIGHT: c = K_C_LEFT; break;
372 case K_LEFT: c = K_RIGHT; break;
373 case K_S_LEFT: c = K_S_RIGHT; break;
374 case K_C_LEFT: c = K_C_RIGHT; break;
375 }
376 }
377#endif
378 }
379
380 /*
381 * Ignore got_int when CTRL-C was typed here.
382 * Don't ignore it in :global, we really need to break then, e.g., for
383 * ":g/pat/normal /pat" (without the <CR>).
384 * Don't ignore it for the input() function.
385 */
386 if ((c == Ctrl_C
387#ifdef UNIX
388 || c == intr_char
389#endif
390 )
391#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
392 && firstc != '@'
393#endif
394#ifdef FEAT_EVAL
395 && !break_ctrl_c
396#endif
397 && !global_busy)
398 got_int = FALSE;
399
400#ifdef FEAT_CMDHIST
401 /* free old command line when finished moving around in the history
402 * list */
403 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000404 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000405 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 && c != K_PAGEDOWN && c != K_PAGEUP
407 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000408 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
410 {
411 vim_free(lookfor);
412 lookfor = NULL;
413 }
414#endif
415
416 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000417 * When there are matching completions to select <S-Tab> works like
418 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000420 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 c = Ctrl_P;
422
423#ifdef FEAT_WILDMENU
424 /* Special translations for 'wildmenu' */
425 if (did_wild_list && p_wmnu)
426 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000427 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000429 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 c = Ctrl_N;
431 }
432 /* Hitting CR after "emenu Name.": complete submenu */
433 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
434 && ccline.cmdpos > 1
435 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
436 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
437 && (c == '\n' || c == '\r' || c == K_KENTER))
438 c = K_DOWN;
439#endif
440
441 /* free expanded names when finished walking through matches */
442 if (xpc.xp_numfiles != -1
443 && !(c == p_wc && KeyTyped) && c != p_wcm
444 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
445 && c != Ctrl_L)
446 {
447 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
448 did_wild_list = FALSE;
449#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000450 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451#endif
452 xpc.xp_context = EXPAND_NOTHING;
453 wim_index = 0;
454#ifdef FEAT_WILDMENU
455 if (p_wmnu && wild_menu_showing != 0)
456 {
457 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000458 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000459
460 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000461 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
463 if (wild_menu_showing == WM_SCROLLED)
464 {
465 /* Entered command line, move it up */
466 cmdline_row--;
467 redrawcmd();
468 }
469 else if (save_p_ls != -1)
470 {
471 /* restore 'laststatus' and 'winminheight' */
472 p_ls = save_p_ls;
473 p_wmh = save_p_wmh;
474 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000475 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000477 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 redrawcmd();
479 save_p_ls = -1;
480 }
481 else
482 {
483# ifdef FEAT_VERTSPLIT
484 win_redraw_last_status(topframe);
485# else
486 lastwin->w_redr_status = TRUE;
487# endif
488 redraw_statuslines();
489 }
490 KeyTyped = skt;
491 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000492 if (ccline.input_fn)
493 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 }
495#endif
496 }
497
498#ifdef FEAT_WILDMENU
499 /* Special translations for 'wildmenu' */
500 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
501 {
502 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000503 if (c == K_DOWN && ccline.cmdpos > 0
504 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000506 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
508 /* Hitting <Up>: Remove one submenu name in front of the
509 * cursor */
510 int found = FALSE;
511
512 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
513 i = 0;
514 while (--j > 0)
515 {
516 /* check for start of menu name */
517 if (ccline.cmdbuff[j] == ' '
518 && ccline.cmdbuff[j - 1] != '\\')
519 {
520 i = j + 1;
521 break;
522 }
523 /* check for start of submenu name */
524 if (ccline.cmdbuff[j] == '.'
525 && ccline.cmdbuff[j - 1] != '\\')
526 {
527 if (found)
528 {
529 i = j + 1;
530 break;
531 }
532 else
533 found = TRUE;
534 }
535 }
536 if (i > 0)
537 cmdline_del(i);
538 c = p_wc;
539 xpc.xp_context = EXPAND_NOTHING;
540 }
541 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000542 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000543 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000544 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
546 char_u upseg[5];
547
548 upseg[0] = PATHSEP;
549 upseg[1] = '.';
550 upseg[2] = '.';
551 upseg[3] = PATHSEP;
552 upseg[4] = NUL;
553
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000554 if (c == K_DOWN
555 && ccline.cmdpos > 0
556 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
557 && (ccline.cmdpos < 3
558 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
560 {
561 /* go down a directory */
562 c = p_wc;
563 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000564 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {
566 /* If in a direct ancestor, strip off one ../ to go down */
567 int found = FALSE;
568
569 j = ccline.cmdpos;
570 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
571 while (--j > i)
572 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000573#ifdef FEAT_MBYTE
574 if (has_mbyte)
575 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 if (vim_ispathsep(ccline.cmdbuff[j]))
578 {
579 found = TRUE;
580 break;
581 }
582 }
583 if (found
584 && ccline.cmdbuff[j - 1] == '.'
585 && ccline.cmdbuff[j - 2] == '.'
586 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
587 {
588 cmdline_del(j - 2);
589 c = p_wc;
590 }
591 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000592 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 {
594 /* go up a directory */
595 int found = FALSE;
596
597 j = ccline.cmdpos - 1;
598 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
599 while (--j > i)
600 {
601#ifdef FEAT_MBYTE
602 if (has_mbyte)
603 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
604#endif
605 if (vim_ispathsep(ccline.cmdbuff[j])
606#ifdef BACKSLASH_IN_FILENAME
607 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
608 == NULL
609#endif
610 )
611 {
612 if (found)
613 {
614 i = j + 1;
615 break;
616 }
617 else
618 found = TRUE;
619 }
620 }
621
622 if (!found)
623 j = i;
624 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
625 j += 4;
626 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
627 && j == i)
628 j += 3;
629 else
630 j = 0;
631 if (j > 0)
632 {
633 /* TODO this is only for DOS/UNIX systems - need to put in
634 * machine-specific stuff here and in upseg init */
635 cmdline_del(j);
636 put_on_cmdline(upseg + 1, 3, FALSE);
637 }
638 else if (ccline.cmdpos > i)
639 cmdline_del(i);
640 c = p_wc;
641 }
642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
644#endif /* FEAT_WILDMENU */
645
646 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
647 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
648 if (c == Ctrl_BSL)
649 {
650 ++no_mapping;
651 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000652 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 --no_mapping;
654 --allow_keys;
655 /* CTRL-\ e doesn't work when obtaining an expression. */
656 if (c != Ctrl_N && c != Ctrl_G
657 && (c != 'e' || ccline.cmdfirstc == '='))
658 {
659 vungetc(c);
660 c = Ctrl_BSL;
661 }
662#ifdef FEAT_EVAL
663 else if (c == 'e')
664 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000665 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
667 /*
668 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000669 * Need to save and restore the current command line, to be
670 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 */
672 if (ccline.cmdpos == ccline.cmdlen)
673 new_cmdpos = 99999; /* keep it at the end */
674 else
675 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000676
677 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000679 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 if (c == '=')
681 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000682 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000683 * to avoid nasty things like going to another buffer when
684 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000685 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000686 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000688 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000689 restore_cmdline(&save_ccline);
690
691 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000693 ccline.cmdlen = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 STRCPY(ccline.cmdbuff, p);
695 vim_free(p);
696
697 /* Restore the cursor or use the position set with
698 * set_cmdline_pos(). */
699 if (new_cmdpos > ccline.cmdlen)
700 ccline.cmdpos = ccline.cmdlen;
701 else
702 ccline.cmdpos = new_cmdpos;
703
704 KeyTyped = FALSE; /* Don't do p_wc completion. */
705 redrawcmd();
706 goto cmdline_changed;
707 }
708 }
709 beep_flush();
710 c = ESC;
711 }
712#endif
713 else
714 {
715 if (c == Ctrl_G && p_im && restart_edit == 0)
716 restart_edit = 'a';
717 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
718 in history */
719 goto returncmd; /* back to Normal mode */
720 }
721 }
722
723#ifdef FEAT_CMDWIN
724 if (c == cedit_key || c == K_CMDWIN)
725 {
726 /*
727 * Open a window to edit the command line (and history).
728 */
729 c = ex_window();
730 some_key_typed = TRUE;
731 }
732# ifdef FEAT_DIGRAPHS
733 else
734# endif
735#endif
736#ifdef FEAT_DIGRAPHS
737 c = do_digraph(c);
738#endif
739
740 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
741 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
742 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000743 /* In Ex mode a backslash escapes a newline. */
744 if (exmode_active
745 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000746 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000747 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000748 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000750 if (c == K_KENTER)
751 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000753 else
754 {
755 gotesc = FALSE; /* Might have typed ESC previously, don't
756 truncate the cmdline now. */
757 if (ccheck_abbr(c + ABBR_OFF))
758 goto cmdline_changed;
759 if (!cmd_silent)
760 {
761 windgoto(msg_row, 0);
762 out_flush();
763 }
764 break;
765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 }
767
768 /*
769 * Completion for 'wildchar' or 'wildcharm' key.
770 * - hitting <ESC> twice means: abandon command line.
771 * - wildcard expansion is only done when the 'wildchar' key is really
772 * typed, not when it comes from a macro
773 */
774 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
775 {
776 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
777 {
778 /* if 'wildmode' contains "list" may still need to list */
779 if (xpc.xp_numfiles > 1
780 && !did_wild_list
781 && (wim_flags[wim_index] & WIM_LIST))
782 {
783 (void)showmatches(&xpc, FALSE);
784 redrawcmd();
785 did_wild_list = TRUE;
786 }
787 if (wim_flags[wim_index] & WIM_LONGEST)
788 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
789 else if (wim_flags[wim_index] & WIM_FULL)
790 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
791 else
792 res = OK; /* don't insert 'wildchar' now */
793 }
794 else /* typed p_wc first time */
795 {
796 wim_index = 0;
797 j = ccline.cmdpos;
798 /* if 'wildmode' first contains "longest", get longest
799 * common part */
800 if (wim_flags[0] & WIM_LONGEST)
801 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
802 else
803 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
804
805 /* if interrupted while completing, behave like it failed */
806 if (got_int)
807 {
808 (void)vpeekc(); /* remove <C-C> from input stream */
809 got_int = FALSE; /* don't abandon the command line */
810 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
811#ifdef FEAT_WILDMENU
812 xpc.xp_context = EXPAND_NOTHING;
813#endif
814 goto cmdline_changed;
815 }
816
817 /* when more than one match, and 'wildmode' first contains
818 * "list", or no change and 'wildmode' contains "longest,list",
819 * list all matches */
820 if (res == OK && xpc.xp_numfiles > 1)
821 {
822 /* a "longest" that didn't do anything is skipped (but not
823 * "list:longest") */
824 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
825 wim_index = 1;
826 if ((wim_flags[wim_index] & WIM_LIST)
827#ifdef FEAT_WILDMENU
828 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
829#endif
830 )
831 {
832 if (!(wim_flags[0] & WIM_LONGEST))
833 {
834#ifdef FEAT_WILDMENU
835 int p_wmnu_save = p_wmnu;
836 p_wmnu = 0;
837#endif
838 nextwild(&xpc, WILD_PREV, 0); /* remove match */
839#ifdef FEAT_WILDMENU
840 p_wmnu = p_wmnu_save;
841#endif
842 }
843#ifdef FEAT_WILDMENU
844 (void)showmatches(&xpc, p_wmnu
845 && ((wim_flags[wim_index] & WIM_LIST) == 0));
846#else
847 (void)showmatches(&xpc, FALSE);
848#endif
849 redrawcmd();
850 did_wild_list = TRUE;
851 if (wim_flags[wim_index] & WIM_LONGEST)
852 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
853 else if (wim_flags[wim_index] & WIM_FULL)
854 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
855 }
856 else
857 vim_beep();
858 }
859#ifdef FEAT_WILDMENU
860 else if (xpc.xp_numfiles == -1)
861 xpc.xp_context = EXPAND_NOTHING;
862#endif
863 }
864 if (wim_index < 3)
865 ++wim_index;
866 if (c == ESC)
867 gotesc = TRUE;
868 if (res == OK)
869 goto cmdline_changed;
870 }
871
872 gotesc = FALSE;
873
874 /* <S-Tab> goes to last match, in a clumsy way */
875 if (c == K_S_TAB && KeyTyped)
876 {
877 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
878 && nextwild(&xpc, WILD_PREV, 0) == OK
879 && nextwild(&xpc, WILD_PREV, 0) == OK)
880 goto cmdline_changed;
881 }
882
883 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
884 c = NL;
885
886 do_abbr = TRUE; /* default: check for abbreviation */
887
888 /*
889 * Big switch for a typed command line character.
890 */
891 switch (c)
892 {
893 case K_BS:
894 case Ctrl_H:
895 case K_DEL:
896 case K_KDEL:
897 case Ctrl_W:
898#ifdef FEAT_FKMAP
899 if (cmd_fkmap && c == K_BS)
900 c = K_DEL;
901#endif
902 if (c == K_KDEL)
903 c = K_DEL;
904
905 /*
906 * delete current character is the same as backspace on next
907 * character, except at end of line
908 */
909 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
910 ++ccline.cmdpos;
911#ifdef FEAT_MBYTE
912 if (has_mbyte && c == K_DEL)
913 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
914 ccline.cmdbuff + ccline.cmdpos);
915#endif
916 if (ccline.cmdpos > 0)
917 {
918 char_u *p;
919
920 j = ccline.cmdpos;
921 p = ccline.cmdbuff + j;
922#ifdef FEAT_MBYTE
923 if (has_mbyte)
924 {
925 p = mb_prevptr(ccline.cmdbuff, p);
926 if (c == Ctrl_W)
927 {
928 while (p > ccline.cmdbuff && vim_isspace(*p))
929 p = mb_prevptr(ccline.cmdbuff, p);
930 i = mb_get_class(p);
931 while (p > ccline.cmdbuff && mb_get_class(p) == i)
932 p = mb_prevptr(ccline.cmdbuff, p);
933 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000934 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 }
936 }
937 else
938#endif
939 if (c == Ctrl_W)
940 {
941 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
942 --p;
943 i = vim_iswordc(p[-1]);
944 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
945 && vim_iswordc(p[-1]) == i)
946 --p;
947 }
948 else
949 --p;
950 ccline.cmdpos = (int)(p - ccline.cmdbuff);
951 ccline.cmdlen -= j - ccline.cmdpos;
952 i = ccline.cmdpos;
953 while (i < ccline.cmdlen)
954 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
955
956 /* Truncate at the end, required for multi-byte chars. */
957 ccline.cmdbuff[ccline.cmdlen] = NUL;
958 redrawcmd();
959 }
960 else if (ccline.cmdlen == 0 && c != Ctrl_W
961 && ccline.cmdprompt == NULL && indent == 0)
962 {
963 /* In ex and debug mode it doesn't make sense to return. */
964 if (exmode_active
965#ifdef FEAT_EVAL
966 || ccline.cmdfirstc == '>'
967#endif
968 )
969 goto cmdline_not_changed;
970
971 vim_free(ccline.cmdbuff); /* no commandline to return */
972 ccline.cmdbuff = NULL;
973 if (!cmd_silent)
974 {
975#ifdef FEAT_RIGHTLEFT
976 if (cmdmsg_rl)
977 msg_col = Columns;
978 else
979#endif
980 msg_col = 0;
981 msg_putchar(' '); /* delete ':' */
982 }
983 redraw_cmdline = TRUE;
984 goto returncmd; /* back to cmd mode */
985 }
986 goto cmdline_changed;
987
988 case K_INS:
989 case K_KINS:
990#ifdef FEAT_FKMAP
991 /* if Farsi mode set, we are in reverse insert mode -
992 Do not change the mode */
993 if (cmd_fkmap)
994 beep_flush();
995 else
996#endif
997 ccline.overstrike = !ccline.overstrike;
998#ifdef CURSOR_SHAPE
999 ui_cursor_shape(); /* may show different cursor shape */
1000#endif
1001 goto cmdline_not_changed;
1002
1003 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001004 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 {
1006 /* ":lmap" mappings exists, toggle use of mappings. */
1007 State ^= LANGMAP;
1008#ifdef USE_IM_CONTROL
1009 im_set_active(FALSE); /* Disable input method */
1010#endif
1011 if (b_im_ptr != NULL)
1012 {
1013 if (State & LANGMAP)
1014 *b_im_ptr = B_IMODE_LMAP;
1015 else
1016 *b_im_ptr = B_IMODE_NONE;
1017 }
1018 }
1019#ifdef USE_IM_CONTROL
1020 else
1021 {
1022 /* There are no ":lmap" mappings, toggle IM. When
1023 * 'imdisable' is set don't try getting the status, it's
1024 * always off. */
1025 if ((p_imdisable && b_im_ptr != NULL)
1026 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1027 {
1028 im_set_active(FALSE); /* Disable input method */
1029 if (b_im_ptr != NULL)
1030 *b_im_ptr = B_IMODE_NONE;
1031 }
1032 else
1033 {
1034 im_set_active(TRUE); /* Enable input method */
1035 if (b_im_ptr != NULL)
1036 *b_im_ptr = B_IMODE_IM;
1037 }
1038 }
1039#endif
1040 if (b_im_ptr != NULL)
1041 {
1042 if (b_im_ptr == &curbuf->b_p_iminsert)
1043 set_iminsert_global();
1044 else
1045 set_imsearch_global();
1046 }
1047#ifdef CURSOR_SHAPE
1048 ui_cursor_shape(); /* may show different cursor shape */
1049#endif
1050#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1051 /* Show/unshow value of 'keymap' in status lines later. */
1052 status_redraw_curbuf();
1053#endif
1054 goto cmdline_not_changed;
1055
1056/* case '@': only in very old vi */
1057 case Ctrl_U:
1058 /* delete all characters left of the cursor */
1059 j = ccline.cmdpos;
1060 ccline.cmdlen -= j;
1061 i = ccline.cmdpos = 0;
1062 while (i < ccline.cmdlen)
1063 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1064 /* Truncate at the end, required for multi-byte chars. */
1065 ccline.cmdbuff[ccline.cmdlen] = NUL;
1066 redrawcmd();
1067 goto cmdline_changed;
1068
1069#ifdef FEAT_CLIPBOARD
1070 case Ctrl_Y:
1071 /* Copy the modeless selection, if there is one. */
1072 if (clip_star.state != SELECT_CLEARED)
1073 {
1074 if (clip_star.state == SELECT_DONE)
1075 clip_copy_modeless_selection(TRUE);
1076 goto cmdline_not_changed;
1077 }
1078 break;
1079#endif
1080
1081 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1082 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001083 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001084 * ":normal" runs out of characters. */
1085 if (exmode_active
1086#ifdef FEAT_EX_EXTRA
1087 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1088#endif
1089 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 goto cmdline_not_changed;
1091
1092 gotesc = TRUE; /* will free ccline.cmdbuff after
1093 putting it in history */
1094 goto returncmd; /* back to cmd mode */
1095
1096 case Ctrl_R: /* insert register */
1097#ifdef USE_ON_FLY_SCROLL
1098 dont_scroll = TRUE; /* disallow scrolling here */
1099#endif
1100 putcmdline('"', TRUE);
1101 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001102 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (i == Ctrl_O)
1104 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1105 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001106 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 --no_mapping;
1108#ifdef FEAT_EVAL
1109 /*
1110 * Insert the result of an expression.
1111 * Need to save the current command line, to be able to enter
1112 * a new one...
1113 */
1114 new_cmdpos = -1;
1115 if (c == '=')
1116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1118 {
1119 beep_flush();
1120 c = ESC;
1121 }
1122 else
1123 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001124 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001126 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 }
1129#endif
1130 if (c != ESC) /* use ESC to cancel inserting register */
1131 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001132 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001133
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001134#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001135 /* When there was a serious error abort getting the
1136 * command line. */
1137 if (aborting())
1138 {
1139 gotesc = TRUE; /* will free ccline.cmdbuff after
1140 putting it in history */
1141 goto returncmd; /* back to cmd mode */
1142 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 KeyTyped = FALSE; /* Don't do p_wc completion. */
1145#ifdef FEAT_EVAL
1146 if (new_cmdpos >= 0)
1147 {
1148 /* set_cmdline_pos() was used */
1149 if (new_cmdpos > ccline.cmdlen)
1150 ccline.cmdpos = ccline.cmdlen;
1151 else
1152 ccline.cmdpos = new_cmdpos;
1153 }
1154#endif
1155 }
1156 redrawcmd();
1157 goto cmdline_changed;
1158
1159 case Ctrl_D:
1160 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1161 break; /* Use ^D as normal char instead */
1162
1163 redrawcmd();
1164 continue; /* don't do incremental search now */
1165
1166 case K_RIGHT:
1167 case K_S_RIGHT:
1168 case K_C_RIGHT:
1169 do
1170 {
1171 if (ccline.cmdpos >= ccline.cmdlen)
1172 break;
1173 i = cmdline_charsize(ccline.cmdpos);
1174 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1175 break;
1176 ccline.cmdspos += i;
1177#ifdef FEAT_MBYTE
1178 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001179 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 + ccline.cmdpos);
1181 else
1182#endif
1183 ++ccline.cmdpos;
1184 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001185 while ((c == K_S_RIGHT || c == K_C_RIGHT
1186 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1188#ifdef FEAT_MBYTE
1189 if (has_mbyte)
1190 set_cmdspos_cursor();
1191#endif
1192 goto cmdline_not_changed;
1193
1194 case K_LEFT:
1195 case K_S_LEFT:
1196 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001197 if (ccline.cmdpos == 0)
1198 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 do
1200 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 --ccline.cmdpos;
1202#ifdef FEAT_MBYTE
1203 if (has_mbyte) /* move to first byte of char */
1204 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1205 ccline.cmdbuff + ccline.cmdpos);
1206#endif
1207 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1208 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001209 while (ccline.cmdpos > 0
1210 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001211 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1213#ifdef FEAT_MBYTE
1214 if (has_mbyte)
1215 set_cmdspos_cursor();
1216#endif
1217 goto cmdline_not_changed;
1218
1219 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001220 /* Ignore mouse event or ex_window() result. */
1221 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Bram Moolenaar4770d092006-01-12 23:22:24 +00001223#ifdef FEAT_GUI_W32
1224 /* On Win32 ignore <M-F4>, we get it when closing the window was
1225 * cancelled. */
1226 case K_F4:
1227 if (mod_mask == MOD_MASK_ALT)
1228 {
1229 redrawcmd(); /* somehow the cmdline is cleared */
1230 goto cmdline_not_changed;
1231 }
1232 break;
1233#endif
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#ifdef FEAT_MOUSE
1236 case K_MIDDLEDRAG:
1237 case K_MIDDLERELEASE:
1238 goto cmdline_not_changed; /* Ignore mouse */
1239
1240 case K_MIDDLEMOUSE:
1241# ifdef FEAT_GUI
1242 /* When GUI is active, also paste when 'mouse' is empty */
1243 if (!gui.in_use)
1244# endif
1245 if (!mouse_has(MOUSE_COMMAND))
1246 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001247# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001249 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001251# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001252 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 redrawcmd();
1254 goto cmdline_changed;
1255
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001256# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001258 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 redrawcmd();
1260 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001261# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
1263 case K_LEFTDRAG:
1264 case K_LEFTRELEASE:
1265 case K_RIGHTDRAG:
1266 case K_RIGHTRELEASE:
1267 /* Ignore drag and release events when the button-down wasn't
1268 * seen before. */
1269 if (ignore_drag_release)
1270 goto cmdline_not_changed;
1271 /* FALLTHROUGH */
1272 case K_LEFTMOUSE:
1273 case K_RIGHTMOUSE:
1274 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1275 ignore_drag_release = TRUE;
1276 else
1277 ignore_drag_release = FALSE;
1278# ifdef FEAT_GUI
1279 /* When GUI is active, also move when 'mouse' is empty */
1280 if (!gui.in_use)
1281# endif
1282 if (!mouse_has(MOUSE_COMMAND))
1283 goto cmdline_not_changed; /* Ignore mouse */
1284# ifdef FEAT_CLIPBOARD
1285 if (mouse_row < cmdline_row && clip_star.available)
1286 {
1287 int button, is_click, is_drag;
1288
1289 /*
1290 * Handle modeless selection.
1291 */
1292 button = get_mouse_button(KEY2TERMCAP1(c),
1293 &is_click, &is_drag);
1294 if (mouse_model_popup() && button == MOUSE_LEFT
1295 && (mod_mask & MOD_MASK_SHIFT))
1296 {
1297 /* Translate shift-left to right button. */
1298 button = MOUSE_RIGHT;
1299 mod_mask &= ~MOD_MASK_SHIFT;
1300 }
1301 clip_modeless(button, is_click, is_drag);
1302 goto cmdline_not_changed;
1303 }
1304# endif
1305
1306 set_cmdspos();
1307 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1308 ++ccline.cmdpos)
1309 {
1310 i = cmdline_charsize(ccline.cmdpos);
1311 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1312 && mouse_col < ccline.cmdspos % Columns + i)
1313 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001314# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 if (has_mbyte)
1316 {
1317 /* Count ">" for double-wide char that doesn't fit. */
1318 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001319 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 + ccline.cmdpos) - 1;
1321 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 ccline.cmdspos += i;
1324 }
1325 goto cmdline_not_changed;
1326
1327 /* Mouse scroll wheel: ignored here */
1328 case K_MOUSEDOWN:
1329 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001330 case K_MOUSELEFT:
1331 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 /* Alternate buttons ignored here */
1333 case K_X1MOUSE:
1334 case K_X1DRAG:
1335 case K_X1RELEASE:
1336 case K_X2MOUSE:
1337 case K_X2DRAG:
1338 case K_X2RELEASE:
1339 goto cmdline_not_changed;
1340
1341#endif /* FEAT_MOUSE */
1342
1343#ifdef FEAT_GUI
1344 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1345 case K_LEFTRELEASE_NM:
1346 goto cmdline_not_changed;
1347
1348 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001349 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
1351 gui_do_scroll();
1352 redrawcmd();
1353 }
1354 goto cmdline_not_changed;
1355
1356 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001357 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001359 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 redrawcmd();
1361 }
1362 goto cmdline_not_changed;
1363#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001364#ifdef FEAT_GUI_TABLINE
1365 case K_TABLINE:
1366 case K_TABMENU:
1367 /* Don't want to change any tabs here. Make sure the same tab
1368 * is still selected. */
1369 if (gui_use_tabline())
1370 gui_mch_set_curtab(tabpage_index(curtab));
1371 goto cmdline_not_changed;
1372#endif
1373
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 case K_SELECT: /* end of Select mode mapping - ignore */
1375 goto cmdline_not_changed;
1376
1377 case Ctrl_B: /* begin of command line */
1378 case K_HOME:
1379 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 case K_S_HOME:
1381 case K_C_HOME:
1382 ccline.cmdpos = 0;
1383 set_cmdspos();
1384 goto cmdline_not_changed;
1385
1386 case Ctrl_E: /* end of command line */
1387 case K_END:
1388 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 case K_S_END:
1390 case K_C_END:
1391 ccline.cmdpos = ccline.cmdlen;
1392 set_cmdspos_cursor();
1393 goto cmdline_not_changed;
1394
1395 case Ctrl_A: /* all matches */
1396 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1397 break;
1398 goto cmdline_changed;
1399
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001400 case Ctrl_L:
1401#ifdef FEAT_SEARCH_EXTRA
1402 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1403 {
1404 /* Add a character from under the cursor for 'incsearch' */
1405 if (did_incsearch
1406 && !equalpos(curwin->w_cursor, old_cursor))
1407 {
1408 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001409 /* If 'ignorecase' and 'smartcase' are set and the
1410 * command line has no uppercase characters, convert
1411 * the character to lowercase */
1412 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1413 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001414 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001415 {
1416 if (c == firstc || vim_strchr((char_u *)(
1417 p_magic ? "\\^$.*[" : "\\^$"), c)
1418 != NULL)
1419 {
1420 /* put a backslash before special characters */
1421 stuffcharReadbuff(c);
1422 c = '\\';
1423 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001424 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001425 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001426 }
1427 goto cmdline_not_changed;
1428 }
1429#endif
1430
1431 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1433 break;
1434 goto cmdline_changed;
1435
1436 case Ctrl_N: /* next match */
1437 case Ctrl_P: /* previous match */
1438 if (xpc.xp_numfiles > 0)
1439 {
1440 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1441 == FAIL)
1442 break;
1443 goto cmdline_changed;
1444 }
1445
1446#ifdef FEAT_CMDHIST
1447 case K_UP:
1448 case K_DOWN:
1449 case K_S_UP:
1450 case K_S_DOWN:
1451 case K_PAGEUP:
1452 case K_KPAGEUP:
1453 case K_PAGEDOWN:
1454 case K_KPAGEDOWN:
1455 if (hislen == 0 || firstc == NUL) /* no history */
1456 goto cmdline_not_changed;
1457
1458 i = hiscnt;
1459
1460 /* save current command string so it can be restored later */
1461 if (lookfor == NULL)
1462 {
1463 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1464 goto cmdline_not_changed;
1465 lookfor[ccline.cmdpos] = NUL;
1466 }
1467
1468 j = (int)STRLEN(lookfor);
1469 for (;;)
1470 {
1471 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001472 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001473 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 {
1475 if (hiscnt == hislen) /* first time */
1476 hiscnt = hisidx[histype];
1477 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1478 hiscnt = hislen - 1;
1479 else if (hiscnt != hisidx[histype] + 1)
1480 --hiscnt;
1481 else /* at top of list */
1482 {
1483 hiscnt = i;
1484 break;
1485 }
1486 }
1487 else /* one step forwards */
1488 {
1489 /* on last entry, clear the line */
1490 if (hiscnt == hisidx[histype])
1491 {
1492 hiscnt = hislen;
1493 break;
1494 }
1495
1496 /* not on a history line, nothing to do */
1497 if (hiscnt == hislen)
1498 break;
1499 if (hiscnt == hislen - 1) /* wrap around */
1500 hiscnt = 0;
1501 else
1502 ++hiscnt;
1503 }
1504 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1505 {
1506 hiscnt = i;
1507 break;
1508 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001509 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001510 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 || STRNCMP(history[histype][hiscnt].hisstr,
1512 lookfor, (size_t)j) == 0)
1513 break;
1514 }
1515
1516 if (hiscnt != i) /* jumped to other entry */
1517 {
1518 char_u *p;
1519 int len;
1520 int old_firstc;
1521
1522 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001523 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (hiscnt == hislen)
1525 p = lookfor; /* back to the old one */
1526 else
1527 p = history[histype][hiscnt].hisstr;
1528
1529 if (histype == HIST_SEARCH
1530 && p != lookfor
1531 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1532 {
1533 /* Correct for the separator character used when
1534 * adding the history entry vs the one used now.
1535 * First loop: count length.
1536 * Second loop: copy the characters. */
1537 for (i = 0; i <= 1; ++i)
1538 {
1539 len = 0;
1540 for (j = 0; p[j] != NUL; ++j)
1541 {
1542 /* Replace old sep with new sep, unless it is
1543 * escaped. */
1544 if (p[j] == old_firstc
1545 && (j == 0 || p[j - 1] != '\\'))
1546 {
1547 if (i > 0)
1548 ccline.cmdbuff[len] = firstc;
1549 }
1550 else
1551 {
1552 /* Escape new sep, unless it is already
1553 * escaped. */
1554 if (p[j] == firstc
1555 && (j == 0 || p[j - 1] != '\\'))
1556 {
1557 if (i > 0)
1558 ccline.cmdbuff[len] = '\\';
1559 ++len;
1560 }
1561 if (i > 0)
1562 ccline.cmdbuff[len] = p[j];
1563 }
1564 ++len;
1565 }
1566 if (i == 0)
1567 {
1568 alloc_cmdbuff(len);
1569 if (ccline.cmdbuff == NULL)
1570 goto returncmd;
1571 }
1572 }
1573 ccline.cmdbuff[len] = NUL;
1574 }
1575 else
1576 {
1577 alloc_cmdbuff((int)STRLEN(p));
1578 if (ccline.cmdbuff == NULL)
1579 goto returncmd;
1580 STRCPY(ccline.cmdbuff, p);
1581 }
1582
1583 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1584 redrawcmd();
1585 goto cmdline_changed;
1586 }
1587 beep_flush();
1588 goto cmdline_not_changed;
1589#endif
1590
1591 case Ctrl_V:
1592 case Ctrl_Q:
1593#ifdef FEAT_MOUSE
1594 ignore_drag_release = TRUE;
1595#endif
1596 putcmdline('^', TRUE);
1597 c = get_literal(); /* get next (two) character(s) */
1598 do_abbr = FALSE; /* don't do abbreviation now */
1599#ifdef FEAT_MBYTE
1600 /* may need to remove ^ when composing char was typed */
1601 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1602 {
1603 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1604 msg_putchar(' ');
1605 cursorcmd();
1606 }
1607#endif
1608 break;
1609
1610#ifdef FEAT_DIGRAPHS
1611 case Ctrl_K:
1612#ifdef FEAT_MOUSE
1613 ignore_drag_release = TRUE;
1614#endif
1615 putcmdline('?', TRUE);
1616#ifdef USE_ON_FLY_SCROLL
1617 dont_scroll = TRUE; /* disallow scrolling here */
1618#endif
1619 c = get_digraph(TRUE);
1620 if (c != NUL)
1621 break;
1622
1623 redrawcmd();
1624 goto cmdline_not_changed;
1625#endif /* FEAT_DIGRAPHS */
1626
1627#ifdef FEAT_RIGHTLEFT
1628 case Ctrl__: /* CTRL-_: switch language mode */
1629 if (!p_ari)
1630 break;
1631#ifdef FEAT_FKMAP
1632 if (p_altkeymap)
1633 {
1634 cmd_fkmap = !cmd_fkmap;
1635 if (cmd_fkmap) /* in Farsi always in Insert mode */
1636 ccline.overstrike = FALSE;
1637 }
1638 else /* Hebrew is default */
1639#endif
1640 cmd_hkmap = !cmd_hkmap;
1641 goto cmdline_not_changed;
1642#endif
1643
1644 default:
1645#ifdef UNIX
1646 if (c == intr_char)
1647 {
1648 gotesc = TRUE; /* will free ccline.cmdbuff after
1649 putting it in history */
1650 goto returncmd; /* back to Normal mode */
1651 }
1652#endif
1653 /*
1654 * Normal character with no special meaning. Just set mod_mask
1655 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1656 * the string <S-Space>. This should only happen after ^V.
1657 */
1658 if (!IS_SPECIAL(c))
1659 mod_mask = 0x0;
1660 break;
1661 }
1662 /*
1663 * End of switch on command line character.
1664 * We come here if we have a normal character.
1665 */
1666
1667 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1668#ifdef FEAT_MBYTE
1669 /* Add ABBR_OFF for characters above 0x100, this is
1670 * what check_abbr() expects. */
1671 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1672#endif
1673 c))
1674 goto cmdline_changed;
1675
1676 /*
1677 * put the character in the command line
1678 */
1679 if (IS_SPECIAL(c) || mod_mask != 0)
1680 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1681 else
1682 {
1683#ifdef FEAT_MBYTE
1684 if (has_mbyte)
1685 {
1686 j = (*mb_char2bytes)(c, IObuff);
1687 IObuff[j] = NUL; /* exclude composing chars */
1688 put_on_cmdline(IObuff, j, TRUE);
1689 }
1690 else
1691#endif
1692 {
1693 IObuff[0] = c;
1694 put_on_cmdline(IObuff, 1, TRUE);
1695 }
1696 }
1697 goto cmdline_changed;
1698
1699/*
1700 * This part implements incremental searches for "/" and "?"
1701 * Jump to cmdline_not_changed when a character has been read but the command
1702 * line did not change. Then we only search and redraw if something changed in
1703 * the past.
1704 * Jump to cmdline_changed when the command line did change.
1705 * (Sorry for the goto's, I know it is ugly).
1706 */
1707cmdline_not_changed:
1708#ifdef FEAT_SEARCH_EXTRA
1709 if (!incsearch_postponed)
1710 continue;
1711#endif
1712
1713cmdline_changed:
1714#ifdef FEAT_SEARCH_EXTRA
1715 /*
1716 * 'incsearch' highlighting.
1717 */
1718 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1719 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001720 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001721#ifdef FEAT_RELTIME
1722 proftime_T tm;
1723#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 /* if there is a character waiting, search and redraw later */
1726 if (char_avail())
1727 {
1728 incsearch_postponed = TRUE;
1729 continue;
1730 }
1731 incsearch_postponed = FALSE;
1732 curwin->w_cursor = old_cursor; /* start at old position */
1733
1734 /* If there is no command line, don't do anything */
1735 if (ccline.cmdlen == 0)
1736 i = 0;
1737 else
1738 {
1739 cursor_off(); /* so the user knows we're busy */
1740 out_flush();
1741 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001742#ifdef FEAT_RELTIME
1743 /* Set the time limit to half a second. */
1744 profile_setlimit(500L, &tm);
1745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001747 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1748#ifdef FEAT_RELTIME
1749 &tm
1750#else
1751 NULL
1752#endif
1753 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 --emsg_off;
1755 /* if interrupted while searching, behave like it failed */
1756 if (got_int)
1757 {
1758 (void)vpeekc(); /* remove <C-C> from input stream */
1759 got_int = FALSE; /* don't abandon the command line */
1760 i = 0;
1761 }
1762 else if (char_avail())
1763 /* cancelled searching because a char was typed */
1764 incsearch_postponed = TRUE;
1765 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001766 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 highlight_match = TRUE; /* highlight position */
1768 else
1769 highlight_match = FALSE; /* remove highlight */
1770
1771 /* first restore the old curwin values, so the screen is
1772 * positioned in the same way as the actual search command */
1773 curwin->w_leftcol = old_leftcol;
1774 curwin->w_topline = old_topline;
1775# ifdef FEAT_DIFF
1776 curwin->w_topfill = old_topfill;
1777# endif
1778 curwin->w_botline = old_botline;
1779 changed_cline_bef_curs();
1780 update_topline();
1781
1782 if (i != 0)
1783 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001784 pos_T save_pos = curwin->w_cursor;
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001787 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 * moves the whole match onto the screen when 'nowrap' is set.
1789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 curwin->w_cursor.lnum += search_match_lines;
1791 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001792 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1793 {
1794 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1795 coladvance((colnr_T)MAXCOL);
1796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001798 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001799 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001801 else
1802 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001805# ifdef FEAT_WINDOWS
1806 /* May redraw the status line to show the cursor position. */
1807 if (p_ru && curwin->w_status_height > 0)
1808 curwin->w_redr_status = TRUE;
1809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001811 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001812 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001813 restore_cmdline(&save_ccline);
1814
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001815 /* Leave it at the end to make CTRL-R CTRL-W work. */
1816 if (i != 0)
1817 curwin->w_cursor = end_pos;
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 msg_starthere();
1820 redrawcmdline();
1821 did_incsearch = TRUE;
1822 }
1823#else /* FEAT_SEARCH_EXTRA */
1824 ;
1825#endif
1826
1827#ifdef FEAT_RIGHTLEFT
1828 if (cmdmsg_rl
1829# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001830 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831# endif
1832 )
1833 /* Always redraw the whole command line to fix shaping and
1834 * right-left typing. Not efficient, but it works. */
1835 redrawcmd();
1836#endif
1837 }
1838
1839returncmd:
1840
1841#ifdef FEAT_RIGHTLEFT
1842 cmdmsg_rl = FALSE;
1843#endif
1844
1845#ifdef FEAT_FKMAP
1846 cmd_fkmap = 0;
1847#endif
1848
1849 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001850 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852#ifdef FEAT_SEARCH_EXTRA
1853 if (did_incsearch)
1854 {
1855 curwin->w_cursor = old_cursor;
1856 curwin->w_curswant = old_curswant;
1857 curwin->w_leftcol = old_leftcol;
1858 curwin->w_topline = old_topline;
1859# ifdef FEAT_DIFF
1860 curwin->w_topfill = old_topfill;
1861# endif
1862 curwin->w_botline = old_botline;
1863 highlight_match = FALSE;
1864 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001865 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
1867#endif
1868
1869 if (ccline.cmdbuff != NULL)
1870 {
1871 /*
1872 * Put line in history buffer (":" and "=" only when it was typed).
1873 */
1874#ifdef FEAT_CMDHIST
1875 if (ccline.cmdlen && firstc != NUL
1876 && (some_key_typed || histype == HIST_SEARCH))
1877 {
1878 add_to_history(histype, ccline.cmdbuff, TRUE,
1879 histype == HIST_SEARCH ? firstc : NUL);
1880 if (firstc == ':')
1881 {
1882 vim_free(new_last_cmdline);
1883 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1884 }
1885 }
1886#endif
1887
1888 if (gotesc) /* abandon command line */
1889 {
1890 vim_free(ccline.cmdbuff);
1891 ccline.cmdbuff = NULL;
1892 if (msg_scrolled == 0)
1893 compute_cmdrow();
1894 MSG("");
1895 redraw_cmdline = TRUE;
1896 }
1897 }
1898
1899 /*
1900 * If the screen was shifted up, redraw the whole screen (later).
1901 * If the line is too long, clear it, so ruler and shown command do
1902 * not get printed in the middle of it.
1903 */
1904 msg_check();
1905 msg_scroll = save_msg_scroll;
1906 redir_off = FALSE;
1907
1908 /* When the command line was typed, no need for a wait-return prompt. */
1909 if (some_key_typed)
1910 need_wait_return = FALSE;
1911
1912 State = save_State;
1913#ifdef USE_IM_CONTROL
1914 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1915 im_save_status(b_im_ptr);
1916 im_set_active(FALSE);
1917#endif
1918#ifdef FEAT_MOUSE
1919 setmouse();
1920#endif
1921#ifdef CURSOR_SHAPE
1922 ui_cursor_shape(); /* may show different cursor shape */
1923#endif
1924
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001925 {
1926 char_u *p = ccline.cmdbuff;
1927
1928 /* Make ccline empty, getcmdline() may try to use it. */
1929 ccline.cmdbuff = NULL;
1930 return p;
1931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932}
1933
1934#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1935/*
1936 * Get a command line with a prompt.
1937 * This is prepared to be called recursively from getcmdline() (e.g. by
1938 * f_input() when evaluating an expression from CTRL-R =).
1939 * Returns the command line in allocated memory, or NULL.
1940 */
1941 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001942getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 int firstc;
1944 char_u *prompt; /* command line prompt */
1945 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001946 int xp_context; /* type of expansion */
1947 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 char_u *s;
1950 struct cmdline_info save_ccline;
1951 int msg_col_save = msg_col;
1952
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001953 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 ccline.cmdprompt = prompt;
1955 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001956# ifdef FEAT_EVAL
1957 ccline.xp_context = xp_context;
1958 ccline.xp_arg = xp_arg;
1959 ccline.input_fn = (firstc == '@');
1960# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001962 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 /* Restore msg_col, the prompt from input() may have changed it. */
1964 msg_col = msg_col_save;
1965
1966 return s;
1967}
1968#endif
1969
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001970/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001971 * Return TRUE when the text must not be changed and we can't switch to
1972 * another window or buffer. Used when editing the command line, evaluating
1973 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001974 */
1975 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001976text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001977{
1978#ifdef FEAT_CMDWIN
1979 if (cmdwin_type != 0)
1980 return TRUE;
1981#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001982 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001983}
1984
1985/*
1986 * Give an error message for a command that isn't allowed while the cmdline
1987 * window is open or editing the cmdline in another way.
1988 */
1989 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001990text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001991{
1992#ifdef FEAT_CMDWIN
1993 if (cmdwin_type != 0)
1994 EMSG(_(e_cmdwin));
1995 else
1996#endif
1997 EMSG(_(e_secure));
1998}
1999
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002000#if defined(FEAT_AUTOCMD) || defined(PROTO)
2001/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002002 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2003 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002004 */
2005 int
2006curbuf_locked()
2007{
2008 if (curbuf_lock > 0)
2009 {
2010 EMSG(_("E788: Not allowed to edit another buffer now"));
2011 return TRUE;
2012 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002013 return allbuf_locked();
2014}
2015
2016/*
2017 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2018 * message.
2019 */
2020 int
2021allbuf_locked()
2022{
2023 if (allbuf_lock > 0)
2024 {
2025 EMSG(_("E811: Not allowed to change buffer information now"));
2026 return TRUE;
2027 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002028 return FALSE;
2029}
2030#endif
2031
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 static int
2033cmdline_charsize(idx)
2034 int idx;
2035{
2036#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2037 if (cmdline_star > 0) /* showing '*', always 1 position */
2038 return 1;
2039#endif
2040 return ptr2cells(ccline.cmdbuff + idx);
2041}
2042
2043/*
2044 * Compute the offset of the cursor on the command line for the prompt and
2045 * indent.
2046 */
2047 static void
2048set_cmdspos()
2049{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002050 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 ccline.cmdspos = 1 + ccline.cmdindent;
2052 else
2053 ccline.cmdspos = 0 + ccline.cmdindent;
2054}
2055
2056/*
2057 * Compute the screen position for the cursor on the command line.
2058 */
2059 static void
2060set_cmdspos_cursor()
2061{
2062 int i, m, c;
2063
2064 set_cmdspos();
2065 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002068 if (m < 0) /* overflow, Columns or Rows at weird value */
2069 m = MAXCOL;
2070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 else
2072 m = MAXCOL;
2073 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2074 {
2075 c = cmdline_charsize(i);
2076#ifdef FEAT_MBYTE
2077 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2078 if (has_mbyte)
2079 correct_cmdspos(i, c);
2080#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002081 /* If the cmdline doesn't fit, show cursor on last visible char.
2082 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 if ((ccline.cmdspos += c) >= m)
2084 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 ccline.cmdspos -= c;
2086 break;
2087 }
2088#ifdef FEAT_MBYTE
2089 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002090 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091#endif
2092 }
2093}
2094
2095#ifdef FEAT_MBYTE
2096/*
2097 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2098 * character that doesn't fit, so that a ">" must be displayed.
2099 */
2100 static void
2101correct_cmdspos(idx, cells)
2102 int idx;
2103 int cells;
2104{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002105 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2107 && ccline.cmdspos % Columns + cells > Columns)
2108 ccline.cmdspos++;
2109}
2110#endif
2111
2112/*
2113 * Get an Ex command line for the ":" command.
2114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002116getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002118 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 int indent; /* indent for inside conditionals */
2120{
2121 /* When executing a register, remove ':' that's in front of each line. */
2122 if (exec_from_reg && vpeekc() == ':')
2123 (void)vgetc();
2124 return getcmdline(c, 1L, indent);
2125}
2126
2127/*
2128 * Get an Ex command line for Ex mode.
2129 * In Ex mode we only use the OS supplied line editing features and no
2130 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002131 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002134getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002135 int promptc; /* normally ':', NUL for ":append" and '?' for
2136 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002137 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 int indent; /* indent for inside conditionals */
2139{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002140 garray_T line_ga;
2141 char_u *pend;
2142 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002143 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002144 int escaped = FALSE; /* CTRL-V typed */
2145 int vcol = 0;
2146 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002147 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148
2149 /* Switch cursor on now. This avoids that it happens after the "\n", which
2150 * confuses the system function that computes tabstops. */
2151 cursor_on();
2152
2153 /* always start in column 0; write a newline if necessary */
2154 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002155 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002157 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002159 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002160 if (p_prompt)
2161 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 while (indent-- > 0)
2163 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166
2167 ga_init2(&line_ga, 1, 30);
2168
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002169 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002170 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002171 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002172 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002173 while (indent >= 8)
2174 {
2175 ga_append(&line_ga, TAB);
2176 msg_puts((char_u *)" ");
2177 indent -= 8;
2178 }
2179 while (indent-- > 0)
2180 {
2181 ga_append(&line_ga, ' ');
2182 msg_putchar(' ');
2183 }
2184 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002185 ++no_mapping;
2186 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002187
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 /*
2189 * Get the line, one character at a time.
2190 */
2191 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002192 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 if (ga_grow(&line_ga, 40) == FAIL)
2195 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002197 /* Get one character at a time. Don't use inchar(), it can't handle
2198 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002199 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002200 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
2202 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002203 * Handle line editing.
2204 * Previously this was left to the system, putting the terminal in
2205 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002207 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002209 msg_putchar('\n');
2210 break;
2211 }
2212
2213 if (!escaped)
2214 {
2215 /* CR typed means "enter", which is NL */
2216 if (c1 == '\r')
2217 c1 = '\n';
2218
2219 if (c1 == BS || c1 == K_BS
2220 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002222 if (line_ga.ga_len > 0)
2223 {
2224 --line_ga.ga_len;
2225 goto redraw;
2226 }
2227 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002230 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232 msg_col = startcol;
2233 msg_clr_eos();
2234 line_ga.ga_len = 0;
2235 continue;
2236 }
2237
2238 if (c1 == Ctrl_T)
2239 {
2240 p = (char_u *)line_ga.ga_data;
2241 p[line_ga.ga_len] = NUL;
2242 indent = get_indent_str(p, 8);
2243 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2244add_indent:
2245 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002247 char_u *s = skipwhite(p);
2248
2249 ga_grow(&line_ga, 1);
2250 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2251 *s = ' ';
2252 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002254redraw:
2255 /* redraw the line */
2256 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002257 vcol = 0;
2258 for (p = (char_u *)line_ga.ga_data;
2259 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002261 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002263 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002265 msg_putchar(' ');
2266 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002268 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002270 msg_outtrans_len(p, 1);
2271 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002274 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002275 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002276 continue;
2277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002279 if (c1 == Ctrl_D)
2280 {
2281 /* Delete one shiftwidth. */
2282 p = (char_u *)line_ga.ga_data;
2283 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002285 if (prev_char == '^')
2286 ex_keep_indent = TRUE;
2287 indent = 0;
2288 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290 else
2291 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002292 p[line_ga.ga_len] = NUL;
2293 indent = get_indent_str(p, 8);
2294 --indent;
2295 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002297 while (get_indent_str(p, 8) > indent)
2298 {
2299 char_u *s = skipwhite(p);
2300
2301 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2302 --line_ga.ga_len;
2303 }
2304 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306
2307 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2308 {
2309 escaped = TRUE;
2310 continue;
2311 }
2312
2313 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2314 if (IS_SPECIAL(c1))
2315 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002317
2318 if (IS_SPECIAL(c1))
2319 c1 = '?';
2320 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002321 if (c1 == '\n')
2322 msg_putchar('\n');
2323 else if (c1 == TAB)
2324 {
2325 /* Don't use chartabsize(), 'ts' can be different */
2326 do
2327 {
2328 msg_putchar(' ');
2329 } while (++vcol % 8);
2330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002333 msg_outtrans_len(
2334 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2335 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002337 ++line_ga.ga_len;
2338 escaped = FALSE;
2339
2340 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002341 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342
2343 /* we are done when a NL is entered, but not when it comes after a
2344 * backslash */
2345 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2346 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002349 --pend;
2350 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002351 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353 }
2354
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002355 --no_mapping;
2356 --allow_keys;
2357
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 /* make following messages go to the next line */
2359 msg_didout = FALSE;
2360 msg_col = 0;
2361 if (msg_row < Rows - 1)
2362 ++msg_row;
2363 emsg_on_display = FALSE; /* don't want ui_delay() */
2364
2365 if (got_int)
2366 ga_clear(&line_ga);
2367
2368 return (char_u *)line_ga.ga_data;
2369}
2370
Bram Moolenaare344bea2005-09-01 20:46:49 +00002371# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2372 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373/*
2374 * Return TRUE if ccline.overstrike is on.
2375 */
2376 int
2377cmdline_overstrike()
2378{
2379 return ccline.overstrike;
2380}
2381
2382/*
2383 * Return TRUE if the cursor is at the end of the cmdline.
2384 */
2385 int
2386cmdline_at_end()
2387{
2388 return (ccline.cmdpos >= ccline.cmdlen);
2389}
2390#endif
2391
Bram Moolenaar9372a112005-12-06 19:59:18 +00002392#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393/*
2394 * Return the virtual column number at the current cursor position.
2395 * This is used by the IM code to obtain the start of the preedit string.
2396 */
2397 colnr_T
2398cmdline_getvcol_cursor()
2399{
2400 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2401 return MAXCOL;
2402
2403# ifdef FEAT_MBYTE
2404 if (has_mbyte)
2405 {
2406 colnr_T col;
2407 int i = 0;
2408
2409 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002410 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
2412 return col;
2413 }
2414 else
2415# endif
2416 return ccline.cmdpos;
2417}
2418#endif
2419
2420#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2421/*
2422 * If part of the command line is an IM preedit string, redraw it with
2423 * IM feedback attributes. The cursor position is restored after drawing.
2424 */
2425 static void
2426redrawcmd_preedit()
2427{
2428 if ((State & CMDLINE)
2429 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002430 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 && !p_imdisable
2432 && im_is_preediting())
2433 {
2434 int cmdpos = 0;
2435 int cmdspos;
2436 int old_row;
2437 int old_col;
2438 colnr_T col;
2439
2440 old_row = msg_row;
2441 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002442 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444# ifdef FEAT_MBYTE
2445 if (has_mbyte)
2446 {
2447 for (col = 0; col < preedit_start_col
2448 && cmdpos < ccline.cmdlen; ++col)
2449 {
2450 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002451 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453 }
2454 else
2455# endif
2456 {
2457 cmdspos += preedit_start_col;
2458 cmdpos += preedit_start_col;
2459 }
2460
2461 msg_row = cmdline_row + (cmdspos / (int)Columns);
2462 msg_col = cmdspos % (int)Columns;
2463 if (msg_row >= Rows)
2464 msg_row = Rows - 1;
2465
2466 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2467 {
2468 int char_len;
2469 int char_attr;
2470
2471 char_attr = im_get_feedback_attr(col);
2472 if (char_attr < 0)
2473 break; /* end of preedit string */
2474
2475# ifdef FEAT_MBYTE
2476 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002477 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 else
2479# endif
2480 char_len = 1;
2481
2482 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2483 cmdpos += char_len;
2484 }
2485
2486 msg_row = old_row;
2487 msg_col = old_col;
2488 }
2489}
2490#endif /* FEAT_XIM && FEAT_GUI_GTK */
2491
2492/*
2493 * Allocate a new command line buffer.
2494 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2495 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2496 */
2497 static void
2498alloc_cmdbuff(len)
2499 int len;
2500{
2501 /*
2502 * give some extra space to avoid having to allocate all the time
2503 */
2504 if (len < 80)
2505 len = 100;
2506 else
2507 len += 20;
2508
2509 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2510 ccline.cmdbufflen = len;
2511}
2512
2513/*
2514 * Re-allocate the command line to length len + something extra.
2515 * return FAIL for failure, OK otherwise
2516 */
2517 static int
2518realloc_cmdbuff(len)
2519 int len;
2520{
2521 char_u *p;
2522
2523 p = ccline.cmdbuff;
2524 alloc_cmdbuff(len); /* will get some more */
2525 if (ccline.cmdbuff == NULL) /* out of memory */
2526 {
2527 ccline.cmdbuff = p; /* keep the old one */
2528 return FAIL;
2529 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002530 /* There isn't always a NUL after the command, but it may need to be
2531 * there, thus copy up to the NUL and add a NUL. */
2532 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2533 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002535
2536 if (ccline.xpc != NULL
2537 && ccline.xpc->xp_pattern != NULL
2538 && ccline.xpc->xp_context != EXPAND_NOTHING
2539 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2540 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002541 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002542
2543 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2544 * to point into the newly allocated memory. */
2545 if (i >= 0 && i <= ccline.cmdlen)
2546 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2547 }
2548
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 return OK;
2550}
2551
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002552#if defined(FEAT_ARABIC) || defined(PROTO)
2553static char_u *arshape_buf = NULL;
2554
2555# if defined(EXITFREE) || defined(PROTO)
2556 void
2557free_cmdline_buf()
2558{
2559 vim_free(arshape_buf);
2560}
2561# endif
2562#endif
2563
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564/*
2565 * Draw part of the cmdline at the current cursor position. But draw stars
2566 * when cmdline_star is TRUE.
2567 */
2568 static void
2569draw_cmdline(start, len)
2570 int start;
2571 int len;
2572{
2573#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2574 int i;
2575
2576 if (cmdline_star > 0)
2577 for (i = 0; i < len; ++i)
2578 {
2579 msg_putchar('*');
2580# ifdef FEAT_MBYTE
2581 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002582 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583# endif
2584 }
2585 else
2586#endif
2587#ifdef FEAT_ARABIC
2588 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 static int buflen = 0;
2591 char_u *p;
2592 int j;
2593 int newlen = 0;
2594 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002595 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 int prev_c = 0;
2597 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002598 int u8c;
2599 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
2602 /*
2603 * Do arabic shaping into a temporary buffer. This is very
2604 * inefficient!
2605 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002606 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 {
2608 /* Re-allocate the buffer. We keep it around to avoid a lot of
2609 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002610 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002611 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002612 arshape_buf = alloc(buflen);
2613 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 return; /* out of memory */
2615 }
2616
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002617 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2618 {
2619 /* Prepend a space to draw the leading composing char on. */
2620 arshape_buf[0] = ' ';
2621 newlen = 1;
2622 }
2623
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 for (j = start; j < start + len; j += mb_l)
2625 {
2626 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002627 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002628 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 if (ARABIC_CHAR(u8c))
2630 {
2631 /* Do Arabic shaping. */
2632 if (cmdmsg_rl)
2633 {
2634 /* displaying from right to left */
2635 pc = prev_c;
2636 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002637 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (j + mb_l >= start + len)
2639 nc = NUL;
2640 else
2641 nc = utf_ptr2char(p + mb_l);
2642 }
2643 else
2644 {
2645 /* displaying from left to right */
2646 if (j + mb_l >= start + len)
2647 pc = NUL;
2648 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002649 {
2650 int pcc[MAX_MCO];
2651
2652 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002654 pc1 = pcc[0];
2655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 nc = prev_c;
2657 }
2658 prev_c = u8c;
2659
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002660 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002662 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002663 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002665 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2666 if (u8cc[1] != 0)
2667 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002668 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 }
2670 }
2671 else
2672 {
2673 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002674 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 newlen += mb_l;
2676 }
2677 }
2678
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002679 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 }
2681 else
2682#endif
2683 msg_outtrans_len(ccline.cmdbuff + start, len);
2684}
2685
2686/*
2687 * Put a character on the command line. Shifts the following text to the
2688 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2689 * "c" must be printable (fit in one display cell)!
2690 */
2691 void
2692putcmdline(c, shift)
2693 int c;
2694 int shift;
2695{
2696 if (cmd_silent)
2697 return;
2698 msg_no_more = TRUE;
2699 msg_putchar(c);
2700 if (shift)
2701 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2702 msg_no_more = FALSE;
2703 cursorcmd();
2704}
2705
2706/*
2707 * Undo a putcmdline(c, FALSE).
2708 */
2709 void
2710unputcmdline()
2711{
2712 if (cmd_silent)
2713 return;
2714 msg_no_more = TRUE;
2715 if (ccline.cmdlen == ccline.cmdpos)
2716 msg_putchar(' ');
2717 else
2718 draw_cmdline(ccline.cmdpos, 1);
2719 msg_no_more = FALSE;
2720 cursorcmd();
2721}
2722
2723/*
2724 * Put the given string, of the given length, onto the command line.
2725 * If len is -1, then STRLEN() is used to calculate the length.
2726 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2727 * part will be redrawn, otherwise it will not. If this function is called
2728 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2729 * called afterwards.
2730 */
2731 int
2732put_on_cmdline(str, len, redraw)
2733 char_u *str;
2734 int len;
2735 int redraw;
2736{
2737 int retval;
2738 int i;
2739 int m;
2740 int c;
2741
2742 if (len < 0)
2743 len = (int)STRLEN(str);
2744
2745 /* Check if ccline.cmdbuff needs to be longer */
2746 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2747 retval = realloc_cmdbuff(ccline.cmdlen + len);
2748 else
2749 retval = OK;
2750 if (retval == OK)
2751 {
2752 if (!ccline.overstrike)
2753 {
2754 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2755 ccline.cmdbuff + ccline.cmdpos,
2756 (size_t)(ccline.cmdlen - ccline.cmdpos));
2757 ccline.cmdlen += len;
2758 }
2759 else
2760 {
2761#ifdef FEAT_MBYTE
2762 if (has_mbyte)
2763 {
2764 /* Count nr of characters in the new string. */
2765 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002766 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 ++m;
2768 /* Count nr of bytes in cmdline that are overwritten by these
2769 * characters. */
2770 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002771 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 --m;
2773 if (i < ccline.cmdlen)
2774 {
2775 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2776 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2777 ccline.cmdlen += ccline.cmdpos + len - i;
2778 }
2779 else
2780 ccline.cmdlen = ccline.cmdpos + len;
2781 }
2782 else
2783#endif
2784 if (ccline.cmdpos + len > ccline.cmdlen)
2785 ccline.cmdlen = ccline.cmdpos + len;
2786 }
2787 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2788 ccline.cmdbuff[ccline.cmdlen] = NUL;
2789
2790#ifdef FEAT_MBYTE
2791 if (enc_utf8)
2792 {
2793 /* When the inserted text starts with a composing character,
2794 * backup to the character before it. There could be two of them.
2795 */
2796 i = 0;
2797 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2798 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2799 {
2800 i = (*mb_head_off)(ccline.cmdbuff,
2801 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2802 ccline.cmdpos -= i;
2803 len += i;
2804 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2805 }
2806# ifdef FEAT_ARABIC
2807 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2808 {
2809 /* Check the previous character for Arabic combining pair. */
2810 i = (*mb_head_off)(ccline.cmdbuff,
2811 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2812 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2813 + ccline.cmdpos - i), c))
2814 {
2815 ccline.cmdpos -= i;
2816 len += i;
2817 }
2818 else
2819 i = 0;
2820 }
2821# endif
2822 if (i != 0)
2823 {
2824 /* Also backup the cursor position. */
2825 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2826 ccline.cmdspos -= i;
2827 msg_col -= i;
2828 if (msg_col < 0)
2829 {
2830 msg_col += Columns;
2831 --msg_row;
2832 }
2833 }
2834 }
2835#endif
2836
2837 if (redraw && !cmd_silent)
2838 {
2839 msg_no_more = TRUE;
2840 i = cmdline_row;
2841 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2842 /* Avoid clearing the rest of the line too often. */
2843 if (cmdline_row != i || ccline.overstrike)
2844 msg_clr_eos();
2845 msg_no_more = FALSE;
2846 }
2847#ifdef FEAT_FKMAP
2848 /*
2849 * If we are in Farsi command mode, the character input must be in
2850 * Insert mode. So do not advance the cmdpos.
2851 */
2852 if (!cmd_fkmap)
2853#endif
2854 {
2855 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002858 if (m < 0) /* overflow, Columns or Rows at weird value */
2859 m = MAXCOL;
2860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 else
2862 m = MAXCOL;
2863 for (i = 0; i < len; ++i)
2864 {
2865 c = cmdline_charsize(ccline.cmdpos);
2866#ifdef FEAT_MBYTE
2867 /* count ">" for a double-wide char that doesn't fit. */
2868 if (has_mbyte)
2869 correct_cmdspos(ccline.cmdpos, c);
2870#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002871 /* Stop cursor at the end of the screen, but do increment the
2872 * insert position, so that entering a very long command
2873 * works, even though you can't see it. */
2874 if (ccline.cmdspos + c < m)
2875 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876#ifdef FEAT_MBYTE
2877 if (has_mbyte)
2878 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002879 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 if (c > len - i - 1)
2881 c = len - i - 1;
2882 ccline.cmdpos += c;
2883 i += c;
2884 }
2885#endif
2886 ++ccline.cmdpos;
2887 }
2888 }
2889 }
2890 if (redraw)
2891 msg_check();
2892 return retval;
2893}
2894
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002895static struct cmdline_info prev_ccline;
2896static int prev_ccline_used = FALSE;
2897
2898/*
2899 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2900 * and overwrite it. But get_cmdline_str() may need it, thus make it
2901 * available globally in prev_ccline.
2902 */
2903 static void
2904save_cmdline(ccp)
2905 struct cmdline_info *ccp;
2906{
2907 if (!prev_ccline_used)
2908 {
2909 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2910 prev_ccline_used = TRUE;
2911 }
2912 *ccp = prev_ccline;
2913 prev_ccline = ccline;
2914 ccline.cmdbuff = NULL;
2915 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002916 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002917}
2918
2919/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002920 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002921 */
2922 static void
2923restore_cmdline(ccp)
2924 struct cmdline_info *ccp;
2925{
2926 ccline = prev_ccline;
2927 prev_ccline = *ccp;
2928}
2929
Bram Moolenaar5a305422006-04-28 22:38:25 +00002930#if defined(FEAT_EVAL) || defined(PROTO)
2931/*
2932 * Save the command line into allocated memory. Returns a pointer to be
2933 * passed to restore_cmdline_alloc() later.
2934 * Returns NULL when failed.
2935 */
2936 char_u *
2937save_cmdline_alloc()
2938{
2939 struct cmdline_info *p;
2940
2941 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2942 if (p != NULL)
2943 save_cmdline(p);
2944 return (char_u *)p;
2945}
2946
2947/*
2948 * Restore the command line from the return value of save_cmdline_alloc().
2949 */
2950 void
2951restore_cmdline_alloc(p)
2952 char_u *p;
2953{
2954 if (p != NULL)
2955 {
2956 restore_cmdline((struct cmdline_info *)p);
2957 vim_free(p);
2958 }
2959}
2960#endif
2961
Bram Moolenaar8299df92004-07-10 09:47:34 +00002962/*
2963 * paste a yank register into the command line.
2964 * used by CTRL-R command in command-line mode
2965 * insert_reg() can't be used here, because special characters from the
2966 * register contents will be interpreted as commands.
2967 *
2968 * return FAIL for failure, OK otherwise
2969 */
2970 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002971cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00002972 int regname;
2973 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002974 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00002975{
2976 long i;
2977 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002978 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002979 int allocated;
2980 struct cmdline_info save_ccline;
2981
2982 /* check for valid regname; also accept special characters for CTRL-R in
2983 * the command line */
2984 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2985 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2986 return FAIL;
2987
2988 /* A register containing CTRL-R can cause an endless loop. Allow using
2989 * CTRL-C to break the loop. */
2990 line_breakcheck();
2991 if (got_int)
2992 return FAIL;
2993
2994#ifdef FEAT_CLIPBOARD
2995 regname = may_get_selection(regname);
2996#endif
2997
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002998 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002999 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003000 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003001 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003002 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003003 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003004 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003005
3006 if (i)
3007 {
3008 /* Got the value of a special register in "arg". */
3009 if (arg == NULL)
3010 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003011
3012 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3013 * part of the word. */
3014 p = arg;
3015 if (p_is && regname == Ctrl_W)
3016 {
3017 char_u *w;
3018 int len;
3019
3020 /* Locate start of last word in the cmd buffer. */
3021 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
3022 {
3023#ifdef FEAT_MBYTE
3024 if (has_mbyte)
3025 {
3026 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3027 if (!vim_iswordc(mb_ptr2char(w - len)))
3028 break;
3029 w -= len;
3030 }
3031 else
3032#endif
3033 {
3034 if (!vim_iswordc(w[-1]))
3035 break;
3036 --w;
3037 }
3038 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003039 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003040 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3041 p += len;
3042 }
3043
3044 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003045 if (allocated)
3046 vim_free(arg);
3047 return OK;
3048 }
3049
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003050 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003051}
3052
3053/*
3054 * Put a string on the command line.
3055 * When "literally" is TRUE, insert literally.
3056 * When "literally" is FALSE, insert as typed, but don't leave the command
3057 * line.
3058 */
3059 void
3060cmdline_paste_str(s, literally)
3061 char_u *s;
3062 int literally;
3063{
3064 int c, cv;
3065
3066 if (literally)
3067 put_on_cmdline(s, -1, TRUE);
3068 else
3069 while (*s != NUL)
3070 {
3071 cv = *s;
3072 if (cv == Ctrl_V && s[1])
3073 ++s;
3074#ifdef FEAT_MBYTE
3075 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003076 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003077 else
3078#endif
3079 c = *s++;
3080 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3081#ifdef UNIX
3082 || c == intr_char
3083#endif
3084 || (c == Ctrl_BSL && *s == Ctrl_N))
3085 stuffcharReadbuff(Ctrl_V);
3086 stuffcharReadbuff(c);
3087 }
3088}
3089
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090#ifdef FEAT_WILDMENU
3091/*
3092 * Delete characters on the command line, from "from" to the current
3093 * position.
3094 */
3095 static void
3096cmdline_del(from)
3097 int from;
3098{
3099 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3100 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3101 ccline.cmdlen -= ccline.cmdpos - from;
3102 ccline.cmdpos = from;
3103}
3104#endif
3105
3106/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003107 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 * search
3109 */
3110 void
3111redrawcmdline()
3112{
3113 if (cmd_silent)
3114 return;
3115 need_wait_return = FALSE;
3116 compute_cmdrow();
3117 redrawcmd();
3118 cursorcmd();
3119}
3120
3121 static void
3122redrawcmdprompt()
3123{
3124 int i;
3125
3126 if (cmd_silent)
3127 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003128 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 msg_putchar(ccline.cmdfirstc);
3130 if (ccline.cmdprompt != NULL)
3131 {
3132 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3133 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3134 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003135 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 --ccline.cmdindent;
3137 }
3138 else
3139 for (i = ccline.cmdindent; i > 0; --i)
3140 msg_putchar(' ');
3141}
3142
3143/*
3144 * Redraw what is currently on the command line.
3145 */
3146 void
3147redrawcmd()
3148{
3149 if (cmd_silent)
3150 return;
3151
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003152 /* when 'incsearch' is set there may be no command line while redrawing */
3153 if (ccline.cmdbuff == NULL)
3154 {
3155 windgoto(cmdline_row, 0);
3156 msg_clr_eos();
3157 return;
3158 }
3159
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 msg_start();
3161 redrawcmdprompt();
3162
3163 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3164 msg_no_more = TRUE;
3165 draw_cmdline(0, ccline.cmdlen);
3166 msg_clr_eos();
3167 msg_no_more = FALSE;
3168
3169 set_cmdspos_cursor();
3170
3171 /*
3172 * An emsg() before may have set msg_scroll. This is used in normal mode,
3173 * in cmdline mode we can reset them now.
3174 */
3175 msg_scroll = FALSE; /* next message overwrites cmdline */
3176
3177 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3178 * in cmdline mode */
3179 skip_redraw = FALSE;
3180}
3181
3182 void
3183compute_cmdrow()
3184{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003185 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 cmdline_row = Rows - 1;
3187 else
3188 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3189 + W_STATUS_HEIGHT(lastwin);
3190}
3191
3192 static void
3193cursorcmd()
3194{
3195 if (cmd_silent)
3196 return;
3197
3198#ifdef FEAT_RIGHTLEFT
3199 if (cmdmsg_rl)
3200 {
3201 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3202 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3203 if (msg_row <= 0)
3204 msg_row = Rows - 1;
3205 }
3206 else
3207#endif
3208 {
3209 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3210 msg_col = ccline.cmdspos % (int)Columns;
3211 if (msg_row >= Rows)
3212 msg_row = Rows - 1;
3213 }
3214
3215 windgoto(msg_row, msg_col);
3216#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3217 redrawcmd_preedit();
3218#endif
3219#ifdef MCH_CURSOR_SHAPE
3220 mch_update_cursor();
3221#endif
3222}
3223
3224 void
3225gotocmdline(clr)
3226 int clr;
3227{
3228 msg_start();
3229#ifdef FEAT_RIGHTLEFT
3230 if (cmdmsg_rl)
3231 msg_col = Columns - 1;
3232 else
3233#endif
3234 msg_col = 0; /* always start in column 0 */
3235 if (clr) /* clear the bottom line(s) */
3236 msg_clr_eos(); /* will reset clear_cmdline */
3237 windgoto(cmdline_row, 0);
3238}
3239
3240/*
3241 * Check the word in front of the cursor for an abbreviation.
3242 * Called when the non-id character "c" has been entered.
3243 * When an abbreviation is recognized it is removed from the text with
3244 * backspaces and the replacement string is inserted, followed by "c".
3245 */
3246 static int
3247ccheck_abbr(c)
3248 int c;
3249{
3250 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3251 return FALSE;
3252
3253 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3254}
3255
3256/*
3257 * Return FAIL if this is not an appropriate context in which to do
3258 * completion of anything, return OK if it is (even if there are no matches).
3259 * For the caller, this means that the character is just passed through like a
3260 * normal character (instead of being expanded). This allows :s/^I^D etc.
3261 */
3262 static int
3263nextwild(xp, type, options)
3264 expand_T *xp;
3265 int type;
3266 int options; /* extra options for ExpandOne() */
3267{
3268 int i, j;
3269 char_u *p1;
3270 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 int difflen;
3272 int v;
3273
3274 if (xp->xp_numfiles == -1)
3275 {
3276 set_expand_context(xp);
3277 cmd_showtail = expand_showtail(xp);
3278 }
3279
3280 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3281 {
3282 beep_flush();
3283 return OK; /* Something illegal on command line */
3284 }
3285 if (xp->xp_context == EXPAND_NOTHING)
3286 {
3287 /* Caller can use the character as a normal char instead */
3288 return FAIL;
3289 }
3290
3291 MSG_PUTS("..."); /* show that we are busy */
3292 out_flush();
3293
3294 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003295 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
3297 if (type == WILD_NEXT || type == WILD_PREV)
3298 {
3299 /*
3300 * Get next/previous match for a previous expanded pattern.
3301 */
3302 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3303 }
3304 else
3305 {
3306 /*
3307 * Translate string into pattern and expand it.
3308 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003309 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3310 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 p2 = NULL;
3312 else
3313 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003314 p2 = ExpandOne(xp, p1,
3315 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3317 |options, type);
3318 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003319 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 if (p2 != NULL && type == WILD_LONGEST)
3321 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003322 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 if (ccline.cmdbuff[i + j] == '*'
3324 || ccline.cmdbuff[i + j] == '?')
3325 break;
3326 if ((int)STRLEN(p2) < j)
3327 {
3328 vim_free(p2);
3329 p2 = NULL;
3330 }
3331 }
3332 }
3333 }
3334
3335 if (p2 != NULL && !got_int)
3336 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003337 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3339 {
3340 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3341 xp->xp_pattern = ccline.cmdbuff + i;
3342 }
3343 else
3344 v = OK;
3345 if (v == OK)
3346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003347 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3348 &ccline.cmdbuff[ccline.cmdpos],
3349 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3350 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 ccline.cmdlen += difflen;
3352 ccline.cmdpos += difflen;
3353 }
3354 }
3355 vim_free(p2);
3356
3357 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003358 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 /* When expanding a ":map" command and no matches are found, assume that
3361 * the key is supposed to be inserted literally */
3362 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3363 return FAIL;
3364
3365 if (xp->xp_numfiles <= 0 && p2 == NULL)
3366 beep_flush();
3367 else if (xp->xp_numfiles == 1)
3368 /* free expanded pattern */
3369 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3370
3371 return OK;
3372}
3373
3374/*
3375 * Do wildcard expansion on the string 'str'.
3376 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003377 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 * Return NULL for failure.
3379 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003380 * "orig" is the originally expanded string, copied to allocated memory. It
3381 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3382 * WILD_PREV "orig" should be NULL.
3383 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003384 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3385 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 *
3387 * mode = WILD_FREE: just free previously expanded matches
3388 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3389 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3390 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3391 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3392 * mode = WILD_ALL: return all matches concatenated
3393 * mode = WILD_LONGEST: return longest matched part
3394 *
3395 * options = WILD_LIST_NOTFOUND: list entries without a match
3396 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3397 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3398 * options = WILD_NO_BEEP: Don't beep for multiple matches
3399 * options = WILD_ADD_SLASH: add a slash after directory names
3400 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3401 * options = WILD_SILENT: don't print warning messages
3402 * options = WILD_ESCAPE: put backslash before special chars
3403 *
3404 * The variables xp->xp_context and xp->xp_backslash must have been set!
3405 */
3406 char_u *
3407ExpandOne(xp, str, orig, options, mode)
3408 expand_T *xp;
3409 char_u *str;
3410 char_u *orig; /* allocated copy of original of expanded string */
3411 int options;
3412 int mode;
3413{
3414 char_u *ss = NULL;
3415 static int findex;
3416 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003417 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 int i;
3419 long_u len;
3420 int non_suf_match; /* number without matching suffix */
3421
3422 /*
3423 * first handle the case of using an old match
3424 */
3425 if (mode == WILD_NEXT || mode == WILD_PREV)
3426 {
3427 if (xp->xp_numfiles > 0)
3428 {
3429 if (mode == WILD_PREV)
3430 {
3431 if (findex == -1)
3432 findex = xp->xp_numfiles;
3433 --findex;
3434 }
3435 else /* mode == WILD_NEXT */
3436 ++findex;
3437
3438 /*
3439 * When wrapping around, return the original string, set findex to
3440 * -1.
3441 */
3442 if (findex < 0)
3443 {
3444 if (orig_save == NULL)
3445 findex = xp->xp_numfiles - 1;
3446 else
3447 findex = -1;
3448 }
3449 if (findex >= xp->xp_numfiles)
3450 {
3451 if (orig_save == NULL)
3452 findex = 0;
3453 else
3454 findex = -1;
3455 }
3456#ifdef FEAT_WILDMENU
3457 if (p_wmnu)
3458 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3459 findex, cmd_showtail);
3460#endif
3461 if (findex == -1)
3462 return vim_strsave(orig_save);
3463 return vim_strsave(xp->xp_files[findex]);
3464 }
3465 else
3466 return NULL;
3467 }
3468
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003469 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3471 {
3472 FreeWild(xp->xp_numfiles, xp->xp_files);
3473 xp->xp_numfiles = -1;
3474 vim_free(orig_save);
3475 orig_save = NULL;
3476 }
3477 findex = 0;
3478
3479 if (mode == WILD_FREE) /* only release file name */
3480 return NULL;
3481
3482 if (xp->xp_numfiles == -1)
3483 {
3484 vim_free(orig_save);
3485 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003486 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 /*
3489 * Do the expansion.
3490 */
3491 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3492 options) == FAIL)
3493 {
3494#ifdef FNAME_ILLEGAL
3495 /* Illegal file name has been silently skipped. But when there
3496 * are wildcards, the real problem is that there was no match,
3497 * causing the pattern to be added, which has illegal characters.
3498 */
3499 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3500 EMSG2(_(e_nomatch2), str);
3501#endif
3502 }
3503 else if (xp->xp_numfiles == 0)
3504 {
3505 if (!(options & WILD_SILENT))
3506 EMSG2(_(e_nomatch2), str);
3507 }
3508 else
3509 {
3510 /* Escape the matches for use on the command line. */
3511 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3512
3513 /*
3514 * Check for matching suffixes in file names.
3515 */
3516 if (mode != WILD_ALL && mode != WILD_LONGEST)
3517 {
3518 if (xp->xp_numfiles)
3519 non_suf_match = xp->xp_numfiles;
3520 else
3521 non_suf_match = 1;
3522 if ((xp->xp_context == EXPAND_FILES
3523 || xp->xp_context == EXPAND_DIRECTORIES)
3524 && xp->xp_numfiles > 1)
3525 {
3526 /*
3527 * More than one match; check suffix.
3528 * The files will have been sorted on matching suffix in
3529 * expand_wildcards, only need to check the first two.
3530 */
3531 non_suf_match = 0;
3532 for (i = 0; i < 2; ++i)
3533 if (match_suffix(xp->xp_files[i]))
3534 ++non_suf_match;
3535 }
3536 if (non_suf_match != 1)
3537 {
3538 /* Can we ever get here unless it's while expanding
3539 * interactively? If not, we can get rid of this all
3540 * together. Don't really want to wait for this message
3541 * (and possibly have to hit return to continue!).
3542 */
3543 if (!(options & WILD_SILENT))
3544 EMSG(_(e_toomany));
3545 else if (!(options & WILD_NO_BEEP))
3546 beep_flush();
3547 }
3548 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3549 ss = vim_strsave(xp->xp_files[0]);
3550 }
3551 }
3552 }
3553
3554 /* Find longest common part */
3555 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3556 {
3557 for (len = 0; xp->xp_files[0][len]; ++len)
3558 {
3559 for (i = 0; i < xp->xp_numfiles; ++i)
3560 {
3561#ifdef CASE_INSENSITIVE_FILENAME
3562 if (xp->xp_context == EXPAND_DIRECTORIES
3563 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003564 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 || xp->xp_context == EXPAND_BUFFERS)
3566 {
3567 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3568 TOLOWER_LOC(xp->xp_files[0][len]))
3569 break;
3570 }
3571 else
3572#endif
3573 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3574 break;
3575 }
3576 if (i < xp->xp_numfiles)
3577 {
3578 if (!(options & WILD_NO_BEEP))
3579 vim_beep();
3580 break;
3581 }
3582 }
3583 ss = alloc((unsigned)len + 1);
3584 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003585 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 findex = -1; /* next p_wc gets first one */
3587 }
3588
3589 /* Concatenate all matching names */
3590 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3591 {
3592 len = 0;
3593 for (i = 0; i < xp->xp_numfiles; ++i)
3594 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3595 ss = lalloc(len, TRUE);
3596 if (ss != NULL)
3597 {
3598 *ss = NUL;
3599 for (i = 0; i < xp->xp_numfiles; ++i)
3600 {
3601 STRCAT(ss, xp->xp_files[i]);
3602 if (i != xp->xp_numfiles - 1)
3603 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3604 }
3605 }
3606 }
3607
3608 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3609 ExpandCleanup(xp);
3610
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003611 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003612 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003613 vim_free(orig);
3614
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 return ss;
3616}
3617
3618/*
3619 * Prepare an expand structure for use.
3620 */
3621 void
3622ExpandInit(xp)
3623 expand_T *xp;
3624{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003625 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003626 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003628#ifndef BACKSLASH_IN_FILENAME
3629 xp->xp_shell = FALSE;
3630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 xp->xp_numfiles = -1;
3632 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003633#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3634 xp->xp_arg = NULL;
3635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636}
3637
3638/*
3639 * Cleanup an expand structure after use.
3640 */
3641 void
3642ExpandCleanup(xp)
3643 expand_T *xp;
3644{
3645 if (xp->xp_numfiles >= 0)
3646 {
3647 FreeWild(xp->xp_numfiles, xp->xp_files);
3648 xp->xp_numfiles = -1;
3649 }
3650}
3651
3652 void
3653ExpandEscape(xp, str, numfiles, files, options)
3654 expand_T *xp;
3655 char_u *str;
3656 int numfiles;
3657 char_u **files;
3658 int options;
3659{
3660 int i;
3661 char_u *p;
3662
3663 /*
3664 * May change home directory back to "~"
3665 */
3666 if (options & WILD_HOME_REPLACE)
3667 tilde_replace(str, numfiles, files);
3668
3669 if (options & WILD_ESCAPE)
3670 {
3671 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003672 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 || xp->xp_context == EXPAND_BUFFERS
3674 || xp->xp_context == EXPAND_DIRECTORIES)
3675 {
3676 /*
3677 * Insert a backslash into a file name before a space, \, %, #
3678 * and wildmatch characters, except '~'.
3679 */
3680 for (i = 0; i < numfiles; ++i)
3681 {
3682 /* for ":set path=" we need to escape spaces twice */
3683 if (xp->xp_backslash == XP_BS_THREE)
3684 {
3685 p = vim_strsave_escaped(files[i], (char_u *)" ");
3686 if (p != NULL)
3687 {
3688 vim_free(files[i]);
3689 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003690#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 p = vim_strsave_escaped(files[i], (char_u *)" ");
3692 if (p != NULL)
3693 {
3694 vim_free(files[i]);
3695 files[i] = p;
3696 }
3697#endif
3698 }
3699 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003700#ifdef BACKSLASH_IN_FILENAME
3701 p = vim_strsave_fnameescape(files[i], FALSE);
3702#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003703 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 if (p != NULL)
3706 {
3707 vim_free(files[i]);
3708 files[i] = p;
3709 }
3710
3711 /* If 'str' starts with "\~", replace "~" at start of
3712 * files[i] with "\~". */
3713 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003714 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003717
3718 /* If the first file starts with a '+' escape it. Otherwise it
3719 * could be seen as "+cmd". */
3720 if (*files[0] == '+')
3721 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
3723 else if (xp->xp_context == EXPAND_TAGS)
3724 {
3725 /*
3726 * Insert a backslash before characters in a tag name that
3727 * would terminate the ":tag" command.
3728 */
3729 for (i = 0; i < numfiles; ++i)
3730 {
3731 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3732 if (p != NULL)
3733 {
3734 vim_free(files[i]);
3735 files[i] = p;
3736 }
3737 }
3738 }
3739 }
3740}
3741
3742/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003743 * Escape special characters in "fname" for when used as a file name argument
3744 * after a Vim command, or, when "shell" is non-zero, a shell command.
3745 * Returns the result in allocated memory.
3746 */
3747 char_u *
3748vim_strsave_fnameescape(fname, shell)
3749 char_u *fname;
3750 int shell;
3751{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003752 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003753#ifdef BACKSLASH_IN_FILENAME
3754 char_u buf[20];
3755 int j = 0;
3756
3757 /* Don't escape '[' and '{' if they are in 'isfname'. */
3758 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3759 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3760 buf[j++] = *p;
3761 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003762 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003763#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003764 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3765 if (shell && csh_like_shell() && p != NULL)
3766 {
3767 char_u *s;
3768
3769 /* For csh and similar shells need to put two backslashes before '!'.
3770 * One is taken by Vim, one by the shell. */
3771 s = vim_strsave_escaped(p, (char_u *)"!");
3772 vim_free(p);
3773 p = s;
3774 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003775#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003776
3777 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3778 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003779 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003780 escape_fname(&p);
3781
3782 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003783}
3784
3785/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003786 * Put a backslash before the file name in "pp", which is in allocated memory.
3787 */
3788 static void
3789escape_fname(pp)
3790 char_u **pp;
3791{
3792 char_u *p;
3793
3794 p = alloc((unsigned)(STRLEN(*pp) + 2));
3795 if (p != NULL)
3796 {
3797 p[0] = '\\';
3798 STRCPY(p + 1, *pp);
3799 vim_free(*pp);
3800 *pp = p;
3801 }
3802}
3803
3804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 * For each file name in files[num_files]:
3806 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3807 */
3808 void
3809tilde_replace(orig_pat, num_files, files)
3810 char_u *orig_pat;
3811 int num_files;
3812 char_u **files;
3813{
3814 int i;
3815 char_u *p;
3816
3817 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3818 {
3819 for (i = 0; i < num_files; ++i)
3820 {
3821 p = home_replace_save(NULL, files[i]);
3822 if (p != NULL)
3823 {
3824 vim_free(files[i]);
3825 files[i] = p;
3826 }
3827 }
3828 }
3829}
3830
3831/*
3832 * Show all matches for completion on the command line.
3833 * Returns EXPAND_NOTHING when the character that triggered expansion should
3834 * be inserted like a normal character.
3835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 static int
3837showmatches(xp, wildmenu)
3838 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003839 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840{
3841#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3842 int num_files;
3843 char_u **files_found;
3844 int i, j, k;
3845 int maxlen;
3846 int lines;
3847 int columns;
3848 char_u *p;
3849 int lastlen;
3850 int attr;
3851 int showtail;
3852
3853 if (xp->xp_numfiles == -1)
3854 {
3855 set_expand_context(xp);
3856 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3857 &num_files, &files_found);
3858 showtail = expand_showtail(xp);
3859 if (i != EXPAND_OK)
3860 return i;
3861
3862 }
3863 else
3864 {
3865 num_files = xp->xp_numfiles;
3866 files_found = xp->xp_files;
3867 showtail = cmd_showtail;
3868 }
3869
3870#ifdef FEAT_WILDMENU
3871 if (!wildmenu)
3872 {
3873#endif
3874 msg_didany = FALSE; /* lines_left will be set */
3875 msg_start(); /* prepare for paging */
3876 msg_putchar('\n');
3877 out_flush();
3878 cmdline_row = msg_row;
3879 msg_didany = FALSE; /* lines_left will be set again */
3880 msg_start(); /* prepare for paging */
3881#ifdef FEAT_WILDMENU
3882 }
3883#endif
3884
3885 if (got_int)
3886 got_int = FALSE; /* only int. the completion, not the cmd line */
3887#ifdef FEAT_WILDMENU
3888 else if (wildmenu)
3889 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3890#endif
3891 else
3892 {
3893 /* find the length of the longest file name */
3894 maxlen = 0;
3895 for (i = 0; i < num_files; ++i)
3896 {
3897 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003898 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 || xp->xp_context == EXPAND_BUFFERS))
3900 {
3901 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3902 j = vim_strsize(NameBuff);
3903 }
3904 else
3905 j = vim_strsize(L_SHOWFILE(i));
3906 if (j > maxlen)
3907 maxlen = j;
3908 }
3909
3910 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3911 lines = num_files;
3912 else
3913 {
3914 /* compute the number of columns and lines for the listing */
3915 maxlen += 2; /* two spaces between file names */
3916 columns = ((int)Columns + 2) / maxlen;
3917 if (columns < 1)
3918 columns = 1;
3919 lines = (num_files + columns - 1) / columns;
3920 }
3921
3922 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3923
3924 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3925 {
3926 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3927 msg_clr_eos();
3928 msg_advance(maxlen - 3);
3929 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3930 }
3931
3932 /* list the files line by line */
3933 for (i = 0; i < lines; ++i)
3934 {
3935 lastlen = 999;
3936 for (k = i; k < num_files; k += lines)
3937 {
3938 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3939 {
3940 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3941 p = files_found[k] + STRLEN(files_found[k]) + 1;
3942 msg_advance(maxlen + 1);
3943 msg_puts(p);
3944 msg_advance(maxlen + 3);
3945 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3946 break;
3947 }
3948 for (j = maxlen - lastlen; --j >= 0; )
3949 msg_putchar(' ');
3950 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003951 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 || xp->xp_context == EXPAND_BUFFERS)
3953 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01003954 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01003955 if (xp->xp_numfiles != -1)
3956 {
3957 char_u *halved_slash;
3958 char_u *exp_path;
3959
3960 /* Expansion was done before and special characters
3961 * were escaped, need to halve backslashes. Also
3962 * $HOME has been replaced with ~/. */
3963 exp_path = expand_env_save_opt(files_found[k], TRUE);
3964 halved_slash = backslash_halve_save(
3965 exp_path != NULL ? exp_path : files_found[k]);
3966 j = mch_isdir(halved_slash != NULL ? halved_slash
3967 : files_found[k]);
3968 vim_free(exp_path);
3969 vim_free(halved_slash);
3970 }
3971 else
3972 /* Expansion was done here, file names are literal. */
3973 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 if (showtail)
3975 p = L_SHOWFILE(k);
3976 else
3977 {
3978 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3979 TRUE);
3980 p = NameBuff;
3981 }
3982 }
3983 else
3984 {
3985 j = FALSE;
3986 p = L_SHOWFILE(k);
3987 }
3988 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3989 }
3990 if (msg_col > 0) /* when not wrapped around */
3991 {
3992 msg_clr_eos();
3993 msg_putchar('\n');
3994 }
3995 out_flush(); /* show one line at a time */
3996 if (got_int)
3997 {
3998 got_int = FALSE;
3999 break;
4000 }
4001 }
4002
4003 /*
4004 * we redraw the command below the lines that we have just listed
4005 * This is a bit tricky, but it saves a lot of screen updating.
4006 */
4007 cmdline_row = msg_row; /* will put it back later */
4008 }
4009
4010 if (xp->xp_numfiles == -1)
4011 FreeWild(num_files, files_found);
4012
4013 return EXPAND_OK;
4014}
4015
4016/*
4017 * Private gettail for showmatches() (and win_redr_status_matches()):
4018 * Find tail of file name path, but ignore trailing "/".
4019 */
4020 char_u *
4021sm_gettail(s)
4022 char_u *s;
4023{
4024 char_u *p;
4025 char_u *t = s;
4026 int had_sep = FALSE;
4027
4028 for (p = s; *p != NUL; )
4029 {
4030 if (vim_ispathsep(*p)
4031#ifdef BACKSLASH_IN_FILENAME
4032 && !rem_backslash(p)
4033#endif
4034 )
4035 had_sep = TRUE;
4036 else if (had_sep)
4037 {
4038 t = p;
4039 had_sep = FALSE;
4040 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004041 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
4043 return t;
4044}
4045
4046/*
4047 * Return TRUE if we only need to show the tail of completion matches.
4048 * When not completing file names or there is a wildcard in the path FALSE is
4049 * returned.
4050 */
4051 static int
4052expand_showtail(xp)
4053 expand_T *xp;
4054{
4055 char_u *s;
4056 char_u *end;
4057
4058 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004059 if (xp->xp_context != EXPAND_FILES
4060 && xp->xp_context != EXPAND_SHELLCMD
4061 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 return FALSE;
4063
4064 end = gettail(xp->xp_pattern);
4065 if (end == xp->xp_pattern) /* there is no path separator */
4066 return FALSE;
4067
4068 for (s = xp->xp_pattern; s < end; s++)
4069 {
4070 /* Skip escaped wildcards. Only when the backslash is not a path
4071 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4072 if (rem_backslash(s))
4073 ++s;
4074 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4075 return FALSE;
4076 }
4077 return TRUE;
4078}
4079
4080/*
4081 * Prepare a string for expansion.
4082 * When expanding file names: The string will be used with expand_wildcards().
4083 * Copy the file name into allocated memory and add a '*' at the end.
4084 * When expanding other names: The string will be used with regcomp(). Copy
4085 * the name into allocated memory and prepend "^".
4086 */
4087 char_u *
4088addstar(fname, len, context)
4089 char_u *fname;
4090 int len;
4091 int context; /* EXPAND_FILES etc. */
4092{
4093 char_u *retval;
4094 int i, j;
4095 int new_len;
4096 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004097 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004099 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004100 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004101 && context != EXPAND_SHELLCMD
4102 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 {
4104 /*
4105 * Matching will be done internally (on something other than files).
4106 * So we convert the file-matching-type wildcards into our kind for
4107 * use with vim_regcomp(). First work out how long it will be:
4108 */
4109
4110 /* For help tags the translation is done in find_help_tags().
4111 * For a tag pattern starting with "/" no translation is needed. */
4112 if (context == EXPAND_HELP
4113 || context == EXPAND_COLORS
4114 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004115 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004116 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 || (context == EXPAND_TAGS && fname[0] == '/'))
4118 retval = vim_strnsave(fname, len);
4119 else
4120 {
4121 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4122 for (i = 0; i < len; i++)
4123 {
4124 if (fname[i] == '*' || fname[i] == '~')
4125 new_len++; /* '*' needs to be replaced by ".*"
4126 '~' needs to be replaced by "\~" */
4127
4128 /* Buffer names are like file names. "." should be literal */
4129 if (context == EXPAND_BUFFERS && fname[i] == '.')
4130 new_len++; /* "." becomes "\." */
4131
4132 /* Custom expansion takes care of special things, match
4133 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004134 if ((context == EXPAND_USER_DEFINED
4135 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 new_len++; /* '\' becomes "\\" */
4137 }
4138 retval = alloc(new_len);
4139 if (retval != NULL)
4140 {
4141 retval[0] = '^';
4142 j = 1;
4143 for (i = 0; i < len; i++, j++)
4144 {
4145 /* Skip backslash. But why? At least keep it for custom
4146 * expansion. */
4147 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004148 && context != EXPAND_USER_LIST
4149 && fname[i] == '\\'
4150 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 break;
4152
4153 switch (fname[i])
4154 {
4155 case '*': retval[j++] = '.';
4156 break;
4157 case '~': retval[j++] = '\\';
4158 break;
4159 case '?': retval[j] = '.';
4160 continue;
4161 case '.': if (context == EXPAND_BUFFERS)
4162 retval[j++] = '\\';
4163 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004164 case '\\': if (context == EXPAND_USER_DEFINED
4165 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 retval[j++] = '\\';
4167 break;
4168 }
4169 retval[j] = fname[i];
4170 }
4171 retval[j] = NUL;
4172 }
4173 }
4174 }
4175 else
4176 {
4177 retval = alloc(len + 4);
4178 if (retval != NULL)
4179 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004180 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
4182 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004183 * Don't add a star to *, ~, ~user, $var or `cmd`.
4184 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 * ~ would be at the start of the file name, but not the tail.
4186 * $ could be anywhere in the tail.
4187 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004188 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 */
4190 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004191 ends_in_star = (len > 0 && retval[len - 1] == '*');
4192#ifndef BACKSLASH_IN_FILENAME
4193 for (i = len - 2; i >= 0; --i)
4194 {
4195 if (retval[i] != '\\')
4196 break;
4197 ends_in_star = !ends_in_star;
4198 }
4199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004201 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 && vim_strchr(tail, '$') == NULL
4203 && vim_strchr(retval, '`') == NULL)
4204 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004205 else if (len > 0 && retval[len - 1] == '$')
4206 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 retval[len] = NUL;
4208 }
4209 }
4210 return retval;
4211}
4212
4213/*
4214 * Must parse the command line so far to work out what context we are in.
4215 * Completion can then be done based on that context.
4216 * This routine sets the variables:
4217 * xp->xp_pattern The start of the pattern to be expanded within
4218 * the command line (ends at the cursor).
4219 * xp->xp_context The type of thing to expand. Will be one of:
4220 *
4221 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4222 * the command line, like an unknown command. Caller
4223 * should beep.
4224 * EXPAND_NOTHING Unrecognised context for completion, use char like
4225 * a normal char, rather than for completion. eg
4226 * :s/^I/
4227 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4228 * it.
4229 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4230 * EXPAND_FILES After command with XFILE set, or after setting
4231 * with P_EXPAND set. eg :e ^I, :w>>^I
4232 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4233 * when we know only directories are of interest. eg
4234 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004235 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4237 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4238 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4239 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4240 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4241 * EXPAND_EVENTS Complete event names
4242 * EXPAND_SYNTAX Complete :syntax command arguments
4243 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4244 * EXPAND_AUGROUP Complete autocommand group names
4245 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4246 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4247 * eg :unmap a^I , :cunab x^I
4248 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4249 * eg :call sub^I
4250 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4251 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4252 * names in expressions, eg :while s^I
4253 * EXPAND_ENV_VARS Complete environment variable names
4254 */
4255 static void
4256set_expand_context(xp)
4257 expand_T *xp;
4258{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004259 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (ccline.cmdfirstc != ':'
4261#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004262 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004263 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264#endif
4265 )
4266 {
4267 xp->xp_context = EXPAND_NOTHING;
4268 return;
4269 }
4270 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4271}
4272
4273 void
4274set_cmd_context(xp, str, len, col)
4275 expand_T *xp;
4276 char_u *str; /* start of command line */
4277 int len; /* length of command line (excl. NUL) */
4278 int col; /* position of cursor */
4279{
4280 int old_char = NUL;
4281 char_u *nextcomm;
4282
4283 /*
4284 * Avoid a UMR warning from Purify, only save the character if it has been
4285 * written before.
4286 */
4287 if (col < len)
4288 old_char = str[col];
4289 str[col] = NUL;
4290 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004291
4292#ifdef FEAT_EVAL
4293 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004294 {
4295# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004296 /* pass CMD_SIZE because there is no real command */
4297 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004298# endif
4299 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004300 else if (ccline.input_fn)
4301 {
4302 xp->xp_context = ccline.xp_context;
4303 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004304# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004305 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004306# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004307 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004308 else
4309#endif
4310 while (nextcomm != NULL)
4311 nextcomm = set_one_cmd_context(xp, nextcomm);
4312
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 str[col] = old_char;
4314}
4315
4316/*
4317 * Expand the command line "str" from context "xp".
4318 * "xp" must have been set by set_cmd_context().
4319 * xp->xp_pattern points into "str", to where the text that is to be expanded
4320 * starts.
4321 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4322 * cursor.
4323 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4324 * key that triggered expansion literally.
4325 * Returns EXPAND_OK otherwise.
4326 */
4327 int
4328expand_cmdline(xp, str, col, matchcount, matches)
4329 expand_T *xp;
4330 char_u *str; /* start of command line */
4331 int col; /* position of cursor */
4332 int *matchcount; /* return: nr of matches */
4333 char_u ***matches; /* return: array of pointers to matches */
4334{
4335 char_u *file_str = NULL;
4336
4337 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4338 {
4339 beep_flush();
4340 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4341 }
4342 if (xp->xp_context == EXPAND_NOTHING)
4343 {
4344 /* Caller can use the character as a normal char instead */
4345 return EXPAND_NOTHING;
4346 }
4347
4348 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004349 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4350 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 if (file_str == NULL)
4352 return EXPAND_UNSUCCESSFUL;
4353
4354 /* find all files that match the description */
4355 if (ExpandFromContext(xp, file_str, matchcount, matches,
4356 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4357 {
4358 *matchcount = 0;
4359 *matches = NULL;
4360 }
4361 vim_free(file_str);
4362
4363 return EXPAND_OK;
4364}
4365
4366#ifdef FEAT_MULTI_LANG
4367/*
4368 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4369 */
4370static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4371
4372 static void
4373cleanup_help_tags(num_file, file)
4374 int num_file;
4375 char_u **file;
4376{
4377 int i, j;
4378 int len;
4379
4380 for (i = 0; i < num_file; ++i)
4381 {
4382 len = (int)STRLEN(file[i]) - 3;
4383 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4384 {
4385 /* Sorting on priority means the same item in another language may
4386 * be anywhere. Search all items for a match up to the "@en". */
4387 for (j = 0; j < num_file; ++j)
4388 if (j != i
4389 && (int)STRLEN(file[j]) == len + 3
4390 && STRNCMP(file[i], file[j], len + 1) == 0)
4391 break;
4392 if (j == num_file)
4393 file[i][len] = NUL;
4394 }
4395 }
4396}
4397#endif
4398
4399/*
4400 * Do the expansion based on xp->xp_context and "pat".
4401 */
4402 static int
4403ExpandFromContext(xp, pat, num_file, file, options)
4404 expand_T *xp;
4405 char_u *pat;
4406 int *num_file;
4407 char_u ***file;
4408 int options;
4409{
4410#ifdef FEAT_CMDL_COMPL
4411 regmatch_T regmatch;
4412#endif
4413 int ret;
4414 int flags;
4415
4416 flags = EW_DIR; /* include directories */
4417 if (options & WILD_LIST_NOTFOUND)
4418 flags |= EW_NOTFOUND;
4419 if (options & WILD_ADD_SLASH)
4420 flags |= EW_ADDSLASH;
4421 if (options & WILD_KEEP_ALL)
4422 flags |= EW_KEEPALL;
4423 if (options & WILD_SILENT)
4424 flags |= EW_SILENT;
4425
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004426 if (xp->xp_context == EXPAND_FILES
4427 || xp->xp_context == EXPAND_DIRECTORIES
4428 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
4430 /*
4431 * Expand file or directory names.
4432 */
4433 int free_pat = FALSE;
4434 int i;
4435
4436 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4437 * space */
4438 if (xp->xp_backslash != XP_BS_NONE)
4439 {
4440 free_pat = TRUE;
4441 pat = vim_strsave(pat);
4442 for (i = 0; pat[i]; ++i)
4443 if (pat[i] == '\\')
4444 {
4445 if (xp->xp_backslash == XP_BS_THREE
4446 && pat[i + 1] == '\\'
4447 && pat[i + 2] == '\\'
4448 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004449 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 if (xp->xp_backslash == XP_BS_ONE
4451 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004452 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 }
4454 }
4455
4456 if (xp->xp_context == EXPAND_FILES)
4457 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004458 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4459 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 else
4461 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaard7834d32009-12-02 16:14:36 +00004462 /* Expand wildcards, supporting %:h and the like. */
4463 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 if (free_pat)
4465 vim_free(pat);
4466 return ret;
4467 }
4468
4469 *file = (char_u **)"";
4470 *num_file = 0;
4471 if (xp->xp_context == EXPAND_HELP)
4472 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004473 /* With an empty argument we would get all the help tags, which is
4474 * very slow. Get matches for "help" instead. */
4475 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4476 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
4478#ifdef FEAT_MULTI_LANG
4479 cleanup_help_tags(*num_file, *file);
4480#endif
4481 return OK;
4482 }
4483 return FAIL;
4484 }
4485
4486#ifndef FEAT_CMDL_COMPL
4487 return FAIL;
4488#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004489 if (xp->xp_context == EXPAND_SHELLCMD)
4490 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 if (xp->xp_context == EXPAND_OLD_SETTING)
4492 return ExpandOldSetting(num_file, file);
4493 if (xp->xp_context == EXPAND_BUFFERS)
4494 return ExpandBufnames(pat, num_file, file, options);
4495 if (xp->xp_context == EXPAND_TAGS
4496 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4497 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4498 if (xp->xp_context == EXPAND_COLORS)
4499 return ExpandRTDir(pat, num_file, file, "colors");
4500 if (xp->xp_context == EXPAND_COMPILER)
4501 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004502 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004503 return ExpandRTDir(pat, num_file, file, "syntax");
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004504 if (xp->xp_context == EXPAND_FILETYPE)
4505 return ExpandRTDir(pat, num_file, file, "{syntax,indent,ftplugin}");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004506# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4507 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004508 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
4511 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4512 if (regmatch.regprog == NULL)
4513 return FAIL;
4514
4515 /* set ignore-case according to p_ic, p_scs and pat */
4516 regmatch.rm_ic = ignorecase(pat);
4517
4518 if (xp->xp_context == EXPAND_SETTINGS
4519 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4520 ret = ExpandSettings(xp, &regmatch, num_file, file);
4521 else if (xp->xp_context == EXPAND_MAPPINGS)
4522 ret = ExpandMappings(&regmatch, num_file, file);
4523# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4524 else if (xp->xp_context == EXPAND_USER_DEFINED)
4525 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4526# endif
4527 else
4528 {
4529 static struct expgen
4530 {
4531 int context;
4532 char_u *((*func)__ARGS((expand_T *, int)));
4533 int ic;
4534 } tab[] =
4535 {
4536 {EXPAND_COMMANDS, get_command_name, FALSE},
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004537 {EXPAND_BEHAVE, get_behave_arg, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538#ifdef FEAT_USR_CMDS
4539 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4540 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4541 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4542 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4543#endif
4544#ifdef FEAT_EVAL
4545 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4546 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4547 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4548 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4549#endif
4550#ifdef FEAT_MENU
4551 {EXPAND_MENUS, get_menu_name, FALSE},
4552 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4553#endif
4554#ifdef FEAT_SYN_HL
4555 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4556#endif
4557 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4558#ifdef FEAT_AUTOCMD
4559 {EXPAND_EVENTS, get_event_name, TRUE},
4560 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4561#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004562#ifdef FEAT_CSCOPE
4563 {EXPAND_CSCOPE, get_cscope_name, TRUE},
4564#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004565#ifdef FEAT_SIGNS
4566 {EXPAND_SIGN, get_sign_name, TRUE},
4567#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004568#ifdef FEAT_PROFILE
4569 {EXPAND_PROFILE, get_profile_name, TRUE},
4570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4572 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4573 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4574#endif
4575 {EXPAND_ENV_VARS, get_env_name, TRUE},
4576 };
4577 int i;
4578
4579 /*
4580 * Find a context in the table and call the ExpandGeneric() with the
4581 * right function to do the expansion.
4582 */
4583 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004584 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (xp->xp_context == tab[i].context)
4586 {
4587 if (tab[i].ic)
4588 regmatch.rm_ic = TRUE;
4589 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4590 break;
4591 }
4592 }
4593
4594 vim_free(regmatch.regprog);
4595
4596 return ret;
4597#endif /* FEAT_CMDL_COMPL */
4598}
4599
4600#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4601/*
4602 * Expand a list of names.
4603 *
4604 * Generic function for command line completion. It calls a function to
4605 * obtain strings, one by one. The strings are matched against a regexp
4606 * program. Matching strings are copied into an array, which is returned.
4607 *
4608 * Returns OK when no problems encountered, FAIL for error (out of memory).
4609 */
4610 int
4611ExpandGeneric(xp, regmatch, num_file, file, func)
4612 expand_T *xp;
4613 regmatch_T *regmatch;
4614 int *num_file;
4615 char_u ***file;
4616 char_u *((*func)__ARGS((expand_T *, int)));
4617 /* returns a string from the list */
4618{
4619 int i;
4620 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004621 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 char_u *str;
4623
4624 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004625 * round == 0: count the number of matching names
4626 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004628 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
4630 for (i = 0; ; ++i)
4631 {
4632 str = (*func)(xp, i);
4633 if (str == NULL) /* end of list */
4634 break;
4635 if (*str == NUL) /* skip empty strings */
4636 continue;
4637
4638 if (vim_regexec(regmatch, str, (colnr_T)0))
4639 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004640 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {
4642 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4643 (*file)[count] = str;
4644#ifdef FEAT_MENU
4645 if (func == get_menu_names && str != NULL)
4646 {
4647 /* test for separator added by get_menu_names() */
4648 str += STRLEN(str) - 1;
4649 if (*str == '\001')
4650 *str = '.';
4651 }
4652#endif
4653 }
4654 ++count;
4655 }
4656 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004657 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 {
4659 if (count == 0)
4660 return OK;
4661 *num_file = count;
4662 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4663 if (*file == NULL)
4664 {
4665 *file = (char_u **)"";
4666 return FAIL;
4667 }
4668 count = 0;
4669 }
4670 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004671
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004672 /* Sort the results. Keep menu's in the specified order. */
4673 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4674 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004675
Bram Moolenaar4f688582007-07-24 12:34:30 +00004676#ifdef FEAT_CMDL_COMPL
4677 /* Reset the variables used for special highlight names expansion, so that
4678 * they don't show up when getting normal highlight names by ID. */
4679 reset_expand_highlight();
4680#endif
4681
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 return OK;
4683}
4684
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004685/*
4686 * Complete a shell command.
4687 * Returns FAIL or OK;
4688 */
4689 static int
4690expand_shellcmd(filepat, num_file, file, flagsarg)
4691 char_u *filepat; /* pattern to match with command names */
4692 int *num_file; /* return: number of matches */
4693 char_u ***file; /* return: array with matches */
4694 int flagsarg; /* EW_ flags */
4695{
4696 char_u *pat;
4697 int i;
4698 char_u *path;
4699 int mustfree = FALSE;
4700 garray_T ga;
4701 char_u *buf = alloc(MAXPATHL);
4702 size_t l;
4703 char_u *s, *e;
4704 int flags = flagsarg;
4705 int ret;
4706
4707 if (buf == NULL)
4708 return FAIL;
4709
4710 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4711 * space */
4712 pat = vim_strsave(filepat);
4713 for (i = 0; pat[i]; ++i)
4714 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004715 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004716
4717 flags |= EW_FILE | EW_EXEC;
4718
4719 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004720 if (mch_isFullName(pat))
4721 path = (char_u *)" ";
4722 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004723 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4724 path = (char_u *)".";
4725 else
4726 path = vim_getenv((char_u *)"PATH", &mustfree);
4727
4728 /*
4729 * Go over all directories in $PATH. Expand matches in that directory and
4730 * collect them in "ga".
4731 */
4732 ga_init2(&ga, (int)sizeof(char *), 10);
4733 for (s = path; *s != NUL; s = e)
4734 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004735 if (*s == ' ')
4736 ++s; /* Skip space used for absolute path name. */
4737
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004738#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4739 e = vim_strchr(s, ';');
4740#else
4741 e = vim_strchr(s, ':');
4742#endif
4743 if (e == NULL)
4744 e = s + STRLEN(s);
4745
4746 l = e - s;
4747 if (l > MAXPATHL - 5)
4748 break;
4749 vim_strncpy(buf, s, l);
4750 add_pathsep(buf);
4751 l = STRLEN(buf);
4752 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4753
4754 /* Expand matches in one directory of $PATH. */
4755 ret = expand_wildcards(1, &buf, num_file, file, flags);
4756 if (ret == OK)
4757 {
4758 if (ga_grow(&ga, *num_file) == FAIL)
4759 FreeWild(*num_file, *file);
4760 else
4761 {
4762 for (i = 0; i < *num_file; ++i)
4763 {
4764 s = (*file)[i];
4765 if (STRLEN(s) > l)
4766 {
4767 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004768 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004769 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4770 }
4771 else
4772 vim_free(s);
4773 }
4774 vim_free(*file);
4775 }
4776 }
4777 if (*e != NUL)
4778 ++e;
4779 }
4780 *file = ga.ga_data;
4781 *num_file = ga.ga_len;
4782
4783 vim_free(buf);
4784 vim_free(pat);
4785 if (mustfree)
4786 vim_free(path);
4787 return OK;
4788}
4789
4790
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004792static 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));
4793
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004795 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004796 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004798 static void *
4799call_user_expand_func(user_expand_func, xp, num_file, file)
4800 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 int *num_file;
4803 char_u ***file;
4804{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004805 char_u keep;
4806 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004809 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004810 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004813 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 *num_file = 0;
4815 *file = NULL;
4816
Bram Moolenaar21669c02008-01-18 12:16:16 +00004817 if (ccline.cmdbuff == NULL)
4818 {
4819 /* Completion from Insert mode, pass fake arguments. */
4820 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004821 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004822 args[1] = xp->xp_pattern;
4823 }
4824 else
4825 {
4826 /* Completion on the command line, pass real arguments. */
4827 keep = ccline.cmdbuff[ccline.cmdlen];
4828 ccline.cmdbuff[ccline.cmdlen] = 0;
4829 sprintf((char *)num, "%d", ccline.cmdpos);
4830 args[1] = ccline.cmdbuff;
4831 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004832 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 args[2] = num;
4834
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004835 /* Save the cmdline, we don't know what the function may do. */
4836 save_ccline = ccline;
4837 ccline.cmdbuff = NULL;
4838 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004840
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004841 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004842
4843 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004845 if (ccline.cmdbuff != NULL)
4846 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004847
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004848 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004849 return ret;
4850}
4851
4852/*
4853 * Expand names with a function defined by the user.
4854 */
4855 static int
4856ExpandUserDefined(xp, regmatch, num_file, file)
4857 expand_T *xp;
4858 regmatch_T *regmatch;
4859 int *num_file;
4860 char_u ***file;
4861{
4862 char_u *retstr;
4863 char_u *s;
4864 char_u *e;
4865 char_u keep;
4866 garray_T ga;
4867
4868 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4869 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 return FAIL;
4871
4872 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004873 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 {
4875 e = vim_strchr(s, '\n');
4876 if (e == NULL)
4877 e = s + STRLEN(s);
4878 keep = *e;
4879 *e = 0;
4880
4881 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4882 {
4883 *e = keep;
4884 if (*e != NUL)
4885 ++e;
4886 continue;
4887 }
4888
4889 if (ga_grow(&ga, 1) == FAIL)
4890 break;
4891
4892 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4893 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
4895 *e = keep;
4896 if (*e != NUL)
4897 ++e;
4898 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004899 vim_free(retstr);
4900 *file = ga.ga_data;
4901 *num_file = ga.ga_len;
4902 return OK;
4903}
4904
4905/*
4906 * Expand names with a list returned by a function defined by the user.
4907 */
4908 static int
4909ExpandUserList(xp, num_file, file)
4910 expand_T *xp;
4911 int *num_file;
4912 char_u ***file;
4913{
4914 list_T *retlist;
4915 listitem_T *li;
4916 garray_T ga;
4917
4918 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4919 if (retlist == NULL)
4920 return FAIL;
4921
4922 ga_init2(&ga, (int)sizeof(char *), 3);
4923 /* Loop over the items in the list. */
4924 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4925 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00004926 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
4927 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004928
4929 if (ga_grow(&ga, 1) == FAIL)
4930 break;
4931
4932 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00004933 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004934 ++ga.ga_len;
4935 }
4936 list_unref(retlist);
4937
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 *file = ga.ga_data;
4939 *num_file = ga.ga_len;
4940 return OK;
4941}
4942#endif
4943
4944/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004945 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004946 * 'runtimepath'/{dirnames}/{pat}.vim
4947 * dirnames may contain one directory (ex: "colorscheme") or can be a glob
4948 * expression matching multiple directories (ex: "{syntax,ftplugin,indent}").
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 */
4950 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004951ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 char_u *pat;
4953 int *num_file;
4954 char_u ***file;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004955 char *dirnames;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956{
4957 char_u *all;
4958 char_u *s;
4959 char_u *e;
4960 garray_T ga;
4961
4962 *num_file = 0;
4963 *file = NULL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004964 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirnames) + 7));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 if (s == NULL)
4966 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004967 sprintf((char *)s, "%s/%s*.vim", dirnames, pat);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00004968 all = globpath(p_rtp, s, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 vim_free(s);
4970 if (all == NULL)
4971 return FAIL;
4972
4973 ga_init2(&ga, (int)sizeof(char *), 3);
4974 for (s = all; *s != NUL; s = e)
4975 {
4976 e = vim_strchr(s, '\n');
4977 if (e == NULL)
4978 e = s + STRLEN(s);
4979 if (ga_grow(&ga, 1) == FAIL)
4980 break;
4981 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4982 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004983 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 if (*s == '\n' || vim_ispathsep(*s))
4985 break;
4986 ++s;
4987 ((char_u **)ga.ga_data)[ga.ga_len] =
4988 vim_strnsave(s, (int)(e - s - 4));
4989 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 if (*e != NUL)
4992 ++e;
4993 }
4994 vim_free(all);
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004995
4996 /* Sort and remove duplicates which can happen when specifying multiple
4997 * directories in dirnames such as "{syntax,ftplugin,indent}".
4998 */
4999 sort_strings((char_u **)ga.ga_data, ga.ga_len);
5000 remove_duplicates(&ga);
5001
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 *file = ga.ga_data;
5003 *num_file = ga.ga_len;
5004 return OK;
5005}
5006
5007#endif
5008
5009#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5010/*
5011 * Expand "file" for all comma-separated directories in "path".
5012 * Returns an allocated string with all matches concatenated, separated by
5013 * newlines. Returns NULL for an error or no matches.
5014 */
5015 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005016globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 char_u *path;
5018 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005019 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
5021 expand_T xpc;
5022 char_u *buf;
5023 garray_T ga;
5024 int i;
5025 int len;
5026 int num_p;
5027 char_u **p;
5028 char_u *cur = NULL;
5029
5030 buf = alloc(MAXPATHL);
5031 if (buf == NULL)
5032 return NULL;
5033
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005034 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005036
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 ga_init2(&ga, 1, 100);
5038
5039 /* Loop over all entries in {path}. */
5040 while (*path != NUL)
5041 {
5042 /* Copy one item of the path to buf[] and concatenate the file name. */
5043 copy_option_part(&path, buf, MAXPATHL, ",");
5044 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5045 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005046# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005047 /* Using the platform's path separator (\) makes vim incorrectly
5048 * treat it as an escape character, use '/' instead. */
5049 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5050 STRCAT(buf, "/");
5051# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005053# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005055 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5056 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005058 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005060 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
5062 /* Concatenate new results to previous ones. */
5063 if (ga_grow(&ga, len) == OK)
5064 {
5065 cur = (char_u *)ga.ga_data + ga.ga_len;
5066 for (i = 0; i < num_p; ++i)
5067 {
5068 STRCPY(cur, p[i]);
5069 cur += STRLEN(p[i]);
5070 *cur++ = '\n';
5071 }
5072 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 FreeWild(num_p, p);
5075 }
5076 }
5077 }
5078 if (cur != NULL)
5079 *--cur = 0; /* Replace trailing newline with NUL */
5080
5081 vim_free(buf);
5082 return (char_u *)ga.ga_data;
5083}
5084
5085#endif
5086
5087#if defined(FEAT_CMDHIST) || defined(PROTO)
5088
5089/*********************************
5090 * Command line history stuff *
5091 *********************************/
5092
5093/*
5094 * Translate a history character to the associated type number.
5095 */
5096 static int
5097hist_char2type(c)
5098 int c;
5099{
5100 if (c == ':')
5101 return HIST_CMD;
5102 if (c == '=')
5103 return HIST_EXPR;
5104 if (c == '@')
5105 return HIST_INPUT;
5106 if (c == '>')
5107 return HIST_DEBUG;
5108 return HIST_SEARCH; /* must be '?' or '/' */
5109}
5110
5111/*
5112 * Table of history names.
5113 * These names are used in :history and various hist...() functions.
5114 * It is sufficient to give the significant prefix of a history name.
5115 */
5116
5117static char *(history_names[]) =
5118{
5119 "cmd",
5120 "search",
5121 "expr",
5122 "input",
5123 "debug",
5124 NULL
5125};
5126
5127/*
5128 * init_history() - Initialize the command line history.
5129 * Also used to re-allocate the history when the size changes.
5130 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005131 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132init_history()
5133{
5134 int newlen; /* new length of history table */
5135 histentry_T *temp;
5136 int i;
5137 int j;
5138 int type;
5139
5140 /*
5141 * If size of history table changed, reallocate it
5142 */
5143 newlen = (int)p_hi;
5144 if (newlen != hislen) /* history length changed */
5145 {
5146 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5147 {
5148 if (newlen)
5149 {
5150 temp = (histentry_T *)lalloc(
5151 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5152 if (temp == NULL) /* out of memory! */
5153 {
5154 if (type == 0) /* first one: just keep the old length */
5155 {
5156 newlen = hislen;
5157 break;
5158 }
5159 /* Already changed one table, now we can only have zero
5160 * length for all tables. */
5161 newlen = 0;
5162 type = -1;
5163 continue;
5164 }
5165 }
5166 else
5167 temp = NULL;
5168 if (newlen == 0 || temp != NULL)
5169 {
5170 if (hisidx[type] < 0) /* there are no entries yet */
5171 {
5172 for (i = 0; i < newlen; ++i)
5173 {
5174 temp[i].hisnum = 0;
5175 temp[i].hisstr = NULL;
5176 }
5177 }
5178 else if (newlen > hislen) /* array becomes bigger */
5179 {
5180 for (i = 0; i <= hisidx[type]; ++i)
5181 temp[i] = history[type][i];
5182 j = i;
5183 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5184 {
5185 temp[i].hisnum = 0;
5186 temp[i].hisstr = NULL;
5187 }
5188 for ( ; j < hislen; ++i, ++j)
5189 temp[i] = history[type][j];
5190 }
5191 else /* array becomes smaller or 0 */
5192 {
5193 j = hisidx[type];
5194 for (i = newlen - 1; ; --i)
5195 {
5196 if (i >= 0) /* copy newest entries */
5197 temp[i] = history[type][j];
5198 else /* remove older entries */
5199 vim_free(history[type][j].hisstr);
5200 if (--j < 0)
5201 j = hislen - 1;
5202 if (j == hisidx[type])
5203 break;
5204 }
5205 hisidx[type] = newlen - 1;
5206 }
5207 vim_free(history[type]);
5208 history[type] = temp;
5209 }
5210 }
5211 hislen = newlen;
5212 }
5213}
5214
5215/*
5216 * Check if command line 'str' is already in history.
5217 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5218 */
5219 static int
5220in_history(type, str, move_to_front)
5221 int type;
5222 char_u *str;
5223 int move_to_front; /* Move the entry to the front if it exists */
5224{
5225 int i;
5226 int last_i = -1;
5227
5228 if (hisidx[type] < 0)
5229 return FALSE;
5230 i = hisidx[type];
5231 do
5232 {
5233 if (history[type][i].hisstr == NULL)
5234 return FALSE;
5235 if (STRCMP(str, history[type][i].hisstr) == 0)
5236 {
5237 if (!move_to_front)
5238 return TRUE;
5239 last_i = i;
5240 break;
5241 }
5242 if (--i < 0)
5243 i = hislen - 1;
5244 } while (i != hisidx[type]);
5245
5246 if (last_i >= 0)
5247 {
5248 str = history[type][i].hisstr;
5249 while (i != hisidx[type])
5250 {
5251 if (++i >= hislen)
5252 i = 0;
5253 history[type][last_i] = history[type][i];
5254 last_i = i;
5255 }
5256 history[type][i].hisstr = str;
5257 history[type][i].hisnum = ++hisnum[type];
5258 return TRUE;
5259 }
5260 return FALSE;
5261}
5262
5263/*
5264 * Convert history name (from table above) to its HIST_ equivalent.
5265 * When "name" is empty, return "cmd" history.
5266 * Returns -1 for unknown history name.
5267 */
5268 int
5269get_histtype(name)
5270 char_u *name;
5271{
5272 int i;
5273 int len = (int)STRLEN(name);
5274
5275 /* No argument: use current history. */
5276 if (len == 0)
5277 return hist_char2type(ccline.cmdfirstc);
5278
5279 for (i = 0; history_names[i] != NULL; ++i)
5280 if (STRNICMP(name, history_names[i], len) == 0)
5281 return i;
5282
5283 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5284 return hist_char2type(name[0]);
5285
5286 return -1;
5287}
5288
5289static int last_maptick = -1; /* last seen maptick */
5290
5291/*
5292 * Add the given string to the given history. If the string is already in the
5293 * history then it is moved to the front. "histype" may be one of he HIST_
5294 * values.
5295 */
5296 void
5297add_to_history(histype, new_entry, in_map, sep)
5298 int histype;
5299 char_u *new_entry;
5300 int in_map; /* consider maptick when inside a mapping */
5301 int sep; /* separator character used (search hist) */
5302{
5303 histentry_T *hisptr;
5304 int len;
5305
5306 if (hislen == 0) /* no history */
5307 return;
5308
5309 /*
5310 * Searches inside the same mapping overwrite each other, so that only
5311 * the last line is kept. Be careful not to remove a line that was moved
5312 * down, only lines that were added.
5313 */
5314 if (histype == HIST_SEARCH && in_map)
5315 {
5316 if (maptick == last_maptick)
5317 {
5318 /* Current line is from the same mapping, remove it */
5319 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5320 vim_free(hisptr->hisstr);
5321 hisptr->hisstr = NULL;
5322 hisptr->hisnum = 0;
5323 --hisnum[histype];
5324 if (--hisidx[HIST_SEARCH] < 0)
5325 hisidx[HIST_SEARCH] = hislen - 1;
5326 }
5327 last_maptick = -1;
5328 }
5329 if (!in_history(histype, new_entry, TRUE))
5330 {
5331 if (++hisidx[histype] == hislen)
5332 hisidx[histype] = 0;
5333 hisptr = &history[histype][hisidx[histype]];
5334 vim_free(hisptr->hisstr);
5335
5336 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005337 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5339 if (hisptr->hisstr != NULL)
5340 hisptr->hisstr[len + 1] = sep;
5341
5342 hisptr->hisnum = ++hisnum[histype];
5343 if (histype == HIST_SEARCH && in_map)
5344 last_maptick = maptick;
5345 }
5346}
5347
5348#if defined(FEAT_EVAL) || defined(PROTO)
5349
5350/*
5351 * Get identifier of newest history entry.
5352 * "histype" may be one of the HIST_ values.
5353 */
5354 int
5355get_history_idx(histype)
5356 int histype;
5357{
5358 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5359 || hisidx[histype] < 0)
5360 return -1;
5361
5362 return history[histype][hisidx[histype]].hisnum;
5363}
5364
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005365static struct cmdline_info *get_ccline_ptr __ARGS((void));
5366
5367/*
5368 * Get pointer to the command line info to use. cmdline_paste() may clear
5369 * ccline and put the previous value in prev_ccline.
5370 */
5371 static struct cmdline_info *
5372get_ccline_ptr()
5373{
5374 if ((State & CMDLINE) == 0)
5375 return NULL;
5376 if (ccline.cmdbuff != NULL)
5377 return &ccline;
5378 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5379 return &prev_ccline;
5380 return NULL;
5381}
5382
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383/*
5384 * Get the current command line in allocated memory.
5385 * Only works when the command line is being edited.
5386 * Returns NULL when something is wrong.
5387 */
5388 char_u *
5389get_cmdline_str()
5390{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005391 struct cmdline_info *p = get_ccline_ptr();
5392
5393 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005395 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396}
5397
5398/*
5399 * Get the current command line position, counted in bytes.
5400 * Zero is the first position.
5401 * Only works when the command line is being edited.
5402 * Returns -1 when something is wrong.
5403 */
5404 int
5405get_cmdline_pos()
5406{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005407 struct cmdline_info *p = get_ccline_ptr();
5408
5409 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005411 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412}
5413
5414/*
5415 * Set the command line byte position to "pos". Zero is the first position.
5416 * Only works when the command line is being edited.
5417 * Returns 1 when failed, 0 when OK.
5418 */
5419 int
5420set_cmdline_pos(pos)
5421 int pos;
5422{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005423 struct cmdline_info *p = get_ccline_ptr();
5424
5425 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 return 1;
5427
5428 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5429 * changed the command line. */
5430 if (pos < 0)
5431 new_cmdpos = 0;
5432 else
5433 new_cmdpos = pos;
5434 return 0;
5435}
5436
5437/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005438 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005439 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005440 * Only works when the command line is being edited.
5441 * Returns NUL when something is wrong.
5442 */
5443 int
5444get_cmdline_type()
5445{
5446 struct cmdline_info *p = get_ccline_ptr();
5447
5448 if (p == NULL)
5449 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005450 if (p->cmdfirstc == NUL)
5451 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005452 return p->cmdfirstc;
5453}
5454
5455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 * Calculate history index from a number:
5457 * num > 0: seen as identifying number of a history entry
5458 * num < 0: relative position in history wrt newest entry
5459 * "histype" may be one of the HIST_ values.
5460 */
5461 static int
5462calc_hist_idx(histype, num)
5463 int histype;
5464 int num;
5465{
5466 int i;
5467 histentry_T *hist;
5468 int wrapped = FALSE;
5469
5470 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5471 || (i = hisidx[histype]) < 0 || num == 0)
5472 return -1;
5473
5474 hist = history[histype];
5475 if (num > 0)
5476 {
5477 while (hist[i].hisnum > num)
5478 if (--i < 0)
5479 {
5480 if (wrapped)
5481 break;
5482 i += hislen;
5483 wrapped = TRUE;
5484 }
5485 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5486 return i;
5487 }
5488 else if (-num <= hislen)
5489 {
5490 i += num + 1;
5491 if (i < 0)
5492 i += hislen;
5493 if (hist[i].hisstr != NULL)
5494 return i;
5495 }
5496 return -1;
5497}
5498
5499/*
5500 * Get a history entry by its index.
5501 * "histype" may be one of the HIST_ values.
5502 */
5503 char_u *
5504get_history_entry(histype, idx)
5505 int histype;
5506 int idx;
5507{
5508 idx = calc_hist_idx(histype, idx);
5509 if (idx >= 0)
5510 return history[histype][idx].hisstr;
5511 else
5512 return (char_u *)"";
5513}
5514
5515/*
5516 * Clear all entries of a history.
5517 * "histype" may be one of the HIST_ values.
5518 */
5519 int
5520clr_history(histype)
5521 int histype;
5522{
5523 int i;
5524 histentry_T *hisptr;
5525
5526 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5527 {
5528 hisptr = history[histype];
5529 for (i = hislen; i--;)
5530 {
5531 vim_free(hisptr->hisstr);
5532 hisptr->hisnum = 0;
5533 hisptr++->hisstr = NULL;
5534 }
5535 hisidx[histype] = -1; /* mark history as cleared */
5536 hisnum[histype] = 0; /* reset identifier counter */
5537 return OK;
5538 }
5539 return FAIL;
5540}
5541
5542/*
5543 * Remove all entries matching {str} from a history.
5544 * "histype" may be one of the HIST_ values.
5545 */
5546 int
5547del_history_entry(histype, str)
5548 int histype;
5549 char_u *str;
5550{
5551 regmatch_T regmatch;
5552 histentry_T *hisptr;
5553 int idx;
5554 int i;
5555 int last;
5556 int found = FALSE;
5557
5558 regmatch.regprog = NULL;
5559 regmatch.rm_ic = FALSE; /* always match case */
5560 if (hislen != 0
5561 && histype >= 0
5562 && histype < HIST_COUNT
5563 && *str != NUL
5564 && (idx = hisidx[histype]) >= 0
5565 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5566 != NULL)
5567 {
5568 i = last = idx;
5569 do
5570 {
5571 hisptr = &history[histype][i];
5572 if (hisptr->hisstr == NULL)
5573 break;
5574 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5575 {
5576 found = TRUE;
5577 vim_free(hisptr->hisstr);
5578 hisptr->hisstr = NULL;
5579 hisptr->hisnum = 0;
5580 }
5581 else
5582 {
5583 if (i != last)
5584 {
5585 history[histype][last] = *hisptr;
5586 hisptr->hisstr = NULL;
5587 hisptr->hisnum = 0;
5588 }
5589 if (--last < 0)
5590 last += hislen;
5591 }
5592 if (--i < 0)
5593 i += hislen;
5594 } while (i != idx);
5595 if (history[histype][idx].hisstr == NULL)
5596 hisidx[histype] = -1;
5597 }
5598 vim_free(regmatch.regprog);
5599 return found;
5600}
5601
5602/*
5603 * Remove an indexed entry from a history.
5604 * "histype" may be one of the HIST_ values.
5605 */
5606 int
5607del_history_idx(histype, idx)
5608 int histype;
5609 int idx;
5610{
5611 int i, j;
5612
5613 i = calc_hist_idx(histype, idx);
5614 if (i < 0)
5615 return FALSE;
5616 idx = hisidx[histype];
5617 vim_free(history[histype][i].hisstr);
5618
5619 /* When deleting the last added search string in a mapping, reset
5620 * last_maptick, so that the last added search string isn't deleted again.
5621 */
5622 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5623 last_maptick = -1;
5624
5625 while (i != idx)
5626 {
5627 j = (i + 1) % hislen;
5628 history[histype][i] = history[histype][j];
5629 i = j;
5630 }
5631 history[histype][i].hisstr = NULL;
5632 history[histype][i].hisnum = 0;
5633 if (--i < 0)
5634 i += hislen;
5635 hisidx[histype] = i;
5636 return TRUE;
5637}
5638
5639#endif /* FEAT_EVAL */
5640
5641#if defined(FEAT_CRYPT) || defined(PROTO)
5642/*
5643 * Very specific function to remove the value in ":set key=val" from the
5644 * history.
5645 */
5646 void
5647remove_key_from_history()
5648{
5649 char_u *p;
5650 int i;
5651
5652 i = hisidx[HIST_CMD];
5653 if (i < 0)
5654 return;
5655 p = history[HIST_CMD][i].hisstr;
5656 if (p != NULL)
5657 for ( ; *p; ++p)
5658 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5659 {
5660 p = vim_strchr(p + 3, '=');
5661 if (p == NULL)
5662 break;
5663 ++p;
5664 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5665 if (p[i] == '\\' && p[i + 1])
5666 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005667 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 --p;
5669 }
5670}
5671#endif
5672
5673#endif /* FEAT_CMDHIST */
5674
5675#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5676/*
5677 * Get indices "num1,num2" that specify a range within a list (not a range of
5678 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5679 * Returns OK if parsed successfully, otherwise FAIL.
5680 */
5681 int
5682get_list_range(str, num1, num2)
5683 char_u **str;
5684 int *num1;
5685 int *num2;
5686{
5687 int len;
5688 int first = FALSE;
5689 long num;
5690
5691 *str = skipwhite(*str);
5692 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5693 {
5694 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5695 *str += len;
5696 *num1 = (int)num;
5697 first = TRUE;
5698 }
5699 *str = skipwhite(*str);
5700 if (**str == ',') /* parse "to" part of range */
5701 {
5702 *str = skipwhite(*str + 1);
5703 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5704 if (len > 0)
5705 {
5706 *num2 = (int)num;
5707 *str = skipwhite(*str + len);
5708 }
5709 else if (!first) /* no number given at all */
5710 return FAIL;
5711 }
5712 else if (first) /* only one number given */
5713 *num2 = *num1;
5714 return OK;
5715}
5716#endif
5717
5718#if defined(FEAT_CMDHIST) || defined(PROTO)
5719/*
5720 * :history command - print a history
5721 */
5722 void
5723ex_history(eap)
5724 exarg_T *eap;
5725{
5726 histentry_T *hist;
5727 int histype1 = HIST_CMD;
5728 int histype2 = HIST_CMD;
5729 int hisidx1 = 1;
5730 int hisidx2 = -1;
5731 int idx;
5732 int i, j, k;
5733 char_u *end;
5734 char_u *arg = eap->arg;
5735
5736 if (hislen == 0)
5737 {
5738 MSG(_("'history' option is zero"));
5739 return;
5740 }
5741
5742 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5743 {
5744 end = arg;
5745 while (ASCII_ISALPHA(*end)
5746 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5747 end++;
5748 i = *end;
5749 *end = NUL;
5750 histype1 = get_histtype(arg);
5751 if (histype1 == -1)
5752 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005753 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
5755 histype1 = 0;
5756 histype2 = HIST_COUNT-1;
5757 }
5758 else
5759 {
5760 *end = i;
5761 EMSG(_(e_trailing));
5762 return;
5763 }
5764 }
5765 else
5766 histype2 = histype1;
5767 *end = i;
5768 }
5769 else
5770 end = arg;
5771 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5772 {
5773 EMSG(_(e_trailing));
5774 return;
5775 }
5776
5777 for (; !got_int && histype1 <= histype2; ++histype1)
5778 {
5779 STRCPY(IObuff, "\n # ");
5780 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5781 MSG_PUTS_TITLE(IObuff);
5782 idx = hisidx[histype1];
5783 hist = history[histype1];
5784 j = hisidx1;
5785 k = hisidx2;
5786 if (j < 0)
5787 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5788 if (k < 0)
5789 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5790 if (idx >= 0 && j <= k)
5791 for (i = idx + 1; !got_int; ++i)
5792 {
5793 if (i == hislen)
5794 i = 0;
5795 if (hist[i].hisstr != NULL
5796 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5797 {
5798 msg_putchar('\n');
5799 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5800 hist[i].hisnum);
5801 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5802 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5803 (int)Columns - 10);
5804 else
5805 STRCAT(IObuff, hist[i].hisstr);
5806 msg_outtrans(IObuff);
5807 out_flush();
5808 }
5809 if (i == idx)
5810 break;
5811 }
5812 }
5813}
5814#endif
5815
5816#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5817static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5818static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5819static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5820static int viminfo_add_at_front = FALSE;
5821
5822static int hist_type2char __ARGS((int type, int use_question));
5823
5824/*
5825 * Translate a history type number to the associated character.
5826 */
5827 static int
5828hist_type2char(type, use_question)
5829 int type;
5830 int use_question; /* use '?' instead of '/' */
5831{
5832 if (type == HIST_CMD)
5833 return ':';
5834 if (type == HIST_SEARCH)
5835 {
5836 if (use_question)
5837 return '?';
5838 else
5839 return '/';
5840 }
5841 if (type == HIST_EXPR)
5842 return '=';
5843 return '@';
5844}
5845
5846/*
5847 * Prepare for reading the history from the viminfo file.
5848 * This allocates history arrays to store the read history lines.
5849 */
5850 void
5851prepare_viminfo_history(asklen)
5852 int asklen;
5853{
5854 int i;
5855 int num;
5856 int type;
5857 int len;
5858
5859 init_history();
5860 viminfo_add_at_front = (asklen != 0);
5861 if (asklen > hislen)
5862 asklen = hislen;
5863
5864 for (type = 0; type < HIST_COUNT; ++type)
5865 {
5866 /*
5867 * Count the number of empty spaces in the history list. If there are
5868 * more spaces available than we request, then fill them up.
5869 */
5870 for (i = 0, num = 0; i < hislen; i++)
5871 if (history[type][i].hisstr == NULL)
5872 num++;
5873 len = asklen;
5874 if (num > len)
5875 len = num;
5876 if (len <= 0)
5877 viminfo_history[type] = NULL;
5878 else
5879 viminfo_history[type] =
5880 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5881 if (viminfo_history[type] == NULL)
5882 len = 0;
5883 viminfo_hislen[type] = len;
5884 viminfo_hisidx[type] = 0;
5885 }
5886}
5887
5888/*
5889 * Accept a line from the viminfo, store it in the history array when it's
5890 * new.
5891 */
5892 int
5893read_viminfo_history(virp)
5894 vir_T *virp;
5895{
5896 int type;
5897 long_u len;
5898 char_u *val;
5899 char_u *p;
5900
5901 type = hist_char2type(virp->vir_line[0]);
5902 if (viminfo_hisidx[type] < viminfo_hislen[type])
5903 {
5904 val = viminfo_readstring(virp, 1, TRUE);
5905 if (val != NULL && *val != NUL)
5906 {
5907 if (!in_history(type, val + (type == HIST_SEARCH),
5908 viminfo_add_at_front))
5909 {
5910 /* Need to re-allocate to append the separator byte. */
5911 len = STRLEN(val);
5912 p = lalloc(len + 2, TRUE);
5913 if (p != NULL)
5914 {
5915 if (type == HIST_SEARCH)
5916 {
5917 /* Search entry: Move the separator from the first
5918 * column to after the NUL. */
5919 mch_memmove(p, val + 1, (size_t)len);
5920 p[len] = (*val == ' ' ? NUL : *val);
5921 }
5922 else
5923 {
5924 /* Not a search entry: No separator in the viminfo
5925 * file, add a NUL separator. */
5926 mch_memmove(p, val, (size_t)len + 1);
5927 p[len + 1] = NUL;
5928 }
5929 viminfo_history[type][viminfo_hisidx[type]++] = p;
5930 }
5931 }
5932 }
5933 vim_free(val);
5934 }
5935 return viminfo_readline(virp);
5936}
5937
5938 void
5939finish_viminfo_history()
5940{
5941 int idx;
5942 int i;
5943 int type;
5944
5945 for (type = 0; type < HIST_COUNT; ++type)
5946 {
5947 if (history[type] == NULL)
5948 return;
5949 idx = hisidx[type] + viminfo_hisidx[type];
5950 if (idx >= hislen)
5951 idx -= hislen;
5952 else if (idx < 0)
5953 idx = hislen - 1;
5954 if (viminfo_add_at_front)
5955 hisidx[type] = idx;
5956 else
5957 {
5958 if (hisidx[type] == -1)
5959 hisidx[type] = hislen - 1;
5960 do
5961 {
5962 if (history[type][idx].hisstr != NULL)
5963 break;
5964 if (++idx == hislen)
5965 idx = 0;
5966 } while (idx != hisidx[type]);
5967 if (idx != hisidx[type] && --idx < 0)
5968 idx = hislen - 1;
5969 }
5970 for (i = 0; i < viminfo_hisidx[type]; i++)
5971 {
5972 vim_free(history[type][idx].hisstr);
5973 history[type][idx].hisstr = viminfo_history[type][i];
5974 if (--idx < 0)
5975 idx = hislen - 1;
5976 }
5977 idx += 1;
5978 idx %= hislen;
5979 for (i = 0; i < viminfo_hisidx[type]; i++)
5980 {
5981 history[type][idx++].hisnum = ++hisnum[type];
5982 idx %= hislen;
5983 }
5984 vim_free(viminfo_history[type]);
5985 viminfo_history[type] = NULL;
5986 }
5987}
5988
5989 void
5990write_viminfo_history(fp)
5991 FILE *fp;
5992{
5993 int i;
5994 int type;
5995 int num_saved;
5996 char_u *p;
5997 int c;
5998
5999 init_history();
6000 if (hislen == 0)
6001 return;
6002 for (type = 0; type < HIST_COUNT; ++type)
6003 {
6004 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6005 if (num_saved == 0)
6006 continue;
6007 if (num_saved < 0) /* Use default */
6008 num_saved = hislen;
6009 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6010 type == HIST_CMD ? _("Command Line") :
6011 type == HIST_SEARCH ? _("Search String") :
6012 type == HIST_EXPR ? _("Expression") :
6013 _("Input Line"));
6014 if (num_saved > hislen)
6015 num_saved = hislen;
6016 i = hisidx[type];
6017 if (i >= 0)
6018 while (num_saved--)
6019 {
6020 p = history[type][i].hisstr;
6021 if (p != NULL)
6022 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006023 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 /* For the search history: put the separator in the second
6025 * column; use a space if there isn't one. */
6026 if (type == HIST_SEARCH)
6027 {
6028 c = p[STRLEN(p) + 1];
6029 putc(c == NUL ? ' ' : c, fp);
6030 }
6031 viminfo_writestring(fp, p);
6032 }
6033 if (--i < 0)
6034 i = hislen - 1;
6035 }
6036 }
6037}
6038#endif /* FEAT_VIMINFO */
6039
6040#if defined(FEAT_FKMAP) || defined(PROTO)
6041/*
6042 * Write a character at the current cursor+offset position.
6043 * It is directly written into the command buffer block.
6044 */
6045 void
6046cmd_pchar(c, offset)
6047 int c, offset;
6048{
6049 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6050 {
6051 EMSG(_("E198: cmd_pchar beyond the command length"));
6052 return;
6053 }
6054 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6055 ccline.cmdbuff[ccline.cmdlen] = NUL;
6056}
6057
6058 int
6059cmd_gchar(offset)
6060 int offset;
6061{
6062 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6063 {
6064 /* EMSG(_("cmd_gchar beyond the command length")); */
6065 return NUL;
6066 }
6067 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6068}
6069#endif
6070
6071#if defined(FEAT_CMDWIN) || defined(PROTO)
6072/*
6073 * Open a window on the current command line and history. Allow editing in
6074 * the window. Returns when the window is closed.
6075 * Returns:
6076 * CR if the command is to be executed
6077 * Ctrl_C if it is to be abandoned
6078 * K_IGNORE if editing continues
6079 */
6080 static int
6081ex_window()
6082{
6083 struct cmdline_info save_ccline;
6084 buf_T *old_curbuf = curbuf;
6085 win_T *old_curwin = curwin;
6086 buf_T *bp;
6087 win_T *wp;
6088 int i;
6089 linenr_T lnum;
6090 int histtype;
6091 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006092#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 int save_restart_edit = restart_edit;
6096 int save_State = State;
6097 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006098#ifdef FEAT_RIGHTLEFT
6099 int save_cmdmsg_rl = cmdmsg_rl;
6100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101
6102 /* Can't do this recursively. Can't do it when typing a password. */
6103 if (cmdwin_type != 0
6104# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6105 || cmdline_star > 0
6106# endif
6107 )
6108 {
6109 beep_flush();
6110 return K_IGNORE;
6111 }
6112
6113 /* Save current window sizes. */
6114 win_size_save(&winsizes);
6115
6116# ifdef FEAT_AUTOCMD
6117 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006118 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006120 /* don't use a new tab page */
6121 cmdmod.tab = 0;
6122
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 /* Create a window for the command-line buffer. */
6124 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6125 {
6126 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006127# ifdef FEAT_AUTOCMD
6128 unblock_autocmds();
6129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 return K_IGNORE;
6131 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006132 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133
6134 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006135 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006136 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6138 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6139 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006140#ifdef FEAT_FOLDING
6141 curwin->w_p_fen = FALSE;
6142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006144 curwin->w_p_rl = cmdmsg_rl;
6145 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146# endif
6147# ifdef FEAT_SCROLLBIND
6148 curwin->w_p_scb = FALSE;
6149# endif
6150
6151# ifdef FEAT_AUTOCMD
6152 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006153 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154# endif
6155
Bram Moolenaar46152342005-09-07 21:18:43 +00006156 /* Showing the prompt may have set need_wait_return, reset it. */
6157 need_wait_return = FALSE;
6158
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006159 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6161 {
6162 if (p_wc == TAB)
6163 {
6164 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6165 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6166 }
6167 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6168 }
6169
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006170 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6171 * sets 'textwidth' to 78). */
6172 curbuf->b_p_tw = 0;
6173
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 /* Fill the buffer with the history. */
6175 init_history();
6176 if (hislen > 0)
6177 {
6178 i = hisidx[histtype];
6179 if (i >= 0)
6180 {
6181 lnum = 0;
6182 do
6183 {
6184 if (++i == hislen)
6185 i = 0;
6186 if (history[histtype][i].hisstr != NULL)
6187 ml_append(lnum++, history[histtype][i].hisstr,
6188 (colnr_T)0, FALSE);
6189 }
6190 while (i != hisidx[histtype]);
6191 }
6192 }
6193
6194 /* Replace the empty last line with the current command-line and put the
6195 * cursor there. */
6196 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6197 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6198 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006199 changed_line_abv_curs();
6200 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006201 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
6203 /* Save the command line info, can be used recursively. */
6204 save_ccline = ccline;
6205 ccline.cmdbuff = NULL;
6206 ccline.cmdprompt = NULL;
6207
6208 /* No Ex mode here! */
6209 exmode_active = 0;
6210
6211 State = NORMAL;
6212# ifdef FEAT_MOUSE
6213 setmouse();
6214# endif
6215
6216# ifdef FEAT_AUTOCMD
6217 /* Trigger CmdwinEnter autocommands. */
6218 typestr[0] = cmdwin_type;
6219 typestr[1] = NUL;
6220 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006221 if (restart_edit != 0) /* autocmd with ":startinsert" */
6222 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223# endif
6224
6225 i = RedrawingDisabled;
6226 RedrawingDisabled = 0;
6227
6228 /*
6229 * Call the main loop until <CR> or CTRL-C is typed.
6230 */
6231 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006232 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233
6234 RedrawingDisabled = i;
6235
6236# ifdef FEAT_AUTOCMD
6237 /* Trigger CmdwinLeave autocommands. */
6238 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6239# endif
6240
Bram Moolenaarccc18222007-05-10 18:25:20 +00006241 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 ccline = save_ccline;
6243 cmdwin_type = 0;
6244
6245 exmode_active = save_exmode;
6246
Bram Moolenaarf9821062008-06-20 16:31:07 +00006247 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 * this happens! */
6249 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6250 {
6251 cmdwin_result = Ctrl_C;
6252 EMSG(_("E199: Active window or buffer deleted"));
6253 }
6254 else
6255 {
6256# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6257 /* autocmds may abort script processing */
6258 if (aborting() && cmdwin_result != K_IGNORE)
6259 cmdwin_result = Ctrl_C;
6260# endif
6261 /* Set the new command line from the cmdline buffer. */
6262 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006263 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006265 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6266
6267 if (histtype == HIST_CMD)
6268 {
6269 /* Execute the command directly. */
6270 ccline.cmdbuff = vim_strsave((char_u *)p);
6271 cmdwin_result = CAR;
6272 }
6273 else
6274 {
6275 /* First need to cancel what we were doing. */
6276 ccline.cmdbuff = NULL;
6277 stuffcharReadbuff(':');
6278 stuffReadbuff((char_u *)p);
6279 stuffcharReadbuff(CAR);
6280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 }
6282 else if (cmdwin_result == K_XF2) /* :qa typed */
6283 {
6284 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6285 cmdwin_result = CAR;
6286 }
6287 else
6288 ccline.cmdbuff = vim_strsave(ml_get_curline());
6289 if (ccline.cmdbuff == NULL)
6290 cmdwin_result = Ctrl_C;
6291 else
6292 {
6293 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6294 ccline.cmdbufflen = ccline.cmdlen + 1;
6295 ccline.cmdpos = curwin->w_cursor.col;
6296 if (ccline.cmdpos > ccline.cmdlen)
6297 ccline.cmdpos = ccline.cmdlen;
6298 if (cmdwin_result == K_IGNORE)
6299 {
6300 set_cmdspos_cursor();
6301 redrawcmd();
6302 }
6303 }
6304
6305# ifdef FEAT_AUTOCMD
6306 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006307 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308# endif
6309 wp = curwin;
6310 bp = curbuf;
6311 win_goto(old_curwin);
6312 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006313
6314 /* win_close() may have already wiped the buffer when 'bh' is
6315 * set to 'wipe' */
6316 if (buf_valid(bp))
6317 close_buffer(NULL, bp, DOBUF_WIPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
6319 /* Restore window sizes. */
6320 win_size_restore(&winsizes);
6321
6322# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006323 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324# endif
6325 }
6326
6327 ga_clear(&winsizes);
6328 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006329# ifdef FEAT_RIGHTLEFT
6330 cmdmsg_rl = save_cmdmsg_rl;
6331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332
6333 State = save_State;
6334# ifdef FEAT_MOUSE
6335 setmouse();
6336# endif
6337
6338 return cmdwin_result;
6339}
6340#endif /* FEAT_CMDWIN */
6341
6342/*
6343 * Used for commands that either take a simple command string argument, or:
6344 * cmd << endmarker
6345 * {script}
6346 * endmarker
6347 * Returns a pointer to allocated memory with {script} or NULL.
6348 */
6349 char_u *
6350script_get(eap, cmd)
6351 exarg_T *eap;
6352 char_u *cmd;
6353{
6354 char_u *theline;
6355 char *end_pattern = NULL;
6356 char dot[] = ".";
6357 garray_T ga;
6358
6359 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6360 return NULL;
6361
6362 ga_init2(&ga, 1, 0x400);
6363
6364 if (cmd[2] != NUL)
6365 end_pattern = (char *)skipwhite(cmd + 2);
6366 else
6367 end_pattern = dot;
6368
6369 for (;;)
6370 {
6371 theline = eap->getline(
6372#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006373 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374#endif
6375 NUL, eap->cookie, 0);
6376
6377 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006378 {
6379 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
6383 ga_concat(&ga, theline);
6384 ga_append(&ga, '\n');
6385 vim_free(theline);
6386 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006387 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388
6389 return (char_u *)ga.ga_data;
6390}