Skip to content

Commit 514d177

Browse files
committed
bind: clean up old codes to bind to [ESC]
1 parent 6f4d040 commit 514d177

File tree

4 files changed

+156
-104
lines changed

4 files changed

+156
-104
lines changed

docs/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@
325325
- main: show details of the loading time (motivated by tessus, Darukutsu) `#D2313` 3d8f6264
326326
- canvas: optimize binary search in tables `#D2325` d56c7d2f d4c812b7
327327
- util: optimize `ble/fd#list` using `compgen -G` `#D2328` xxxxxxxx
328+
- bind: clean up old codes to bind to <kbd>ESC</kbd> `#D2334` xxxxxxxx
328329

329330
<!---------------------------------------------------------------------------->
330331
# ble-0.4.0-devel3

lib/init-bind.sh

Lines changed: 13 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
# Note: #D1300 bind -s で束縛するマクロの非終端文字は
99
# decode.sh (ble/decode/nonblocking-read) でチェックする必要がある。
1010
# 現在の実装では 0xC0 と 0xDE をチェックしている。
11-
# (( esc1B != 3 && esc1B1B )) の時には 0x1B も追加でチェックする必要がある。
1211
# マクロを追加する時にはそれに応じてチェックを追加する必要がある。
1312

1413
function ble/init:bind/append {
@@ -100,69 +99,26 @@ function ble/init:bind/generate-binder {
10099
bind18XX=1
101100
fi
102101

103-
# ESC について
102+
# ESC 受信方法
104103
#
105-
# * esc1B=1: bash-3 では "ESC *" の組合せも全部登録しておかないと駄目??
106-
# (もしかすると bind -r 等に失敗していただけかも知れないが)
107-
#
108-
# 追記: bash-4.0 bash-4.3 でも必要
109-
# 追記: bash-4.1 でも bind -x '"\ez":fg' 等を一回もしていない場合は必要
110-
# 追記: この方法でも ESC を 2 つ以上連続で入力する時に
111-
# bash_execute_unix_command のエラーが発生する。
112-
#
113-
# * esc1B=2: 2017-10-22 実は bind '"\e": "\xC0\x9B"' とすれば全バージョンで OK の様だ。
114-
#
115-
# しかし、これだと単体の ESC と続きのある ESC の区別ができない。
116-
# 続きがあるとき Readline が標準入力からひとまとまりで読み取ってから hook を呼び出す。
117-
# 従って、標準入力に文字が残っているかどうか見ても判定できないし、
118-
# 標準入力から次の文字をタイムアウト付きで読み取るとシーケンスの順序が狂う。
119-
#
120-
# * esc1B=3: 2017-10-22 代替案として
104+
# * 2017-10-22 新しい方法として
121105
#
122106
# bind '"\e":"\e[27;5;91~"'
123107
# bind '"\e?":"\xC0\x9B?"'
124108
# bind '"\e\e":"\xC0\x9B\e[27;5;91~"'
125109
#
126-
# などの様に bind -s で1文字のものと2文字のものを両方登録して、
127-
# Readline に ESC に続きがあるかどうかを判定させて単独 ESC を区別するという手がある。
128-
#
129-
local esc1B=3
110+
# などの様に bind -s で1文字のものと2文字のものを両方登録して、Readline に
111+
# ESC に続きがあるかどうかを判定させて単独 ESC を区別するという手がある。
130112

131-
# bind1B4FXX (esc1B == 3 の時) 2025-05-03
113+
# bind1B4FXX 2025-05-03
132114
#
133115
# * bind1B4FXX=1: bash 4.4 以下では SS3 の矢印キー (ESC O A) を受信した時、常
134116
# に is-stdin-ready が常に失敗する (bash が内部的に先読みして何か処理してい
135117
# る?) ので wait-input で M-O と区別しようとしてもできない。isolated ESC
136118
# と同様に ESC O と ESC ? の両方に登録して区別する。
137119
#
138-
# "ESC O" に束縛を設定していることが前提。これは esc1B == 1 または esc1B ==
139-
# 3 に対応する。
140-
local bind1B4FXX=$(((esc1B==1||esc1B==3)&&40000<=_ble_bash&&_ble_bash<50000))
141-
142-
# esc1B5B (esc1B == 1 の時に有効)
143-
#
144-
# * bash-3.1
145-
# ESC [ を bind -x で捕まえようとしてもエラーになるので、
146-
# 一旦 "ESC [" の ESC を UTF-8 2-byte code にしてから受信し直す。
147-
# bash-3.1 で確認。bash-4.1 ではOK。他は未確認。
148-
# * bash-4.3, bash-4.1
149-
# ESC *, ESC [ *, etc を全部割り当てないと以下のエラーになる。
150-
# bash_execute_unix_command: cannot find keymap for command
151-
# これを避ける為に二つの方針がある
152-
# 1 全てを登録する方針 (bindAllSeq)
153-
# 2 ESC [ を別のシーケンスに割り当てる (esc1B5B)
154-
# 初め 1 の方法を用いていたが 2 でも動く事が分かったので 2 を使う。
155-
#
156-
local esc1B5B=1 bindAllSeq=0
157-
158-
# esc1B1B (esc1B != 3 の時に有効)
159-
#
160-
# * bash-4.1 では ESC ESC に bind すると
161-
# bash_execute_unix_command: cannot find keymap for command
162-
# が出るので ESC [ ^ に適当に redirect して ESC [ ^ を
163-
# ESC ESC として解釈する様にする。
164-
#
165-
local esc1B1B=$((40100<=_ble_bash&&_ble_bash<40300))
120+
# "ESC O" に束縛を設定していることが前提。
121+
local bind1B4FXX=$((40000<=_ble_bash&&_ble_bash<50000))
166122

167123
# Note: 'set convert-meta on' 対策
168124
#
@@ -191,13 +147,7 @@ function ble/init:bind/generate-binder {
191147
fi
192148
elif ((i==27)); then
193149
# C-[
194-
if ((esc1B==0)); then
195-
ble/init:bind/append "$ret" "$i"
196-
elif ((esc1B==2)); then
197-
ble/init:bind/append-macro '\e' "$altdqs27"
198-
elif ((esc1B==3)); then
199-
ble/init:bind/append-macro '\e' "$isolated27" # C-[
200-
fi
150+
ble/init:bind/append-macro '\e' "$isolated27" # C-[
201151
else
202152
# Note: Bash-5.0 では \C-\\ で bind すると変な事になる #D1162 #D1078
203153
((i==28&&_ble_bash>=50000)) && ret='\x1C'
@@ -222,38 +172,12 @@ function ble/init:bind/generate-binder {
222172
fi
223173

224174
# ESC ?
225-
if ((esc1B==3)); then
226-
if ((i==0)); then
227-
ble/init:bind/append-macro '\e'"$ret" "$altdqs27$altdqs00"
228-
elif ((bind18XX&&i==24)); then
229-
ble/init:bind/append-macro '\e'"$ret" "$altdqs27$altdqs24"
230-
else
231-
ble/init:bind/append-macro '\e'"$ret" "$altdqs27$ret"
232-
fi
175+
if ((i==0)); then
176+
ble/init:bind/append-macro '\e'"$ret" "$altdqs27$altdqs00"
177+
elif ((bind18XX&&i==24)); then
178+
ble/init:bind/append-macro '\e'"$ret" "$altdqs27$altdqs24"
233179
else
234-
if ((esc1B==1)); then
235-
# ESC [
236-
if ((i==91&&esc1B5B)); then
237-
# * obsoleted workaround
238-
# ESC [ を CSI (encoded in utf-8) に変換して受信する。
239-
# 受信した後で CSI を ESC [ に戻す。
240-
# CSI = \u009B = utf8{\xC2\x9B} = utf8{\302\233}
241-
# printf 'bind %q' '"\e[":"\302\233"' >> "$fbind1"
242-
# ble/util/print "ble-bind -f 'CSI' '.CHARS 27 91'" >> "$fbind1"
243-
244-
ble/init:bind/append-macro '\e[' "$altdqs27["
245-
else
246-
ble/init:bind/append "\\e$ret" "27 $i"
247-
fi
248-
fi
249-
250-
# ESC ESC
251-
if ((i==27&&esc1B1B)); then
252-
# ESC ESC for bash-4.1
253-
ble/init:bind/append-macro '\e\e' '\e[^'
254-
ble/util/print "ble-bind -k 'ESC [ ^' __esc__" >> "$fbind1"
255-
ble/util/print "ble-bind -f __esc__ '.CHARS 27 27'" >> "$fbind1"
256-
fi
180+
ble/init:bind/append-macro '\e'"$ret" "$altdqs27$ret"
257181
fi
258182

259183
# ESC O ?
@@ -268,15 +192,6 @@ function ble/init:bind/generate-binder {
268192
fi
269193
done
270194

271-
if ((bindAllSeq)); then
272-
# 決まったパターンのキーシーケンスは全て登録
273-
# bash-4.3 で keymap が見付かりませんのエラーが出るので。
274-
# ※3文字以上の bind -x ができるのは bash-4.3 以降
275-
# (bash-4.3-alpha で bugfix が入っている)
276-
ble/util/print 'source "$_ble_decode_bind_fbinder.bind"' >> "$fbind1"
277-
ble/util/print 'source "$_ble_decode_bind_fbinder.unbind"' >> "$fbind2"
278-
fi
279-
280195
ble/function#try ble/encoding:"$bleopt_input_encoding"/generate-binder
281196

282197
ble/edit/info/immediate-show text "ble.sh: updating binders... done"

note.txt

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,142 @@ bash_tips
19261926
- make_command.sh の整理 (scan 分離, char_width 分離)
19271927
- note.txt -> memo.txt
19281928

1929+
2025-05-01
1930+
1931+
* decode: keyboard-macro 再生時に char -> key の timeout をどう処理するか
1932+
Ref #D2333
1933+
1934+
"C-x ( C-x ) C-x e" によるキーボードマクロの記録再生で ble/decode/charlog
1935+
という仕組みが実装されている。文字の列を記録して、それを再生する事で改めて
1936+
同じ操作をするのに使う。
1937+
1938+
ここで、文字の列から key の列への変換が一意でない場合がある。これらは普通の
1939+
入力時には timeout で区別している。具体的には或るキーの表現が別のキーの表現
1940+
の接頭辞になっている時に ble/decode/wait-input を用いて timeout したらその
1941+
キーを採択するという事になっている。しかし、charlog では当然時間間隔は記録
1942+
していないし、入力時の遅延まで再現したくはない。現在の愚直な実装だと、キー
1943+
ボードマクロとしての再生だと既に次の文字が来ている事になるので timeout はせ
1944+
ずにより長いキー表現の一致を必ず試みる事になる。結果として実際に生成される
1945+
キーの列が変わってしまうという問題が起こる。
1946+
1947+
* この区別は @ESC (IsolatedESC) と ESC の場合には (bash の version に関係な
1948+
く) 記録される char の値レベルで区別されている。
1949+
1950+
* 然し、一般のキーについて wait-input を使って区別をしている場合には、マク
1951+
ロ再生時にその区別を再現できない。或いは別の方法として key を直接 CSI u
1952+
形式でエンコードするという手もあるかもしれないが、現在の実装ではキーボー
1953+
ドマクロは実際に入力された文字列を使っている。そうしないとユーザーに対し
1954+
て現在記録しているマクロの内容を提示する時に不都合だからである。
1955+
1956+
* 特に "M-O" (ESC O) と "up" (ESC O A, bash <= 4.4) の区別は問題である。通
1957+
常の入力時には "ESC O A" の方については O を @PrO に変換して読み取ってい
1958+
るので、ちゃんと区別して記録される。一方で、M-O についてはそのまま ESC O
1959+
で受信しているので、本当に M-O として受け取ったのか bash <= 4.4 の
1960+
workaround を使わずに受け取ったのかの区別がつかない。というか bash >= 5.0
1961+
では結局 @PrO は使わずに完全に timeout だけで判定している。
1962+
1963+
もしこれだけ部分的に対応するのだとしたら IsolatedESC ならぬ IsolatedSS3
1964+
もしくは @IsO を定義することになる。もし全般に wait-input の枠組みを設計
1965+
し直すか、それに関する情報を charlog に埋め込む事にするのであれば、特別に
1966+
SS3 (M-O) の対策をする必要もない。
1967+
1968+
2025-05-01
1969+
1970+
* update でキーボードが入力できなくなる報告がまたあった。
1971+
https://github.com/akinomyoga/ble.sh/issues/576
1972+
https://github.com/akinomyoga/ble.sh/issues/484
1973+
1974+
やっぱり何か対処する必要があるだろうか。
1975+
1976+
* 何も入力できなくなったら取り敢えず --clear-cache を試みてもらうというのは
1977+
README に書いても良い。
1978+
1979+
それとは別にキャッシュ不整合を検出して強制アップデートするのを実装する? そ
1980+
の為には
1981+
1982+
うーん。lib/init-cmap.sh を見てみたが最近の変更はない? なのに何故最近になっ
1983+
て動かなくなったという報告がたくさん出ているのか? 何れかの変更が問題を起こ
1984+
したという事? 或いは NixOS での利用が増えて問題が起こりやすくなったという事?
1985+
1986+
2025-04-26
1987+
1988+
* connect_tty に inherit が設定されていても ble.pp の最初でそれをチェックして
1989+
いないから、結局 >/dev/tty も 1 も 2 も見つからなかった場合には、親 ble.sh
1990+
が記録した tty が未だ生きていても ble.sh のロードがキャンセルされてしまうの
1991+
では。
1992+
1993+
これは最近何処かのタイミングで諦める事にしたのだったか?
1994+
1995+
2025-04-10
1996+
1997+
* complete: auto-complete を個別に off にする方法の記述
1998+
https://github.com/akinomyoga/ble.sh/issues/572
1999+
2000+
やはり set -x 等で出力する仕組みを作る
2001+
2002+
* cobra に関しては問答無用でコマンドを conditional-sync 経由の呼び出しに置
2003+
き換えても良いのではないか? と思ったがそんなに時間がかかると分かっていな
2004+
い物にまでそのような処理をするのも憚られる。やはり問題のある補完をリスト
2005+
にして適用する方が良いだろうか…
2006+
2007+
kubectl はリストに追加しておく。
2008+
2009+
* wiki: auto-complete で各補完を無効にする方法について。 _comp_load を先に
2010+
使える様にするということを明記するべき。また古い bash-completion の為に
2011+
_completion_loader だか __load_completion を使った方法についても記述する。
2012+
2013+
或いはそれ専用の設定関数を用意すれば良い。と思ったが bash-completion の読
2014+
み込みまでは制御できない気がする。また後で考える事にする。
2015+
2016+
2025-04-03
2017+
2018+
* edit: zle の様々の関数
2019+
https://github.com/atuinsh/atuin/pull/1469
2020+
2021+
zle の次の様な widget は便利なのではないか。
2022+
2023+
- infer-next-history
2024+
- asccept-and-infer-next-history
2025+
- 加えて直前の同じ内容の履歴項目に移動する機能があっても良いのでは
2026+
2027+
zle には他にも様々な便利そうな widget が実装されている。それらを実装するの
2028+
も良いのではないか。
2029+
2030+
* complete: auto-menu もしくは menu-filter で絞り込み表示がされていて、tab を
2031+
押した時に共通部分がなくて何も起こらないとき、代わりに menu に入っても良い
2032+
のではないか?
2033+
2034+
* global: #D2192 で ":" の使用を抑制した筈なのに while :; do がたくさんある
2035+
2036+
他にも eval ": ${...}" というものが残っている。うーん。
2037+
2038+
./src/edit.sh:11579:function ble-edit/bind/stdout.on { :;}
2039+
./src/edit.sh:11581:function ble-edit/bind/stdout.finalize { :;}
2040+
2041+
以下も気になる。
2042+
2043+
./lib/core-complete.sh:6761:function ble/complete/candidates/filter:none/test { true; }
2044+
./lib/core-syntax-def.sh:50:function ble/highlight/layer:syntax/update { true; }
2045+
./lib/core-syntax-def.sh:53:function ble/highlight/layer:syntax/getg { true; }
2046+
./lib/core-syntax-def.sh:58:function ble/syntax:bash/is-complete { true; }
2047+
./lib/test-bash.sh:140: function f1/sub { true; }
2048+
./lib/test-main.sh:45: function ble/test/dummy-1 { true; }
2049+
./lib/test-main.sh:46: function ble/test/dummy-2 { true; }
2050+
./lib/test-main.sh:47: function ble/test/dummy-3 { true; }
2051+
2052+
./ble.pp:1267: if ! type "$cmd" &>/dev/null; then
2053+
./ble.pp:1413: if [[ $OSTYPE == solaris* ]] && type /usr/xpg4/bin/awk >/dev/null; then
2054+
2055+
* 以下の D22XX がそのままになっている
2056+
2057+
./src/edit.sh:6766: # Note (#D22XX): In bash < 4.0, "_ble_builtin_trap_processing" may be set by
2058+
2059+
* iTerm2-Color-Schemes, Gogh の更新も bump 時の todo に入れるべきか?
2060+
2061+
* function ble/complete/source:argument の中の *f* をチェックしているのは実
2062+
際にここに来ることはあるのだろうか。この部分は c1b053253 で追加された。一
2063+
方で、最近 6a426954
2064+
19292065
2025-02-20
19302066

19312067
* complete: _comp_load / __load_completion に hook する? (reported by quantumfrost)
@@ -7758,6 +7894,12 @@ bash_tips
77587894
Done (実装ログ)
77597895
-------------------------------------------------------------------------------
77607896

7897+
2025-05-03
7898+
7899+
* decode: 古い init-bind.sh の方針は削除して良い気がする [#D2334]
7900+
7901+
最近安定しているので今後改めて使うことはないのではないか。
7902+
77617903
2025-05-02
77627904

77637905
* decode: "ESC O A" vs "ESC O" の区別が 4.0 <= bash < 5.0 でついていない [#D2333]

src/decode.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,12 +2827,6 @@ function ble/widget/.MACRO {
28272827
ble-decode-char "${chars[@]}"
28282828
}
28292829

2830-
## @fn ble/widget/.CHARS char...
2831-
## lib/init-bind.sh で特別なバイト列を受信するのに使う関数
2832-
function ble/widget/.CHARS {
2833-
ble-decode-char "$@"
2834-
}
2835-
28362830
#------------------------------------------------------------------------------
28372831
# key definitions (c.f. init-cmap.sh) @decode.cmap
28382832

0 commit comments

Comments
 (0)