Skip to content

Commit

Permalink
util, color (ble-face,blehook,bleopt): fix argument analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed May 28, 2021
1 parent 24f79da commit bb53271
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 36 deletions.
6 changes: 3 additions & 3 deletions README-ja_JP.md
Expand Up @@ -323,7 +323,7 @@ bleopt edit_abell=

## 2.6 着色の設定

構文に従った着色で使用される、各文法要素の色と属性は `ble-color-setface` シェル関数で設定します。
構文に従った着色で使用される、各文法要素の色と属性は `ble-face` シェル関数で設定します。
既定の設定は以下のコードに対応します:
```bash
# 編集に関連する着色の設定
Expand Down Expand Up @@ -393,9 +393,9 @@ ble-face -s varname_transform fg=29,bold
ble-face -s varname_unset fg=124
```

現在の描画設定の一覧は以下のコマンドでも確認できます (`ble-color-setface` を無引数で呼び出す)。
現在の描画設定の一覧は以下のコマンドでも確認できます (`ble-face` を無引数で呼び出す)。
```console
$ ble-color-setface
$ ble-face
```

色コードはシェル関数 `ble-color-show` (`ble.sh` 内で定義) で確認できます。
Expand Down
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -296,7 +296,7 @@ bleopt edit_abell=

## 2.6 Highlight Colors

The colors and attributes used in the syntax highlighting are controlled by `ble-color-setface` function. The following code reproduces the default configuration:
The colors and attributes used in the syntax highlighting are controlled by `ble-face` function. The following code reproduces the default configuration:
```bash
# highlighting related to editing
ble-face -s region bg=60,fg=white
Expand Down Expand Up @@ -365,9 +365,9 @@ ble-face -s varname_transform fg=29,bold
ble-face -s varname_unset fg=124
```

The current list of faces can be obtained by the following command (`ble-color-setface` without arguments):
The current list of faces can be obtained by the following command (`ble-face` without arguments):
```console
$ ble-color-setface
$ ble-face
```

The color codes can be checked in output of the function `ble-color-show` (defined in `ble.sh`):
Expand Down
14 changes: 7 additions & 7 deletions lib/test-util.sh
Expand Up @@ -1033,41 +1033,41 @@ function is-global() (readonly "$1"; ! local "$1" 2>/dev/null)
(
# declare hook
blehook/declare FOO
ble/test 'blehook FOO' stdout='blehook FOO='
ble/test 'blehook --color=never FOO' stdout='blehook FOO='
ble/test 'blehook/has-hook FOO' exit=1

# add/remove hook
blehook FOO+='echo hello'
ble/test 'blehook FOO' \
ble/test 'blehook --color=never FOO' \
stdout="blehook FOO+='echo hello'"
ble/test 'blehook/has-hook FOO'
blehook FOO+='echo world'
ble/test 'blehook FOO' \
ble/test 'blehook --color=never FOO' \
stdout="blehook FOO+='echo hello'" \
stdout="blehook FOO+='echo world'"
ble/test 'blehook/has-hook FOO'
blehook FOO-='echo hello'
ble/test 'blehook FOO' \
ble/test 'blehook --color=never FOO' \
stdout="blehook FOO+='echo world'"
ble/test 'blehook/has-hook FOO'
blehook FOO-='echo world'
ble/test 'blehook FOO' \
ble/test 'blehook --color=never FOO' \
stdout='blehook FOO='
ble/test 'blehook/has-hook FOO' exit=1

# reset hook
blehook FOO+='echo hello'
blehook FOO+='echo world'
blehook FOO='echo empty'
ble/test 'blehook FOO' \
ble/test 'blehook --color=never FOO' \
stdout="blehook FOO+='echo empty'"
ble/test 'blehook/has-hook FOO'

# clear hook
blehook FOO+='echo hello'
blehook FOO+='echo world'
blehook FOO=
ble/test 'blehook FOO' \
ble/test 'blehook --color=never FOO' \
stdout='blehook FOO='
ble/test 'blehook/has-hook FOO' exit=1

Expand Down
8 changes: 5 additions & 3 deletions memo/ChangeLog.md
Expand Up @@ -41,9 +41,11 @@
- syntax: support undocumented `${a~}` and `${a~~}` `#D1561` 4df29a6
- lib: support `lib/vim-airline` (motivated by huresche) `#D1565` da1d0ff
- util (`ble/gdict`): refactor `#D1569` 0000000
- color: support new face setting function `ble-face` `#D1568` 0000000
- util (`bleopt`): support option `-r` and `-u` and wildcards in option names `#D1568` 0000000
- util (`blehook`): hide internal hooks by default and support option `-a` `#D1568` 0000000
- util, color: refactor configuration interfaces (`bleopt`, `blehook`, `ble-face`) `#D1568` c94d292
- color: support new face setting function `ble-face`
- util (`bleopt`): support option `-r` and `-u` and wildcards in option names
- util (`blehook`): hide internal hooks by default and support option `-a`
- util, color: fix argument analysis of `bleopt`, `blehook`, and `ble-face` (fixup c94d292) `#D1571` 0000000

## Changes

Expand Down
16 changes: 16 additions & 0 deletions note.txt
Expand Up @@ -4655,6 +4655,22 @@ bash_tips

2021-05-27

* 新しい ble-face, blehook, bleopt で問題が起こっている [#D1571]

* fixed: blehook で新しい hook を作成できなくなっている。何故だろうか。これ
は引数解析のバグである。

* fixed: ble-face --hel でエラーにならずにコマンドを実行することができてい
る。これは修正した。他にも '-' に対する処理を blehook, bleopt と共に修正
した。

* fixed: ble-face --color=xxxx のエラーメッセージ表示時にエラーが発生する。
直した。blehook, bleopt についても --color=xxxx と指定した時のエラーメッ
セージを改善した。

* done: ble-face の使い方を wiki にまとめる
* done: --help に color の説明を含める。

* bash-3.0 の初期化時に bleopt が出力されてしまっている [#D1570]

% bash-3.1 では特に問題は起こっていない。という事はまた何かの bash bug に嵌っ
Expand Down
41 changes: 27 additions & 14 deletions src/color.sh
Expand Up @@ -1030,7 +1030,7 @@ function ble/color/initialize-faces {
(copy:*|face:*|iface:*)
# `face:*' and `iface:*' are obsoleted forms.
[[ $spec == copy:* ]] ||
ble/util/print "ble-color-setface: \"${spec%%:*}:*\" is obsoleted. Use \"copy:*\" instead." >&2
ble/util/print "ble-face: \"${spec%%:*}:*\" is obsoleted. Use \"copy:*\" instead." >&2
if [[ ! ${value//[0-9]} ]]; then
ble/color/iface2g "$value"
else
Expand Down Expand Up @@ -1139,14 +1139,17 @@ function ble-face/.read-arguments {
(--help) flags=H$flags ;;
(--color)
opt_color=always ;;
(--color=always | --color=auto | --color=never)
(--color=always|--color=auto|--color=never)
opt_color=${arg#*=} ;;
(--color=*)
ble/util/print "ble-face: unrecognized option argument for '--color' (${arg*#=})" >&2
ble/util/print "ble-face: '${arg#*=}': unrecognized option argument for '--color'." >&2
flags=E$flags ;;
(--reset) flags=r$flags ;;
(--changed) flags=u$flags ;;
(--) flags=L$flags ;;
(--*)
ble/util/print "ble-face: unrecognized long option '$arg'." >&2
flags=E$flags ;;
(-?*)
local i c
for ((i=1;i<${#arg};i++)); do
Expand All @@ -1167,10 +1170,13 @@ function ble-face/.read-arguments {
fi
ble-face/.read-arguments/process-set "${arg::2}" "$lhs" "$rhs"
break ;;
(*)
ble/util/print "ble-face: unrecognized option '-$c'." >&2
flags=E$flags ;;
esac
done ;;
(*)
ble/util/print "ble-face: unrecognized option '$arg'." >&2
(-)
ble/util/print "ble-face: unrecognized argument '$arg'." >&2
flags=E$flags ;;
esac
fi
Expand Down Expand Up @@ -1200,26 +1206,33 @@ function ble-face/.read-arguments {
function ble-face/.print-help {
ble/util/print-lines >&2 \
'ble-face --help' \
'ble-face [FACE[:=|=][TYPE:]SPEC | -[sd] FACE [TYPE:]SPEC]]...' \
'ble-face [-ur] [FACE...]' \
'ble-face [FACEPAT[:=|=][TYPE:]SPEC | -[sd] FACEPAT [TYPE:]SPEC]]...' \
'ble-face [-ur|--color[=WHEN]] [FACE...]' \
'' \
' OPTIONS/ARGUMENTS' \
'' \
' FACE=[TYPE:]SPEC' \
' -s FACE [TYPE:]SPEC' \
' Set a face' \
' FACEPAT=[TYPE:]SPEC' \
' -s FACEPAT [TYPE:]SPEC' \
' Set a face. FACEPAT can include a wildcard @ which matches one or' \
' more characters.' \
'' \
' FACE:=[TYPE:]SPEC' \
' -d FACE [TYPE:]SPEC' \
' Define a face' \
'' \
' FACE...' \
' [-u | --color[=always|never|auto]]... FACEPAT...' \
' Print faces. If faces are not specified, all faces are selected.' \
' -r FACE...' \
' If -u is specified, only the faces with different values from their' \
' default will be printed. The option "--color" controls the output' \
' color settings. The default is "auto".' \
'' \
' -r FACEPAT...' \
' Reset faces. If faces are not specified, all faces are selected.' \
'' \
' FACE Specifies a face name. The character @ in the face name is treated' \
' as a wildcard' \
' FACEPAT Specifies a face name. The character @ in the face name is treated' \
' as a wildcard.' \
'' \
' FACE Specifies a face name. Wildcard @ cannot be used.' \
'' \
' TYPE Specifies the format of SPEC. The following values are available.' \
' gspec Comma separated graphic attribute list' \
Expand Down
28 changes: 22 additions & 6 deletions src/util.sh
Expand Up @@ -15,10 +15,13 @@ function bleopt/.read-arguments/process-option {
flags=n${flags//[cn]} ;;
(color=auto)
flags=${flags//[cn]} ;;
(color=*)
ble/util/print "bleopt: '${name#*=}': unrecognized option argument for '--color'." >&2
flags=E$flags ;;
(reset) flags=r$flags ;;
(changed) flags=u$flags ;;
(*)
ble/util/print "bleopt: unrecognized long option '--$name'" >&2
ble/util/print "bleopt: unrecognized long option '--$name'." >&2
flags=E$flags ;;
esac
}
Expand Down Expand Up @@ -54,6 +57,9 @@ function bleopt/.read-arguments {
(--)
ble/array#push specs "$@"
break ;;
(-)
ble/util/print "bleopt: unrecognized argument '$arg'." >&2
flags=E$flags ;;
(--*)
bleopt/.read-arguments/process-option "${arg:2}" ;;
(-*)
Expand All @@ -64,7 +70,7 @@ function bleopt/.read-arguments {
(r) bleopt/.read-arguments/process-option reset ;;
(u) bleopt/.read-arguments/process-option changed ;;
(*)
ble/util/print "bleopt: unrecognized option '-c'" >&2
ble/util/print "bleopt: unrecognized option '-c'." >&2
flags=E$flags ;;
esac
done ;;
Expand Down Expand Up @@ -138,6 +144,8 @@ function bleopt {
' --help Print this help.' \
' -r, --reset Reset options to the default values' \
' -u, --changed Only select changed options' \
' --color[=always|never|auto]' \
' Change color settings.' \
'' \
' Arguments' \
' NAME Print the value of the option.' \
Expand Down Expand Up @@ -1529,6 +1537,8 @@ function blehook/.print-help {
' Options:' \
' --help Print this help.' \
' -a, --all Print all including internal hooks.' \
' --color[=always|never|auto]' \
' Change color settings.' \
'' \
' Arguments:' \
' NAME Print the corresponding hooks.' \
Expand All @@ -1550,13 +1560,19 @@ function blehook/.read-arguments {
case $arg in
(--help)
flags=H$flags ;;
(--color|--color=always) opt_color=always ;;
(--color=auto) opt_color=auto ;;
(--color=never) opt_color=never ;;
(--color) opt_color=always ;;
(--color=always|--color=auto|--color=never)
opt_color=${arg#*=} ;;
(--color=*)
ble/util/print "blehook: '${arg#*=}': unrecognized option argument for '--color'." >&2
flags=E$flags ;;
(--all) flags=a$flags ;;
(--*)
ble/util/print "blehook: unrecognized long option '$arg'." >&2
flags=E$flags ;;
(-)
ble/util/print "blehook: unrecognized argument '$arg'." >&2
flags=E$flags ;;
(*)
local i c
for ((i=1;i<${#arg};i++)); do
Expand All @@ -1571,7 +1587,7 @@ function blehook/.read-arguments {
esac
elif [[ $arg =~ $rex1 ]]; then
local hookvar=_ble_hook_h_$arg
if [[ ${!hookvar+set} ]]; then
if ble/is-array "$hookvar"; then
ble/array#push print "$hookvar"
else
ble/util/print "blehook: undefined hook '$arg'." >&2
Expand Down

0 comments on commit bb53271

Please sign in to comment.