Skip to content

Commit

Permalink
edit: support "bleopt edit_magic_accept=verify-syntax"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Feb 25, 2024
1 parent a12deda commit ac84c15
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 14 deletions.
7 changes: 6 additions & 1 deletion blerc.template
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions docs/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions note.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 を参照するのも忍びない。かと言っ
Expand Down Expand Up @@ -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]
Expand Down
73 changes: 62 additions & 11 deletions src/edit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,27 @@ 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] <expanded
## command>" 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.
## We cancel accept-line and let the user continue to edit the command
## 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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -7562,15 +7594,20 @@ 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
fi

# 履歴展開
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
Expand All @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit ac84c15

Please sign in to comment.