Skip to content

Commit

Permalink
color: fix the face initialiation order for uses in prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jun 10, 2021
1 parent 040016d commit 321371f
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 79 deletions.
2 changes: 1 addition & 1 deletion ble.pp
Expand Up @@ -1181,8 +1181,8 @@ function ble-update {
#%x inc.r|@|src/canvas|
#%x inc.r|@|src/history|
#%x inc.r|@|src/edit|
#%x inc.r|@|lib/core-complete-def|
#%x inc.r|@|lib/core-syntax-def|
#%x inc.r|@|lib/core-complete-def|

bleopt/check-all
#------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion lib/core-complete-def.sh
@@ -1,6 +1,6 @@
#!/bin/bash

ble/function#try ble/util/idle.push 'ble/util/import "$_ble_base/lib/core-complete.sh"'
ble/is-function ble/util/idle.push && ble-import -d "$_ble_base/lib/core-complete.sh"

#------------------------------------------------------------------------------
# 公開関数と公開関数
Expand Down
76 changes: 75 additions & 1 deletion lib/core-syntax-def.sh
Expand Up @@ -85,7 +85,7 @@ ble/util/autoload "$_ble_base/lib/core-syntax.sh" \
function ble/syntax/import {
ble/util/import "$_ble_base/lib/core-syntax.sh"
}
ble-import -d lib/core-syntax
ble/is-function ble/util/idle.push && ble-import -d "$_ble_base/lib/core-syntax.sh"

#------------------------------------------------------------------------------
# グローバル変数の定義 (関数内からではできないのでここで先に定義)
Expand All @@ -105,3 +105,77 @@ builtin eval -- "${_ble_util_gdict_declare//NAME/_ble_syntax_highlight_filetype}
builtin eval -- "${_ble_util_gdict_declare//NAME/_ble_syntax_highlight_lscolors_ext}"
builtin eval -- "${_ble_util_gdict_declare//NAME/_ble_syntax_bash_simple_eval}"
builtin eval -- "${_ble_util_gdict_declare//NAME/_ble_syntax_bash_simple_eval_full}"

#------------------------------------------------------------------------------
# face の定義
#
# プロンプトで face を参照していると最初のプロンプト表示時に initialize-faces
# が実行され、ユーザーが blerc に設定した setface も実行される。この時点では
# core-syntax.sh は未だ読み込まれていないので、face の定義が core-syntax.sh の
# 中にあると face が見つからないエラーになる。

function ble/syntax/attr2g { ble/color/initialize-faces && ble/syntax/attr2g "$@"; }

function ble/syntax/defface.onload {
function ble/syntax/attr2g {
local iface=${_ble_syntax_attr2iface[$1]:-_ble_faces__syntax_default}
g=${_ble_faces[iface]}
}

# Note: navy was replaced by 26 for dark background
# Note: gray was replaced by 242 for dark background

ble/color/defface syntax_default none
ble/color/defface syntax_command fg=brown
ble/color/defface syntax_quoted fg=green
ble/color/defface syntax_quotation fg=green,bold
ble/color/defface syntax_escape fg=magenta
ble/color/defface syntax_expr fg=26
ble/color/defface syntax_error bg=203,fg=231 # bg=224
ble/color/defface syntax_varname fg=202
ble/color/defface syntax_delimiter bold
ble/color/defface syntax_param_expansion fg=purple
ble/color/defface syntax_history_expansion bg=94,fg=231
ble/color/defface syntax_function_name fg=92,bold # fg=purple
ble/color/defface syntax_comment fg=242
ble/color/defface syntax_glob fg=198,bold
ble/color/defface syntax_brace fg=37,bold
ble/color/defface syntax_tilde fg=navy,bold
ble/color/defface syntax_document fg=94
ble/color/defface syntax_document_begin fg=94,bold

ble/color/defface command_builtin_dot fg=red,bold
ble/color/defface command_builtin fg=red
ble/color/defface command_alias fg=teal
ble/color/defface command_function fg=92 # fg=purple
ble/color/defface command_file fg=green
ble/color/defface command_keyword fg=blue
ble/color/defface command_jobs fg=red,bold
ble/color/defface command_directory fg=26,underline
ble/color/defface filename_directory underline,fg=26
ble/color/defface filename_directory_sticky underline,fg=white,bg=26
ble/color/defface filename_link underline,fg=teal
ble/color/defface filename_orphan underline,fg=teal,bg=224
ble/color/defface filename_setuid underline,fg=black,bg=220
ble/color/defface filename_setgid underline,fg=black,bg=191
ble/color/defface filename_executable underline,fg=green
ble/color/defface filename_other underline
ble/color/defface filename_socket underline,fg=cyan,bg=black
ble/color/defface filename_pipe underline,fg=lime,bg=black
ble/color/defface filename_character underline,fg=white,bg=black
ble/color/defface filename_block underline,fg=yellow,bg=black
ble/color/defface filename_warning underline,fg=red
ble/color/defface filename_url underline,fg=blue
ble/color/defface filename_ls_colors underline
ble/color/defface varname_unset fg=124
ble/color/defface varname_empty fg=31
ble/color/defface varname_number fg=64
ble/color/defface varname_expr fg=92,bold
ble/color/defface varname_array fg=orange,bold
ble/color/defface varname_hash fg=70,bold
ble/color/defface varname_readonly fg=200
ble/color/defface varname_transform fg=29,bold
ble/color/defface varname_export fg=200,bold
ble/color/defface argument_option fg=teal
}
blehook/eval-after-load color_defface ble/syntax/defface.onload
67 changes: 2 additions & 65 deletions lib/core-syntax.sh
Expand Up @@ -6005,75 +6005,13 @@ function ble/syntax:bash/extract-command-by-noderef {

# 遅延初期化対象
_ble_syntax_attr2iface=()
function ble/syntax/attr2g { ble/color/initialize-faces && ble/syntax/attr2g "$@"; }

# 遅延初期化子
function ble/syntax/faces-onload-hook {
function ble/syntax/attr2iface/color_defface.onload {
function ble/syntax/attr2iface/.define {
((_ble_syntax_attr2iface[$1]=_ble_faces__$2))
}

function ble/syntax/attr2g {
local iface=${_ble_syntax_attr2iface[$1]:-_ble_faces__syntax_default}
g=${_ble_faces[iface]}
}

# Note: navy was replaced by 26 for dark background
# Note: gray was replaced by 242 for dark background

ble/color/defface syntax_default none
ble/color/defface syntax_command fg=brown
ble/color/defface syntax_quoted fg=green
ble/color/defface syntax_quotation fg=green,bold
ble/color/defface syntax_escape fg=magenta
ble/color/defface syntax_expr fg=26
ble/color/defface syntax_error bg=203,fg=231 # bg=224
ble/color/defface syntax_varname fg=202
ble/color/defface syntax_delimiter bold
ble/color/defface syntax_param_expansion fg=purple
ble/color/defface syntax_history_expansion bg=94,fg=231
ble/color/defface syntax_function_name fg=92,bold # fg=purple
ble/color/defface syntax_comment fg=242
ble/color/defface syntax_glob fg=198,bold
ble/color/defface syntax_brace fg=37,bold
ble/color/defface syntax_tilde fg=navy,bold
ble/color/defface syntax_document fg=94
ble/color/defface syntax_document_begin fg=94,bold

ble/color/defface command_builtin_dot fg=red,bold
ble/color/defface command_builtin fg=red
ble/color/defface command_alias fg=teal
ble/color/defface command_function fg=92 # fg=purple
ble/color/defface command_file fg=green
ble/color/defface command_keyword fg=blue
ble/color/defface command_jobs fg=red,bold
ble/color/defface command_directory fg=26,underline
ble/color/defface filename_directory underline,fg=26
ble/color/defface filename_directory_sticky underline,fg=white,bg=26
ble/color/defface filename_link underline,fg=teal
ble/color/defface filename_orphan underline,fg=teal,bg=224
ble/color/defface filename_setuid underline,fg=black,bg=220
ble/color/defface filename_setgid underline,fg=black,bg=191
ble/color/defface filename_executable underline,fg=green
ble/color/defface filename_other underline
ble/color/defface filename_socket underline,fg=cyan,bg=black
ble/color/defface filename_pipe underline,fg=lime,bg=black
ble/color/defface filename_character underline,fg=white,bg=black
ble/color/defface filename_block underline,fg=yellow,bg=black
ble/color/defface filename_warning underline,fg=red
ble/color/defface filename_url underline,fg=blue
ble/color/defface filename_ls_colors underline
ble/color/defface varname_unset fg=124
ble/color/defface varname_empty fg=31
ble/color/defface varname_number fg=64
ble/color/defface varname_expr fg=92,bold
ble/color/defface varname_array fg=orange,bold
ble/color/defface varname_hash fg=70,bold
ble/color/defface varname_readonly fg=200
ble/color/defface varname_transform fg=29,bold
ble/color/defface varname_export fg=200,bold
ble/color/defface argument_option fg=teal

ble/syntax/attr2iface/.define CTX_ARGX syntax_default
ble/syntax/attr2iface/.define CTX_ARGX0 syntax_default
ble/syntax/attr2iface/.define CTX_ARGI syntax_default
Expand Down Expand Up @@ -6200,8 +6138,7 @@ function ble/syntax/faces-onload-hook {
ble/syntax/attr2iface/.define ATTR_VAR_TRANSFORM varname_transform
ble/syntax/attr2iface/.define ATTR_VAR_EXPORT varname_export
}

blehook color_init_defface+=ble/syntax/faces-onload-hook
blehook/eval-after-load color_defface ble/syntax/attr2iface/color_defface.onload

#------------------------------------------------------------------------------
# ble/syntax/highlight/cmdtype
Expand Down
1 change: 1 addition & 0 deletions memo/ChangeLog.md
Expand Up @@ -44,6 +44,7 @@
- vim-airline: support `bleopt vim_airline_theme` `#D1589` 0000000
- prompt: track dependencies and detect changes `#D1590` `#D1591` 0000000
- prompt: preserve `LINES` and `COLUMNS` for custom sequences `#D1592` 0000000
- color: fix the face initialiation order for uses in prompts (motivated by jmederosalvarado) `#D1593` 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
Expand Down
36 changes: 36 additions & 0 deletions note.txt
Expand Up @@ -4698,6 +4698,42 @@ bash_tips

2021-06-09

* color: initialize-faces の遅延に関する問題 (motivated by jmederosalvarado) [#D1593]
https://github.com/akinomyoga/ble.sh/issues/96#issuecomment-813185300

* vim-airline: theme を blerc で設定しても何故か暫くすると dark の配色が読み
込まれている気がする。改めて設定し直すと問題は起こらない。theme を変えた瞬
間の配色が何だか変になる。

うーん。調べてみると initialize-faces が2回呼び出されている気がする。何故だ
ろうか。ble/syntax/attr2g 経由である。うーん。分かった。core-syntax.sh が定
義している関数で ble/syntax/attr2g の初期化を遅延しようとしている。然し、最
初にプロンプトを表示した時点で既に initialize-faces が実行されるので、その
後で ble/syntax/attr2g { initialie-faces && ble/syntax/attr2g "$@"; } 等と
すると二重に初期化される様になるのである。eval-after-load 的な仕組みで登録
する必要があるのではないか。

* また、initialize-faces が実行されたら実行した hook はクリアする事にする。

* この際なので GitHub #96 のコメントで指摘されている遅延についても対処する。

沢山の setface 設定を仕込んでいると最初のキーストロークの反応が遅いという
話。これに関しては initialize-faces を idle で実行すれば良い。

idle の登録順序はどうなっているだろうか。ロードした直後の idle の順序を確
認する。complet を絶対パスでロードするように変更して、また syntax より後
にロードする様に変更した。変更後の様子は以下の通り。

[0]="R\\ble/history:bash/resolve-multiline async"
[1]="R\\ble/util/import '/home/murase/.mwg/src/ble.sh/out/lib/core-syntax.sh'"
[2]="R\\ble/util/import '/home/murase/.mwg/src/ble.sh/out/lib/core-complete.sh'"
[3]="R\\ble/util/import '/etc/profile.d/modules.sh'"
[10000]="R\\ble/util/msleep/calibrate"
[10001]="R\\ble/textarea#render-defer.idle")

うーん。syntax よりは前に faces は initialize して良いのではないか。とい
う事を考えると、実は一番最初に登録してしまっても問題ない。登録した。

* vim-airline を blerc からロードする様にすると余分な行が表示される [#D1592]

| 調べると \q{lib/vim-airline} の中で問題が発生している。更に調べると
Expand Down
29 changes: 21 additions & 8 deletions src/color.sh
Expand Up @@ -918,8 +918,8 @@ function ble/color/ansi2g {
# _ble_faces

# 遅延初期化登録
# @hook color_init_defface (defined in src/def.sh)
# @hook color_init_setface (defined in src/def.sh)
# @hook color_defface_load (defined in src/def.sh)
# @hook color_setface_load (defined in src/def.sh)

# 遅延初期化
if [[ ! ${_ble_faces_count-} ]]; then # reload #D0875
Expand Down Expand Up @@ -981,14 +981,15 @@ function ble-color-setface {
}

# 遅延関数 (後で上書き)
function ble/color/defface { local q=\' Q="'\''"; blehook color_init_defface+="ble/color/defface '${1//$q/$Q}' '${2//$q/$Q}'"; }
function ble/color/setface { local q=\' Q="'\''"; blehook color_init_setface+="ble/color/setface '${1//$q/$Q}' '${2//$q/$Q}'"; }
function ble/color/defface { local q=\' Q="'\''"; blehook color_defface_load+="ble/color/defface '${1//$q/$Q}' '${2//$q/$Q}'"; }
function ble/color/setface { local q=\' Q="'\''"; blehook color_setface_load+="ble/color/setface '${1//$q/$Q}' '${2//$q/$Q}'"; }
function ble/color/face2g { ble/color/initialize-faces && ble/color/face2g "$@"; }
function ble/color/face2sgr { ble/color/initialize-faces && ble/color/face2sgr "$@"; }
function ble/color/iface2g { ble/color/initialize-faces && ble/color/iface2g "$@"; }
function ble/color/iface2sgr { ble/color/initialize-faces && ble/color/iface2sgr "$@"; }

# 遅延初期化子
_ble_color_faces_initialized=
function ble/color/initialize-faces {
local _ble_color_faces_initializing=1
local -a _ble_color_faces_errors=()
Expand Down Expand Up @@ -1065,8 +1066,11 @@ function ble/color/initialize-faces {
fi
}

blehook/invoke color_init_defface
blehook/invoke color_init_setface
_ble_color_faces_initialized=1
blehook/invoke color_defface_load
blehook/invoke color_setface_load
blehook color_defface_load=
blehook color_setface_load=

if ((${#_ble_color_faces_errors[@]})); then
if ((_ble_edit_attached)) && [[ ! $_ble_textarea_invalidated && $_ble_term_state == internal ]]; then
Expand All @@ -1080,6 +1084,7 @@ function ble/color/initialize-faces {
return 0
fi
}
ble/function#try ble/util/idle.push ble/color/initialize-faces

## @fn ble/color/list-faces opts
function ble/color/list-faces {
Expand Down Expand Up @@ -1291,6 +1296,14 @@ function ble-face {
print=(@)
fi

((${#print[@]})) && ble/color/initialize-faces
if [[ ! $_ble_color_faces_initialized ]]; then
local ret
ble/string#quote-command ble-face "${setface[@]}"
blehook color_setface_load+="$ret"
return 0
fi

local spec
for spec in "${setface[@]}"; do
if local rex='^([_a-zA-Z@][_a-zA-Z0-9@]*)(:?=)(.*)$'; ! [[ $spec =~ $rex ]]; then
Expand Down Expand Up @@ -1564,15 +1577,15 @@ function ble/highlight/layer:plain/getg {
#------------------------------------------------------------------------------
# ble/highlight/layer:region

function ble/color/faces-defface-hook {
function ble/color/defface.onload {
ble/color/defface region bg=60,fg=white
ble/color/defface region_target bg=153,fg=black
ble/color/defface region_match bg=55,fg=white
ble/color/defface region_insert fg=12,bg=252
ble/color/defface disabled fg=242
ble/color/defface overwrite_mode fg=black,bg=51
}
blehook color_init_defface+=ble/color/faces-defface-hook
blehook color_defface_load+=ble/color/defface.onload

## @arr _ble_highlight_layer_region_buff
##
Expand Down
4 changes: 2 additions & 2 deletions src/def.sh
Expand Up @@ -30,8 +30,8 @@ blehook/declare DA2R

# color.sh

blehook/declare color_init_defface
blehook/declare color_init_setface
blehook/declare color_defface_load
blehook/declare color_setface_load

# history.sh

Expand Down
2 changes: 1 addition & 1 deletion src/util.sh
Expand Up @@ -5041,7 +5041,7 @@ function ble/term/visible-bell/defface.hook {
ble/color/defface vbell_flash reverse,fg=green
ble/color/defface vbell_erase bg=252
}
blehook color_init_defface+=ble/term/visible-bell/defface.hook
blehook color_defface_load+=ble/term/visible-bell/defface.hook

function ble/term/visible-bell/.show {
local bell_type=${_ble_term_visible_bell_prev[0]}
Expand Down

0 comments on commit 321371f

Please sign in to comment.