diff options
author | Bram Moolenaar <Bram@vim.org> | 2018-05-22 16:58:47 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2018-05-22 16:58:47 +0200 |
commit | 62fe66f251263715968442e237742d9d3dfd5fa1 (patch) | |
tree | 17a95bf476e85695ea332ef5bc2e50371d66622d /src/ex_getln.c | |
parent | d45aa55d42211eb2f42e0a04ff77537a5df51c40 (diff) |
patch 8.1.0017: shell command completion has duplicatesv8.1.0017
Problem: Shell command completion has duplicates. (Yegappan Lakshmanan)
Solution: Use a hash table to avoid duplicates. (Ozaki Kiichi, closes #539,
closes #2733)
Diffstat (limited to 'src/ex_getln.c')
-rw-r--r-- | src/ex_getln.c | 43 |
1 files changed, 26 insertions, 17 deletions
diff --git a/src/ex_getln.c b/src/ex_getln.c index acaaef4705..c4b9acd75d 100644 --- a/src/ex_getln.c +++ b/src/ex_getln.c @@ -5147,7 +5147,7 @@ expand_shellcmd( { char_u *pat; int i; - char_u *path; + char_u *path = NULL; int mustfree = FALSE; garray_T ga; char_u *buf = alloc(MAXPATHL); @@ -5156,6 +5156,9 @@ expand_shellcmd( int flags = flagsarg; int ret; int did_curdir = FALSE; + hashtab_T found_ht; + hashitem_T *hi; + hash_T hash; if (buf == NULL) return FAIL; @@ -5169,15 +5172,14 @@ expand_shellcmd( flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; - /* For an absolute name we don't use $PATH. */ - if (mch_isFullName(pat)) - path = (char_u *)" "; - else if ((pat[0] == '.' && (vim_ispathsep(pat[1]) - || (pat[1] == '.' && vim_ispathsep(pat[2]))))) + if (pat[0] == '.' && (vim_ispathsep(pat[1]) + || (pat[1] == '.' && vim_ispathsep(pat[2])))) path = (char_u *)"."; else { - path = vim_getenv((char_u *)"PATH", &mustfree); + /* For an absolute name we don't use $PATH. */ + if (!mch_isFullName(pat)) + path = vim_getenv((char_u *)"PATH", &mustfree); if (path == NULL) path = (char_u *)""; } @@ -5188,6 +5190,7 @@ expand_shellcmd( * current directory, to find "subdir/cmd". */ ga_init2(&ga, (int)sizeof(char *), 10); + hash_init(&found_ht); for (s = path; ; s = e) { if (*s == NUL) @@ -5200,9 +5203,6 @@ expand_shellcmd( else if (*s == '.') did_curdir = TRUE; - if (*s == ' ') - ++s; /* Skip space used for absolute path name. */ - #if defined(MSWIN) e = vim_strchr(s, ';'); #else @@ -5229,15 +5229,23 @@ expand_shellcmd( { for (i = 0; i < *num_file; ++i) { - s = (*file)[i]; - if (STRLEN(s) > l) + char_u *name = (*file)[i]; + + if (STRLEN(name) > l) { - /* Remove the path again. */ - STRMOVE(s, s + l); - ((char_u **)ga.ga_data)[ga.ga_len++] = s; + // Check if this name was already found. + hash = hash_hash(name + l); + hi = hash_lookup(&found_ht, name + l, hash); + if (HASHITEM_EMPTY(hi)) + { + // Remove the path that was prepended. + STRMOVE(name, name + l); + ((char_u **)ga.ga_data)[ga.ga_len++] = name; + hash_add_item(&found_ht, hi, name, hash); + name = NULL; + } } - else - vim_free(s); + vim_free(name); } vim_free(*file); } @@ -5252,6 +5260,7 @@ expand_shellcmd( vim_free(pat); if (mustfree) vim_free(path); + hash_clear(&found_ht); return OK; } |