Skip to content

Commit cac9bbc

Browse files
committed
fix(mdtool): use -f -X '!pat' instead of -G pat
#1297 (comment) The compgen option `-X '!pat'` has a problem that it only generates the filenames in the current directory and that it does not perform the filtering by the "cur" specified to the argument of `compgen`: $ compgen -G '*.md' -- doc/ CHANGELOG.md CONTRIBUTING.md README.md style.md In this patch, we use `-f -X '!pat'`, which does not have those limitations.
1 parent 81cfa1f commit cac9bbc

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ Also, please bear the following coding guidelines in mind:
132132
expansions will be unexpectedly performed, which becomes a vulnerability. In
133133
the latter case, checks by shellcheck and shfmt will not be performed inside
134134
`'...'`. Also, `_comp_compgen_split` is `IFS`-safe.
135+
136+
Avoid using `_comp_compgen -- -G "pattern"` to generate completions. The
137+
result is not filtered by the current word `cur` due to the Bash design of
138+
`compgen`. Also, this cannot be used to generate filenames with a specified
139+
extension because the `-G` specification only generates the matching
140+
filepaths in the current directory. It does not look into subdirectories
141+
even when `$cur` implies the completion in a subdirectory. One can instead
142+
use `_comp_compgen -- -f -X '!pattern'`.
135143

136144
- When completing available options, offer only the most descriptive
137145
ones as completion results if there are multiple options that do the

completions/mdtool

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ _comp_cmd_mdtool()
2121
#if [[ "$prev" == *: ]]; then
2222
# case $prev in
2323
# @(--p:|--project:))
24-
# _comp_compgen -- -G "*.mdp"
24+
# _comp_compgen -- -f -X '!*.mdp'
2525
# ;;
2626
# @(--f:|--buildfile:))
27-
# _comp_compgen -- -G "*.md[ps]"
27+
# _comp_compgen -- -f -X '!*.md[ps]'
2828
# ;;
2929
# esac
3030
#fi
3131
return
3232
;;
3333
"generate-makefiles")
3434
compopt -o filenames
35-
_comp_compgen -- -o filenames -G"*.mds"
35+
_comp_compgen -- -o filenames -f -X '!*.mds'
3636
if [[ $prev == *mds ]]; then
3737
_comp_compgen -- -W '--simple-makefiles --s --d:'
3838
fi

0 commit comments

Comments
 (0)