Skip to content

Commit 6ab9e42

Browse files
committed
patch 8.1.0223: completing shell command finds sub-directories in $PATH
Problem: Completing shell command finds sub-directories in $PATH. Solution: Remove EW_DIR when completing an item in $PATH. (Jason Franklin)
1 parent 73b4aba commit 6ab9e42

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

src/ex_getln.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5193,16 +5193,6 @@ expand_shellcmd(
51935193
hash_init(&found_ht);
51945194
for (s = path; ; s = e)
51955195
{
5196-
if (*s == NUL)
5197-
{
5198-
if (did_curdir)
5199-
break;
5200-
/* Find directories in the current directory, path is empty. */
5201-
did_curdir = TRUE;
5202-
}
5203-
else if (*s == '.')
5204-
did_curdir = TRUE;
5205-
52065196
#if defined(MSWIN)
52075197
e = vim_strchr(s, ';');
52085198
#else
@@ -5211,6 +5201,23 @@ expand_shellcmd(
52115201
if (e == NULL)
52125202
e = s + STRLEN(s);
52135203

5204+
if (*s == NUL)
5205+
{
5206+
if (did_curdir)
5207+
break;
5208+
// Find directories in the current directory, path is empty.
5209+
did_curdir = TRUE;
5210+
flags |= EW_DIR;
5211+
}
5212+
else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5213+
{
5214+
did_curdir = TRUE;
5215+
flags |= EW_DIR;
5216+
}
5217+
else
5218+
// Do not match directories inside a $PATH item.
5219+
flags &= ~EW_DIR;
5220+
52145221
l = e - s;
52155222
if (l > MAXPATHL - 5)
52165223
break;
@@ -5266,8 +5273,6 @@ expand_shellcmd(
52665273

52675274

52685275
# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5269-
static void * call_user_expand_func(void *(*user_expand_func)(char_u *, int, typval_T *, int), expand_T *xp, int *num_file, char_u ***file);
5270-
52715276
/*
52725277
* Call "user_expand_func()" to invoke a user defined Vim script function and
52735278
* return the result (either a string or a List).

src/testdir/test_cmdline.vim

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func Test_getcompletion()
231231
call assert_equal([], l)
232232

233233
let l = getcompletion('.', 'shellcmd')
234-
call assert_equal(['./', '../'], l[0:1])
234+
call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
235235
call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
236236
let root = has('win32') ? 'C:\\' : '/'
237237
let l = getcompletion(root, 'shellcmd')
@@ -290,6 +290,29 @@ func Test_getcompletion()
290290
call assert_fails('call getcompletion("", "burp")', 'E475:')
291291
endfunc
292292

293+
func Test_shellcmd_completion()
294+
let save_path = $PATH
295+
296+
call mkdir('Xpathdir/Xpathsubdir', 'p')
297+
call writefile([''], 'Xpathdir/Xfile.exe')
298+
call setfperm('Xpathdir/Xfile.exe', 'rwx------')
299+
300+
" Set PATH to example directory without trailing slash.
301+
let $PATH = getcwd() . '/Xpathdir'
302+
303+
" Test for the ":!<TAB>" case. Previously, this would include subdirs of
304+
" dirs in the PATH, even though they won't be executed. We check that only
305+
" subdirs of the PWD and executables from the PATH are included in the
306+
" suggestions.
307+
let actual = getcompletion('X', 'shellcmd')
308+
let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
309+
call insert(expected, 'Xfile.exe')
310+
call assert_equal(expected, actual)
311+
312+
call delete('Xpathdir', 'rf')
313+
let $PATH = save_path
314+
endfunc
315+
293316
func Test_expand_star_star()
294317
call mkdir('a/b', 'p')
295318
call writefile(['asdfasdf'], 'a/b/fileXname')

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ static char *(features[]) =
798798

799799
static int included_patches[] =
800800
{ /* Add new patch number below this line */
801+
/**/
802+
223,
801803
/**/
802804
222,
803805
/**/

0 commit comments

Comments
 (0)