xref: /vim-8.2.3635/src/cmdexpand.c (revision ed37d9b3)
1 /* vi:set ts=8 sts=4 sw=4 noet:
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  * cmdexpand.c: functions for command-line completion
12  */
13 
14 #include "vim.h"
15 
16 static int	cmd_showtail;	// Only show path tail in lists ?
17 
18 static void	set_expand_context(expand_T *xp);
19 static int      ExpandGeneric(expand_T *xp, regmatch_T *regmatch,
20 			      int *num_file, char_u ***file,
21 			      char_u *((*func)(expand_T *, int)), int escaped);
22 static int	ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
23 static int	expand_showtail(expand_T *xp);
24 static int	expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
25 #if defined(FEAT_EVAL)
26 static int	ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
27 static int	ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
28 #endif
29 
30     static int
31 sort_func_compare(const void *s1, const void *s2)
32 {
33     char_u *p1 = *(char_u **)s1;
34     char_u *p2 = *(char_u **)s2;
35 
36     if (*p1 != '<' && *p2 == '<') return -1;
37     if (*p1 == '<' && *p2 != '<') return 1;
38     return STRCMP(p1, p2);
39 }
40 
41     static void
42 ExpandEscape(
43     expand_T	*xp,
44     char_u	*str,
45     int		numfiles,
46     char_u	**files,
47     int		options)
48 {
49     int		i;
50     char_u	*p;
51 
52     // May change home directory back to "~"
53     if (options & WILD_HOME_REPLACE)
54 	tilde_replace(str, numfiles, files);
55 
56     if (options & WILD_ESCAPE)
57     {
58 	if (xp->xp_context == EXPAND_FILES
59 		|| xp->xp_context == EXPAND_FILES_IN_PATH
60 		|| xp->xp_context == EXPAND_SHELLCMD
61 		|| xp->xp_context == EXPAND_BUFFERS
62 		|| xp->xp_context == EXPAND_DIRECTORIES)
63 	{
64 	    // Insert a backslash into a file name before a space, \, %, #
65 	    // and wildmatch characters, except '~'.
66 	    for (i = 0; i < numfiles; ++i)
67 	    {
68 		// for ":set path=" we need to escape spaces twice
69 		if (xp->xp_backslash == XP_BS_THREE)
70 		{
71 		    p = vim_strsave_escaped(files[i], (char_u *)" ");
72 		    if (p != NULL)
73 		    {
74 			vim_free(files[i]);
75 			files[i] = p;
76 #if defined(BACKSLASH_IN_FILENAME)
77 			p = vim_strsave_escaped(files[i], (char_u *)" ");
78 			if (p != NULL)
79 			{
80 			    vim_free(files[i]);
81 			    files[i] = p;
82 			}
83 #endif
84 		    }
85 		}
86 #ifdef BACKSLASH_IN_FILENAME
87 		p = vim_strsave_fnameescape(files[i], FALSE);
88 #else
89 		p = vim_strsave_fnameescape(files[i], xp->xp_shell);
90 #endif
91 		if (p != NULL)
92 		{
93 		    vim_free(files[i]);
94 		    files[i] = p;
95 		}
96 
97 		// If 'str' starts with "\~", replace "~" at start of
98 		// files[i] with "\~".
99 		if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
100 		    escape_fname(&files[i]);
101 	    }
102 	    xp->xp_backslash = XP_BS_NONE;
103 
104 	    // If the first file starts with a '+' escape it.  Otherwise it
105 	    // could be seen as "+cmd".
106 	    if (*files[0] == '+')
107 		escape_fname(&files[0]);
108 	}
109 	else if (xp->xp_context == EXPAND_TAGS)
110 	{
111 	    // Insert a backslash before characters in a tag name that
112 	    // would terminate the ":tag" command.
113 	    for (i = 0; i < numfiles; ++i)
114 	    {
115 		p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
116 		if (p != NULL)
117 		{
118 		    vim_free(files[i]);
119 		    files[i] = p;
120 		}
121 	    }
122 	}
123     }
124 }
125 
126 /*
127  * Return FAIL if this is not an appropriate context in which to do
128  * completion of anything, return OK if it is (even if there are no matches).
129  * For the caller, this means that the character is just passed through like a
130  * normal character (instead of being expanded).  This allows :s/^I^D etc.
131  */
132     int
133 nextwild(
134     expand_T	*xp,
135     int		type,
136     int		options,	// extra options for ExpandOne()
137     int		escape)		// if TRUE, escape the returned matches
138 {
139     cmdline_info_T	*ccline = get_cmdline_info();
140     int		i, j;
141     char_u	*p1;
142     char_u	*p2;
143     int		difflen;
144     int		v;
145 
146     if (xp->xp_numfiles == -1)
147     {
148 	set_expand_context(xp);
149 	cmd_showtail = expand_showtail(xp);
150     }
151 
152     if (xp->xp_context == EXPAND_UNSUCCESSFUL)
153     {
154 	beep_flush();
155 	return OK;  // Something illegal on command line
156     }
157     if (xp->xp_context == EXPAND_NOTHING)
158     {
159 	// Caller can use the character as a normal char instead
160 	return FAIL;
161     }
162 
163     msg_puts("...");	    // show that we are busy
164     out_flush();
165 
166     i = (int)(xp->xp_pattern - ccline->cmdbuff);
167     xp->xp_pattern_len = ccline->cmdpos - i;
168 
169     if (type == WILD_NEXT || type == WILD_PREV)
170     {
171 	// Get next/previous match for a previous expanded pattern.
172 	p2 = ExpandOne(xp, NULL, NULL, 0, type);
173     }
174     else
175     {
176 	// Translate string into pattern and expand it.
177 	if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
178 						     xp->xp_context)) == NULL)
179 	    p2 = NULL;
180 	else
181 	{
182 	    int use_options = options |
183 		    WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
184 	    if (escape)
185 		use_options |= WILD_ESCAPE;
186 
187 	    if (p_wic)
188 		use_options += WILD_ICASE;
189 	    p2 = ExpandOne(xp, p1,
190 			 vim_strnsave(&ccline->cmdbuff[i], xp->xp_pattern_len),
191 							   use_options, type);
192 	    vim_free(p1);
193 	    // longest match: make sure it is not shorter, happens with :help
194 	    if (p2 != NULL && type == WILD_LONGEST)
195 	    {
196 		for (j = 0; j < xp->xp_pattern_len; ++j)
197 		     if (ccline->cmdbuff[i + j] == '*'
198 			     || ccline->cmdbuff[i + j] == '?')
199 			 break;
200 		if ((int)STRLEN(p2) < j)
201 		    VIM_CLEAR(p2);
202 	    }
203 	}
204     }
205 
206     if (p2 != NULL && !got_int)
207     {
208 	difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
209 	if (ccline->cmdlen + difflen + 4 > ccline->cmdbufflen)
210 	{
211 	    v = realloc_cmdbuff(ccline->cmdlen + difflen + 4);
212 	    xp->xp_pattern = ccline->cmdbuff + i;
213 	}
214 	else
215 	    v = OK;
216 	if (v == OK)
217 	{
218 	    mch_memmove(&ccline->cmdbuff[ccline->cmdpos + difflen],
219 		    &ccline->cmdbuff[ccline->cmdpos],
220 		    (size_t)(ccline->cmdlen - ccline->cmdpos + 1));
221 	    mch_memmove(&ccline->cmdbuff[i], p2, STRLEN(p2));
222 	    ccline->cmdlen += difflen;
223 	    ccline->cmdpos += difflen;
224 	}
225     }
226     vim_free(p2);
227 
228     redrawcmd();
229     cursorcmd();
230 
231     // When expanding a ":map" command and no matches are found, assume that
232     // the key is supposed to be inserted literally
233     if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
234 	return FAIL;
235 
236     if (xp->xp_numfiles <= 0 && p2 == NULL)
237 	beep_flush();
238     else if (xp->xp_numfiles == 1)
239 	// free expanded pattern
240 	(void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
241 
242     return OK;
243 }
244 
245 /*
246  * Do wildcard expansion on the string 'str'.
247  * Chars that should not be expanded must be preceded with a backslash.
248  * Return a pointer to allocated memory containing the new string.
249  * Return NULL for failure.
250  *
251  * "orig" is the originally expanded string, copied to allocated memory.  It
252  * should either be kept in orig_save or freed.  When "mode" is WILD_NEXT or
253  * WILD_PREV "orig" should be NULL.
254  *
255  * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
256  * is WILD_EXPAND_FREE or WILD_ALL.
257  *
258  * mode = WILD_FREE:	    just free previously expanded matches
259  * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
260  * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
261  * mode = WILD_NEXT:	    use next match in multiple match, wrap to first
262  * mode = WILD_PREV:	    use previous match in multiple match, wrap to first
263  * mode = WILD_ALL:	    return all matches concatenated
264  * mode = WILD_LONGEST:	    return longest matched part
265  * mode = WILD_ALL_KEEP:    get all matches, keep matches
266  *
267  * options = WILD_LIST_NOTFOUND:    list entries without a match
268  * options = WILD_HOME_REPLACE:	    do home_replace() for buffer names
269  * options = WILD_USE_NL:	    Use '\n' for WILD_ALL
270  * options = WILD_NO_BEEP:	    Don't beep for multiple matches
271  * options = WILD_ADD_SLASH:	    add a slash after directory names
272  * options = WILD_KEEP_ALL:	    don't remove 'wildignore' entries
273  * options = WILD_SILENT:	    don't print warning messages
274  * options = WILD_ESCAPE:	    put backslash before special chars
275  * options = WILD_ICASE:	    ignore case for files
276  *
277  * The variables xp->xp_context and xp->xp_backslash must have been set!
278  */
279     char_u *
280 ExpandOne(
281     expand_T	*xp,
282     char_u	*str,
283     char_u	*orig,	    // allocated copy of original of expanded string
284     int		options,
285     int		mode)
286 {
287     char_u	*ss = NULL;
288     static int	findex;
289     static char_u *orig_save = NULL;	// kept value of orig
290     int		orig_saved = FALSE;
291     int		i;
292     long_u	len;
293     int		non_suf_match;		// number without matching suffix
294 
295     // first handle the case of using an old match
296     if (mode == WILD_NEXT || mode == WILD_PREV)
297     {
298 	if (xp->xp_numfiles > 0)
299 	{
300 	    if (mode == WILD_PREV)
301 	    {
302 		if (findex == -1)
303 		    findex = xp->xp_numfiles;
304 		--findex;
305 	    }
306 	    else    // mode == WILD_NEXT
307 		++findex;
308 
309 	    // When wrapping around, return the original string, set findex to
310 	    // -1.
311 	    if (findex < 0)
312 	    {
313 		if (orig_save == NULL)
314 		    findex = xp->xp_numfiles - 1;
315 		else
316 		    findex = -1;
317 	    }
318 	    if (findex >= xp->xp_numfiles)
319 	    {
320 		if (orig_save == NULL)
321 		    findex = 0;
322 		else
323 		    findex = -1;
324 	    }
325 #ifdef FEAT_WILDMENU
326 	    if (p_wmnu)
327 		win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
328 							findex, cmd_showtail);
329 #endif
330 	    if (findex == -1)
331 		return vim_strsave(orig_save);
332 	    return vim_strsave(xp->xp_files[findex]);
333 	}
334 	else
335 	    return NULL;
336     }
337 
338     // free old names
339     if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
340     {
341 	FreeWild(xp->xp_numfiles, xp->xp_files);
342 	xp->xp_numfiles = -1;
343 	VIM_CLEAR(orig_save);
344     }
345     findex = 0;
346 
347     if (mode == WILD_FREE)	// only release file name
348 	return NULL;
349 
350     if (xp->xp_numfiles == -1)
351     {
352 	vim_free(orig_save);
353 	orig_save = orig;
354 	orig_saved = TRUE;
355 
356 	// Do the expansion.
357 	if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
358 							     options) == FAIL)
359 	{
360 #ifdef FNAME_ILLEGAL
361 	    // Illegal file name has been silently skipped.  But when there
362 	    // are wildcards, the real problem is that there was no match,
363 	    // causing the pattern to be added, which has illegal characters.
364 	    if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
365 		semsg(_(e_nomatch2), str);
366 #endif
367 	}
368 	else if (xp->xp_numfiles == 0)
369 	{
370 	    if (!(options & WILD_SILENT))
371 		semsg(_(e_nomatch2), str);
372 	}
373 	else
374 	{
375 	    // Escape the matches for use on the command line.
376 	    ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
377 
378 	    // Check for matching suffixes in file names.
379 	    if (mode != WILD_ALL && mode != WILD_ALL_KEEP
380 						      && mode != WILD_LONGEST)
381 	    {
382 		if (xp->xp_numfiles)
383 		    non_suf_match = xp->xp_numfiles;
384 		else
385 		    non_suf_match = 1;
386 		if ((xp->xp_context == EXPAND_FILES
387 			    || xp->xp_context == EXPAND_DIRECTORIES)
388 			&& xp->xp_numfiles > 1)
389 		{
390 		    // More than one match; check suffix.
391 		    // The files will have been sorted on matching suffix in
392 		    // expand_wildcards, only need to check the first two.
393 		    non_suf_match = 0;
394 		    for (i = 0; i < 2; ++i)
395 			if (match_suffix(xp->xp_files[i]))
396 			    ++non_suf_match;
397 		}
398 		if (non_suf_match != 1)
399 		{
400 		    // Can we ever get here unless it's while expanding
401 		    // interactively?  If not, we can get rid of this all
402 		    // together. Don't really want to wait for this message
403 		    // (and possibly have to hit return to continue!).
404 		    if (!(options & WILD_SILENT))
405 			emsg(_(e_toomany));
406 		    else if (!(options & WILD_NO_BEEP))
407 			beep_flush();
408 		}
409 		if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
410 		    ss = vim_strsave(xp->xp_files[0]);
411 	    }
412 	}
413     }
414 
415     // Find longest common part
416     if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
417     {
418 	int mb_len = 1;
419 	int c0, ci;
420 
421 	for (len = 0; xp->xp_files[0][len]; len += mb_len)
422 	{
423 	    if (has_mbyte)
424 	    {
425 		mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
426 		c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
427 	    }
428 	    else
429 		c0 = xp->xp_files[0][len];
430 	    for (i = 1; i < xp->xp_numfiles; ++i)
431 	    {
432 		if (has_mbyte)
433 		    ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
434 		else
435 		    ci = xp->xp_files[i][len];
436 		if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
437 			|| xp->xp_context == EXPAND_FILES
438 			|| xp->xp_context == EXPAND_SHELLCMD
439 			|| xp->xp_context == EXPAND_BUFFERS))
440 		{
441 		    if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
442 			break;
443 		}
444 		else if (c0 != ci)
445 		    break;
446 	    }
447 	    if (i < xp->xp_numfiles)
448 	    {
449 		if (!(options & WILD_NO_BEEP))
450 		    vim_beep(BO_WILD);
451 		break;
452 	    }
453 	}
454 
455 	ss = alloc(len + 1);
456 	if (ss)
457 	    vim_strncpy(ss, xp->xp_files[0], (size_t)len);
458 	findex = -1;			    // next p_wc gets first one
459     }
460 
461     // Concatenate all matching names
462     if (mode == WILD_ALL && xp->xp_numfiles > 0)
463     {
464 	len = 0;
465 	for (i = 0; i < xp->xp_numfiles; ++i)
466 	    len += (long_u)STRLEN(xp->xp_files[i]) + 1;
467 	ss = alloc(len);
468 	if (ss != NULL)
469 	{
470 	    *ss = NUL;
471 	    for (i = 0; i < xp->xp_numfiles; ++i)
472 	    {
473 		STRCAT(ss, xp->xp_files[i]);
474 		if (i != xp->xp_numfiles - 1)
475 		    STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
476 	    }
477 	}
478     }
479 
480     if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
481 	ExpandCleanup(xp);
482 
483     // Free "orig" if it wasn't stored in "orig_save".
484     if (!orig_saved)
485 	vim_free(orig);
486 
487     return ss;
488 }
489 
490 /*
491  * Prepare an expand structure for use.
492  */
493     void
494 ExpandInit(expand_T *xp)
495 {
496     xp->xp_pattern = NULL;
497     xp->xp_pattern_len = 0;
498     xp->xp_backslash = XP_BS_NONE;
499 #ifndef BACKSLASH_IN_FILENAME
500     xp->xp_shell = FALSE;
501 #endif
502     xp->xp_numfiles = -1;
503     xp->xp_files = NULL;
504 #if defined(FEAT_EVAL)
505     xp->xp_arg = NULL;
506 #endif
507     xp->xp_line = NULL;
508 }
509 
510 /*
511  * Cleanup an expand structure after use.
512  */
513     void
514 ExpandCleanup(expand_T *xp)
515 {
516     if (xp->xp_numfiles >= 0)
517     {
518 	FreeWild(xp->xp_numfiles, xp->xp_files);
519 	xp->xp_numfiles = -1;
520     }
521 }
522 
523 /*
524  * Show all matches for completion on the command line.
525  * Returns EXPAND_NOTHING when the character that triggered expansion should
526  * be inserted like a normal character.
527  */
528     int
529 showmatches(expand_T *xp, int wildmenu UNUSED)
530 {
531     cmdline_info_T	*ccline = get_cmdline_info();
532 #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
533     int		num_files;
534     char_u	**files_found;
535     int		i, j, k;
536     int		maxlen;
537     int		lines;
538     int		columns;
539     char_u	*p;
540     int		lastlen;
541     int		attr;
542     int		showtail;
543 
544     if (xp->xp_numfiles == -1)
545     {
546 	set_expand_context(xp);
547 	i = expand_cmdline(xp, ccline->cmdbuff, ccline->cmdpos,
548 						    &num_files, &files_found);
549 	showtail = expand_showtail(xp);
550 	if (i != EXPAND_OK)
551 	    return i;
552 
553     }
554     else
555     {
556 	num_files = xp->xp_numfiles;
557 	files_found = xp->xp_files;
558 	showtail = cmd_showtail;
559     }
560 
561 #ifdef FEAT_WILDMENU
562     if (!wildmenu)
563     {
564 #endif
565 	msg_didany = FALSE;		// lines_left will be set
566 	msg_start();			// prepare for paging
567 	msg_putchar('\n');
568 	out_flush();
569 	cmdline_row = msg_row;
570 	msg_didany = FALSE;		// lines_left will be set again
571 	msg_start();			// prepare for paging
572 #ifdef FEAT_WILDMENU
573     }
574 #endif
575 
576     if (got_int)
577 	got_int = FALSE;	// only int. the completion, not the cmd line
578 #ifdef FEAT_WILDMENU
579     else if (wildmenu)
580 	win_redr_status_matches(xp, num_files, files_found, -1, showtail);
581 #endif
582     else
583     {
584 	// find the length of the longest file name
585 	maxlen = 0;
586 	for (i = 0; i < num_files; ++i)
587 	{
588 	    if (!showtail && (xp->xp_context == EXPAND_FILES
589 			  || xp->xp_context == EXPAND_SHELLCMD
590 			  || xp->xp_context == EXPAND_BUFFERS))
591 	    {
592 		home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
593 		j = vim_strsize(NameBuff);
594 	    }
595 	    else
596 		j = vim_strsize(L_SHOWFILE(i));
597 	    if (j > maxlen)
598 		maxlen = j;
599 	}
600 
601 	if (xp->xp_context == EXPAND_TAGS_LISTFILES)
602 	    lines = num_files;
603 	else
604 	{
605 	    // compute the number of columns and lines for the listing
606 	    maxlen += 2;    // two spaces between file names
607 	    columns = ((int)Columns + 2) / maxlen;
608 	    if (columns < 1)
609 		columns = 1;
610 	    lines = (num_files + columns - 1) / columns;
611 	}
612 
613 	attr = HL_ATTR(HLF_D);	// find out highlighting for directories
614 
615 	if (xp->xp_context == EXPAND_TAGS_LISTFILES)
616 	{
617 	    msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
618 	    msg_clr_eos();
619 	    msg_advance(maxlen - 3);
620 	    msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
621 	}
622 
623 	// list the files line by line
624 	for (i = 0; i < lines; ++i)
625 	{
626 	    lastlen = 999;
627 	    for (k = i; k < num_files; k += lines)
628 	    {
629 		if (xp->xp_context == EXPAND_TAGS_LISTFILES)
630 		{
631 		    msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
632 		    p = files_found[k] + STRLEN(files_found[k]) + 1;
633 		    msg_advance(maxlen + 1);
634 		    msg_puts((char *)p);
635 		    msg_advance(maxlen + 3);
636 		    msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
637 		    break;
638 		}
639 		for (j = maxlen - lastlen; --j >= 0; )
640 		    msg_putchar(' ');
641 		if (xp->xp_context == EXPAND_FILES
642 					  || xp->xp_context == EXPAND_SHELLCMD
643 					  || xp->xp_context == EXPAND_BUFFERS)
644 		{
645 		    // highlight directories
646 		    if (xp->xp_numfiles != -1)
647 		    {
648 			char_u	*halved_slash;
649 			char_u	*exp_path;
650 			char_u	*path;
651 
652 			// Expansion was done before and special characters
653 			// were escaped, need to halve backslashes.  Also
654 			// $HOME has been replaced with ~/.
655 			exp_path = expand_env_save_opt(files_found[k], TRUE);
656 			path = exp_path != NULL ? exp_path : files_found[k];
657 			halved_slash = backslash_halve_save(path);
658 			j = mch_isdir(halved_slash != NULL ? halved_slash
659 							    : files_found[k]);
660 			vim_free(exp_path);
661 			if (halved_slash != path)
662 			    vim_free(halved_slash);
663 		    }
664 		    else
665 			// Expansion was done here, file names are literal.
666 			j = mch_isdir(files_found[k]);
667 		    if (showtail)
668 			p = L_SHOWFILE(k);
669 		    else
670 		    {
671 			home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
672 									TRUE);
673 			p = NameBuff;
674 		    }
675 		}
676 		else
677 		{
678 		    j = FALSE;
679 		    p = L_SHOWFILE(k);
680 		}
681 		lastlen = msg_outtrans_attr(p, j ? attr : 0);
682 	    }
683 	    if (msg_col > 0)	// when not wrapped around
684 	    {
685 		msg_clr_eos();
686 		msg_putchar('\n');
687 	    }
688 	    out_flush();		    // show one line at a time
689 	    if (got_int)
690 	    {
691 		got_int = FALSE;
692 		break;
693 	    }
694 	}
695 
696 	// we redraw the command below the lines that we have just listed
697 	// This is a bit tricky, but it saves a lot of screen updating.
698 	cmdline_row = msg_row;	// will put it back later
699     }
700 
701     if (xp->xp_numfiles == -1)
702 	FreeWild(num_files, files_found);
703 
704     return EXPAND_OK;
705 }
706 
707 /*
708  * Private gettail for showmatches() (and win_redr_status_matches()):
709  * Find tail of file name path, but ignore trailing "/".
710  */
711     char_u *
712 sm_gettail(char_u *s)
713 {
714     char_u	*p;
715     char_u	*t = s;
716     int		had_sep = FALSE;
717 
718     for (p = s; *p != NUL; )
719     {
720 	if (vim_ispathsep(*p)
721 #ifdef BACKSLASH_IN_FILENAME
722 		&& !rem_backslash(p)
723 #endif
724 	   )
725 	    had_sep = TRUE;
726 	else if (had_sep)
727 	{
728 	    t = p;
729 	    had_sep = FALSE;
730 	}
731 	MB_PTR_ADV(p);
732     }
733     return t;
734 }
735 
736 /*
737  * Return TRUE if we only need to show the tail of completion matches.
738  * When not completing file names or there is a wildcard in the path FALSE is
739  * returned.
740  */
741     static int
742 expand_showtail(expand_T *xp)
743 {
744     char_u	*s;
745     char_u	*end;
746 
747     // When not completing file names a "/" may mean something different.
748     if (xp->xp_context != EXPAND_FILES
749 	    && xp->xp_context != EXPAND_SHELLCMD
750 	    && xp->xp_context != EXPAND_DIRECTORIES)
751 	return FALSE;
752 
753     end = gettail(xp->xp_pattern);
754     if (end == xp->xp_pattern)		// there is no path separator
755 	return FALSE;
756 
757     for (s = xp->xp_pattern; s < end; s++)
758     {
759 	// Skip escaped wildcards.  Only when the backslash is not a path
760 	// separator, on DOS the '*' "path\*\file" must not be skipped.
761 	if (rem_backslash(s))
762 	    ++s;
763 	else if (vim_strchr((char_u *)"*?[", *s) != NULL)
764 	    return FALSE;
765     }
766     return TRUE;
767 }
768 
769 /*
770  * Prepare a string for expansion.
771  * When expanding file names: The string will be used with expand_wildcards().
772  * Copy "fname[len]" into allocated memory and add a '*' at the end.
773  * When expanding other names: The string will be used with regcomp().  Copy
774  * the name into allocated memory and prepend "^".
775  */
776     char_u *
777 addstar(
778     char_u	*fname,
779     int		len,
780     int		context)	// EXPAND_FILES etc.
781 {
782     char_u	*retval;
783     int		i, j;
784     int		new_len;
785     char_u	*tail;
786     int		ends_in_star;
787 
788     if (context != EXPAND_FILES
789 	    && context != EXPAND_FILES_IN_PATH
790 	    && context != EXPAND_SHELLCMD
791 	    && context != EXPAND_DIRECTORIES)
792     {
793 	// Matching will be done internally (on something other than files).
794 	// So we convert the file-matching-type wildcards into our kind for
795 	// use with vim_regcomp().  First work out how long it will be:
796 
797 	// For help tags the translation is done in find_help_tags().
798 	// For a tag pattern starting with "/" no translation is needed.
799 	if (context == EXPAND_HELP
800 		|| context == EXPAND_COLORS
801 		|| context == EXPAND_COMPILER
802 		|| context == EXPAND_OWNSYNTAX
803 		|| context == EXPAND_FILETYPE
804 		|| context == EXPAND_PACKADD
805 		|| ((context == EXPAND_TAGS_LISTFILES
806 			|| context == EXPAND_TAGS)
807 		    && fname[0] == '/'))
808 	    retval = vim_strnsave(fname, len);
809 	else
810 	{
811 	    new_len = len + 2;		// +2 for '^' at start, NUL at end
812 	    for (i = 0; i < len; i++)
813 	    {
814 		if (fname[i] == '*' || fname[i] == '~')
815 		    new_len++;		// '*' needs to be replaced by ".*"
816 					// '~' needs to be replaced by "\~"
817 
818 		// Buffer names are like file names.  "." should be literal
819 		if (context == EXPAND_BUFFERS && fname[i] == '.')
820 		    new_len++;		// "." becomes "\."
821 
822 		// Custom expansion takes care of special things, match
823 		// backslashes literally (perhaps also for other types?)
824 		if ((context == EXPAND_USER_DEFINED
825 			  || context == EXPAND_USER_LIST) && fname[i] == '\\')
826 		    new_len++;		// '\' becomes "\\"
827 	    }
828 	    retval = alloc(new_len);
829 	    if (retval != NULL)
830 	    {
831 		retval[0] = '^';
832 		j = 1;
833 		for (i = 0; i < len; i++, j++)
834 		{
835 		    // Skip backslash.  But why?  At least keep it for custom
836 		    // expansion.
837 		    if (context != EXPAND_USER_DEFINED
838 			    && context != EXPAND_USER_LIST
839 			    && fname[i] == '\\'
840 			    && ++i == len)
841 			break;
842 
843 		    switch (fname[i])
844 		    {
845 			case '*':   retval[j++] = '.';
846 				    break;
847 			case '~':   retval[j++] = '\\';
848 				    break;
849 			case '?':   retval[j] = '.';
850 				    continue;
851 			case '.':   if (context == EXPAND_BUFFERS)
852 					retval[j++] = '\\';
853 				    break;
854 			case '\\':  if (context == EXPAND_USER_DEFINED
855 					    || context == EXPAND_USER_LIST)
856 					retval[j++] = '\\';
857 				    break;
858 		    }
859 		    retval[j] = fname[i];
860 		}
861 		retval[j] = NUL;
862 	    }
863 	}
864     }
865     else
866     {
867 	retval = alloc(len + 4);
868 	if (retval != NULL)
869 	{
870 	    vim_strncpy(retval, fname, len);
871 
872 	    // Don't add a star to *, ~, ~user, $var or `cmd`.
873 	    // * would become **, which walks the whole tree.
874 	    // ~ would be at the start of the file name, but not the tail.
875 	    // $ could be anywhere in the tail.
876 	    // ` could be anywhere in the file name.
877 	    // When the name ends in '$' don't add a star, remove the '$'.
878 	    tail = gettail(retval);
879 	    ends_in_star = (len > 0 && retval[len - 1] == '*');
880 #ifndef BACKSLASH_IN_FILENAME
881 	    for (i = len - 2; i >= 0; --i)
882 	    {
883 		if (retval[i] != '\\')
884 		    break;
885 		ends_in_star = !ends_in_star;
886 	    }
887 #endif
888 	    if ((*retval != '~' || tail != retval)
889 		    && !ends_in_star
890 		    && vim_strchr(tail, '$') == NULL
891 		    && vim_strchr(retval, '`') == NULL)
892 		retval[len++] = '*';
893 	    else if (len > 0 && retval[len - 1] == '$')
894 		--len;
895 	    retval[len] = NUL;
896 	}
897     }
898     return retval;
899 }
900 
901 /*
902  * Must parse the command line so far to work out what context we are in.
903  * Completion can then be done based on that context.
904  * This routine sets the variables:
905  *  xp->xp_pattern	    The start of the pattern to be expanded within
906  *				the command line (ends at the cursor).
907  *  xp->xp_context	    The type of thing to expand.  Will be one of:
908  *
909  *  EXPAND_UNSUCCESSFUL	    Used sometimes when there is something illegal on
910  *			    the command line, like an unknown command.	Caller
911  *			    should beep.
912  *  EXPAND_NOTHING	    Unrecognised context for completion, use char like
913  *			    a normal char, rather than for completion.	eg
914  *			    :s/^I/
915  *  EXPAND_COMMANDS	    Cursor is still touching the command, so complete
916  *			    it.
917  *  EXPAND_BUFFERS	    Complete file names for :buf and :sbuf commands.
918  *  EXPAND_FILES	    After command with EX_XFILE set, or after setting
919  *			    with P_EXPAND set.	eg :e ^I, :w>>^I
920  *  EXPAND_DIRECTORIES	    In some cases this is used instead of the latter
921  *			    when we know only directories are of interest.  eg
922  *			    :set dir=^I
923  *  EXPAND_SHELLCMD	    After ":!cmd", ":r !cmd"  or ":w !cmd".
924  *  EXPAND_SETTINGS	    Complete variable names.  eg :set d^I
925  *  EXPAND_BOOL_SETTINGS    Complete boolean variables only,  eg :set no^I
926  *  EXPAND_TAGS		    Complete tags from the files in p_tags.  eg :ta a^I
927  *  EXPAND_TAGS_LISTFILES   As above, but list filenames on ^D, after :tselect
928  *  EXPAND_HELP		    Complete tags from the file 'helpfile'/tags
929  *  EXPAND_EVENTS	    Complete event names
930  *  EXPAND_SYNTAX	    Complete :syntax command arguments
931  *  EXPAND_HIGHLIGHT	    Complete highlight (syntax) group names
932  *  EXPAND_AUGROUP	    Complete autocommand group names
933  *  EXPAND_USER_VARS	    Complete user defined variable names, eg :unlet a^I
934  *  EXPAND_MAPPINGS	    Complete mapping and abbreviation names,
935  *			      eg :unmap a^I , :cunab x^I
936  *  EXPAND_FUNCTIONS	    Complete internal or user defined function names,
937  *			      eg :call sub^I
938  *  EXPAND_USER_FUNC	    Complete user defined function names, eg :delf F^I
939  *  EXPAND_EXPRESSION	    Complete internal or user defined function/variable
940  *			    names in expressions, eg :while s^I
941  *  EXPAND_ENV_VARS	    Complete environment variable names
942  *  EXPAND_USER		    Complete user names
943  */
944     static void
945 set_expand_context(expand_T *xp)
946 {
947     cmdline_info_T	*ccline = get_cmdline_info();
948 
949     // only expansion for ':', '>' and '=' command-lines
950     if (ccline->cmdfirstc != ':'
951 #ifdef FEAT_EVAL
952 	    && ccline->cmdfirstc != '>' && ccline->cmdfirstc != '='
953 	    && !ccline->input_fn
954 #endif
955 	    )
956     {
957 	xp->xp_context = EXPAND_NOTHING;
958 	return;
959     }
960     set_cmd_context(xp, ccline->cmdbuff, ccline->cmdlen, ccline->cmdpos, TRUE);
961 }
962 
963 /*
964  * This is all pretty much copied from do_one_cmd(), with all the extra stuff
965  * we don't need/want deleted.	Maybe this could be done better if we didn't
966  * repeat all this stuff.  The only problem is that they may not stay
967  * perfectly compatible with each other, but then the command line syntax
968  * probably won't change that much -- webb.
969  */
970     static char_u *
971 set_one_cmd_context(
972     expand_T	*xp,
973     char_u	*buff)	    // buffer for command string
974 {
975     char_u		*p;
976     char_u		*cmd, *arg;
977     int			len = 0;
978     exarg_T		ea;
979     int			compl = EXPAND_NOTHING;
980     int			delim;
981     int			forceit = FALSE;
982     int			usefilter = FALSE;  // filter instead of file name
983 
984     ExpandInit(xp);
985     xp->xp_pattern = buff;
986     xp->xp_context = EXPAND_COMMANDS;	// Default until we get past command
987     ea.argt = 0;
988 
989     // 1. skip comment lines and leading space, colons or bars
990     for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
991 	;
992     xp->xp_pattern = cmd;
993 
994     if (*cmd == NUL)
995 	return NULL;
996     if (*cmd == '"')	    // ignore comment lines
997     {
998 	xp->xp_context = EXPAND_NOTHING;
999 	return NULL;
1000     }
1001 
1002     // 3. Skip over the range to find the command.
1003     cmd = skip_range(cmd, &xp->xp_context);
1004     xp->xp_pattern = cmd;
1005     if (*cmd == NUL)
1006 	return NULL;
1007     if (*cmd == '"')
1008     {
1009 	xp->xp_context = EXPAND_NOTHING;
1010 	return NULL;
1011     }
1012 
1013     if (*cmd == '|' || *cmd == '\n')
1014 	return cmd + 1;			// There's another command
1015 
1016     // Isolate the command and search for it in the command table.
1017     // Exceptions:
1018     // - the 'k' command can directly be followed by any character, but
1019     //   do accept "keepmarks", "keepalt" and "keepjumps".
1020     // - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
1021     if (*cmd == 'k' && cmd[1] != 'e')
1022     {
1023 	ea.cmdidx = CMD_k;
1024 	p = cmd + 1;
1025     }
1026     else
1027     {
1028 	p = cmd;
1029 	while (ASCII_ISALPHA(*p) || *p == '*')    // Allow * wild card
1030 	    ++p;
1031 	// a user command may contain digits
1032 	if (ASCII_ISUPPER(cmd[0]))
1033 	    while (ASCII_ISALNUM(*p) || *p == '*')
1034 		++p;
1035 	// for python 3.x: ":py3*" commands completion
1036 	if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
1037 	{
1038 	    ++p;
1039 	    while (ASCII_ISALPHA(*p) || *p == '*')
1040 		++p;
1041 	}
1042 	// check for non-alpha command
1043 	if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
1044 	    ++p;
1045 	len = (int)(p - cmd);
1046 
1047 	if (len == 0)
1048 	{
1049 	    xp->xp_context = EXPAND_UNSUCCESSFUL;
1050 	    return NULL;
1051 	}
1052 
1053 	ea.cmdidx = excmd_get_cmdidx(cmd, len);
1054 
1055 	if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1056 	    while (ASCII_ISALNUM(*p) || *p == '*')	// Allow * wild card
1057 		++p;
1058     }
1059 
1060     // If the cursor is touching the command, and it ends in an alpha-numeric
1061     // character, complete the command name.
1062     if (*p == NUL && ASCII_ISALNUM(p[-1]))
1063 	return NULL;
1064 
1065     if (ea.cmdidx == CMD_SIZE)
1066     {
1067 	if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
1068 	{
1069 	    ea.cmdidx = CMD_substitute;
1070 	    p = cmd + 1;
1071 	}
1072 	else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
1073 	{
1074 	    ea.cmd = cmd;
1075 	    p = find_ucmd(&ea, p, NULL, xp, &compl);
1076 	    if (p == NULL)
1077 		ea.cmdidx = CMD_SIZE;	// ambiguous user command
1078 	}
1079     }
1080     if (ea.cmdidx == CMD_SIZE)
1081     {
1082 	// Not still touching the command and it was an illegal one
1083 	xp->xp_context = EXPAND_UNSUCCESSFUL;
1084 	return NULL;
1085     }
1086 
1087     xp->xp_context = EXPAND_NOTHING; // Default now that we're past command
1088 
1089     if (*p == '!')		    // forced commands
1090     {
1091 	forceit = TRUE;
1092 	++p;
1093     }
1094 
1095     // 6. parse arguments
1096     if (!IS_USER_CMDIDX(ea.cmdidx))
1097 	ea.argt = excmd_get_argt(ea.cmdidx);
1098 
1099     arg = skipwhite(p);
1100 
1101     if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
1102     {
1103 	if (*arg == '>')			// append
1104 	{
1105 	    if (*++arg == '>')
1106 		++arg;
1107 	    arg = skipwhite(arg);
1108 	}
1109 	else if (*arg == '!' && ea.cmdidx == CMD_write)	// :w !filter
1110 	{
1111 	    ++arg;
1112 	    usefilter = TRUE;
1113 	}
1114     }
1115 
1116     if (ea.cmdidx == CMD_read)
1117     {
1118 	usefilter = forceit;			// :r! filter if forced
1119 	if (*arg == '!')			// :r !filter
1120 	{
1121 	    ++arg;
1122 	    usefilter = TRUE;
1123 	}
1124     }
1125 
1126     if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
1127     {
1128 	while (*arg == *cmd)	    // allow any number of '>' or '<'
1129 	    ++arg;
1130 	arg = skipwhite(arg);
1131     }
1132 
1133     // Does command allow "+command"?
1134     if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
1135     {
1136 	// Check if we're in the +command
1137 	p = arg + 1;
1138 	arg = skip_cmd_arg(arg, FALSE);
1139 
1140 	// Still touching the command after '+'?
1141 	if (*arg == NUL)
1142 	    return p;
1143 
1144 	// Skip space(s) after +command to get to the real argument
1145 	arg = skipwhite(arg);
1146     }
1147 
1148     // Check for '|' to separate commands and '"' to start comments.
1149     // Don't do this for ":read !cmd" and ":write !cmd".
1150     if ((ea.argt & EX_TRLBAR) && !usefilter)
1151     {
1152 	p = arg;
1153 	// ":redir @" is not the start of a comment
1154 	if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
1155 	    p += 2;
1156 	while (*p)
1157 	{
1158 	    if (*p == Ctrl_V)
1159 	    {
1160 		if (p[1] != NUL)
1161 		    ++p;
1162 	    }
1163 	    else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
1164 		    || *p == '|' || *p == '\n')
1165 	    {
1166 		if (*(p - 1) != '\\')
1167 		{
1168 		    if (*p == '|' || *p == '\n')
1169 			return p + 1;
1170 		    return NULL;    // It's a comment
1171 		}
1172 	    }
1173 	    MB_PTR_ADV(p);
1174 	}
1175     }
1176 
1177     if (!(ea.argt & EX_EXTRA) && *arg != NUL
1178 				  && vim_strchr((char_u *)"|\"", *arg) == NULL)
1179 	// no arguments allowed but there is something
1180 	return NULL;
1181 
1182     // Find start of last argument (argument just before cursor):
1183     p = buff;
1184     xp->xp_pattern = p;
1185     len = (int)STRLEN(buff);
1186     while (*p && p < buff + len)
1187     {
1188 	if (*p == ' ' || *p == TAB)
1189 	{
1190 	    // argument starts after a space
1191 	    xp->xp_pattern = ++p;
1192 	}
1193 	else
1194 	{
1195 	    if (*p == '\\' && *(p + 1) != NUL)
1196 		++p; // skip over escaped character
1197 	    MB_PTR_ADV(p);
1198 	}
1199     }
1200 
1201     if (ea.argt & EX_XFILE)
1202     {
1203 	int	c;
1204 	int	in_quote = FALSE;
1205 	char_u	*bow = NULL;	// Beginning of word
1206 
1207 	// Allow spaces within back-quotes to count as part of the argument
1208 	// being expanded.
1209 	xp->xp_pattern = skipwhite(arg);
1210 	p = xp->xp_pattern;
1211 	while (*p != NUL)
1212 	{
1213 	    if (has_mbyte)
1214 		c = mb_ptr2char(p);
1215 	    else
1216 		c = *p;
1217 	    if (c == '\\' && p[1] != NUL)
1218 		++p;
1219 	    else if (c == '`')
1220 	    {
1221 		if (!in_quote)
1222 		{
1223 		    xp->xp_pattern = p;
1224 		    bow = p + 1;
1225 		}
1226 		in_quote = !in_quote;
1227 	    }
1228 	    // An argument can contain just about everything, except
1229 	    // characters that end the command and white space.
1230 	    else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
1231 #ifdef SPACE_IN_FILENAME
1232 					&& (!(ea.argt & EX_NOSPC) || usefilter)
1233 #endif
1234 		    ))
1235 	    {
1236 		len = 0;  // avoid getting stuck when space is in 'isfname'
1237 		while (*p != NUL)
1238 		{
1239 		    if (has_mbyte)
1240 			c = mb_ptr2char(p);
1241 		    else
1242 			c = *p;
1243 		    if (c == '`' || vim_isfilec_or_wc(c))
1244 			break;
1245 		    if (has_mbyte)
1246 			len = (*mb_ptr2len)(p);
1247 		    else
1248 			len = 1;
1249 		    MB_PTR_ADV(p);
1250 		}
1251 		if (in_quote)
1252 		    bow = p;
1253 		else
1254 		    xp->xp_pattern = p;
1255 		p -= len;
1256 	    }
1257 	    MB_PTR_ADV(p);
1258 	}
1259 
1260 	// If we are still inside the quotes, and we passed a space, just
1261 	// expand from there.
1262 	if (bow != NULL && in_quote)
1263 	    xp->xp_pattern = bow;
1264 	xp->xp_context = EXPAND_FILES;
1265 
1266 	// For a shell command more chars need to be escaped.
1267 	if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
1268 	{
1269 #ifndef BACKSLASH_IN_FILENAME
1270 	    xp->xp_shell = TRUE;
1271 #endif
1272 	    // When still after the command name expand executables.
1273 	    if (xp->xp_pattern == skipwhite(arg))
1274 		xp->xp_context = EXPAND_SHELLCMD;
1275 	}
1276 
1277 	// Check for environment variable
1278 	if (*xp->xp_pattern == '$'
1279 #if defined(MSWIN)
1280 		|| *xp->xp_pattern == '%'
1281 #endif
1282 		)
1283 	{
1284 	    for (p = xp->xp_pattern + 1; *p != NUL; ++p)
1285 		if (!vim_isIDc(*p))
1286 		    break;
1287 	    if (*p == NUL)
1288 	    {
1289 		xp->xp_context = EXPAND_ENV_VARS;
1290 		++xp->xp_pattern;
1291 		// Avoid that the assignment uses EXPAND_FILES again.
1292 		if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
1293 		    compl = EXPAND_ENV_VARS;
1294 	    }
1295 	}
1296 	// Check for user names
1297 	if (*xp->xp_pattern == '~')
1298 	{
1299 	    for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
1300 		;
1301 	    // Complete ~user only if it partially matches a user name.
1302 	    // A full match ~user<Tab> will be replaced by user's home
1303 	    // directory i.e. something like ~user<Tab> -> /home/user/
1304 	    if (*p == NUL && p > xp->xp_pattern + 1
1305 				       && match_user(xp->xp_pattern + 1) >= 1)
1306 	    {
1307 		xp->xp_context = EXPAND_USER;
1308 		++xp->xp_pattern;
1309 	    }
1310 	}
1311     }
1312 
1313     // 6. Switch on command name.
1314     switch (ea.cmdidx)
1315     {
1316 	case CMD_find:
1317 	case CMD_sfind:
1318 	case CMD_tabfind:
1319 	    if (xp->xp_context == EXPAND_FILES)
1320 		xp->xp_context = EXPAND_FILES_IN_PATH;
1321 	    break;
1322 	case CMD_cd:
1323 	case CMD_chdir:
1324 	case CMD_tcd:
1325 	case CMD_tchdir:
1326 	case CMD_lcd:
1327 	case CMD_lchdir:
1328 	    if (xp->xp_context == EXPAND_FILES)
1329 		xp->xp_context = EXPAND_DIRECTORIES;
1330 	    break;
1331 	case CMD_help:
1332 	    xp->xp_context = EXPAND_HELP;
1333 	    xp->xp_pattern = arg;
1334 	    break;
1335 
1336 	// Command modifiers: return the argument.
1337 	// Also for commands with an argument that is a command.
1338 	case CMD_aboveleft:
1339 	case CMD_argdo:
1340 	case CMD_belowright:
1341 	case CMD_botright:
1342 	case CMD_browse:
1343 	case CMD_bufdo:
1344 	case CMD_cdo:
1345 	case CMD_cfdo:
1346 	case CMD_confirm:
1347 	case CMD_debug:
1348 	case CMD_folddoclosed:
1349 	case CMD_folddoopen:
1350 	case CMD_hide:
1351 	case CMD_keepalt:
1352 	case CMD_keepjumps:
1353 	case CMD_keepmarks:
1354 	case CMD_keeppatterns:
1355 	case CMD_ldo:
1356 	case CMD_leftabove:
1357 	case CMD_lfdo:
1358 	case CMD_lockmarks:
1359 	case CMD_noautocmd:
1360 	case CMD_noswapfile:
1361 	case CMD_rightbelow:
1362 	case CMD_sandbox:
1363 	case CMD_silent:
1364 	case CMD_tab:
1365 	case CMD_tabdo:
1366 	case CMD_topleft:
1367 	case CMD_verbose:
1368 	case CMD_vertical:
1369 	case CMD_windo:
1370 	    return arg;
1371 
1372 	case CMD_filter:
1373 	    if (*arg != NUL)
1374 		arg = skip_vimgrep_pat(arg, NULL, NULL);
1375 	    if (arg == NULL || *arg == NUL)
1376 	    {
1377 		xp->xp_context = EXPAND_NOTHING;
1378 		return NULL;
1379 	    }
1380 	    return skipwhite(arg);
1381 
1382 #ifdef FEAT_SEARCH_EXTRA
1383 	case CMD_match:
1384 	    if (*arg == NUL || !ends_excmd(*arg))
1385 	    {
1386 		// also complete "None"
1387 		set_context_in_echohl_cmd(xp, arg);
1388 		arg = skipwhite(skiptowhite(arg));
1389 		if (*arg != NUL)
1390 		{
1391 		    xp->xp_context = EXPAND_NOTHING;
1392 		    arg = skip_regexp(arg + 1, *arg, p_magic);
1393 		}
1394 	    }
1395 	    return find_nextcmd(arg);
1396 #endif
1397 
1398 	// All completion for the +cmdline_compl feature goes here.
1399 
1400 	case CMD_command:
1401 	    return set_context_in_user_cmd(xp, arg);
1402 
1403 	case CMD_delcommand:
1404 	    xp->xp_context = EXPAND_USER_COMMANDS;
1405 	    xp->xp_pattern = arg;
1406 	    break;
1407 
1408 	case CMD_global:
1409 	case CMD_vglobal:
1410 	    delim = *arg;	    // get the delimiter
1411 	    if (delim)
1412 		++arg;		    // skip delimiter if there is one
1413 
1414 	    while (arg[0] != NUL && arg[0] != delim)
1415 	    {
1416 		if (arg[0] == '\\' && arg[1] != NUL)
1417 		    ++arg;
1418 		++arg;
1419 	    }
1420 	    if (arg[0] != NUL)
1421 		return arg + 1;
1422 	    break;
1423 	case CMD_and:
1424 	case CMD_substitute:
1425 	    delim = *arg;
1426 	    if (delim)
1427 	    {
1428 		// skip "from" part
1429 		++arg;
1430 		arg = skip_regexp(arg, delim, p_magic);
1431 	    }
1432 	    // skip "to" part
1433 	    while (arg[0] != NUL && arg[0] != delim)
1434 	    {
1435 		if (arg[0] == '\\' && arg[1] != NUL)
1436 		    ++arg;
1437 		++arg;
1438 	    }
1439 	    if (arg[0] != NUL)	// skip delimiter
1440 		++arg;
1441 	    while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
1442 		++arg;
1443 	    if (arg[0] != NUL)
1444 		return arg;
1445 	    break;
1446 	case CMD_isearch:
1447 	case CMD_dsearch:
1448 	case CMD_ilist:
1449 	case CMD_dlist:
1450 	case CMD_ijump:
1451 	case CMD_psearch:
1452 	case CMD_djump:
1453 	case CMD_isplit:
1454 	case CMD_dsplit:
1455 	    arg = skipwhite(skipdigits(arg));	    // skip count
1456 	    if (*arg == '/')	// Match regexp, not just whole words
1457 	    {
1458 		for (++arg; *arg && *arg != '/'; arg++)
1459 		    if (*arg == '\\' && arg[1] != NUL)
1460 			arg++;
1461 		if (*arg)
1462 		{
1463 		    arg = skipwhite(arg + 1);
1464 
1465 		    // Check for trailing illegal characters
1466 		    if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
1467 			xp->xp_context = EXPAND_NOTHING;
1468 		    else
1469 			return arg;
1470 		}
1471 	    }
1472 	    break;
1473 
1474 	case CMD_autocmd:
1475 	    return set_context_in_autocmd(xp, arg, FALSE);
1476 	case CMD_doautocmd:
1477 	case CMD_doautoall:
1478 	    return set_context_in_autocmd(xp, arg, TRUE);
1479 	case CMD_set:
1480 	    set_context_in_set_cmd(xp, arg, 0);
1481 	    break;
1482 	case CMD_setglobal:
1483 	    set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
1484 	    break;
1485 	case CMD_setlocal:
1486 	    set_context_in_set_cmd(xp, arg, OPT_LOCAL);
1487 	    break;
1488 	case CMD_tag:
1489 	case CMD_stag:
1490 	case CMD_ptag:
1491 	case CMD_ltag:
1492 	case CMD_tselect:
1493 	case CMD_stselect:
1494 	case CMD_ptselect:
1495 	case CMD_tjump:
1496 	case CMD_stjump:
1497 	case CMD_ptjump:
1498 	    if (*p_wop != NUL)
1499 		xp->xp_context = EXPAND_TAGS_LISTFILES;
1500 	    else
1501 		xp->xp_context = EXPAND_TAGS;
1502 	    xp->xp_pattern = arg;
1503 	    break;
1504 	case CMD_augroup:
1505 	    xp->xp_context = EXPAND_AUGROUP;
1506 	    xp->xp_pattern = arg;
1507 	    break;
1508 #ifdef FEAT_SYN_HL
1509 	case CMD_syntax:
1510 	    set_context_in_syntax_cmd(xp, arg);
1511 	    break;
1512 #endif
1513 #ifdef FEAT_EVAL
1514 	case CMD_const:
1515 	case CMD_let:
1516 	case CMD_if:
1517 	case CMD_elseif:
1518 	case CMD_while:
1519 	case CMD_for:
1520 	case CMD_echo:
1521 	case CMD_echon:
1522 	case CMD_execute:
1523 	case CMD_echomsg:
1524 	case CMD_echoerr:
1525 	case CMD_call:
1526 	case CMD_return:
1527 	case CMD_cexpr:
1528 	case CMD_caddexpr:
1529 	case CMD_cgetexpr:
1530 	case CMD_lexpr:
1531 	case CMD_laddexpr:
1532 	case CMD_lgetexpr:
1533 	    set_context_for_expression(xp, arg, ea.cmdidx);
1534 	    break;
1535 
1536 	case CMD_unlet:
1537 	    while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1538 		arg = xp->xp_pattern + 1;
1539 
1540 	    xp->xp_context = EXPAND_USER_VARS;
1541 	    xp->xp_pattern = arg;
1542 
1543 	    if (*xp->xp_pattern == '$')
1544 	    {
1545 		xp->xp_context = EXPAND_ENV_VARS;
1546 		++xp->xp_pattern;
1547 	    }
1548 
1549 	    break;
1550 
1551 	case CMD_function:
1552 	case CMD_delfunction:
1553 	case CMD_disassemble:
1554 	    xp->xp_context = EXPAND_USER_FUNC;
1555 	    xp->xp_pattern = arg;
1556 	    break;
1557 
1558 	case CMD_echohl:
1559 	    set_context_in_echohl_cmd(xp, arg);
1560 	    break;
1561 #endif
1562 	case CMD_highlight:
1563 	    set_context_in_highlight_cmd(xp, arg);
1564 	    break;
1565 #ifdef FEAT_CSCOPE
1566 	case CMD_cscope:
1567 	case CMD_lcscope:
1568 	case CMD_scscope:
1569 	    set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
1570 	    break;
1571 #endif
1572 #ifdef FEAT_SIGNS
1573 	case CMD_sign:
1574 	    set_context_in_sign_cmd(xp, arg);
1575 	    break;
1576 #endif
1577 	case CMD_bdelete:
1578 	case CMD_bwipeout:
1579 	case CMD_bunload:
1580 	    while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1581 		arg = xp->xp_pattern + 1;
1582 	    // FALLTHROUGH
1583 	case CMD_buffer:
1584 	case CMD_sbuffer:
1585 	case CMD_checktime:
1586 	    xp->xp_context = EXPAND_BUFFERS;
1587 	    xp->xp_pattern = arg;
1588 	    break;
1589 #ifdef FEAT_DIFF
1590 	case CMD_diffget:
1591 	case CMD_diffput:
1592 	    // If current buffer is in diff mode, complete buffer names
1593 	    // which are in diff mode, and different than current buffer.
1594 	    xp->xp_context = EXPAND_DIFF_BUFFERS;
1595 	    xp->xp_pattern = arg;
1596 	    break;
1597 #endif
1598 	case CMD_USER:
1599 	case CMD_USER_BUF:
1600 	    if (compl != EXPAND_NOTHING)
1601 	    {
1602 		// EX_XFILE: file names are handled above
1603 		if (!(ea.argt & EX_XFILE))
1604 		{
1605 #ifdef FEAT_MENU
1606 		    if (compl == EXPAND_MENUS)
1607 			return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1608 #endif
1609 		    if (compl == EXPAND_COMMANDS)
1610 			return arg;
1611 		    if (compl == EXPAND_MAPPINGS)
1612 			return set_context_in_map_cmd(xp, (char_u *)"map",
1613 					 arg, forceit, FALSE, FALSE, CMD_map);
1614 		    // Find start of last argument.
1615 		    p = arg;
1616 		    while (*p)
1617 		    {
1618 			if (*p == ' ')
1619 			    // argument starts after a space
1620 			    arg = p + 1;
1621 			else if (*p == '\\' && *(p + 1) != NUL)
1622 			    ++p; // skip over escaped character
1623 			MB_PTR_ADV(p);
1624 		    }
1625 		    xp->xp_pattern = arg;
1626 		}
1627 		xp->xp_context = compl;
1628 	    }
1629 	    break;
1630 
1631 	case CMD_map:	    case CMD_noremap:
1632 	case CMD_nmap:	    case CMD_nnoremap:
1633 	case CMD_vmap:	    case CMD_vnoremap:
1634 	case CMD_omap:	    case CMD_onoremap:
1635 	case CMD_imap:	    case CMD_inoremap:
1636 	case CMD_cmap:	    case CMD_cnoremap:
1637 	case CMD_lmap:	    case CMD_lnoremap:
1638 	case CMD_smap:	    case CMD_snoremap:
1639 	case CMD_tmap:	    case CMD_tnoremap:
1640 	case CMD_xmap:	    case CMD_xnoremap:
1641 	    return set_context_in_map_cmd(xp, cmd, arg, forceit,
1642 						     FALSE, FALSE, ea.cmdidx);
1643 	case CMD_unmap:
1644 	case CMD_nunmap:
1645 	case CMD_vunmap:
1646 	case CMD_ounmap:
1647 	case CMD_iunmap:
1648 	case CMD_cunmap:
1649 	case CMD_lunmap:
1650 	case CMD_sunmap:
1651 	case CMD_tunmap:
1652 	case CMD_xunmap:
1653 	    return set_context_in_map_cmd(xp, cmd, arg, forceit,
1654 						      FALSE, TRUE, ea.cmdidx);
1655 	case CMD_mapclear:
1656 	case CMD_nmapclear:
1657 	case CMD_vmapclear:
1658 	case CMD_omapclear:
1659 	case CMD_imapclear:
1660 	case CMD_cmapclear:
1661 	case CMD_lmapclear:
1662 	case CMD_smapclear:
1663 	case CMD_tmapclear:
1664 	case CMD_xmapclear:
1665 	    xp->xp_context = EXPAND_MAPCLEAR;
1666 	    xp->xp_pattern = arg;
1667 	    break;
1668 
1669 	case CMD_abbreviate:	case CMD_noreabbrev:
1670 	case CMD_cabbrev:	case CMD_cnoreabbrev:
1671 	case CMD_iabbrev:	case CMD_inoreabbrev:
1672 	    return set_context_in_map_cmd(xp, cmd, arg, forceit,
1673 						      TRUE, FALSE, ea.cmdidx);
1674 	case CMD_unabbreviate:
1675 	case CMD_cunabbrev:
1676 	case CMD_iunabbrev:
1677 	    return set_context_in_map_cmd(xp, cmd, arg, forceit,
1678 						       TRUE, TRUE, ea.cmdidx);
1679 #ifdef FEAT_MENU
1680 	case CMD_menu:	    case CMD_noremenu:	    case CMD_unmenu:
1681 	case CMD_amenu:	    case CMD_anoremenu:	    case CMD_aunmenu:
1682 	case CMD_nmenu:	    case CMD_nnoremenu:	    case CMD_nunmenu:
1683 	case CMD_vmenu:	    case CMD_vnoremenu:	    case CMD_vunmenu:
1684 	case CMD_omenu:	    case CMD_onoremenu:	    case CMD_ounmenu:
1685 	case CMD_imenu:	    case CMD_inoremenu:	    case CMD_iunmenu:
1686 	case CMD_cmenu:	    case CMD_cnoremenu:	    case CMD_cunmenu:
1687 	case CMD_tlmenu:    case CMD_tlnoremenu:    case CMD_tlunmenu:
1688 	case CMD_tmenu:				    case CMD_tunmenu:
1689 	case CMD_popup:	    case CMD_tearoff:	    case CMD_emenu:
1690 	    return set_context_in_menu_cmd(xp, cmd, arg, forceit);
1691 #endif
1692 
1693 	case CMD_colorscheme:
1694 	    xp->xp_context = EXPAND_COLORS;
1695 	    xp->xp_pattern = arg;
1696 	    break;
1697 
1698 	case CMD_compiler:
1699 	    xp->xp_context = EXPAND_COMPILER;
1700 	    xp->xp_pattern = arg;
1701 	    break;
1702 
1703 	case CMD_ownsyntax:
1704 	    xp->xp_context = EXPAND_OWNSYNTAX;
1705 	    xp->xp_pattern = arg;
1706 	    break;
1707 
1708 	case CMD_setfiletype:
1709 	    xp->xp_context = EXPAND_FILETYPE;
1710 	    xp->xp_pattern = arg;
1711 	    break;
1712 
1713 	case CMD_packadd:
1714 	    xp->xp_context = EXPAND_PACKADD;
1715 	    xp->xp_pattern = arg;
1716 	    break;
1717 
1718 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1719 	case CMD_language:
1720 	    p = skiptowhite(arg);
1721 	    if (*p == NUL)
1722 	    {
1723 		xp->xp_context = EXPAND_LANGUAGE;
1724 		xp->xp_pattern = arg;
1725 	    }
1726 	    else
1727 	    {
1728 		if ( STRNCMP(arg, "messages", p - arg) == 0
1729 		  || STRNCMP(arg, "ctype", p - arg) == 0
1730 		  || STRNCMP(arg, "time", p - arg) == 0)
1731 		{
1732 		    xp->xp_context = EXPAND_LOCALES;
1733 		    xp->xp_pattern = skipwhite(p);
1734 		}
1735 		else
1736 		    xp->xp_context = EXPAND_NOTHING;
1737 	    }
1738 	    break;
1739 #endif
1740 #if defined(FEAT_PROFILE)
1741 	case CMD_profile:
1742 	    set_context_in_profile_cmd(xp, arg);
1743 	    break;
1744 #endif
1745 	case CMD_behave:
1746 	    xp->xp_context = EXPAND_BEHAVE;
1747 	    xp->xp_pattern = arg;
1748 	    break;
1749 
1750 	case CMD_messages:
1751 	    xp->xp_context = EXPAND_MESSAGES;
1752 	    xp->xp_pattern = arg;
1753 	    break;
1754 
1755 	case CMD_history:
1756 	    xp->xp_context = EXPAND_HISTORY;
1757 	    xp->xp_pattern = arg;
1758 	    break;
1759 #if defined(FEAT_PROFILE)
1760 	case CMD_syntime:
1761 	    xp->xp_context = EXPAND_SYNTIME;
1762 	    xp->xp_pattern = arg;
1763 	    break;
1764 #endif
1765 
1766 	case CMD_argdelete:
1767 	    while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
1768 		arg = xp->xp_pattern + 1;
1769 	    xp->xp_context = EXPAND_ARGLIST;
1770 	    xp->xp_pattern = arg;
1771 	    break;
1772 
1773 	default:
1774 	    break;
1775     }
1776     return NULL;
1777 }
1778 
1779     void
1780 set_cmd_context(
1781     expand_T	*xp,
1782     char_u	*str,	    // start of command line
1783     int		len,	    // length of command line (excl. NUL)
1784     int		col,	    // position of cursor
1785     int		use_ccline UNUSED) // use ccline for info
1786 {
1787 #ifdef FEAT_EVAL
1788     cmdline_info_T	*ccline = get_cmdline_info();
1789 #endif
1790     int		old_char = NUL;
1791     char_u	*nextcomm;
1792 
1793     // Avoid a UMR warning from Purify, only save the character if it has been
1794     // written before.
1795     if (col < len)
1796 	old_char = str[col];
1797     str[col] = NUL;
1798     nextcomm = str;
1799 
1800 #ifdef FEAT_EVAL
1801     if (use_ccline && ccline->cmdfirstc == '=')
1802     {
1803 	// pass CMD_SIZE because there is no real command
1804 	set_context_for_expression(xp, str, CMD_SIZE);
1805     }
1806     else if (use_ccline && ccline->input_fn)
1807     {
1808 	xp->xp_context = ccline->xp_context;
1809 	xp->xp_pattern = ccline->cmdbuff;
1810 	xp->xp_arg = ccline->xp_arg;
1811     }
1812     else
1813 #endif
1814 	while (nextcomm != NULL)
1815 	    nextcomm = set_one_cmd_context(xp, nextcomm);
1816 
1817     // Store the string here so that call_user_expand_func() can get to them
1818     // easily.
1819     xp->xp_line = str;
1820     xp->xp_col = col;
1821 
1822     str[col] = old_char;
1823 }
1824 
1825 /*
1826  * Expand the command line "str" from context "xp".
1827  * "xp" must have been set by set_cmd_context().
1828  * xp->xp_pattern points into "str", to where the text that is to be expanded
1829  * starts.
1830  * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
1831  * cursor.
1832  * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
1833  * key that triggered expansion literally.
1834  * Returns EXPAND_OK otherwise.
1835  */
1836     int
1837 expand_cmdline(
1838     expand_T	*xp,
1839     char_u	*str,		// start of command line
1840     int		col,		// position of cursor
1841     int		*matchcount,	// return: nr of matches
1842     char_u	***matches)	// return: array of pointers to matches
1843 {
1844     char_u	*file_str = NULL;
1845     int		options = WILD_ADD_SLASH|WILD_SILENT;
1846 
1847     if (xp->xp_context == EXPAND_UNSUCCESSFUL)
1848     {
1849 	beep_flush();
1850 	return EXPAND_UNSUCCESSFUL;  // Something illegal on command line
1851     }
1852     if (xp->xp_context == EXPAND_NOTHING)
1853     {
1854 	// Caller can use the character as a normal char instead
1855 	return EXPAND_NOTHING;
1856     }
1857 
1858     // add star to file name, or convert to regexp if not exp. files.
1859     xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
1860     file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
1861     if (file_str == NULL)
1862 	return EXPAND_UNSUCCESSFUL;
1863 
1864     if (p_wic)
1865 	options += WILD_ICASE;
1866 
1867     // find all files that match the description
1868     if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
1869     {
1870 	*matchcount = 0;
1871 	*matches = NULL;
1872     }
1873     vim_free(file_str);
1874 
1875     return EXPAND_OK;
1876 }
1877 
1878 #ifdef FEAT_MULTI_LANG
1879 /*
1880  * Cleanup matches for help tags:
1881  * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
1882  * tag matches it.  Otherwise remove "@en" if "en" is the only language.
1883  */
1884     static void
1885 cleanup_help_tags(int num_file, char_u **file)
1886 {
1887     int		i, j;
1888     int		len;
1889     char_u	buf[4];
1890     char_u	*p = buf;
1891 
1892     if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
1893     {
1894 	*p++ = '@';
1895 	*p++ = p_hlg[0];
1896 	*p++ = p_hlg[1];
1897     }
1898     *p = NUL;
1899 
1900     for (i = 0; i < num_file; ++i)
1901     {
1902 	len = (int)STRLEN(file[i]) - 3;
1903 	if (len <= 0)
1904 	    continue;
1905 	if (STRCMP(file[i] + len, "@en") == 0)
1906 	{
1907 	    // Sorting on priority means the same item in another language may
1908 	    // be anywhere.  Search all items for a match up to the "@en".
1909 	    for (j = 0; j < num_file; ++j)
1910 		if (j != i && (int)STRLEN(file[j]) == len + 3
1911 			   && STRNCMP(file[i], file[j], len + 1) == 0)
1912 		    break;
1913 	    if (j == num_file)
1914 		// item only exists with @en, remove it
1915 		file[i][len] = NUL;
1916 	}
1917     }
1918 
1919     if (*buf != NUL)
1920 	for (i = 0; i < num_file; ++i)
1921 	{
1922 	    len = (int)STRLEN(file[i]) - 3;
1923 	    if (len <= 0)
1924 		continue;
1925 	    if (STRCMP(file[i] + len, buf) == 0)
1926 	    {
1927 		// remove the default language
1928 		file[i][len] = NUL;
1929 	    }
1930 	}
1931 }
1932 #endif
1933 
1934 /*
1935  * Function given to ExpandGeneric() to obtain the possible arguments of the
1936  * ":behave {mswin,xterm}" command.
1937  */
1938     static char_u *
1939 get_behave_arg(expand_T *xp UNUSED, int idx)
1940 {
1941     if (idx == 0)
1942 	return (char_u *)"mswin";
1943     if (idx == 1)
1944 	return (char_u *)"xterm";
1945     return NULL;
1946 }
1947 
1948 /*
1949  * Function given to ExpandGeneric() to obtain the possible arguments of the
1950  * ":messages {clear}" command.
1951  */
1952     static char_u *
1953 get_messages_arg(expand_T *xp UNUSED, int idx)
1954 {
1955     if (idx == 0)
1956 	return (char_u *)"clear";
1957     return NULL;
1958 }
1959 
1960     static char_u *
1961 get_mapclear_arg(expand_T *xp UNUSED, int idx)
1962 {
1963     if (idx == 0)
1964 	return (char_u *)"<buffer>";
1965     return NULL;
1966 }
1967 
1968 /*
1969  * Do the expansion based on xp->xp_context and "pat".
1970  */
1971     static int
1972 ExpandFromContext(
1973     expand_T	*xp,
1974     char_u	*pat,
1975     int		*num_file,
1976     char_u	***file,
1977     int		options)  // WILD_ flags
1978 {
1979     regmatch_T	regmatch;
1980     int		ret;
1981     int		flags;
1982     char_u	*tofree = NULL;
1983 
1984     flags = EW_DIR;	// include directories
1985     if (options & WILD_LIST_NOTFOUND)
1986 	flags |= EW_NOTFOUND;
1987     if (options & WILD_ADD_SLASH)
1988 	flags |= EW_ADDSLASH;
1989     if (options & WILD_KEEP_ALL)
1990 	flags |= EW_KEEPALL;
1991     if (options & WILD_SILENT)
1992 	flags |= EW_SILENT;
1993     if (options & WILD_NOERROR)
1994 	flags |= EW_NOERROR;
1995     if (options & WILD_ALLLINKS)
1996 	flags |= EW_ALLLINKS;
1997 
1998     if (xp->xp_context == EXPAND_FILES
1999 	    || xp->xp_context == EXPAND_DIRECTORIES
2000 	    || xp->xp_context == EXPAND_FILES_IN_PATH)
2001     {
2002 	// Expand file or directory names.
2003 	int	free_pat = FALSE;
2004 	int	i;
2005 
2006 	// for ":set path=" and ":set tags=" halve backslashes for escaped
2007 	// space
2008 	if (xp->xp_backslash != XP_BS_NONE)
2009 	{
2010 	    free_pat = TRUE;
2011 	    pat = vim_strsave(pat);
2012 	    for (i = 0; pat[i]; ++i)
2013 		if (pat[i] == '\\')
2014 		{
2015 		    if (xp->xp_backslash == XP_BS_THREE
2016 			    && pat[i + 1] == '\\'
2017 			    && pat[i + 2] == '\\'
2018 			    && pat[i + 3] == ' ')
2019 			STRMOVE(pat + i, pat + i + 3);
2020 		    if (xp->xp_backslash == XP_BS_ONE
2021 			    && pat[i + 1] == ' ')
2022 			STRMOVE(pat + i, pat + i + 1);
2023 		}
2024 	}
2025 
2026 	if (xp->xp_context == EXPAND_FILES)
2027 	    flags |= EW_FILE;
2028 	else if (xp->xp_context == EXPAND_FILES_IN_PATH)
2029 	    flags |= (EW_FILE | EW_PATH);
2030 	else
2031 	    flags = (flags | EW_DIR) & ~EW_FILE;
2032 	if (options & WILD_ICASE)
2033 	    flags |= EW_ICASE;
2034 
2035 	// Expand wildcards, supporting %:h and the like.
2036 	ret = expand_wildcards_eval(&pat, num_file, file, flags);
2037 	if (free_pat)
2038 	    vim_free(pat);
2039 #ifdef BACKSLASH_IN_FILENAME
2040 	if (p_csl[0] != NUL && (options & WILD_IGNORE_COMPLETESLASH) == 0)
2041 	{
2042 	    int	    i;
2043 
2044 	    for (i = 0; i < *num_file; ++i)
2045 	    {
2046 		char_u	*ptr = (*file)[i];
2047 
2048 		while (*ptr != NUL)
2049 		{
2050 		    if (p_csl[0] == 's' && *ptr == '\\')
2051 			*ptr = '/';
2052 		    else if (p_csl[0] == 'b' && *ptr == '/')
2053 			*ptr = '\\';
2054 		    ptr += (*mb_ptr2len)(ptr);
2055 		}
2056 	    }
2057 	}
2058 #endif
2059 	return ret;
2060     }
2061 
2062     *file = (char_u **)"";
2063     *num_file = 0;
2064     if (xp->xp_context == EXPAND_HELP)
2065     {
2066 	// With an empty argument we would get all the help tags, which is
2067 	// very slow.  Get matches for "help" instead.
2068 	if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
2069 						 num_file, file, FALSE) == OK)
2070 	{
2071 #ifdef FEAT_MULTI_LANG
2072 	    cleanup_help_tags(*num_file, *file);
2073 #endif
2074 	    return OK;
2075 	}
2076 	return FAIL;
2077     }
2078 
2079     if (xp->xp_context == EXPAND_SHELLCMD)
2080 	return expand_shellcmd(pat, num_file, file, flags);
2081     if (xp->xp_context == EXPAND_OLD_SETTING)
2082 	return ExpandOldSetting(num_file, file);
2083     if (xp->xp_context == EXPAND_BUFFERS)
2084 	return ExpandBufnames(pat, num_file, file, options);
2085 #ifdef FEAT_DIFF
2086     if (xp->xp_context == EXPAND_DIFF_BUFFERS)
2087 	return ExpandBufnames(pat, num_file, file, options | BUF_DIFF_FILTER);
2088 #endif
2089     if (xp->xp_context == EXPAND_TAGS
2090 	    || xp->xp_context == EXPAND_TAGS_LISTFILES)
2091 	return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
2092     if (xp->xp_context == EXPAND_COLORS)
2093     {
2094 	char *directories[] = {"colors", NULL};
2095 	return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
2096 								directories);
2097     }
2098     if (xp->xp_context == EXPAND_COMPILER)
2099     {
2100 	char *directories[] = {"compiler", NULL};
2101 	return ExpandRTDir(pat, 0, num_file, file, directories);
2102     }
2103     if (xp->xp_context == EXPAND_OWNSYNTAX)
2104     {
2105 	char *directories[] = {"syntax", NULL};
2106 	return ExpandRTDir(pat, 0, num_file, file, directories);
2107     }
2108     if (xp->xp_context == EXPAND_FILETYPE)
2109     {
2110 	char *directories[] = {"syntax", "indent", "ftplugin", NULL};
2111 	return ExpandRTDir(pat, 0, num_file, file, directories);
2112     }
2113 # if defined(FEAT_EVAL)
2114     if (xp->xp_context == EXPAND_USER_LIST)
2115 	return ExpandUserList(xp, num_file, file);
2116 # endif
2117     if (xp->xp_context == EXPAND_PACKADD)
2118 	return ExpandPackAddDir(pat, num_file, file);
2119 
2120     // When expanding a function name starting with s:, match the <SNR>nr_
2121     // prefix.
2122     if (xp->xp_context == EXPAND_USER_FUNC && STRNCMP(pat, "^s:", 3) == 0)
2123     {
2124 	int len = (int)STRLEN(pat) + 20;
2125 
2126 	tofree = alloc(len);
2127 	vim_snprintf((char *)tofree, len, "^<SNR>\\d\\+_%s", pat + 3);
2128 	pat = tofree;
2129     }
2130 
2131     regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
2132     if (regmatch.regprog == NULL)
2133 	return FAIL;
2134 
2135     // set ignore-case according to p_ic, p_scs and pat
2136     regmatch.rm_ic = ignorecase(pat);
2137 
2138     if (xp->xp_context == EXPAND_SETTINGS
2139 	    || xp->xp_context == EXPAND_BOOL_SETTINGS)
2140 	ret = ExpandSettings(xp, &regmatch, num_file, file);
2141     else if (xp->xp_context == EXPAND_MAPPINGS)
2142 	ret = ExpandMappings(&regmatch, num_file, file);
2143 # if defined(FEAT_EVAL)
2144     else if (xp->xp_context == EXPAND_USER_DEFINED)
2145 	ret = ExpandUserDefined(xp, &regmatch, num_file, file);
2146 # endif
2147     else
2148     {
2149 	static struct expgen
2150 	{
2151 	    int		context;
2152 	    char_u	*((*func)(expand_T *, int));
2153 	    int		ic;
2154 	    int		escaped;
2155 	} tab[] =
2156 	{
2157 	    {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
2158 	    {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
2159 	    {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
2160 	    {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
2161 	    {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
2162 	    {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
2163 	    {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
2164 	    {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
2165 	    {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
2166 	    {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
2167 # ifdef FEAT_EVAL
2168 	    {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
2169 	    {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
2170 	    {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
2171 	    {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
2172 # endif
2173 # ifdef FEAT_MENU
2174 	    {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
2175 	    {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
2176 # endif
2177 # ifdef FEAT_SYN_HL
2178 	    {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
2179 # endif
2180 # ifdef FEAT_PROFILE
2181 	    {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
2182 # endif
2183 	    {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
2184 	    {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
2185 	    {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
2186 # ifdef FEAT_CSCOPE
2187 	    {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
2188 # endif
2189 # ifdef FEAT_SIGNS
2190 	    {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
2191 # endif
2192 # ifdef FEAT_PROFILE
2193 	    {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
2194 # endif
2195 # if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
2196 	    {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
2197 	    {EXPAND_LOCALES, get_locales, TRUE, FALSE},
2198 # endif
2199 	    {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
2200 	    {EXPAND_USER, get_users, TRUE, FALSE},
2201 	    {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
2202 	};
2203 	int	i;
2204 
2205 	// Find a context in the table and call the ExpandGeneric() with the
2206 	// right function to do the expansion.
2207 	ret = FAIL;
2208 	for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
2209 	    if (xp->xp_context == tab[i].context)
2210 	    {
2211 		if (tab[i].ic)
2212 		    regmatch.rm_ic = TRUE;
2213 		ret = ExpandGeneric(xp, &regmatch, num_file, file,
2214 						tab[i].func, tab[i].escaped);
2215 		break;
2216 	    }
2217     }
2218 
2219     vim_regfree(regmatch.regprog);
2220     vim_free(tofree);
2221 
2222     return ret;
2223 }
2224 
2225 /*
2226  * Expand a list of names.
2227  *
2228  * Generic function for command line completion.  It calls a function to
2229  * obtain strings, one by one.	The strings are matched against a regexp
2230  * program.  Matching strings are copied into an array, which is returned.
2231  *
2232  * Returns OK when no problems encountered, FAIL for error (out of memory).
2233  */
2234     static int
2235 ExpandGeneric(
2236     expand_T	*xp,
2237     regmatch_T	*regmatch,
2238     int		*num_file,
2239     char_u	***file,
2240     char_u	*((*func)(expand_T *, int)),
2241 					  // returns a string from the list
2242     int		escaped)
2243 {
2244     int		i;
2245     int		count = 0;
2246     int		round;
2247     char_u	*str;
2248 
2249     // do this loop twice:
2250     // round == 0: count the number of matching names
2251     // round == 1: copy the matching names into allocated memory
2252     for (round = 0; round <= 1; ++round)
2253     {
2254 	for (i = 0; ; ++i)
2255 	{
2256 	    str = (*func)(xp, i);
2257 	    if (str == NULL)	    // end of list
2258 		break;
2259 	    if (*str == NUL)	    // skip empty strings
2260 		continue;
2261 
2262 	    if (vim_regexec(regmatch, str, (colnr_T)0))
2263 	    {
2264 		if (round)
2265 		{
2266 		    if (escaped)
2267 			str = vim_strsave_escaped(str, (char_u *)" \t\\.");
2268 		    else
2269 			str = vim_strsave(str);
2270 		    if (str == NULL)
2271 		    {
2272 			FreeWild(count, *file);
2273 			*num_file = 0;
2274 			*file = NULL;
2275 			return FAIL;
2276 		    }
2277 		    (*file)[count] = str;
2278 # ifdef FEAT_MENU
2279 		    if (func == get_menu_names && str != NULL)
2280 		    {
2281 			// test for separator added by get_menu_names()
2282 			str += STRLEN(str) - 1;
2283 			if (*str == '\001')
2284 			    *str = '.';
2285 		    }
2286 # endif
2287 		}
2288 		++count;
2289 	    }
2290 	}
2291 	if (round == 0)
2292 	{
2293 	    if (count == 0)
2294 		return OK;
2295 	    *file = ALLOC_MULT(char_u *, count);
2296 	    if (*file == NULL)
2297 	    {
2298 		*num_file = 0;
2299 		*file = NULL;
2300 		return FAIL;
2301 	    }
2302 	    *num_file = count;
2303 	    count = 0;
2304 	}
2305     }
2306 
2307     // Sort the results.  Keep menu's in the specified order.
2308     if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
2309     {
2310 	if (xp->xp_context == EXPAND_EXPRESSION
2311 		|| xp->xp_context == EXPAND_FUNCTIONS
2312 		|| xp->xp_context == EXPAND_USER_FUNC)
2313 	    // <SNR> functions should be sorted to the end.
2314 	    qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
2315 							   sort_func_compare);
2316 	else
2317 	    sort_strings(*file, *num_file);
2318     }
2319 
2320 #if defined(FEAT_SYN_HL)
2321     // Reset the variables used for special highlight names expansion, so that
2322     // they don't show up when getting normal highlight names by ID.
2323     reset_expand_highlight();
2324 #endif
2325     return OK;
2326 }
2327 
2328 /*
2329  * Complete a shell command.
2330  * Returns FAIL or OK;
2331  */
2332     static int
2333 expand_shellcmd(
2334     char_u	*filepat,	// pattern to match with command names
2335     int		*num_file,	// return: number of matches
2336     char_u	***file,	// return: array with matches
2337     int		flagsarg)	// EW_ flags
2338 {
2339     char_u	*pat;
2340     int		i;
2341     char_u	*path = NULL;
2342     int		mustfree = FALSE;
2343     garray_T    ga;
2344     char_u	*buf;
2345     size_t	l;
2346     char_u	*s, *e;
2347     int		flags = flagsarg;
2348     int		ret;
2349     int		did_curdir = FALSE;
2350     hashtab_T	found_ht;
2351     hashitem_T	*hi;
2352     hash_T	hash;
2353 
2354     buf = alloc(MAXPATHL);
2355     if (buf == NULL)
2356 	return FAIL;
2357 
2358     // for ":set path=" and ":set tags=" halve backslashes for escaped space
2359     pat = vim_strsave(filepat);
2360     if (pat == NULL)
2361     {
2362 	vim_free(buf);
2363 	return FAIL;
2364     }
2365 
2366     for (i = 0; pat[i]; ++i)
2367 	if (pat[i] == '\\' && pat[i + 1] == ' ')
2368 	    STRMOVE(pat + i, pat + i + 1);
2369 
2370     flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
2371 
2372     if (pat[0] == '.' && (vim_ispathsep(pat[1])
2373 			       || (pat[1] == '.' && vim_ispathsep(pat[2]))))
2374 	path = (char_u *)".";
2375     else
2376     {
2377 	// For an absolute name we don't use $PATH.
2378 	if (!mch_isFullName(pat))
2379 	    path = vim_getenv((char_u *)"PATH", &mustfree);
2380 	if (path == NULL)
2381 	    path = (char_u *)"";
2382     }
2383 
2384     // Go over all directories in $PATH.  Expand matches in that directory and
2385     // collect them in "ga".  When "." is not in $PATH also expand for the
2386     // current directory, to find "subdir/cmd".
2387     ga_init2(&ga, (int)sizeof(char *), 10);
2388     hash_init(&found_ht);
2389     for (s = path; ; s = e)
2390     {
2391 # if defined(MSWIN)
2392 	e = vim_strchr(s, ';');
2393 # else
2394 	e = vim_strchr(s, ':');
2395 # endif
2396 	if (e == NULL)
2397 	    e = s + STRLEN(s);
2398 
2399 	if (*s == NUL)
2400 	{
2401 	    if (did_curdir)
2402 		break;
2403 	    // Find directories in the current directory, path is empty.
2404 	    did_curdir = TRUE;
2405 	    flags |= EW_DIR;
2406 	}
2407 	else if (STRNCMP(s, ".", (int)(e - s)) == 0)
2408 	{
2409 	    did_curdir = TRUE;
2410 	    flags |= EW_DIR;
2411 	}
2412 	else
2413 	    // Do not match directories inside a $PATH item.
2414 	    flags &= ~EW_DIR;
2415 
2416 	l = e - s;
2417 	if (l > MAXPATHL - 5)
2418 	    break;
2419 	vim_strncpy(buf, s, l);
2420 	add_pathsep(buf);
2421 	l = STRLEN(buf);
2422 	vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
2423 
2424 	// Expand matches in one directory of $PATH.
2425 	ret = expand_wildcards(1, &buf, num_file, file, flags);
2426 	if (ret == OK)
2427 	{
2428 	    if (ga_grow(&ga, *num_file) == FAIL)
2429 		FreeWild(*num_file, *file);
2430 	    else
2431 	    {
2432 		for (i = 0; i < *num_file; ++i)
2433 		{
2434 		    char_u *name = (*file)[i];
2435 
2436 		    if (STRLEN(name) > l)
2437 		    {
2438 			// Check if this name was already found.
2439 			hash = hash_hash(name + l);
2440 			hi = hash_lookup(&found_ht, name + l, hash);
2441 			if (HASHITEM_EMPTY(hi))
2442 			{
2443 			    // Remove the path that was prepended.
2444 			    STRMOVE(name, name + l);
2445 			    ((char_u **)ga.ga_data)[ga.ga_len++] = name;
2446 			    hash_add_item(&found_ht, hi, name, hash);
2447 			    name = NULL;
2448 			}
2449 		    }
2450 		    vim_free(name);
2451 		}
2452 		vim_free(*file);
2453 	    }
2454 	}
2455 	if (*e != NUL)
2456 	    ++e;
2457     }
2458     *file = ga.ga_data;
2459     *num_file = ga.ga_len;
2460 
2461     vim_free(buf);
2462     vim_free(pat);
2463     if (mustfree)
2464 	vim_free(path);
2465     hash_clear(&found_ht);
2466     return OK;
2467 }
2468 
2469 # if defined(FEAT_EVAL)
2470 /*
2471  * Call "user_expand_func()" to invoke a user defined Vim script function and
2472  * return the result (either a string or a List).
2473  */
2474     static void *
2475 call_user_expand_func(
2476     void	*(*user_expand_func)(char_u *, int, typval_T *),
2477     expand_T	*xp,
2478     int		*num_file,
2479     char_u	***file)
2480 {
2481     cmdline_info_T	*ccline = get_cmdline_info();
2482     int		keep = 0;
2483     typval_T	args[4];
2484     sctx_T	save_current_sctx = current_sctx;
2485     char_u	*pat = NULL;
2486     void	*ret;
2487 
2488     if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
2489 	return NULL;
2490     *num_file = 0;
2491     *file = NULL;
2492 
2493     if (ccline->cmdbuff != NULL)
2494     {
2495 	keep = ccline->cmdbuff[ccline->cmdlen];
2496 	ccline->cmdbuff[ccline->cmdlen] = 0;
2497     }
2498 
2499     pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
2500 
2501     args[0].v_type = VAR_STRING;
2502     args[0].vval.v_string = pat;
2503     args[1].v_type = VAR_STRING;
2504     args[1].vval.v_string = xp->xp_line;
2505     args[2].v_type = VAR_NUMBER;
2506     args[2].vval.v_number = xp->xp_col;
2507     args[3].v_type = VAR_UNKNOWN;
2508 
2509     current_sctx = xp->xp_script_ctx;
2510 
2511     ret = user_expand_func(xp->xp_arg, 3, args);
2512 
2513     current_sctx = save_current_sctx;
2514     if (ccline->cmdbuff != NULL)
2515 	ccline->cmdbuff[ccline->cmdlen] = keep;
2516 
2517     vim_free(pat);
2518     return ret;
2519 }
2520 
2521 /*
2522  * Expand names with a function defined by the user.
2523  */
2524     static int
2525 ExpandUserDefined(
2526     expand_T	*xp,
2527     regmatch_T	*regmatch,
2528     int		*num_file,
2529     char_u	***file)
2530 {
2531     char_u	*retstr;
2532     char_u	*s;
2533     char_u	*e;
2534     int		keep;
2535     garray_T	ga;
2536     int		skip;
2537 
2538     retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
2539     if (retstr == NULL)
2540 	return FAIL;
2541 
2542     ga_init2(&ga, (int)sizeof(char *), 3);
2543     for (s = retstr; *s != NUL; s = e)
2544     {
2545 	e = vim_strchr(s, '\n');
2546 	if (e == NULL)
2547 	    e = s + STRLEN(s);
2548 	keep = *e;
2549 	*e = NUL;
2550 
2551 	skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
2552 	*e = keep;
2553 
2554 	if (!skip)
2555 	{
2556 	    if (ga_grow(&ga, 1) == FAIL)
2557 		break;
2558 	    ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
2559 	    ++ga.ga_len;
2560 	}
2561 
2562 	if (*e != NUL)
2563 	    ++e;
2564     }
2565     vim_free(retstr);
2566     *file = ga.ga_data;
2567     *num_file = ga.ga_len;
2568     return OK;
2569 }
2570 
2571 /*
2572  * Expand names with a list returned by a function defined by the user.
2573  */
2574     static int
2575 ExpandUserList(
2576     expand_T	*xp,
2577     int		*num_file,
2578     char_u	***file)
2579 {
2580     list_T      *retlist;
2581     listitem_T	*li;
2582     garray_T	ga;
2583 
2584     retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
2585     if (retlist == NULL)
2586 	return FAIL;
2587 
2588     ga_init2(&ga, (int)sizeof(char *), 3);
2589     // Loop over the items in the list.
2590     FOR_ALL_LIST_ITEMS(retlist, li)
2591     {
2592 	if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
2593 	    continue;  // Skip non-string items and empty strings
2594 
2595 	if (ga_grow(&ga, 1) == FAIL)
2596 	    break;
2597 
2598 	((char_u **)ga.ga_data)[ga.ga_len] =
2599 					 vim_strsave(li->li_tv.vval.v_string);
2600 	++ga.ga_len;
2601     }
2602     list_unref(retlist);
2603 
2604     *file = ga.ga_data;
2605     *num_file = ga.ga_len;
2606     return OK;
2607 }
2608 # endif
2609 
2610 /*
2611  * Expand "file" for all comma-separated directories in "path".
2612  * Adds the matches to "ga".  Caller must init "ga".
2613  */
2614     void
2615 globpath(
2616     char_u	*path,
2617     char_u	*file,
2618     garray_T	*ga,
2619     int		expand_options)
2620 {
2621     expand_T	xpc;
2622     char_u	*buf;
2623     int		i;
2624     int		num_p;
2625     char_u	**p;
2626 
2627     buf = alloc(MAXPATHL);
2628     if (buf == NULL)
2629 	return;
2630 
2631     ExpandInit(&xpc);
2632     xpc.xp_context = EXPAND_FILES;
2633 
2634     // Loop over all entries in {path}.
2635     while (*path != NUL)
2636     {
2637 	// Copy one item of the path to buf[] and concatenate the file name.
2638 	copy_option_part(&path, buf, MAXPATHL, ",");
2639 	if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
2640 	{
2641 # if defined(MSWIN)
2642 	    // Using the platform's path separator (\) makes vim incorrectly
2643 	    // treat it as an escape character, use '/' instead.
2644 	    if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
2645 		STRCAT(buf, "/");
2646 # else
2647 	    add_pathsep(buf);
2648 # endif
2649 	    STRCAT(buf, file);
2650 	    if (ExpandFromContext(&xpc, buf, &num_p, &p,
2651 			     WILD_SILENT|expand_options) != FAIL && num_p > 0)
2652 	    {
2653 		ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
2654 
2655 		if (ga_grow(ga, num_p) == OK)
2656 		    // take over the pointers and put them in "ga"
2657 		    for (i = 0; i < num_p; ++i)
2658 		    {
2659 			((char_u **)ga->ga_data)[ga->ga_len] = p[i];
2660 			++ga->ga_len;
2661 		    }
2662 		vim_free(p);
2663 	    }
2664 	}
2665     }
2666 
2667     vim_free(buf);
2668 }
2669 
2670 #if defined(FEAT_EVAL) || defined(PROTO)
2671 /*
2672  * "getcompletion()" function
2673  */
2674     void
2675 f_getcompletion(typval_T *argvars, typval_T *rettv)
2676 {
2677     char_u	*pat;
2678     expand_T	xpc;
2679     int		filtered = FALSE;
2680     int		options = WILD_SILENT | WILD_USE_NL | WILD_ADD_SLASH
2681 					| WILD_NO_BEEP;
2682 
2683     if (argvars[2].v_type != VAR_UNKNOWN)
2684 	filtered = tv_get_number_chk(&argvars[2], NULL);
2685 
2686     if (p_wic)
2687 	options |= WILD_ICASE;
2688 
2689     // For filtered results, 'wildignore' is used
2690     if (!filtered)
2691 	options |= WILD_KEEP_ALL;
2692 
2693     ExpandInit(&xpc);
2694     xpc.xp_pattern = tv_get_string(&argvars[0]);
2695     xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2696     xpc.xp_context = cmdcomplete_str_to_type(tv_get_string(&argvars[1]));
2697     if (xpc.xp_context == EXPAND_NOTHING)
2698     {
2699 	if (argvars[1].v_type == VAR_STRING)
2700 	    semsg(_(e_invarg2), argvars[1].vval.v_string);
2701 	else
2702 	    emsg(_(e_invarg));
2703 	return;
2704     }
2705 
2706 # if defined(FEAT_MENU)
2707     if (xpc.xp_context == EXPAND_MENUS)
2708     {
2709 	set_context_in_menu_cmd(&xpc, (char_u *)"menu", xpc.xp_pattern, FALSE);
2710 	xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2711     }
2712 # endif
2713 # ifdef FEAT_CSCOPE
2714     if (xpc.xp_context == EXPAND_CSCOPE)
2715     {
2716 	set_context_in_cscope_cmd(&xpc, xpc.xp_pattern, CMD_cscope);
2717 	xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2718     }
2719 # endif
2720 # ifdef FEAT_SIGNS
2721     if (xpc.xp_context == EXPAND_SIGN)
2722     {
2723 	set_context_in_sign_cmd(&xpc, xpc.xp_pattern);
2724 	xpc.xp_pattern_len = (int)STRLEN(xpc.xp_pattern);
2725     }
2726 # endif
2727 
2728     pat = addstar(xpc.xp_pattern, xpc.xp_pattern_len, xpc.xp_context);
2729     if ((rettv_list_alloc(rettv) != FAIL) && (pat != NULL))
2730     {
2731 	int	i;
2732 
2733 	ExpandOne(&xpc, pat, NULL, options, WILD_ALL_KEEP);
2734 
2735 	for (i = 0; i < xpc.xp_numfiles; i++)
2736 	    list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
2737     }
2738     vim_free(pat);
2739     ExpandCleanup(&xpc);
2740 }
2741 #endif // FEAT_EVAL
2742