Skip to content

Commit

Permalink
sabbrev: support options "-r" and "--reset" to remove entries
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Mar 4, 2022
1 parent 7b70a0e commit 29b8be3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -94,6 +94,7 @@
- edit: work around a bash-4.4..5.1 bug of `exit` outputting time to stderr of exit context `#D1765` 3de751e e61dbaa
- 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

## Changes

Expand Down
29 changes: 27 additions & 2 deletions lib/core-complete.sh
Expand Up @@ -7808,6 +7808,10 @@ function ble/complete/sabbrev/.print-definition {
## 登録されている静的略語展開の一覧を表示します。
## @var[in] flags
##
## @fn ble/complete/sabbrev/reset [keys...]
## 登録されている静的略語展開を削除します。
## @var[in] flags
##
## @fn ble/complete/sabbrev/get key
## 静的略語展開の展開値を取得します。
## @param[in] key
Expand Down Expand Up @@ -7841,6 +7845,17 @@ function ble/complete/sabbrev/list {
done
return "$ext"
}
function ble/complete/sabbrev/reset {
if (($#)); then
local key
for key; do
ble/gdict#unset _ble_complete_sabbrev "$key"
done
else
ble/gdict#clear _ble_complete_sabbrev
fi
return 0
}
function ble/complete/sabbrev/get {
local key=$1
ble/gdict#get _ble_complete_sabbrev "$key"
Expand All @@ -7860,6 +7875,8 @@ function ble/complete/sabbrev/read-arguments {
case $arg in
(--help)
flags=H$flags ;;
(--reset)
flags=r$flags
(--color|--color=always)
flags=c${flags//[cn]} ;;
(--color=never)
Expand All @@ -7881,6 +7898,8 @@ function ble/complete/sabbrev/read-arguments {
else
ble/array#push specs "$c:$1"; shift
fi ;;
(r)
flags=r$flags ;;
(*)
ble/util/print "ble-sabbrev: unknown option '-$c'." >&2
flags=E$flags ;;
Expand All @@ -7902,14 +7921,20 @@ function ble-sabbrev {
if [[ $flags == *H* || $flags == *E* ]]; then
[[ $flags == *E* ]] && ble/util/print
ble/util/print-lines \
'usage: ble-sabbrev [key=value|-m key=function|--help]' \
'usage: ble-sabbrev [KEY=VALUE|-m KEY=FUNCTION]...' \
'usage: ble-sabbrev [-r|--reset] [KEY...]' \
'usage: ble-sabbrev --help' \
' Register sabbrev expansion.'
[[ ! $flags == *E* ]]; return "$?"
fi

local ext=0
if ((${#specs[@]}==0||${#print[@]})); then
ble/complete/sabbrev/list "${print[@]}" || ext=$?
if [[ $flags == *r* ]]; then
ble/complete/sabbrev/reset "${print[@]}"
else
ble/complete/sabbrev/list "${print[@]}"
fi || ext=$?
fi

local spec key type value
Expand Down
31 changes: 16 additions & 15 deletions note.txt
Expand Up @@ -1832,21 +1832,9 @@ bash_tips
* rsteube の記事
https://dev.to/rsteube/a-pragmatic-approach-to-shell-completion-4gp0

filtering を description に対しても適用するという事が書かれている。
それは確かに便利そうである。現状の実装では対応が難しい様には思われるが。

* sabbrev を削除する機能がない

削除の仕方としては以下の2種類が考えられる。

ble-sabbrev -r a # bind, complete と同様
ble-sabbrev a=- # trap と同様

後者は本当に "-" に展開させたい時 (実際そういう場合が存在するのかは謎だが)
と衝突するので前者の方が良い気がする。

また、前者を採用するとしたら -r に対して optarg を要求するのか、或いは -r
に対して optarg を要求するのかで振る舞いが変わってくる。
filtering を description に対しても適用するという事が書かれている。それは確
かに便利そうである。現状の実装では対応が難しい様には思われるが。これは fzf
の様に一旦絞り込み専用の入力欄に移動するなどの事がないと使いにくいだろう。

* trap (lastarg): 一応 heredoc 等を使えば eval の中から複数行の lastarg を設
定する事ができるのではないか。他に複数行で、eval されても余分な実行が起こら
Expand Down Expand Up @@ -6167,6 +6155,19 @@ bash_tips

2022-03-02

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

削除の仕方としては以下の2種類が考えられる。

ble-sabbrev -r a # bind, complete と同様
ble-sabbrev a=- # trap と同様

後者は本当に "-" に展開させたい時 (実際そういう場合が存在するのかは謎だが)
と衝突するので前者の方が良い気がする。

また、前者を採用するとしたら -r に対して optarg を要求するのか、或いは -r
に対して optarg を要求するのかで振る舞いが変わってくる。

* progcomp: ble.sh 既定の候補を生成しない機能 (motivated by rsteube) [#D1789]
https://github.com/rsteube/carapace/issues/431

Expand Down

0 comments on commit 29b8be3

Please sign in to comment.