Skip to content

Commit

Permalink
util (blehook): support "hook!=handler" and "hook+-=handler"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Mar 4, 2022
1 parent 29b8be3 commit 0b8c097
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -95,6 +95,7 @@
- util: preserve original traps and restore them on unload `#D1775` `#D1776` `#D1777` 398e404
- progcomp: support `compopt -o ble/no-default` to suppress default completions `#D1789` 0000000
- sabbrev: support options `-r` and `--reset` to remove entries `#D1790` 0000000
- util (blehook): support `hook!=handler` and `hook+-=handler` `#D1791` 0000000

## Changes

Expand Down
18 changes: 17 additions & 1 deletion lib/test-util.sh
Expand Up @@ -2,7 +2,7 @@

ble-import lib/core-test

ble/test/start-section 'ble/util' 1193
ble/test/start-section 'ble/util' 1197

# bleopt

Expand Down Expand Up @@ -1073,7 +1073,23 @@ function is-global() (readonly "$1"; ! local "$1" 2>/dev/null)
stdout='blehook FOO='
ble/test 'blehook/has-hook FOO' exit=1

# uniq hook
blehook FOO+='echo hello'
blehook FOO+='echo world'
blehook FOO!='echo hello'
ble/test 'blehook --color=never FOO' \
stdout="blehook FOO+='echo hello'${_ble_term_nl}blehook FOO+='echo world'"
# uniq append
blehook FOO-+='echo hello'
ble/test 'blehook --color=never FOO' \
stdout="blehook FOO+='echo world'${_ble_term_nl}blehook FOO+='echo hello'"
# uniq prepend
blehook FOO+-='echo hello'
ble/test 'blehook --color=never FOO' \
stdout="blehook FOO+='echo hello'${_ble_term_nl}blehook FOO+='echo world'"

# invoke hook
blehook FOO=
blehook FOO+='echo hello'
blehook FOO+='echo empty'
blehook FOO+='echo world'
Expand Down
14 changes: 11 additions & 3 deletions note.txt
Expand Up @@ -1813,10 +1813,10 @@ bash_tips

2022-02-20

* 何故か2回目の char_width_@=auto がちゃんと動いていない気がする。何故だろう
。後で確認する。
* BUG 何故か2回目の char_width_@=auto がちゃんと動いていない気がする。何故だ
ろうか。後で確認する。

* complete: cygwin$ pdflatex [TAB] で /usr/bin/cat: '': No such file or
* BUG complete: cygwin$ pdflatex [TAB] で /usr/bin/cat: '': No such file or
directory というエラーが出力される。chat では再現しない。bash-completion は
ロードされていない。

Expand Down Expand Up @@ -6155,6 +6155,14 @@ bash_tips

2022-03-02

* blehook -+= は入力しにくいし見にくいので != を一意追加として対応する [#D1791]

* done: != ... 未だ登録されていない時に追加する
* done: -+= ... 後ろに持ってくるという意味に変える。
* done: +-= ... 前に持ってくるという意味で追加する。

* update wiki

* complete: sabbrev を削除する機能がない [#D1790]

削除の仕方としては以下の2種類が考えられる。
Expand Down
35 changes: 26 additions & 9 deletions src/util.sh
Expand Up @@ -1727,7 +1727,9 @@ function blehook/.print-help {
' NAME=COMMAND Set hook after removing the existing hooks.' \
' NAME+=COMMAND Add hook.' \
' NAME-=COMMAND Remove hook.' \
' NAME-+=COMMAND Add hook if the command is not registered.' \
' 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.' \
''
}

Expand Down Expand Up @@ -1802,7 +1804,7 @@ function blehook {

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 rex2='^([a-zA-Z_][a-zA-Z_0-9]*)(:?([-+!]|-\+|\+-)?=)(.*)$'
blehook/.read-arguments "$@"
if [[ $flags == *[HE]* ]]; then
if [[ $flags == *H* ]]; then
Expand All @@ -1825,20 +1827,35 @@ function blehook {
for proc in "${process[@]}"; do
[[ $proc =~ $rex2 ]]
local name=${BASH_REMATCH[1]}
local type=${BASH_REMATCH[2]}
local value=${BASH_REMATCH[3]}
if [[ $type == *-* ]]; then
local type=${BASH_REMATCH[3]}
local value=${BASH_REMATCH[4]}

local append=$value
case $type in
(*-*) # -=, -+=, +-=
local ret
ble/array#last-index "_ble_hook_h_$name" "$value"
if ((ret>=0)); then
ble/array#remove-at "_ble_hook_h_$name" "$ret"
elif [[ ${type#:} == '-=' ]]; then
ext=1
fi
fi
[[ ${type#:} == '=' ]] && builtin eval "_ble_hook_h_$name=()"
[[ ${type#:} != '-=' && $value ]] &&
ble/array#push "_ble_hook_h_$name" "$value"

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

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

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

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

0 comments on commit 0b8c097

Please sign in to comment.