Skip to content

Commit 461ae45

Browse files
authored
Merge pull request #1358 from akinomyoga/compgen-G
* refactor(rcs): declare "local i" closer * fix(rcs): avoid unexpected pathname expansion * fix(mdtool): fix usage of `compgen -G` * fix(mdtool): use `-f -X '!pat'` instead of `-G pat` * docs(CONTRIBUTING): discourage use of `compgen -G pat` * style(feh,upgradepkg,valgrind): remove unnecessary `@(...)`
2 parents 247ba7b + 272bbed commit 461ae45

File tree

7 files changed

+23
-9
lines changed

7 files changed

+23
-9
lines changed

CONTRIBUTING.md

+8
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 completion in a subdirectory. One can instead use
142+
`_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/feh

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ _comp_cmd_feh()
2929
fi
3030
local font_path
3131
# font_path="$(imlib2-config --prefix 2>/dev/null)/share/imlib2/data/fonts"
32-
# _comp_compgen -C "$font_path" -- -f -X "!*.@([tT][tT][fF])" -S /
32+
# _comp_compgen -C "$font_path" -- -f -X "!*.[tT][tT][fF]" -S /
3333
for ((i = ${#words[@]} - 2; i > 0; i--)); do
3434
if [[ ${words[i]} == -@(C|-fontpath) ]]; then
3535
font_path="${words[i + 1]}"
3636
if [[ -d $font_path ]]; then
3737
_comp_compgen -aC "$font_path" -- \
38-
-f -X "!*.@([tT][tT][fF])" -S /
38+
-f -X "!*.[tT][tT][fF]" -S /
3939
fi
4040
fi
4141
done

completions/mdtool

+3-3
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 -- -f -G "*.mdp"
24+
# _comp_compgen -- -f -X '!*.mdp'
2525
# ;;
2626
# @(--f:|--buildfile:))
27-
# _comp_compgen -- -f -G "*.mdp" -G "*.mds"
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

completions/rcs

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ _comp_cmd_rcs()
55
local cur prev words cword comp_args
66
_comp_initialize -- "$@" || return
77

8-
local file dir i
8+
local file dir
99

1010
file=${cur##*/}
1111
dir=${cur%/*}
@@ -21,8 +21,11 @@ _comp_cmd_rcs()
2121
COMPREPLY[i]=$dir$file
2222
done
2323

24-
_comp_compgen -aR -- -G "$dir/$file*,v"
24+
local files
25+
_comp_expand_glob files '"$dir/$file"*,v' &&
26+
_comp_compgen -aR -- -W '"${files[@]}"'
2527

28+
local i
2629
for i in ${!COMPREPLY[*]}; do
2730
COMPREPLY[i]=${COMPREPLY[i]%,v}
2831
done

completions/upgradepkg

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ _comp_cmd_upgradepkg()
1515
cur="${cur#*%}"
1616
local nofiles=""
1717
compopt -o filenames
18-
_comp_compgen -- -P "$prev%" -f -X "!*.@(t[bgxl]z)" || nofiles=set
18+
_comp_compgen -- -P "$prev%" -f -X "!*.t[bgxl]z" || nofiles=set
1919
_comp_compgen -a -- -P "$prev%" -S '/' -d
2020
[[ $nofiles ]] && compopt -o nospace
2121
return

completions/valgrind

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ _comp_cmd_valgrind()
77

88
local i
99
for ((i = 1; i <= cword; i++)); do
10-
if [[ ${words[i]} != @([-=])* ]]; then
10+
if [[ ${words[i]} != [-=]* ]]; then
1111
_comp_command_offset $i
1212
return
1313
fi

test/runLint

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ gitgrep '(?<!command)'"$cmdstart"'(grep|ls|sed|cd)(\s|$)' \
5252
gitgrep '(?<!command)'"$cmdstart"'awk(\s|$)' \
5353
'invoke awk through "_comp_awk"'
5454

55+
gitgrep '@\([^()|$]+\)' \
56+
'@(...) may not be needed when ... does not contain |.'
57+
5558
#------------------------------------------------------------------------------
5659
# Bash pitfalls/styles/compatibilities (which are not detected by shellcheck)
5760

0 commit comments

Comments
 (0)