Skip to content

Commit

Permalink
util (blehook): support wildcards in hook names
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Aug 24, 2022
1 parent c393c93 commit 480b7b3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -56,6 +56,7 @@
- util (`bleopt`): do no select obsoleted options by wildcards `#D1595` f4312df
- util (`bleopt`): fix error messages for unknown options `#D1610` 66df3e2
- util (`bleopt`, `bind`): fix error message and exit status, respectively `#D1640` b663cee
- util (`blehook`): support wildcards `#D1861` XXXXXXX
- progcomp: support quoted commands and better `progcomp_alias` `#D1581` `#D1583` dbe87c3
- progcomp: fix a bug that command names may stray into completer function names `#D1611` 1f2d45f
- syntax: highlight quotes of the `\?` form `#D1584` 5076a03
Expand Down
5 changes: 5 additions & 0 deletions note.txt
Expand Up @@ -6636,6 +6636,11 @@ bash_tips

2022-08-21

* blehook: wildcard @ に対応する [#D1861]

bleopt, ble-face が対応しているのに blehook が対応していないのは混乱の元。
実際に使いたくなるので対応する。

* history: blehook history_* を blehook history_onchange に統合する [#D1860]

history_{delete,insert,clear} は何れも同じ箇所でセットで登録している。だと
Expand Down
96 changes: 78 additions & 18 deletions src/util.sh
Expand Up @@ -1636,7 +1636,7 @@ function blehook/.print-help {
'' \
' Options:' \
' --help Print this help.' \
' -a, --all Print all including internal hooks.' \
' -a, --all Print all hooks including the internal ones.' \
' --color[=always|never|auto]' \
' Change color settings.' \
'' \
Expand All @@ -1648,6 +1648,9 @@ function blehook/.print-help {
' NAME!=COMMAND Add hook if the command is not registered.' \
' NAME-+=COMMAND Append the hook and remove the duplicates.' \
' NAME+-=COMMAND Prepend the hook and remove the duplicates.' \
'' \
' NAME:' \
' The hook name. The character `@'\'' may be used as a wildcard.' \
''
}

Expand Down Expand Up @@ -1688,22 +1691,29 @@ function blehook/.read-arguments {
done ;;
esac
elif [[ $arg =~ $rex1 ]]; then
local hookvar=_ble_hook_h_$arg
if ble/is-array "$hookvar"; then
ble/array#push print "$hookvar"
if [[ $arg == *@* ]] || ble/is-array "_ble_hook_h_$arg"; then
ble/array#push print "$arg"
else
ble/util/print "blehook: undefined hook '$arg'." >&2
flags=E$flags
fi
elif [[ $arg =~ $rex2 ]]; then
local name=${BASH_REMATCH[1]}
local var_counter=_ble_hook_c_$name
if [[ ! ${!var_counter+set} ]]; then
if [[ $name == *@* ]]; then
if [[ ${BASH_REMATCH[2]} == :* ]]; then
(($var_counter=0))
else
ble/util/print "blehook: hook \"$name\" is not defined." >&2
ble/util/print "blehook: hook pattern cannot be combined with '${BASH_REMATCH[2]}'." >&2
flags=E$flags
continue
fi
else
local var_counter=_ble_hook_c_$name
if [[ ! ${!var_counter+set} ]]; then
if [[ ${BASH_REMATCH[2]} == :* ]]; then
(($var_counter=0))
else
ble/util/print "blehook: hook \"$name\" is not defined." >&2
flags=E$flags
continue
fi
fi
fi
ble/array#push process "$arg"
Expand All @@ -1712,6 +1722,56 @@ function blehook/.read-arguments {
flags=E$flags
fi
done

# resolve patterns
local pat ret out; out=()
for pat in "${print[@]}"; do
if [[ $pat == *@* ]]; then
bleopt/expand-variable-pattern "_ble_hook_h_$pat"
ble/array#filter ret ble/is-array
[[ $pat == *[a-z]* || $flags == *a* ]] ||
ble/array#remove-by-glob ret '_ble_hook_h_*[a-z]*'
if ((!${#ret[@]})); then
ble/util/print "blehook: '$pat': matching hook not found." >&2
flags=E$flags
continue
fi
else
ret=("_ble_hook_h_$pat")
fi
ble/array#push out "${ret[@]}"
done
print=("${out[@]}")

out=()
for pat in "${process[@]}"; do
[[ $pat =~ $rex2 ]]
local name=${BASH_REMATCH[1]}
if [[ $name == *@* ]]; then
local type=${BASH_REMATCH[3]}
local value=${BASH_REMATCH[4]}

bleopt/expand-variable-pattern "_ble_hook_h_$pat"
ble/array#filter ret ble/is-array
[[ $pat == *[a-z]* || $flags == *a* ]] ||
ble/array#remove-by-glob ret '_ble_hook_h_*[a-z]*'
if ((!${#ret[@]})); then
ble/util/print "blehook: '$pat': matching hook not found." >&2
flags=E$flags
continue
fi
if ((_ble_bash>=40300)) && ! shopt -q compat42; then
ret=("${ret[@]/%/"$type$value"}") # WA #D1570 #D1751 checked
else
ret=("${ret[@]/%/$type$value}") # WA #D1570 #D1738 checked
fi
else
ret=("_ble_hook_h_$pat")
fi
ble/array#push out "${ret[@]}"
done
process=("${out[@]}")

[[ $opt_color == always || $opt_color == auto && -t 1 ]] && flags=c$flags
}

Expand All @@ -1721,8 +1781,8 @@ function blehook {
ble/base/.adjust-bash-options set shopt

local flags print process
local rex1='^([a-zA-Z_][a-zA-Z_0-9]*)$'
local rex2='^([a-zA-Z_][a-zA-Z_0-9]*)(:?([-+!]|-\+|\+-)?=)(.*)$'
local rex1='^([a-zA-Z_@][a-zA-Z_0-9@]*)$'
local rex2='^([a-zA-Z_@][a-zA-Z_0-9@]*)(:?([-+!]|-\+|\+-)?=)(.*)$'
blehook/.read-arguments "$@"
if [[ $flags == *[HE]* ]]; then
if [[ $flags == *H* ]]; then
Expand Down Expand Up @@ -1752,28 +1812,28 @@ function blehook {
case $type in
(*-*) # -=, -+=, +-=
local ret
ble/array#last-index "_ble_hook_h_$name" "$value"
ble/array#last-index "$name" "$value"
if ((ret>=0)); then
ble/array#remove-at "_ble_hook_h_$name" "$ret"
ble/array#remove-at "$name" "$ret"
elif [[ ${type#:} == '-=' ]]; then
ext=1
fi

if [[ $type != -+ ]]; then
append=
[[ $type == +- ]] &&
ble/array#unshift "_ble_hook_h_$name" "$value"
ble/array#unshift "$name" "$value"
fi ;;

('!') # !=
local ret
ble/array#last-index "_ble_hook_h_$name" "$value"
ble/array#last-index "$name" "$value"
((ret>=0)) && append= ;;

('') builtin eval "_ble_hook_h_$name=()" ;; # =
('') builtin eval "$name=()" ;; # =
('+'|*) ;; # +=
esac
[[ $append ]] && ble/array#push "_ble_hook_h_$name" "$append"
[[ $append ]] && ble/array#push "$name" "$append"
done

if ((${#print[@]})); then
Expand Down

0 comments on commit 480b7b3

Please sign in to comment.