Skip to content

Commit 430a174

Browse files
committed
main: delay attaching in kitty, Ghostty, and VS Code Terminal
* contrib/integration/zoxide: fix the problem of unquoted filenames
1 parent 154386d commit 430a174

File tree

4 files changed

+119
-20
lines changed

4 files changed

+119
-20
lines changed

ble.pp

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,20 @@ function ble/util/readlink {
16701670

16711671
#---------------------------------------
16721672

1673+
function ble/init/adjust-environment {
1674+
builtin unset -f "$FUNCNAME"
1675+
1676+
if [[ ${IN_NIX_SHELL-} ]]; then
1677+
# Since "nix-shell" overwrites BASH to the path to a binary image different
1678+
# from the current one, the Bash process crashes on attempting loading
1679+
# loadable builtins. We rewrite it to the correct one.
1680+
local ret=
1681+
ble/util/readlink "/proc/$$/exe" 2>/dev/null
1682+
[[ -x $ret ]] && BASH=$ret
1683+
fi
1684+
}
1685+
ble/init/adjust-environment
1686+
16731687
_ble_bash_path=
16741688
function ble/bin/.load-builtin {
16751689
local name=$1 path=$2
@@ -2624,6 +2638,47 @@ function ble/base/load-rcfile {
26242638
fi
26252639
}
26262640

2641+
# ble-attach needs to be performed at the very end of the Bash startup file.
2642+
# However, in some environment, the terminal or the session manager would start
2643+
# Bash with a custom startup file, and ~/.bashrc is sourced from the custom
2644+
# startup file. In this case, when the user puts "ble-attach" at the end of
2645+
# ~/.bashrc, other settings would continue to be executed even after the
2646+
# execution of "ble-attach".
2647+
function ble/base/attach/.needs-prompt-attach {
2648+
local ext=1
2649+
2650+
[[ $1 == *:force:* ]] && return 1
2651+
2652+
# nix-shell loads the Bash startup file from inside its custom file "rc".
2653+
if [[ ${IN_NIX_SHELL-} && "${BASH_SOURCE[*]}" == */rc ]]; then
2654+
# We force prompt-attach when ble-attach is run inside "nix-shell rc".
2655+
ext=0
2656+
fi
2657+
2658+
if [[ ${VSCODE_INJECTION-} ]]; then
2659+
# VS Code also tries to source ~/.bashrc from its
2660+
# "shellIntegration-bash.sh". VS Code shell integration seems to set the
2661+
# variable "VSCODE_INJECTION" while it sources the user's startup file, and
2662+
# it unsets the variable after the initialization.
2663+
ext=0
2664+
elif [[ ${kitty_bash_inject-} ]]; then
2665+
# When the startup file is sourced from kitty's shell ingteration
2666+
# "kitty.bash", the variable "kitty_bash_inject" is set. The variable is
2667+
# unset after the initialization. If we find it, we cancel the manual
2668+
# attaching and switch to the prompt attach.
2669+
ext=0
2670+
elif [[ ${ghostty_bash_inject-} ]]; then
2671+
# Ghostty seems to use a shell-integration code derived from kitty's. By
2672+
# the way, kitty is licensed under GPL-3.0, while Ghostty is licensed under
2673+
# the MIT license. Is it allowed to include a derivative of a part of
2674+
# kitty in the MIT-licensed Ghostty? It may be non-trivial whether the
2675+
# shell integration is an essential part of Ghostty.
2676+
ext=0
2677+
fi
2678+
2679+
return "$ext"
2680+
}
2681+
26272682
## @fn ble-attach [opts]
26282683
function ble-attach {
26292684
#%if leakvar
@@ -2670,31 +2725,24 @@ function ble-attach {
26702725
#%if leakvar
26712726
ble/debug/leakvar#check $"leakvar" A4-adjust
26722727
#%end.i
2673-
if [[ ${IN_NIX_SHELL-} ]]; then
2674-
# nix-shell rc の中から実行している時は強制的に prompt-attach にする
2675-
if [[ "${BASH_SOURCE[*]}" == */rc && $1 != *:force:* ]]; then
2676-
ble/base/install-prompt-attach
2677-
_ble_attached=
2678-
BLE_ATTACHED=
2679-
ble/base/restore-BASH_REMATCH
2680-
ble/base/restore-bash-options
2681-
ble/base/restore-builtin-wrappers
2682-
ble/base/restore-POSIXLY_CORRECT
2728+
2729+
if ble/base/attach/.needs-prompt-attach; then
2730+
ble/base/install-prompt-attach
2731+
_ble_attached=
2732+
BLE_ATTACHED=
2733+
ble/base/restore-BASH_REMATCH
2734+
ble/base/restore-bash-options
2735+
ble/base/restore-builtin-wrappers
2736+
ble/base/restore-POSIXLY_CORRECT
26832737
#%if leakvar
26842738
ble/debug/leakvar#check $"leakvar" A4b1
26852739
#%end.i
2686-
builtin eval -- "$_ble_bash_FUNCNEST_restore"
2687-
return 0
2688-
fi
2689-
2690-
# nix-shell は BASH を誤った値に書き換えるので上書きする。
2691-
local ret
2692-
ble/util/readlink "/proc/$$/exe"
2693-
[[ -x $ret ]] && BASH=$ret
2740+
builtin eval -- "$_ble_bash_FUNCNEST_restore"
2741+
return 0
2742+
fi
26942743
#%if leakvar
26952744
ble/debug/leakvar#check $"leakvar" A4b2
26962745
#%end.i
2697-
fi
26982746

26992747
# reconnect standard streams
27002748
ble/fd/save-external-standard-streams

contrib

docs/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
- canvas: update tables for Unicode 16.0.0 `#D2283` 5b43ca3f 25a10a6f
211211
- complete: work around `mawk <= 1.3.4-20230525` type-inference bug (reported by KaKi87) `#D2295` 546499b5
212212
- main: work around macOS sed (reported by Mossop) `#D2298` a16aa594
213+
- main: delay attaching in kitty, Ghostty, and VS Code Terminal `#D2215` xxxxxxxx
213214

214215
## Contrib
215216

@@ -242,6 +243,7 @@
242243
- integration/fzf-initialize: (reported by 3ximus) `#D2285` a36d13ce
243244
- config: add `github499-append-to-last-modified` (motivated by vaab) `#D2286` 32f290df
244245
- integration: add `skim` integration for completion (reported by cmm) `#D2287` a36d13ce
246+
- integration/zoxide: fix the problem of unquoted filenames (reported by tessus) `#D2216` xxxxxxxx
245247

246248
## Documentation
247249

note.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7728,8 +7728,57 @@ bash_tips
77287728
Done (実装ログ)
77297729
-------------------------------------------------------------------------------
77307730

7731+
2025-01-07
7732+
7733+
* integration/zoxide: compopt -o noquote は余分 (reported by tessus) [#D2316]
7734+
https://github.com/akinomyoga/ble.sh/issues/549
7735+
7736+
うーん。integration/zoxide.bash で明示的に compopt -o noquote を指定してい
7737+
るのが悪かった。元々何処からこの compopt -o noquote が来たのかと思ったが、
7738+
最初の commit から存在している様だ。そして、どうもこれは
7739+
fzf-completion.bash を zoxide 用に修正した時に、fzf 用の設定をそのまま持っ
7740+
てきたという事の様である。
7741+
7742+
そもそも fzf で compopt -o noquote を指定していたのは fzf によって履歴項目
7743+
が補完された時には余分の quote は不要だと判断したからであった。然し、zoxide
7744+
は単にディレクトリ名を指定するだけのコマンドなので compopt -o noquote を指
7745+
定する必要性もない様に思われる。
7746+
7747+
更に、fzf に関しては以下の報告によって compopt -o noquote は使用しない形に
7748+
改められた。つまり、その修正の漏れとも言える。
7749+
7750+
https://github.com/akinomyoga/ble.sh/issues/250
7751+
https://github.com/akinomyoga/ble.sh/commit/0c6291f0c1609b6159013e6ba4aeae3acb35db14
7752+
https://github.com/akinomyoga/blesh-contrib/commit/e102241466dfda8cf3e7efb6891e223102e0b2a9
7753+
7754+
何れにしても単に compopt -o noquote を削除すれば良いだけの話に思われる。
7755+
77317756
2025-01-05
77327757

7758+
* main: 特定の環境で ble-attach の実行を延期する [#D2315]
7759+
https://github.com/akinomyoga/ble.sh/issues/543
7760+
https://github.com/akinomyoga/ble.sh/discussions/524#discussioncomment-11656742
7761+
7762+
ghostty も kitty を真似して ENV 経由で初期化して .bashrc をスキップし、手動
7763+
で .bashrc を読み込もうとしている。その後で PS1 を修正している。これによっ
7764+
て問題が発生しているのである。これはマニュアルにある通りに設定していないか
7765+
ら悪いのだと書いたが issue を閉じてくれない。というか説明したのにバグだとか
7766+
主張している。
7767+
7768+
実は既に nix shell では ble-attach を延期して prompt-attach に切り替える様
7769+
にしている。なので、ghostty についてもその様に変更する事にする。kitty も同
7770+
様に修正する。更に VS Code terminal でも同様の事をしている為に問題が起こっ
7771+
ていた。これについても修正する。
7772+
7773+
? 或いはトップレベルのファイルで ble-attach しているかどうかをチェックして
7774+
そうでなかったら警告を発生する様にする? 然し、既に関数内部やファイル内部
7775+
から呼び出すように設定している人も多いし、そもそも問題が起こらないように
7776+
ちゃんとしている人もいる。という事を考えると、単にそれで警告を出力するよ
7777+
うに変更するのも気が引ける。
7778+
7779+
もしその様にするのであれば最初から警告を出力する様にするべきだった。今に
7780+
なって変更する事はできない。
7781+
77337782
* edit (TMOUT): (reported by Anyborr, georglauterbach) [#D2314]
77347783
https://github.com/akinomyoga/ble.sh/issues/424#issuecomment-2016531757
77357784
https://github.com/akinomyoga/ble.sh/issues/548

0 commit comments

Comments
 (0)