Skip to content

Commit

Permalink
util: fix and refactor "_ble_term_TERM"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jan 23, 2022
1 parent dc3827b commit 63fba6b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 38 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -82,6 +82,7 @@
- menu (menu-style:desc): support multicolumns (motivated by Shahabaz-Bagwan) `#D1686` 231dc39
- menu (menu-style:desc): fix not working `bleopt menu_desc_multicolumn_width=` `#D1727` 2140d1e
- term: let <kbd>DECSCUSR</kbd> pass through terminal multiplexers (motivated by cmplstofB) `#D1697` a3349e4
- util: refactor `_ble_term_TERM` `#D1746` 0000000
- complete: requote for more compact representations on full completions `#D1700` a1859b6
- complete: improve support for `declare` and `[[ ... ]]` `#D1701` da38404
- syntax: fix completion and highlighting of `declare` with assignment arguments `#D1704` `#D1705` e12bae4
Expand Down
2 changes: 1 addition & 1 deletion lib/core-complete.sh
Expand Up @@ -408,7 +408,7 @@ function ble/complete/menu-style:desc/construct-page {
# white list で右端に接する事を許可する。
local available_width=$cols
case $_ble_term_TERM in
(screen|tmux|kitty|contra) ;;
(screen:*|tmux:*|kitty:*|contra:*) ;;
(*) ((available_width--)) ;;
esac

Expand Down
11 changes: 8 additions & 3 deletions note.txt
Expand Up @@ -1727,9 +1727,7 @@ bash_tips

2022-01-23

* mintty で _ble_term_TERM が空になっている。何故か unknown が設定されるので
はないか。ちゃんと DA2R は受信できている。そして passthrough seq がちゃんと
動いていない in screen. これは修正する必要がある。
* 元々存在した DEBUG trap を保持する

* bash-preexec に API 安定化のお願いをする。

Expand Down Expand Up @@ -5896,6 +5894,13 @@ bash_tips

2022-01-23

* util: _ble_term_TERM が空になっている問題 [#D1746]

mintty で _ble_term_TERM が空になっている。何故か。unknown が設定されるので
はないか。ちゃんと DA2R は受信できている。そして passthrough seq がちゃんと
動いていない in screen. これは修正する必要がある→確認したら物凄く単純なミ
スをしていた。これは恐らく置換か何かをした時の残りであろう。

* 折返しがある時に実際に端末に改行が挿入される (reported by banoris) [#D1745]
https://github.com/akinomyoga/ble.sh/issues/170

Expand Down
8 changes: 4 additions & 4 deletions src/decode.sh
Expand Up @@ -797,7 +797,7 @@ function ble-decode-char/csi/.modify-key {
# 文字自体もそれに応じて変化させ、更に修飾フラグも設定する。
# Note: RLogin は修飾がある場合は常に英大文字に統一する。
if ((33<=key&&key<_ble_decode_FunctionKeyBase)); then
if (((mod&0x01)&&0x31<=key&&key<=0x39)) && [[ $_ble_term_TERM == RLogin ]]; then
if (((mod&0x01)&&0x31<=key&&key<=0x39)) && [[ $_ble_term_TERM == RLogin:* ]]; then
# RLogin は数字に対する S- 修飾の解決はしてくれない。
((key-=16,mod&=~0x01))
elif ((mod==0x01)); then
Expand Down Expand Up @@ -849,7 +849,7 @@ function ble-decode-char/csi/.decode {
local rematch1=${BASH_REMATCH[1]}
if [[ $rematch1 != 1 ]]; then
local key=$((10#0$rematch1)) mods=$((10#0${BASH_REMATCH:${#rematch1}+1}))
[[ $_ble_term_TERM == kitty ]] && ble-decode/char/csi/.translate-kitty-csi-u
[[ $_ble_term_TERM == kitty:* ]] && ble-decode/char/csi/.translate-kitty-csi-u
ble-decode-char/csi/.modify-key "$mods"
csistat=$key
fi
Expand Down Expand Up @@ -4033,11 +4033,11 @@ function ble/decode/attach {
(linux)
# Note #D1213: linux コンソール (kernel 5.0.0) は "\e[>"
# でエスケープシーケンスを閉じてしまう。5.4.8 は大丈夫。
_ble_term_TERM=linux ;;
_ble_term_TERM=linux:- ;;
(st|st-*)
# st の unknown csi sequence メッセージに対して文句を言う人がいた。
# st は TERM で判定できるので DA2 はスキップできる。
_ble_term_TERM=st ;;
_ble_term_TERM=st:- ;;
(*)
ble/util/buffer $'\e[>c' # DA2 要求 (ble-decode-char/csi/.decode で受信)
esac
Expand Down
12 changes: 7 additions & 5 deletions src/edit.sh
Expand Up @@ -1497,16 +1497,16 @@ function ble/prompt/update {
fi

# bleopt prompt_xterm_title
case ${_ble_term_TERM:-$TERM} in
(sun*|minix) ;; # black list
case ${_ble_term_TERM:-$TERM:-} in
(sun*|minix*) ;; # black list
(*)
[[ $bleopt_prompt_xterm_title || ${_ble_prompt_xterm_title_data[10]} ]] &&
ble/prompt/unit#update _ble_prompt_xterm_title && dirty=1 ;;
esac

# bleopt prompt_screen_title
case ${_ble_term_TERM:-$TERM} in
(screen|tmux|contra|screen.*|screen-*)
case ${_ble_term_TERM:-$TERM:-} in
(screen:*|tmux:*|contra:*|screen.*|screen-*)
[[ $bleopt_prompt_screen_title || ${_ble_prompt_screen_title_data[10]} ]] &&
ble/prompt/unit#update _ble_prompt_screen_title && dirty=1 ;;
esac
Expand Down Expand Up @@ -2559,6 +2559,8 @@ function ble/textarea#slice-text-buffer {
IFS= builtin eval "ret=\"\$ret\${$_ble_textarea_bufferName[*]:i1:i2-i1}\""

if [[ $_ble_textarea_bufferName == _ble_textarea_buffer ]]; then
# Note #D1745: 自動折返し改行は \r で符号化されている。末尾及び \n 直前の
# 自動折返し (\r) は \n に変換し、それ以外の \r は削除する。
local out= rex_nl='^(\[[ -?]*[@-~]|[ -/]+[@-~]|[])*'$_ble_term_nl
while [[ $ret == *"$_ble_term_cr"* ]]; do
out=$out${ret%%"$_ble_term_cr"*}
Expand Down Expand Up @@ -5155,7 +5157,7 @@ function ble-edit/exec/.adjust-eol {

# EOL adjustment
local advance=$((_ble_term_xenl?cols-2:cols-3))
if [[ $_ble_term_TERM == cygwin ]]; then
if [[ $_ble_term_TERM == cygwin:* ]]; then
# Note (#D1144): Cygwin console では何故か行き先が
# 丁度 cols+1 列目になる様な CUF は一文字も動かない。
# cols列目またはcols+2列目以降は大丈夫である。
Expand Down
58 changes: 33 additions & 25 deletions src/util.sh
Expand Up @@ -5272,13 +5272,13 @@ function ble/term:cygwin/initialize.hook {
function ble/term/DA2R.hook {
blehook DA2R-=ble/term/DA2R.hook
case $_ble_term_TERM in
(contra)
(contra:*)
_ble_term_cuu=$'\e[%dk'
_ble_term_cud=$'\e[%de'
_ble_term_cuf=$'\e[%da'
_ble_term_cub=$'\e[%dj'
_ble_term_cup=$'\e[%l;%cf' ;;
(cygwin)
(cygwin:*)
ble/term:cygwin/initialize.hook ;;
esac
}
Expand Down Expand Up @@ -5678,6 +5678,12 @@ function ble/term/cursor-state/.update {
local state=$(($1))
[[ $_ble_term_cursor_current == "$state" ]] && return 0

if [[ ! $_ble_term_Ss ]]; then
case $_ble_term_TERM in
(mintty:*|xterm:*|RLogin:*|kitty:*|screen:*|tmux:*|contra:*|cygwin:*)
local _ble_term_Ss=$'\e[@1 q' ;;
esac
fi
local ret=${_ble_term_Ss//@1/"$state"}

# Note: 既に pass-through seq が含まれている時はスキップする。
Expand Down Expand Up @@ -5750,37 +5756,39 @@ function ble/term/DA2/initialize-term {

case $DA2R in
('1;0'?????';0')
_ble_term_TERM[depth]=foot ;;
_ble_term_TERM[depth]=foot:${DA2R:3:5} ;;
('1;'*)
if ((4000<=da2r[1]&&da2r[1]<4100&&3<=da2r[2])); then
_ble_term_TERM[depth]=kitty
if ((4000<=da2r[1]&&da2r[1]<=4009&&3<=da2r[2])); then
_ble_term_TERM[depth]=kitty:$((da2r[1]-4000))
elif ((2000<=da2r[1]&&da2r[1]<5400&&da2r[2]==0)); then
_ble_term_TERM[depth]=vte
_ble_term_TERM[depth]=vte:$((da2r[1]))
fi ;;
('99;'*)
_ble_term_TERM[depth]=contra ;;
_ble_term_TERM[depth]=contra:$((da2r[1])) ;;
('65;'*)
if ((5300<=da2r[1]&&da2r[2]==1)); then
_ble_term_TERM[depth]=vte
_ble_term_TERM[depth]=vte:$((da2r[1]))
elif ((da2r[1]>=100)); then
_ble_term_TERM[depth]=RLogin
_ble_term_TERM[depth]=RLogin:$((da2r[1]))
fi ;;
('67;'*)
local rex='^67;[0-9]{3,};0$'
if [[ $TERM == cygwin && $DA2R =~ $rex ]]; then
_ble_term_TERM[depth]=cygwin
_ble_term_TERM[depth]=cygwin:$((da2r[1]))
fi ;;
('77;'*';0')
_ble_term_TERM[depth]=mintty:$((da2r[1])) ;;
('83;'*)
local rex='^83;[0-9]+;0$'
[[ $DA2R =~ $rex ]] && _ble_term_TERM[depth]=screen ;;
[[ $DA2R =~ $rex ]] && _ble_term_TERM[depth]=screen:$((da2r[1])) ;;
('84;0;0')
_ble_term_TERM[depth]=tmux ;;
_ble_term_TERM[depth]=tmux:0 ;;
esac
[[ $_ble_term_TERM[depth] ]] && return 0
[[ ${_ble_term_TERM[depth]} ]] && return 0

# xterm
if rex='^xterm(-|$)'; [[ $TERM =~ $rex ]]; then
local version=${da2r[1]}
local version=$((da2r[1]))
if rex='^1;[0-9]+;0$'; [[ $DA2R =~ $rex ]]; then
# Note: vte (2000以上), kitty (4000以上) は処理済み
true
Expand All @@ -5795,7 +5803,7 @@ function ble/term/DA2/initialize-term {
fi && { _ble_term_TERM[depth]=xterm:$version; return; }
fi

_ble_term_TERM[depth]=unknown
_ble_term_TERM[depth]=unknown:-
return 0
}

Expand All @@ -5805,17 +5813,17 @@ function ble/term/DA2/notify {
# 事がある。2回目以降に受信した内容は ble.sh の内部では使用しない事
# にする。
local depth=${#_ble_term_DA2R[@]}
if ((depth==0)) || ble/string#match "${_ble_term_TERM[depth-1]}" '^(screen|tmux)$'; then
if ((depth==0)) || ble/string#match "${_ble_term_TERM[depth-1]}" '^(screen|tmux):'; then
_ble_term_DA2R[depth]=$1
ble/term/DA2/initialize-term "$depth"
case ${_ble_term_TERM[depth]} in
(screen|tmux)
(screen:*|tmux:*)
# 外側の端末にも DA2 要求を出す。[ Note: 最初の DA2 要求は
# ble/decode/attach (decode.sh) から送信されている。 ]
local ret
ble/term/quote-passthrough $'\e[>c' $((depth+1))
ble/util/buffer "$ret" ;;
(contra)
(contra:*)
: "${_ble_term_Ss:=$'\e[@1 q'}" ;;
esac

Expand Down Expand Up @@ -5855,7 +5863,7 @@ function ble/term/quote-passthrough {
[[ $seq ]] || return 0
local i
for ((i=level;--i>=0;)); do
if [[ ${_ble_term_TERM[i]} == tmux ]]; then
if [[ ${_ble_term_TERM[i]} == tmux:* ]]; then
# Note: tmux では pass-through seq の中に含まれる \e は \e\e の様に
# escape する。
ret=$'\ePtmux;'${ret//$'\e'/$'\e\e'}$'\e\\'${all:+$seq}
Expand Down Expand Up @@ -5923,22 +5931,22 @@ function ble/term/modifyOtherKeys/.update {
# また、RLogin は modifyStringKeys にすると S-数字 を
# 記号に翻訳してくれないので注意。
case $_ble_term_TERM in
(RLogin)
(RLogin:*)
case $1 in
(0) ble/util/buffer $'\e[>5;0m' ;;
(1) ble/util/buffer $'\e[>5;1m' ;;
(2) ble/util/buffer $'\e[>5;1m\e[>5;2m' ;;
esac ;;
(kitty)
(kitty:*)
local da2r
ble/string#split da2r ';' "$_ble_term_DA2R"
if ((da2r[2]>=23)); then
# Note: Kovid removed the support for modifyOtherKeys in kitty 0.24 after
# vim has pointed out the quirk of kitty. The kitty keboard mode only
# has push/pop operations so that they need to be balanced.
# vim has pointed out the quirk of kitty. The kitty keyboard mode only
# has push/pop operations so that their numbers need to be balanced.
case $1 in
(0|1) # pop keyboard mode
# When this is empty, ble.sh has nto yet pushed any keyboard modes, so
# When this is empty, ble.sh has not yet pushed any keyboard modes, so
# we just ignore the keyboard mode change.
[[ $_ble_term_modifyOtherKeys_current ]] || return 0

Expand Down Expand Up @@ -5974,7 +5982,7 @@ function ble/term/modifyOtherKeys/.update {
}
function ble/term/modifyOtherKeys/.supported {
# libvte は SGR(>4) を直接画面に表示してしまう。
[[ $_ble_term_TERM == vte ]] && return 1
[[ $_ble_term_TERM == vte:* ]] && return 1

# 改造版 Poderosa は通知でウィンドウサイズを毎回変更するので表示が乱れてしまう
[[ $MWG_LOGINTERM == rosaterm ]] && return 1
Expand Down

0 comments on commit 63fba6b

Please sign in to comment.