Skip to content

Commit

Permalink
prompt: use "${PS1@P}" when the prompt contains only safe prompt sequ…
Browse files Browse the repository at this point in the history
…ences
  • Loading branch information
akinomyoga committed Jun 15, 2021
1 parent a034c91 commit 8b5da08
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 40 deletions.
25 changes: 13 additions & 12 deletions memo/ChangeLog.md
Expand Up @@ -47,20 +47,20 @@
- prompt: preserve `LINES` and `COLUMNS` for custom sequences `#D1592` 040016d
- color: fix the face initialiation order for uses in prompts (motivated by jmederosalvarado) `#D1593` 321371f
- prompt (`contrib/prompt-git`): support dirty checking `#D1601` b2713d9
- prompt (`contrib/prompt-git`): do not use `ble/util/idle` in Bash 3 `#D1606` 0000000
- util (`bleopt`): add new option `-I` to reinitialize user settings on reload `#D1607` 0000000
- prompt (`contrib/prompt-git`): do not use `ble/util/idle` in Bash 3 `#D1606` 959cf27
- util (`bleopt`): add new option `-I` to reinitialize user settings on reload `#D1607` 959cf27
- util, color: refactor configuration interfaces (`bleopt`, `blehook`, `ble-face`) `#D1568` c94d292
- color: support new face setting function `ble-face`
- util (`bleopt`): support option `-r` and `-u` and wildcards in option names
- util (`blehook`): hide internal hooks by default and support option `-a`
- util, color: fix argument analysis of `bleopt`, `blehook`, and `ble-face` (fixup c94d292) `#D1571` bb53271
- util (`blehook`): show explicitly specified internal hooks `#D1594` f4312df
- util (`bleopt`): do no select obsoleted options by wildcards `#D1595` f4312df
- util (`bleopt`): fix error messages for unknown options `#D1610` 0000000
- util (`bleopt`): fix error messages for unknown options `#D1610` 66df3e2
- progcomp: support quoted commands and better `progcomp_alias` `#D1581` `#D1583` dbe87c3
- progcomp: fix a bug that command names may stray into completer function names `#D1611` 0000000
- progcomp: fix a bug that command names may stray into completer function names `#D1611` 1f2d45f
- syntax: highlight quotes of the `\?` form `#D1584` 5076a03
- prompt: support a new backslash sequence `\g{...}` `#D1609` 0000000
- prompt: support a new backslash sequence `\g{...}` `#D1609` be31391

## Changes

Expand Down Expand Up @@ -91,7 +91,7 @@
- decode (`ble-bind`): support colored output `#D1547` 750ca38
- decode (`ble-bind`): output bindings of the specified keymaps with `ble-bind -m KEYMAP` (fixup 750ca38) `#D1559` 6e0245a
- keymap/vi: update mode names on change of `bleopt keymap_vi_mode_name_*` (motivated by huresche) `#D1565` 11ac106
- main: show notifications against debug versions of Bash `#D1612` 0000000
- main: show notifications against debug versions of Bash `#D1612` 8f974aa

## Fixes

Expand Down Expand Up @@ -136,10 +136,10 @@
- main (adjust-bash-options): adjust `LC_COLLATE=C` `#D1588` e87ac21
- highlight (`layer:region`): fix blocked lower-layer changes without selection changes `#D1596` 5ede3c6
- complete (`auto-menu`): fix sleep loops by clock/sclock difference `#D1597` 53dd018
- history: fix a bug that history data is cleared on `history -r` `#D1605` 0000000
- util (`ble/string#quote-command`): remove redundant trailing spaces for single word command `#D1613` 0000000
- util: work around the Bash 3 bug of array assignments with `^A` and `^?` in Bash 3.2 `#D1614` 0000000
- benchmark (`ble-measure`): fix a bug that the result is always 0 in Bash 3 and 4 (fixup bbc2a904) `#D1615` 0000000
- history: fix a bug that history data is cleared on `history -r` `#D1605` 72c274e
- util (`ble/string#quote-command`): remove redundant trailing spaces for single word command `#D1613` 94556b4
- util: work around the Bash 3 bug of array assignments with `^A` and `^?` in Bash 3.2 `#D1614` b9f7611
- benchmark (`ble-measure`): fix a bug that the result is always 0 in Bash 3 and 4 (fixup bbc2a904) `#D1615` a034c91

## Optimization

Expand All @@ -152,11 +152,12 @@
- complete: support `bleopt complete_timeout_compvar` to time out pathname expansions for `COMP_WORDS` / `COMP_LINE` `#D1457` cc2881a
- complete (`ble/complete/source:file`): remove slow old codes (reported by timjrd) `#D1512` e5be0c1
- syntax (`ble/syntax:bash/simple-word/eval`): optimize large array passing (motivated by timjrd) `#D1522` c89aa23
- syntax (`ble/syntax:bash/simple-word/eval`): use `mapfile -d ''` for Bash 5.2 `#D1604` 0000000
- syntax (`ble/syntax:bash/simple-word/eval`): use `mapfile -d ''` for Bash 5.2 `#D1604` 72c274e
- main: prefer `nawk` over `mawk` and `gawk` `#D1523` `#D1524` c89aa23
- main (`ble/bin/.freeze-utility-path`): fix unupdated temporary implementations `#D1528` c70a3b4
- util (`ble/util/assign`): work around subshell conflicts `#D1578` 6e4bb12
- history: use `mapfile -d ''` to load history in Bash 5.2 `#D1603` 0000000
- history: use `mapfile -d ''` to load history in Bash 5.2 `#D1603` 72c274e
- prompt: use `${PS1@P}` when the prompt contains only safe prompt sequences `#D1617` 0000000

## Compatibility

Expand Down
39 changes: 39 additions & 0 deletions note.txt
Expand Up @@ -4666,6 +4666,45 @@ bash_tips
Done (実装ログ)
-------------------------------------------------------------------------------

2021-06-13

* 2021-06-10 prompt: [最適化] 単純なプロンプト内容は ${PS1@P} で展開? [#D1617]

プロンプトが単純な内容しか含んでいない時には ${PS@P} で計算を省略できるので
はないか。つまり、\X で特別な物を含んでいない限りは bashに任せる事ができる。
(一方で \w の追跡などについては個別に判定する必要がある気もする。)

然しそんなに速度向上には寄与しないのではないかという気もする。結局 trace は
しなければならないからである。

取り敢えず PS1 が単純化どうかの判定をどうするか。\ の後に "安全" でない文字
が続いている物は全て除外するのが良い様に思われる。"安全" な文字を列挙する。

- 0-7aenrdtAT@DhHjlsuvV[]!$\
- wW ... これらには add-hash を付加したい様に思われる。
- # ... これは駄目。ble.sh 的には内部の bash の番号をちゃんと更新できていない。

意外と簡単に実装できた。動いている。\# も大丈夫。\w の更新も大丈夫。

* 2021-06-06 prompt: エラーはまとめて出力するべき? [#D1616]

現在の実装だとエラーを直接その場に出力する仕組みになっている。然しそうでは
なくて、例えば新しい行を表示する直前等にエラーメッセージを出力するべきの気
がする。その場合にはプロンプトの類を一旦仕舞うなどの工夫が必要である。或い
は、trace 等で計測して出力する事になる。trace で計測したとしても画面の範囲
よりも大きい場合には表示しきれないという事等色々考えるとプロンプトの類を一
旦仕舞うのが妥当な気がする。

特にプロンプトに関連するエラーはプロンプトの表示直前にまとめて表示するのが
良いのではないか。と思ったが、プロンプトの部分更新でもエラーは発生する。と
いう事など考えていくと微妙。というかプロンプトの処理に関しては、
visible-bell で良いのではないかという気がする。

というかそもそもエラーメッセージを表示する意味はあるのだろうか。認識してい
なかったら単純に \q{...} のまま出力すれば良いのではないか。うーん。取り敢え
ずは \q{...} の形でそのまま出力すると共に、visible-bell でも表示する事にし
た。

2021-06-12

* benchmark: ble-measure が bash-4.4 未満で結果を返さなくなっている [#D1615]
Expand Down
68 changes: 40 additions & 28 deletions src/edit.sh
Expand Up @@ -820,17 +820,19 @@ function ble/prompt/backslash:q {
local rematch=$BASH_REMATCH
((i+=${#rematch}))
local word; ble/string#split-words word "${BASH_REMATCH[1]}"
if [[ ! $word ]]; then
ble/util/print "ble/prompt: invalid sequence \\q$rematch" >&2
return 2
elif ! ble/is-function ble/prompt/backslash:"$word"; then
ble/util/print "ble/propmt: undefined named sequence \\q{$word}" >&2
return 2
else
if [[ $word ]] && ble/is-function ble/prompt/backslash:"$word"; then
ble/util/joblist.check
ble/prompt/backslash:"${word[@]}"; local ext=$?
ble/util/joblist.check ignore-volatile-jobs
return "$?"
else
if [[ ! $word ]]; then
ble/term/visible-bell "ble/prompt: invalid sequence \\q$rematch"
elif ! ble/is-function ble/prompt/backslash:"$word"; then
ble/term/visible-bell "ble/propmt: undefined named sequence \\q{$word}"
fi
ble/prompt/print "\\q$BASH_REMATCH"
return 2
fi
else
ble/prompt/print "\\$c"
Expand Down Expand Up @@ -1079,12 +1081,37 @@ function ble/prompt/.instantiate {
local ps=$1 opts=$2 x0=$3 y0=$4 g0=$5 lc0=$6 lg0=$7 esc0=$8 trace_hash0=$9
[[ ! $ps ]] && return 0

# 設定
local prompt_noesc=
shopt -q promptvars &>/dev/null || prompt_noesc=1
local expanded=
local chars_safe_esc='][0-7aenrdtAT@DhHjlsuvV!$\wW'
if ((_ble_bash>=40400)) && [[ $ps != *'\'[!"$chars_safe_esc"]* ]]; then
[[ $ps == *'\'[wW]* ]] && ble/prompt/unit/add-hash '$PWD'
BASH_COMMAND=$_ble_edit_exec_BASH_COMMAND \
builtin eval 'expanded=${ps@P}'

else
# 展開設定
local prompt_noesc=
shopt -q promptvars &>/dev/null || prompt_noesc=1

# 1. PS1 に含まれる \c を処理する
local -a DRAW_BUFF=()
ble/prompt/process-prompt-string "$ps"
local processed; ble/canvas/sflush.draw -v processed

# 2. PS1 に含まれる \\ や " をエスケープし、
# eval して各種シェル展開を実行する。
if [[ ! $prompt_noesc ]]; then
local ret
ble/prompt/.escape "$processed"; local escaped=$ret
expanded=${trace_hash0#*:} # Note: これは次行が失敗した時の既定値
ble-edit/exec/.setexit "$_ble_edit_exec_lastarg"
BASH_COMMAND=$_ble_edit_exec_BASH_COMMAND \
builtin eval "expanded=\"$escaped\""
else
expanded=$processed
fi
fi

# 1. PS1 に含まれる \c を処理する
local -a DRAW_BUFF=()
if [[ :$opts: == *:show-mode-in-prompt:* ]]; then
if ble/util/test-rl-variable show-mode-in-prompt; then
local keymap; ble/prompt/.get-keymap-for-current-mode
Expand All @@ -1095,24 +1122,9 @@ function ble/prompt/.instantiate {
(vi_[noxs]map) ble/util/read-rl-variable vi-cmd-mode-string ;;
(emacs) ble/util/read-rl-variable emacs-mode-string ;;
esac
[[ $ret ]] && ble/prompt/print "$ret"
[[ $ret ]] && expanded=$expanded$ret
fi
fi
ble/prompt/process-prompt-string "$ps"
local processed; ble/canvas/sflush.draw -v processed

# 2. PS1 に含まれる \\ や " をエスケープし、
# eval して各種シェル展開を実行する。
if [[ ! $prompt_noesc ]]; then
local ret
ble/prompt/.escape "$processed"; local escaped=$ret
local expanded=${trace_hash0#*:} # Note: これは次行が失敗した時の既定値
ble-edit/exec/.setexit "$_ble_edit_exec_lastarg"
BASH_COMMAND=$_ble_edit_exec_BASH_COMMAND \
builtin eval "expanded=\"$escaped\""
else
local expanded=$processed
fi

# 3. 端末への出力を構成する
if [[ :$opts: == *:no-trace:* ]]; then
Expand Down

0 comments on commit 8b5da08

Please sign in to comment.