Skip to content

Commit

Permalink
main: support "ble", "blehook ATTACH DETACH", "BLE_ONLOAD", and "BLE_…
Browse files Browse the repository at this point in the history
…ATTACHED"
  • Loading branch information
akinomyoga committed May 11, 2021
1 parent 8b0257e commit 750ca38
Show file tree
Hide file tree
Showing 10 changed files with 471 additions and 164 deletions.
6 changes: 4 additions & 2 deletions .github/ISSUE_TEMPLATE/00_T1-bug_report.md
Expand Up @@ -4,8 +4,10 @@ about: Create a report to fix problems

---

**ble version**: <!-- The result of `echo $BLE_VERSION` (version, commit-hash) -->
**Bash version**: <!-- The result of `echo $BASH_VERSION, $MACHTYPE` -->
**ble version**: <!-- The result of `echo "$BLE_VERSION"` or version and commit-hash -->
**Bash version**: <!-- The result of `echo "$BASH_VERSION ($MACHTYPE)"` -->
<!-- You can also obtain the above information by pressing [Ctrl-x][Ctrl-v] in
the ble.sh session -->

<!-- Describe the problem here. If example terminal contents (in either the
text form or an image) are available, you may paste/insert them to explain
Expand Down
11 changes: 8 additions & 3 deletions .github/ISSUE_TEMPLATE/01_T1-bug_report.md
Expand Up @@ -4,7 +4,12 @@ about: 問題報告ありがとうございます! どんなに些細なこと

---

**ble version**: <!-- `ble.sh` をロードした上で `echo $BLE_VERSION` で確認できる値 -->
**Bash version**: <!-- 問題が再現する Bash のバージョン (例: 4.4.12) -->
**ble version**: <!-- ble.sh をロードした上で `echo $BLE_VERSION` で確認できる値。または ble のバージョン及びコミットハッシュ -->
**Bash version**: <!-- `echo "$BASH_VERSION ($MACHTYPE)"` で確認できる値。または Bash のバージョン及び環境 -->
<!-- 以上の情報は ble.sh をロードした状態で [Ctrl-x][Ctrl-v] を押す事によっても
取得できます。-->

<!--- 問題についてここに自由に記述して下さって差し支えありません! 不明な点があればまたこちらからお尋ねしますのでご安心下さいませ。もし問題の発生している端末の画面の様子があれば、内容をコピー&ペーストして頂けると分かりやすいかもしれないので御一考頂ければ幸いです。 -->
<!--- 問題についてここに自由に記述して下さって差し支えありません! 不明な点があ
ればまたこちらからお尋ねしますのでご安心下さいませ。もし問題の発生している端
末の画面の様子があれば、内容をコピー&ペーストして頂けると分かりやすいかもしれ
ないので御一考頂ければ幸いです。 -->
76 changes: 73 additions & 3 deletions ble.pp
Expand Up @@ -857,6 +857,7 @@ function ble-update {

#------------------------------------------------------------------------------
_ble_attached=
BLE_ATTACHED=

#%x inc.r|@|src/def|
#%x inc.r|@|src/util|
Expand All @@ -882,6 +883,69 @@ function ble-update {
bleopt/check-all
#------------------------------------------------------------------------------

## @fn ble [SUBCOMMAND]
##
## 無引数で呼び出した時、現在 ble.sh の内部空間に居るかどうかを判定します。
##
# Bluetooth Low Energy のツールが存在するかもしれない
ble/bin/.freeze-utility-path ble
function ble/dispatch/.help {
ble/util/print-lines \
'usage: ble [SUBCOMMAND [ARGS...]]' \
'' \
'SUBCOMMAND' \
' # Manage ble.sh' \
' attach ... alias of ble-attach' \
' detach ... alias of ble-detach' \
' update ... alias of ble-update' \
' reload ... alias of ble-reload' \
' help ... Show this help' \
' version ... Show version' \
' check ... Run unit tests' \
'' \
' # Configuration' \
' opt ... alias of bleopt' \
' bind ... alias of ble-bind' \
' face ... alias of ble-color-setface' \
' hook ... alias of blehook' \
' sabbrev ... alias of ble-sabbrev' \
' palette ... alias of ble-color-show' \
''
}
function ble/dispatch {
if (($#==0)); then
[[ $_ble_attach && ! $_ble_edit_exec_inside_userspace ]]
return $?
fi

# import autoload measure assert stackdump color-show decode-{byte,char,key}
local cmd=$1; shift
case $cmd in
(attach) ble-attach "$@" ;;
(detach) ble-detach "$@" ;;
(update) ble-update "$@" ;;
(reload) ble-reload "$@" ;;
(face) ble-color-setface "$@" ;;
(bind) ble-bind "$@" ;;
(opt) bleopt "$@" ;;
(hook) blehook "$@" ;;
(sabbrev) ble-sabbrev "$@" ;;
(palette) ble-color-show "$@" ;;
(help|--help) ble/dispatch/.help "$@" ;;
(version|--version) ble/util/print "ble.sh, version $BLE_VERSION (noarch)" ;;
(check|--test) ble/base/test "$@" ;;
(*)
if ble/is-function ble/bin/ble; then
ble/bin/ble "$cmd" "$@"
else
ble/util/print "ble (ble.sh): unrecognized subcommand '$cmd'." >&2
return 2
fi
esac
}
function ble { ble/dispatch "$@"; }


# blerc
_ble_base_rcfile=
_ble_base_rcfile_initialized=
Expand All @@ -901,7 +965,6 @@ function ble/base/load-rcfile {
fi
}

_ble_attached=
function ble-attach {
if (($#)); then
ble/base/print-usage-for-no-argument-command 'Attach to ble.sh.' "$@"
Expand All @@ -918,6 +981,7 @@ function ble-attach {

[[ $_ble_attached ]] && return 0
_ble_attached=1
BLE_ATTACHED=1

# 特殊シェル設定を待避
builtin eval -- "$_ble_bash_FUNCNEST_adjust"
Expand All @@ -942,12 +1006,15 @@ function ble-attach {
ble/decode/reset-default-keymap # 264ms (keymap/vi.sh)
if ! ble/decode/attach; then # 53ms
_ble_attached=
BLE_ATTACHED=
ble-edit/detach
return 1
fi

ble/history:bash/reset # 27s for bash-3.0

blehook/invoke ATTACH

# Note: 再描画 (初期化中のエラーメッセージ・プロンプト変更等の為)
ble/textarea#redraw

Expand All @@ -971,6 +1038,8 @@ function ble-detach {
function ble-detach/impl {
[[ $_ble_attached ]] || return 1
_ble_attached=
BLE_ATTACHED=
blehook/invoke DETACH

ble-edit/detach
ble/decode/detach
Expand Down Expand Up @@ -1104,8 +1173,6 @@ function ble/base/process-blesh-arguments {
esac
done

ble/base/load-rcfile # blerc

# rlvar (#D1148)
# 勝手だが ble.sh の参照する readline 変数を
# 便利だと思われる設定の方向に書き換えてしまう。
Expand All @@ -1117,6 +1184,9 @@ function ble/base/process-blesh-arguments {
((_ble_bash>=40400)) && builtin bind 'set colored-completion-prefix on'
fi

ble/base/load-rcfile # blerc
ble/util/invoke-hook BLE_ONLOAD

# attach
case $opt_attach in
(attach) ble-attach ;;
Expand Down
147 changes: 98 additions & 49 deletions lib/core-complete.sh
Expand Up @@ -1934,7 +1934,7 @@ function ble/complete/source:tilde {
}

function ble/complete/source:fd {
IFS=: eval 'local fdlist=":${_ble_util_openat_fdlist[*]}:"'
IFS=: builtin eval 'local fdlist=":${_ble_util_openat_fdlist[*]}:"'

[[ $comp_filter_type == none ]] &&
local comp_filter_type=head
Expand Down Expand Up @@ -2520,7 +2520,7 @@ function ble/complete/progcomp/.compgen {
printf -v "$2" %s "${1:1}"
else
printf -v "$2" %q "$1"
[[ ${!2} == \$* ]] && eval $2=${!2}
[[ ${!2} == \$* ]] && builtin eval "$2=${!2}"
fi
}
ble/function#suppress-stderr _filedir
Expand Down Expand Up @@ -5926,25 +5926,37 @@ function ble-decode/keymap:auto_complete/define {
# sabbrev
#

function ble/complete/sabbrev/.initialize-print {
sgr0= sgr1= sgr2= sgr3= sgro=
if [[ $flags == *c* || $flags != *n* && -t 1 ]]; then
local ret
ble/color/face2sgr command_function; sgr1=$ret
ble/color/face2sgr syntax_varname; sgr2=$ret
ble/color/face2sgr syntax_quoted; sgr3=$ret
ble/color/face2sgr argument_option; sgro=$ret
sgr0=$_ble_term_sgr0
fi
}
function ble/complete/sabbrev/.print-definition {
local key=$1 type=${2%%:*} value=${2#*:}
local flags=
[[ $type == m ]] && flags='-m '
local option=
[[ $type == m ]] && option=$sgro'-m'$sgr0' '

local q=\' Q="'\''" shell_specialchars=$' \n\t&|;<>()''\$`"'\''[]*?!~'
if [[ $key == *["$shell_specialchars"]* ]]; then
printf "ble-sabbrev %s'%s=%s'\n" "$flags" "${key//$q/$Q}" "${value//$q/$Q}"
else
printf "ble-sabbrev %s%s='%s'\n" "$flags" "$key" "${value//$q/$Q}"
fi
local ret
ble/string#quote-word "$key" quote-empty:sgrq="$sgr3":sgr0="$sgr2"
key=$sgr2$ret$sgr0
ble/string#quote-word "$value" sgrq="$sgr3":sgr0="$sgr0"
value=$ret
ble/util/print "${sgr1}ble-sabbrev$sgr0 $option$key=$value"
}

## @fn ble/complete/sabbrev/register key value
## 静的略語展開を登録します。
## @param[in] key value
##
## @fn ble/complete/sabbrev/list
## @fn ble/complete/sabbrev/list [keys...]
## 登録されている静的略語展開の一覧を表示します。
## @var[in] flags
##
## @fn ble/complete/sabbrev/get key
## 静的略語展開の展開値を取得します。
Expand All @@ -5957,11 +5969,23 @@ if ((_ble_bash>=40300||_ble_bash>=40000&&!_ble_bash_loaded_in_function)); then
_ble_complete_sabbrev[$key]=$value
}
function ble/complete/sabbrev/list {
local key
for key in "${!_ble_complete_sabbrev[@]}"; do
local value=${_ble_complete_sabbrev[$key]}
ble/complete/sabbrev/.print-definition "$key" "$value"
(($#)) || set -- "${!_ble_complete_sabbrev[@]}"
(($#)) || return 0

local sgr0 sgr1 sgr2 sgr3 sgro
ble/complete/sabbrev/.initialize-print

local key ext=0
for key; do
if [[ ${_ble_complete_sabbrev[$key]+set} ]]; then
local value=${_ble_complete_sabbrev[$key]}
ble/complete/sabbrev/.print-definition "$key" "$value"
else
ble/util/print "ble-sabbrev: $key: not found." >&2
ext=1
fi
done
return "$ext"
}
function ble/complete/sabbrev/get {
local key=$1
Expand All @@ -5986,13 +6010,30 @@ else
_ble_complete_sabbrev_values[i]=$value
}
function ble/complete/sabbrev/list {
local shell_specialchars=$' \n\t&|;<>()''\$`"'\''[]*?!~'
local i N=${#_ble_complete_sabbrev_keys[@]} q=\' Q="'\''"
for ((i=0;i<N;i++)); do
local key=${_ble_complete_sabbrev_keys[i]}
local value=${_ble_complete_sabbrev_values[i]}
ble/complete/sabbrev/.print-definition "$key" "$value"
done
local sgr0 sgr1 sgr2 sgr3 sgro
ble/complete/sabbrev/.initialize-print

if (($#)); then
local key ret ext=0
for key; do
if ble/array#index _ble_complete_sabbrev_keys "$key"; then
local value=${_ble_complete_sabbrev_values[ret]}
ble/complete/sabbrev/.print-definition "$key" "$value"
else
ble/util/print "ble-sabbrev: $key: not found." >&2
ext=1
fi
done
return "$ext"
else
local i N=${#_ble_complete_sabbrev_keys[@]}
for ((i=0;i<N;i++)); do
local key=${_ble_complete_sabbrev_keys[i]}
local value=${_ble_complete_sabbrev_values[i]}
ble/complete/sabbrev/.print-definition "$key" "$value"
done
return 0
fi
}
function ble/complete/sabbrev/get {
ret=
Expand All @@ -6019,7 +6060,14 @@ function ble/complete/sabbrev/read-arguments {
ble/array#push specs "s:$arg"
else
case $arg in
(--help) flag_help=1 ;;
(--help)
flags=H$flags ;;
(--color|--color=always)
flags=c${flags//[cn]} ;;
(--color=never)
flags=n${flags//[cn]} ;;
(--color=auto)
flags=${flags//[cn]} ;;
(-*)
local i n=${#arg} c
for ((i=1;i<n;i++)); do
Expand All @@ -6028,21 +6076,20 @@ function ble/complete/sabbrev/read-arguments {
(m)
if ((!$#)); then
ble/util/print "ble-sabbrev: option argument for '-$c' is missing" >&2
flag_error=1
flags=E$flags
elif [[ $1 != ?*=* ]]; then
ble/util/print "ble-sabbrev: invalid option argument '-$c $1' (expected form: '-c key=value')" >&2
flag_error=1
ble/util/print "ble-sabbrev: invalid option argument '-$c $1' (expected form: '-$c key=value')" >&2
flags=E$flags
else
ble/array#push specs "$c:$1"; shift
fi ;;
(*)
ble/util/print "ble-sabbrev: unknown option '-$c'." >&2
flag_error=1 ;;
flags=E$flags ;;
esac
done ;;
(*)
ble/util/print "ble-sabbrev: unrecognized argument '$arg'." >&2
flag_error=1 ;;
ble/array#push print "$arg" ;;
esac
fi
done
Expand All @@ -6051,28 +6098,30 @@ function ble/complete/sabbrev/read-arguments {
## @fn ble-sabbrev key=value
## 静的略語展開を登録します。
function ble-sabbrev {
if (($#)); then
local -a specs=()
local flag_help= flag_error=
ble/complete/sabbrev/read-arguments "$@"
if [[ $flag_help || $flag_error ]]; then
[[ $flag_error ]] && ble/util/print
ble/util/print-lines \
'usage: ble-sabbrev [key=value|-m key=function|--help]' \
' Register sabbrev expansion.'
[[ ! $flag_error ]]; return "$?"
fi
local -a specs=() print=()
local flags=
ble/complete/sabbrev/read-arguments "$@"
if [[ $flags == *H* || $flags == *E* ]]; then
[[ $flags == *E* ]] && ble/util/print
ble/util/print-lines \
'usage: ble-sabbrev [key=value|-m key=function|--help]' \
' Register sabbrev expansion.'
[[ ! $flags == *E* ]]; return "$?"
fi

local spec key type value
for spec in "${specs[@]}"; do
# spec は t:key=value の形式
type=${spec::1} spec=${spec:2}
key=${spec%%=*} value=${spec#*=}
ble/complete/sabbrev/register "$key" "$type:$value"
done
else
ble/complete/sabbrev/list
local ext=0
if ((${#specs[@]}==0||${#print[@]})); then
ble/complete/sabbrev/list "${print[@]}" || ext=$?
fi

local spec key type value
for spec in "${specs[@]}"; do
# spec は t:key=value の形式
type=${spec::1} spec=${spec:2}
key=${spec%%=*} value=${spec#*=}
ble/complete/sabbrev/register "$key" "$type:$value"
done
return "$ext"
}

function ble/complete/sabbrev/expand {
Expand Down

0 comments on commit 750ca38

Please sign in to comment.