From ac84c153a5038814960475b0721f6ac6fad64bf2 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sun, 25 Feb 2024 21:36:58 +0900 Subject: [PATCH] edit: support "bleopt edit_magic_accept=verify-syntax" --- blerc.template | 7 ++++- docs/ChangeLog.md | 5 ++-- note.txt | 30 +++++++++++++++++++ src/edit.sh | 73 ++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 101 insertions(+), 14 deletions(-) diff --git a/blerc.template b/blerc.template index 25b8268f..53331954 100644 --- a/blerc.template +++ b/blerc.template @@ -140,7 +140,12 @@ ## specified, if sabbrev or alias expansions changed the command line, the ## execution of the command line is canceled so the user can examine or ## continue to edit the expanded line. The history expansion can be controlled -## by "shopt -s histverify" in a similar manner. The default is empty. +## by "shopt -s histverify" in a similar manner. When "verify-syntax" is +## specified and any expansions change the command string, a syntax check is +## performed. The command execution is canceled when the command string is not +## syntactically complete. When "history-line" is specified, the history +## expansion replaces the command line instead of just printing the expansion +## result. The default value of this option is empty. #bleopt edit_magic_accept=sabbrev diff --git a/docs/ChangeLog.md b/docs/ChangeLog.md index 15f4486c..da03d375 100644 --- a/docs/ChangeLog.md +++ b/docs/ChangeLog.md @@ -24,8 +24,9 @@ - keymap/vi_nmap: support `shell-expand-line` `#D2145` aa92b42a - main: support `bash ble.sh --install` `#D2169` 986d26a3 3801a87e - util(stty): support `bleopt term_stty_restore` (requested by TheFantasticWarrior) `#D2170` e64b02b7 -- edit: support `bleopt edit_magic_accept` (requested by pl643, bkerin) `#D2175` xxxxxxxx -- main: support shell variable `BLE_VER` `#D2177` xxxxxxxx +- edit: support `bleopt edit_magic_accept` (requested by pl643, bkerin) `#D2175` 3e9d8907 +- main: support shell variable `BLE_VER` `#D2177` a12dedab +- edit: support `bleopt edit_magic_accept=verify-syntax` `#D2178` xxxxxxxx ## Changes diff --git a/note.txt b/note.txt index a2df6231..3e454fe8 100644 --- a/note.txt +++ b/note.txt @@ -7385,6 +7385,18 @@ bash_tips 2024-02-25 + * edit: support "bleopt edit_magic_accept=verify-syntax" [#D2178] + + 履歴展開で文法的におかしな事になる事も結構ある。展開後に文法的に壊れていた + ら一旦停止する様にするべきではないか。 + + * done: blerc.template + * done: wiki + * done: これは既定のオプションにする。こうするとbash/readline の振る舞いと + 異なって、文法チェックの為に必ずコマンドラインが置き換わる形になるが、ま + あ元の振る舞いの方が良い訳でもないのでこれで良い。元の振る舞いが良い人は + verify-syntax を外してもらう事にする。 + * main: BLE_VER 対応 [#D2177] wezterm や atuin integration で _ble_version を参照するのも忍びない。かと言っ @@ -7509,6 +7521,24 @@ bash_tips * done: 関数名を色々変える。展開結果は hist_expanded ではなく ret を通し て返す様に変更する。hist_expanded がもう残っていないか確認する。 + ---- + + 完成したと思ってすぐに push してしまったがテストするのを忘れていた。通常の + 展開に関してはいい感じに動いているが verify が全く動いていない。どうも + magic-space から実装を持ってきた時に type-status を sabbrev/expand に指定し + たままになっていたので、常に sabbrev expansion が失敗した取り扱いになってい + て、然し sabbrev expansion は inline で _ble_edit_str を書き換えるのでその + まま動いている感じになっていたという事。 + + * is_line_expanded=1 を設定していたが、これだとインラインで展開してコマンド + ラインが既に書き換わっているのに改めて展開結果を表示する形になって変であ + る。然しだからと言って _ble_edit_str を展開前に保持したまま、展開結果を味 + 気ない [ble: expand] で表示するのも変な感じがする。なので + is_line_expanded=1 は履歴展開だけで有効にする事にした。 + + * verify の時に何も起こらずにその場で展開だけ起こるのも少し変な気がするので、 + 履歴展開と同様に次の行に移動する事にした。 + 2024-02-24 * util(joblist): 5.3 でまた色々沢山 foreground dead jobs が表示される [#D2174] diff --git a/src/edit.sh b/src/edit.sh index efa51312..166e9ab9 100644 --- a/src/edit.sh +++ b/src/edit.sh @@ -106,6 +106,12 @@ bleopt/declare -v edit_magic_opts '' ## when "history" is specified here, or the shell option `set -H` (or `set ## -o histexpand`) is specified. ## +## @opt history-inline +## By default, the result of the history expansion is not applied to the +## command line, but it is printed in the form "[ble: expand] " mimicking the behavior of Bash/Readline. When this option is +## specified, the result of the history expansion replaces the content of +## the command line. ## @opt verify ## When any expansion (except for the history expansion) is performed and ## the command line is changed, we update the change in the command line. @@ -113,7 +119,14 @@ bleopt/declare -v edit_magic_opts '' ## line. This is similar to "shopt -s histverify" for the shell's history ## expansions. For the history expansion, "shopt -s histverify" is ## referenced instead. -bleopt/declare -v edit_magic_accept '' +## @opt verify-syntax +## If this option is specified, when any expansions change the command, the +## syntax check for the resulting command is performed. If the expanded +## result is not a complete shell command, the command execution is +## canceled so the user can modify the expanded command. This implies +## "history-inline". +## +bleopt/declare -v edit_magic_accept 'verify-syntax' function ble/edit/use-textmap { ble/textmap#is-up-to-date && return 0 @@ -7523,6 +7536,20 @@ function ble/widget/accept-line/.is-mc-init { function ble/widget/accept-line { ble/decode/widget/keymap-dispatch "$@" } + +## @fn ble/widget/default/accept-line/.prepare-verify new_str new_ind +## @var[in] old_str old_ind +function ble/widget/default/accept-line/.prepare-verify { + local new_str=$1 new_ind=$2 + ble-edit/content/reset-and-check-dirty "$old_str" + _ble_edit_ind=$old_ind + _ble_edit_line_disabled=1 ble/widget/.insert-newline keep-info + ble-edit/content/reset-and-check-dirty "$new_str" + _ble_edit_ind=$new_ind + _ble_edit_mark=0 + _ble_edit_mark_active= + return 0 +} function ble/widget/default/accept-line { # 文法的に不完全の時は改行挿入 # Note: mc (midnight commander) が改行を含むコマンドを書き込んでくる #D1392 @@ -7548,11 +7575,16 @@ function ble/widget/default/accept-line { fi local is_line_expanded= + local orig_str=$_ble_edit_str orig_ind=$_ble_edit_ind # 静的略語展開 if [[ :$bleopt_edit_magic_accept: == *:sabbrev:* ]]; then - if ble/complete/sabbrev/expand type-status; then - [[ :$bleopt_edit_magic_accept: == *:verify:* ]] && return 0 + local old_str=$_ble_edit_str old_ind=$_ble_edit_ind + if ble/complete/sabbrev/expand; then + if [[ :$bleopt_edit_magic_accept: == *:verify:* ]]; then + ble/widget/default/accept-line/.prepare-verify "$_ble_edit_str" "$_ble_edit_ind" + return 0 + fi command=$_ble_edit_str is_line_expanded=1 elif (($?==147)); then @@ -7562,8 +7594,12 @@ function ble/widget/default/accept-line { # エイリアス展開 if [[ :$bleopt_edit_magic_accept: == *:alias:* ]]; then + local old_str=$_ble_edit_str old_ind=$_ble_edit_ind if ble/complete/alias/expand; then - [[ :$bleopt_edit_magic_accept: == *:verify:* ]] && return 0 + if [[ :$bleopt_edit_magic_accept: == *:verify:* ]]; then + ble/widget/default/accept-line/.prepare-verify "$_ble_edit_str" "$_ble_edit_ind" + return 0 + fi command=$_ble_edit_str is_line_expanded=1 fi @@ -7571,6 +7607,7 @@ function ble/widget/default/accept-line { # 履歴展開 if [[ -o histexpand || :$bleopt_edit_magic_accept: == *:history:* ]]; then + local old_str=$_ble_edit_str old_ind=$_ble_edit_ind if local ret; ble/edit/histexpand "$command"; then local expanded=$ret else @@ -7582,19 +7619,31 @@ function ble/widget/default/accept-line { if [[ $expanded != "$command" ]]; then if shopt -q histverify &>/dev/null; then - _ble_edit_line_disabled=1 ble/widget/.insert-newline keep-info - ble-edit/content/reset-and-check-dirty "$expanded" - _ble_edit_ind=${#expanded} - _ble_edit_mark=0 - _ble_edit_mark_active= + ble/widget/default/accept-line/.prepare-verify "$expanded" "${#expanded}" return 0 fi - command=$expanded is_line_expanded=1 + command=$expanded + if [[ :$bleopt_edit_magic_accept: == *:history-inline:* ]]; then + ble-edit/content/reset-and-check-dirty "$command" + _ble_edit_ind=${#command} + fi fi fi + if [[ $is_line_expanded && :$bleopt_edit_magic_accept: == *:verify-syntax:* ]]; then + if [[ $command != "$_ble_edit_str" ]]; then + ble-edit/content/reset-and-check-dirty "$command" + _ble_edit_ind=${#command} + fi + ble-edit/content/update-syntax + if ! ble/syntax:bash/is-complete; then + local old_str=$orig_str old_ind=$orig_ind + ble/widget/default/accept-line/.prepare-verify "$_ble_edit_str" "$_ble_edit_ind" + return 0 + fi + fi # 実行を登録 local old_cmd=$_ble_edit_CMD @@ -7603,8 +7652,10 @@ function ble/widget/default/accept-line { # 編集文字列を履歴に追加 ble/history/add "$command" + local show_expanded + [[ $command != "$_ble_edit_str" ]] && show_expanded=1 _ble_edit_CMD=$old_cmd ble/widget/.newline # #D1800 register - if [[ $is_line_expanded ]]; then + if [[ $show_expanded ]]; then local ret ble/edit/marker#instantiate 'expand' non-empty ble/util/buffer.print "$ret $command"