Skip to content

Commit

Permalink
highlight/vartype: reference the saved states of variables
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Aug 25, 2024
1 parent 4984570 commit 063249b
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 19 deletions.
75 changes: 75 additions & 0 deletions ble.pp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,34 @@ function ble/base/is-POSIXLY_CORRECT {
[[ $_ble_bash_POSIXLY_CORRECT_adjusted && $_ble_bash_POSIXLY_CORRECT_set ]]
}

function ble/variable#load-user-state/variable:FUNCNEST {
if [[ $_ble_bash_FUNCNEST_adjusted ]]; then
__ble_var_set=$_ble_bash_FUNCNEST_set
__ble_var_val=$_ble_bash_FUNCNEST
return 0
elif [[ ${_ble_local_FUNCNEST_set-} ]]; then
__ble_var_set=$_ble_local_FUNCNEST_set
__ble_var_set=$_ble_local_FUNCNEST
return 0
else
return 1
fi
}

function ble/variable#load-user-state/variable:POSIXLY_CORRECT {
if [[ $_ble_bash_POSIXLY_CORRECT_adjusted ]]; then
__ble_var_set=$_ble_bash_POSIXLY_CORRECT_set
__ble_var_val=$_ble_bash_POSIXLY_CORRECT
return 0
elif [[ ${_ble_local_POSIXLY_CORRECT_set-} ]]; then
__ble_var_set=$_ble_local_POSIXLY_CORRECT_set
__ble_var_set=$_ble_local_POSIXLY_CORRECT
return 0
else
return 1
fi
}

## @fn ble/base/list-shopt names...
## @var[out] shopt
if ((_ble_bash>=40100)); then
Expand Down Expand Up @@ -692,6 +720,35 @@ function ble/base/recover-bash-options {
fi
}

function ble/variable#load-user-state/variable:LC_ALL/.impl {
local __ble_save=_ble_bash_$1
__ble_var_set=${!__ble_save+set}
__ble_var_val=${!__ble_save-}
[[ $__ble_var_set ]] && ble/variable#get-attr -v __ble_var_att "$1"
return 0
}
function ble/variable#load-user-state/variable:LC_COLLATE {
ble/variable#load-user-state/variable:LC_ALL/.impl LC_COLLATE
}
function ble/variable#load-user-state/variable:LC_ALL {
ble/variable#load-user-state/variable:LC_ALL/.impl LC_ALL
}
function ble/variable#load-user-state/variable:LC_CTYPE {
[[ $_ble_bash_LC_ALL ]] && ble/variable#load-user-state/variable:LC_ALL/.impl LC_CTYPE
}
function ble/variable#load-user-state/variable:LC_MESSAGES {
[[ $_ble_bash_LC_ALL ]] && ble/variable#load-user-state/variable:LC_ALL/.impl LC_MESSAGES
}
function ble/variable#load-user-state/variable:LC_NUMERIC {
[[ $_ble_bash_LC_ALL ]] && ble/variable#load-user-state/variable:LC_ALL/.impl LC_NUMERIC
}
function ble/variable#load-user-state/variable:LC_TIME {
[[ $_ble_bash_LC_ALL ]] && ble/variable#load-user-state/variable:LC_ALL/.impl LC_TIME
}
function ble/variable#load-user-state/variable:LANG {
[[ $_ble_bash_LC_ALL ]] && ble/variable#load-user-state/variable:LC_ALL/.impl LANG
}

{ ble/base/adjust-bash-options; } &>/dev/null # set -x 対策 #D0930

builtin bind &>/dev/null # force to load .inputrc
Expand Down Expand Up @@ -740,6 +797,13 @@ function ble/init/restore-IFS {
builtin unset -v _ble_init_original_IFS
}

function ble/variable#load-user-state/variable:IFS {
__ble_var_set=${_ble_init_original_IFS_set-}
__ble_var_val=${_ble_init_original_IFS-}
ble/variable#get-attr -v __ble_var_att IFS
return 0
}

if ((_ble_bash>=50100)); then
_ble_bash_BASH_REMATCH_level=0
_ble_bash_BASH_REMATCH=()
Expand Down Expand Up @@ -849,6 +913,17 @@ function ble/init/restore-IFS {
}
fi

function ble/variable#load-user-state/variable:BASH_REMATCH {
if ((_ble_bash_BASH_REMATCH_level)); then
__ble_var_set=${BASH_REMATCH+set}
__ble_var_val=("${_ble_bash_BASH_REMATCH[@]}")
ble/variable#get-attr -v __ble_var_att BASH_REMATCH
return 0
else
return 1
fi
}

ble/init/adjust-IFS
ble/base/adjust-BASH_REMATCH

Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
- canvas: use `_ble_term_invis` to hide characters used to determine char-width modes (requested by tessus) `#D2223` 8bb302e0
- canvas: hide cursor during char-width detection (requested by tessus) `#D2232` 0ff29b26
- exec: refine the elapsed time resolution `#D2249` 67548656 713c4215
- highlight (`ble/syntax/highlight/vartype`): reference the saved states of variables `#D2268` xxxxxxxx

## Fixes

Expand Down
65 changes: 47 additions & 18 deletions lib/core-syntax.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,42 @@ function ble/syntax:bash/initialize-vars {
#------------------------------------------------------------------------------
# 共通の字句の一致判定

## @fn ble/variable#load-user-state
## @var[out] __ble_var_set __ble_var_val __ble_var_att
function ble/variable#load-user-state {
__ble_var_set= __ble_var_val= __ble_var_att=
[[ $1 == __ble_* || $1 == _ble_local_* ]] && return 0
ble/function#try ble/variable#load-user-state/variable:"$1" && return 0

# restore positional parameters if necesssary
if ble/string#match "$1" '^[1-9][0-9]*$'; then
local __ble_name=$1
if [[ ${_ble_edit_exec_lastparams[0]+set} ]]; then
set -- "${_ble_edit_exec_lastparams[@]}"
else
set --
fi
__ble_var_set=${!__ble_name+set}
__ble_var_val=${!__ble_name-}
__ble_var_att=
return 0
fi

# XXX---We may possibly extract the global state, but this requires at least
# one fork to check if the currently visible variable is global or not, and
# even another fork and complicated processing if global. To avoid the
# overhead, we currently do not process the hidden global variables. When we
# support this, we should also consider it in the custom loader
# ble/variable#load-user-state/variable:"$1".
# if ble/variable#is-global "$1"; then
# ...
# fi

__ble_var_set=${!1+set}
__ble_var_val=${!1-}
ble/variable#get-attr -v __ble_var_att "$1"
}

## @fn ble/syntax/highlight/vartype/.impl name [opts [tail]]
## @arr[out] __ble_vartype_ret=(ret [lookahead])
## 属性値 ret と先読み文字数 lookahead を返します。
Expand All @@ -1956,34 +1992,27 @@ function ble/syntax/highlight/vartype/.impl {

local __ble_name=$1 __ble_opts=$2 __ble_tail=$3

# restore positional parameters if necesssary
if ble/string#match "$__ble_name" '^[1-9][0-9]*$'; then
if [[ ${_ble_edit_exec_lastparams[0]+set} ]]; then
set -- "${_ble_edit_exec_lastparams[@]}"
else
set --
fi
fi
local __ble_var_set __ble_var_val __ble_var_att
ble/variable#load-user-state "$__ble_name"

local __ble_attr; ble/variable#get-attr -v __ble_attr "$__ble_name"
if [[ ${!__ble_name+set} || $__ble_attr == *[aA]* ]]; then
if [[ $__ble_var_set || $__ble_var_att == *[aA]* ]]; then
local __ble_rex='^-?[0-9]+(#[_a-zA-Z0-9@]*)?$'
if [[ ${!__ble_name-} && :$__ble_opts: == *:expr:* && ! ( ${!__ble_name} =~ $__ble_rex ) ]]; then
if [[ $__ble_var_val && :$__ble_opts: == *:expr:* && ! ( $__ble_var_val =~ $__ble_rex ) ]]; then
__ble_vartype_ret=$ATTR_VAR_EXPR
elif [[ ${!__ble_name+set} && $__ble_attr == *x* ]]; then
elif [[ $__ble_var_set && $__ble_var_att == *x* ]]; then
# Note: 配列の場合には第0要素が設定されている時のみ。
__ble_vartype_ret=$ATTR_VAR_EXPORT
elif [[ $__ble_attr == *a* ]]; then
elif [[ $__ble_var_att == *a* ]]; then
__ble_vartype_ret=$ATTR_VAR_ARRAY
elif [[ $__ble_attr == *A* ]]; then
elif [[ $__ble_var_att == *A* ]]; then
__ble_vartype_ret=$ATTR_VAR_HASH
elif [[ $__ble_attr == *r* && :$__ble_opts: != *:no-readonly:* ]]; then
elif [[ $__ble_var_att == *r* && :$__ble_opts: != *:no-readonly:* ]]; then
__ble_vartype_ret=$ATTR_VAR_READONLY
elif [[ $__ble_attr == *i* ]]; then
elif [[ $__ble_var_att == *i* ]]; then
__ble_vartype_ret=$ATTR_VAR_NUMBER
elif [[ $__ble_attr == *[luc]* ]]; then
elif [[ $__ble_var_att == *[luc]* ]]; then
__ble_vartype_ret=$ATTR_VAR_TRANSFORM
elif [[ ! ${!__ble_name} ]]; then
elif [[ ! $__ble_var_val ]]; then
__ble_vartype_ret=$ATTR_VAR_EMPTY
else
__ble_vartype_ret=$ATTR_VAR
Expand Down
28 changes: 28 additions & 0 deletions note.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7373,6 +7373,34 @@ bash_tips

2024-08-25

* syntax: FUNCNEST 等待避している変数の存在が構文着床kから見えない [#D2268]
Ref: #D2267

#D2267 のテスト中に気になった事。

print-global-definitions から見えないという事か。何故? と思ったが builtin
unset しているのだから当然の事である。print-global-definitions に於いて待
避している変数の復元も実行する必要があるだろうか?

既に何らかの変数について対策しているのではないか? と思ったが分からない。
確認する。うーん。別に復元はしていない気がする。というか抑々 FUNCNEST だ
とか POSIXLY_CORRECT だとか危険な変数を復元したら変な事が起こるので不用意
に復元できない。

また、確認してみると変数の構文着色では別に print-global-definitions で変
数の値を復元している訳ではない。

* _ble_local_predicate_cmd が leak している。修正した。これは 0.3 にも存在
する leak の様なので別 commit にする。と思ったがこの対応自体を独立した項
目にする事にしたのでやはり別 commit にしなくても良い。

新しく ble/variable#load-user-state という関数を用意して、更に待避を行っ
ている変数について個別に特殊化関数で対応する事にした。動いている。

うーん。これは varname_unset の色変更 commit の方で対応するべきでは? と思っ
たが varname_unset の commit は一連の色変更の commit の追加修正として取り扱
いたい。この変更は独立している気がするので項目は分ける事にする。

* main: bash-5.3 -o posix で --atatch=prompt に失敗する [#D2267]

prompt-attach に失敗する。ble-attach を明示的に呼び出した時は問題ない。set
Expand Down
2 changes: 1 addition & 1 deletion src/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ function ble/array#filter/.eval {
builtin eval -- "$_ble_local_predicate_cmd"
}
function ble/array#filter {
local _ble_local_predicate=$2
local _ble_local_predicate=$2 _ble_local_predicate_cmd=
if [[ $2 == *'$'* ]] || ! ble/is-function "$2"; then
_ble_local_predicate=ble/array#filter/.eval
_ble_local_predicate_cmd=$2
Expand Down

0 comments on commit 063249b

Please sign in to comment.