Skip to content

Commit

Permalink
fix(_comp_array_filter): support consistent anchoring also for -E
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Apr 13, 2022
1 parent f0bcdcc commit d06a9b4
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,19 @@ _upvars()
#
# Options:
# -E $2 is interpreted as a POSIX extended regular expression.
# This is always the partial matching unless ^, $ is included
# in $2.
# -F $2 is interpreted as a fixed string.
# -G $2 is interpreted as a glob pattern.
#
# -p Combined with -F or -G, it performs the prefix matching.
# -s Combined with -F or -G, it performs the suffix matching.
# -m Combined with -F or -G, it performs the middle matching.
# The default anchoring is `-m` (see below).
# -F $2 is interpreted as a fixed string. The default anchoring
# is `-m` (see below).
# -G $2 is interpreted as a glob pattern. The default anchoring
# is `-x` (see below).
#
# -p Combined with -EFG, it performs the prefix matching.
# -s Combined with -EFG, it performs the suffix matching.
# -m Combined with -EFG, it performs the middle matching.
# -x Combined with -EFG, it performs the exact matching.
#
# -r Revert the condition, i.e., remove elements that satisfy
# the original condition.
#
# -C Array compaction is not performed.
#
# @return 2 with a wrong usage, 1 when any elements are removed, 0 when the set
Expand All @@ -278,14 +280,14 @@ _comp_array_filter()
{
local _comp_local_flags='' _comp_local_pattype='' _comp_local_anchoring=''
local OPTIND=1 OPTARG='' OPTERR=0 _comp_local_opt=''
while getopts 'EFGpsmrC' _comp_local_opt "$@"; do
while getopts 'EFGpsmxrC' _comp_local_opt "$@"; do
case $_comp_local_opt in
[EFG]) _comp_local_pattype=$_comp_local_opt ;;
[psm]) _comp_local_anchoring=$_comp_local_opt ;;
[psmx]) _comp_local_anchoring=$_comp_local_opt ;;
[rC]) _comp_local_flags=$_comp_local_opt$_comp_local_flags ;;
*)
printf 'bash_completion: %s: %s\n' "$FUNCNAME" 'usage error' >&2
printf 'usage: %s %s\n' "$FUNCNAME" "[-EFGpsmrC] ARRAY_NAME CONDITION" >&2
printf 'usage: %s %s\n' "$FUNCNAME" "[-EFGpsmxrC] ARRAY_NAME CONDITION" >&2
return 2
;;
esac
Expand All @@ -294,7 +296,7 @@ _comp_array_filter()
shift $((OPTIND - 1))
if (($# != 2)); then
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "unexpected number of arguments: $#" >&2
printf 'usage: %s %s\n' "$FUNCNAME" "[-EFGpsmrC] ARRAY_NAME CONDITION" >&2
printf 'usage: %s %s\n' "$FUNCNAME" "[-EFGpsmxrC] ARRAY_NAME CONDITION" >&2
return 2
elif [[ $1 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "invalid array name '$1'." >&2
Expand All @@ -312,14 +314,19 @@ _comp_array_filter()
local _comp_local_predicate='' _comp_local_pattern=$2
case $_comp_local_pattype in
E)
_comp_local_predicate='[[ $_comp_local_value =~ $_comp_local_pattern ]]'
case $_comp_local_anchoring in
p) _comp_local_predicate='[[ $_comp_local_value =~ ^($_comp_local_pattern) ]]' ;;
s) _comp_local_predicate='[[ $_comp_local_value =~ ($_comp_local_pattern)$ ]]' ;;
x) _comp_local_predicate='[[ $_comp_local_value =~ ^($_comp_local_pattern)$ ]]' ;;
*) _comp_local_predicate='[[ $_comp_local_value =~ $_comp_local_pattern ]]' ;;
esac
;;
F)
case $_comp_local_anchoring in
p) _comp_local_predicate='[[ $_comp_local_value == "$_comp_local_pattern"* ]]' ;;
s) _comp_local_predicate='[[ $_comp_local_value == *"$_comp_local_pattern" ]]' ;;
m) _comp_local_predicate='[[ $_comp_local_value == *"$_comp_local_pattern"* ]]' ;;
*) _comp_local_predicate='[[ $_comp_local_value == "$_comp_local_pattern" ]]' ;;
x) _comp_local_predicate='[[ $_comp_local_value == "$_comp_local_pattern" ]]' ;;
*) _comp_local_predicate='[[ $_comp_local_value == *"$_comp_local_pattern"* ]]' ;;
esac
;;
G)
Expand Down

0 comments on commit d06a9b4

Please sign in to comment.