From 92f200631edd1958d5c6c6831e70ac23d14ad509 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Tue, 31 Jan 2023 14:42:34 +0900 Subject: [PATCH] main: support an option "--inputrc={diff,all,user,none}" --- ble.pp | 58 +++++++++++++++++++++++++++++++++++++---------- docs/ChangeLog.md | 3 +++ note.txt | 32 ++++++++++++++++++++++++++ src/decode.sh | 53 +++++++++++++++++++++++++++++++++---------- 4 files changed, 122 insertions(+), 24 deletions(-) diff --git a/ble.pp b/ble.pp index 28023730..c5f8876c 100644 --- a/ble.pp +++ b/ble.pp @@ -98,19 +98,35 @@ function ble/debug/measure-set-timeformat { '' \ ' --rcfile=BLERC' \ ' --init-file=BLERC' \ - ' Specify the ble init file. The default is ~/.blerc.' \ + ' Specify the ble init file. The default is ~/.blerc if any, or' \ + ' ~/.config/blesh/init.sh.' \ '' \ ' --norc' \ ' Do not load the ble init file.' \ '' \ ' --attach=ATTACH' \ ' --noattach' \ - ' The option "--attach" selects the strategy of "ble-attach" from the' \ - ' list: ATTACH = "attach" | "prompt" | "none". The default strategy is' \ - ' "prompt". The option "--noattach" is a synonym for "--attach=none".' \ + ' The option "--attach" selects the strategy of "ble-attach" from the list:' \ + ' ATTACH = "attach" | "prompt" | "none". The default strategy is "prompt".' \ + ' When "attach" is specified, ble.sh immediately attaches to the session in' \ + ' "source ble.sh". When "prompt" is specified, ble.sh attaches to the' \ + ' session before the first prompt using PROMPT_COMMAND. When "none" is' \ + ' specified, ble.sh does not attach to the session automatically, so' \ + ' ble-attach needs to be called explicitly. The option "--noattach" is a' \ + ' synonym for "--attach=none".' \ '' \ + ' --inputrc=TYPE' \ ' --noinputrc' \ - ' Do not read inputrc settings for ble.sh' \ + ' The option "--inputrc" selects the strategy of reconstructing user' \ + ' keybindings from the list: "auto" (default), "diff", "all", "user", "none".' \ + ' When "diff" is specified, user keybindings are extracted by the diff of the' \ + ' outputs of the "bind" builtin between the current session and the plain' \ + ' Bash. When "all" is specified, the user keybindings are extracted from' \ + ' /etc/inputrc and ${INPUTRC:-~/.inputrc*}. When "user" is specified, the' \ + ' user keybindings are extracted from ${INPUTRC:-~/.inputrc*}. When "none"' \ + ' is specified, the user keybindings are not reconstructed from the state of' \ + ' Readline, and only the bindings by "ble-bind" are applied. The option' \ + ' "--noinputrc" is a synonym for "--inputrc=none".' \ '' \ ' --keep-rlvars' \ ' Do not change readline settings for ble.sh' \ @@ -741,6 +757,7 @@ function ble/util/print-lines { builtin printf '%s\n' "$@"; } function ble/base/read-blesh-arguments { local opts= local opt_attach=prompt + local opt_inputrc=auto _ble_init_command= # 再解析 while (($#)); do @@ -757,8 +774,19 @@ function ble/base/read-blesh-arguments { opts=$opts:E ble/util/print "ble.sh ($arg): an option argument is missing." >&2 fi ;; + (--noinputrc) - opts=$opts:noinputrc ;; + opt_inputrc=none ;; + (--inputrc=*) opt_inputrc=${arg#*=} ;; + (--inputrc) + if (($#)); then + opt_inputrc=$1; shift + else + opt_inputrc=inputrc + opts=$opts:E + ble/util/print "ble.sh ($arg): an option argument is missing." >&2 + fi ;; + (--rcfile=*|--init-file=*|--rcfile|--init-file) if [[ $arg != *=* ]]; then local rcfile=$1; shift @@ -827,6 +855,7 @@ function ble/base/read-blesh-arguments { _ble_base_arguments_opts=$opts _ble_base_arguments_attach=$opt_attach + _ble_base_arguments_inputrc=$opt_inputrc [[ :$opts: != *:E:* ]] } if ! ble/base/read-blesh-arguments "$@"; then @@ -981,6 +1010,10 @@ function ble/init/check-environment { fi _ble_base_env_HOSTNAME=$HOSTNAME + if [[ ! $LANG ]]; then + ble/util/print "ble.sh: suspicious environment: \$LANG is empty." >&2 + fi + # 暫定的な ble/bin/$cmd 設定 ble/bin/.default-utility-path "${_ble_init_posix_command_list[@]}" @@ -1573,8 +1606,10 @@ function ble/base/print-usage-for-no-argument-command { function ble-reload { local -a options=() ble/array#push options --rcfile="${_ble_base_rcfile:-/dev/null}" + [[ $_ble_base_arguments_inputrc == auto ]] || + ble/array#push options --inputrc="$_ble_base_arguments_inputrc" local name - for name in keep-rlvars noinputrc; do + for name in keep-rlvars; do if [[ :$_ble_base_arguments_opts: == *:"$name":* ]]; then ble/array#push options "--$name" fi @@ -2238,11 +2273,7 @@ function ble/base/attach-from-PROMPT_COMMAND { function ble/base/process-blesh-arguments { local opts=$_ble_base_arguments_opts local attach=$_ble_base_arguments_attach - - if [[ :$opts: == *:noinputrc:* ]]; then - _ble_builtin_bind_user_settings_loaded=noinputrc - _ble_builtin_bind_inputrc_done=noinputrc - fi + local inputrc=$_ble_base_arguments_inputrc _ble_base_rcfile=$_ble_base_arguments_rcfile #%if measure_load_time @@ -2261,6 +2292,9 @@ function ble/base/process-blesh-arguments { (none) ;; (*) ble/util/print "ble.sh: unrecognized attach method --attach='$attach'." ;; esac + + # reconstruction type of user-bindings + _ble_decode_initialize_inputrc=$inputrc } function ble/base/sub:test { diff --git a/docs/ChangeLog.md b/docs/ChangeLog.md index 4b003c39..bfbc7d4a 100644 --- a/docs/ChangeLog.md +++ b/docs/ChangeLog.md @@ -118,6 +118,7 @@ - complete: support ambiguous completion for command paths `#D1922` 8a716ad - complete: preserve original path segments as long as possible `#D1923` `#D1924` e3cdb9d - main: support `BLE_SESSION_ID` and `BLE_COMMAND_ID` `#D1925` xxxxxxx +- main: support an option `--inputrc={diff,all,user,none}` `#D1926` xxxxxxx ## Changes @@ -449,6 +450,8 @@ - util (`modifyOtherKeys`): work around delayed terminal identification `#D1842` 14f3c81 - util (`modifyOtherKeys`): fix a bug that kitty protocol is never activated `#D1842` 14f3c81 - util (`modifyOtherKeys`): pass-through kitty protocol sequences (motivated by ferdinandyb) `#D1845` f66e0c1 +- main: show warning for empty locale (movivated by Ultra980) `#D1927` xxxxxxx +- main: never load `/etc/inputrc` in openSUSE (motivated by Ultra980) `#D1926` xxxxxxx ## Test diff --git a/note.txt b/note.txt index fa747cec..584da535 100644 --- a/note.txt +++ b/note.txt @@ -6846,6 +6846,38 @@ bash_tips Done (実装ログ) ------------------------------------------------------------------------------- +2023-01-30 + + * main: 空の LANG に対して警告を表示する (movivated by Ultra980) [#D1927] + https://github.com/akinomyoga/ble.sh/issues/269 + + 文字幅計算がずれる。空の LANG なのに starship&terminal が UTF-8 を想定して + いる。 + + * main: openSUSE inputrc.keys は強制的に排除する? (motivated by Ultra980) [#D1926] + https://github.com/akinomyoga/ble.sh/issues/268 + + これが何度も問題を起こしている。 + + a 最早強制的に off にしても良いのではないか。 + + b 或いは自前で ~/.inputrc だけ読み取る事にする。 + + 元々 --noinputrc は最早 inputrc とは関係なく bind の出力を plain Bash と + diff を取ってユーザー設定を復元している。という事を考えたらどの方式でユー + ザー設定を復元するかをオプションで選べる様にしても良いのではないか。 + + --inputrc={none,user,all,diff} + + c 或いは、デフォルトとの bind diff を取った後で比較するのではなくて、仮に + ble-bind で全部登録した後の状態で diff を取って残った物だけ採用する? と思っ + たがこれだと問題は解決しない。 + + と思ったら既に opensuse では振る舞いを変える様になっていた。今までは + https://github.com/openSUSE/aaa_base/pull/84 の修正が入っているかどうかで問 + 題があるか判定していた気がするが、もう inputrc.keys が存在する時点で問題が + あると判定する事にする。 + 2023-01-26 * 2021-05-17 histdb: 履歴が余りに消滅するので ble.sh の側で独立した履歴の仕組みを作りたい [#D1925] diff --git a/src/decode.sh b/src/decode.sh index 9b5dd505..0b400ed6 100644 --- a/src/decode.sh +++ b/src/decode.sh @@ -3763,6 +3763,11 @@ _ble_builtin_bind_inputrc_done= function ble/builtin/bind/initialize-inputrc { [[ $_ble_builtin_bind_inputrc_done ]] && return 0 _ble_builtin_bind_inputrc_done=1 + + if [[ $1 == all ]]; then + local sys_inputrc=/etc/inputrc + [[ -e $sys_inputrc ]] && ble/decode/read-inputrc "$sys_inputrc" + fi local inputrc=${INPUTRC:-$HOME/.inputrc} [[ -e $inputrc ]] && ble/decode/read-inputrc "$inputrc" } @@ -3987,27 +3992,51 @@ function bind { ble/builtin/bind "$@"; } # ble/decode/initialize, attach, detach @decode.attach function ble/decode/initialize/.has-broken-suse-inputrc { - ((_ble_bash<50000)) || return 1 # Bash 5.0+ are not suffered - [[ -s /etc/inputrc.keys ]] || return 1 - local content - ble/util/readfile content /etc/inputrc.keys - [[ $content == *'"\M-[2~":'* ]] + # 1. Check if this is openSUSE and has /etc/input.keys. + local content= + [[ -s /etc/inputrc.keys && -r /etc/os-release ]] && + ble/util/readfile content /etc/os-release && + [[ $content == *'openSUSE'* ]] || return 1 + + # Note #1926: Even after the fix + # https://github.com/openSUSE/aaa_base/pull/84, "inputrc.keys" causes + # problems through extra bindings of the [home]/[end] escape sequences to + # [prior], [Enter] to "accept-line", etc. Thus, we comment out the following + # part of codes and always return 0 when there is "/etc/inputrc.keys". + + # 2. Check if the file "inputrc.keys" has the bug. + # ((_ble_bash<50000)) || return 1 # Bash 5.0+ are not suffered + # ble/util/readfile content /etc/inputrc.keys && + # [[ $content == *'"\M-[2~":'* ]] || return 1 + + return 0 } _ble_decode_initialized= +_ble_decode_initialize_inputrc=auto function ble/decode/initialize { [[ $_ble_decode_initialized ]] && return 0 _ble_decode_initialized=1 ble/decode/cmap/initialize - if ble/decode/initialize/.has-broken-suse-inputrc; then - # Note: #D1662 WA openSUSE (aaa_base < 202102) has broken /etc/inputrc - [[ ${INPUTRC-} == /etc/inputrc || ${INPUTRC-} == /etc/inputrc.keys ]] && - local INPUTRC=~/.inputrc - ble/builtin/bind/initialize-inputrc - else - ble/builtin/bind/read-user-settings + if [[ $_ble_decode_initialize_inputrc == auto ]]; then + if ble/decode/initialize/.has-broken-suse-inputrc; then + # Note: #D1662 WA openSUSE (aaa_base < 202102) has broken /etc/inputrc + [[ ${INPUTRC-} == /etc/inputrc || ${INPUTRC-} == /etc/inputrc.keys ]] && + local INPUTRC=~/.inputrc + _ble_decode_initialize_inputrc=all + else + _ble_decode_initialize_inputrc=diff + fi fi + case $_ble_decode_initialize_inputrc in + (all) + ble/builtin/bind/initialize-inputrc all ;; + (user) + ble/builtin/bind/initialize-inputrc ;; + (diff) + ble/builtin/bind/read-user-settings ;; + esac } function ble/decode/reset-default-keymap {