Skip to content

Commit

Permalink
edit: adjust cursor position after "bind -x" in vi_nmap
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jan 16, 2025
1 parent 430a174 commit 36ab934
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
- complete: work around `mawk <= 1.3.4-20230525` type-inference bug (reported by KaKi87) `#D2295` 546499b5
- main: work around macOS sed (reported by Mossop) `#D2298` a16aa594
- main: delay attaching in kitty, Ghostty, and VS Code Terminal `#D2215` xxxxxxxx
- edit: adjust cursor position after `bind -x` in vi_nmap (requested by miltieIV2) `#D2317` xxxxxxxx

## Contrib

Expand Down
69 changes: 69 additions & 0 deletions note.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,10 @@ bash_tips
- make_command.sh の整理 (scan 分離, char_width 分離)
- note.txt -> memo.txt

2025-01-16

* README: ble-attach は startup files の最後でなければならないという話。

2024-12-24

* vscode における初期化問題
Expand Down Expand Up @@ -7728,6 +7732,71 @@ bash_tips
Done (実装ログ)
-------------------------------------------------------------------------------

2025-01-16

* vi_nmap: "bind -x" 実行後のカーソルの位置 (requested by miltieIV2) [#D2317]
https://github.com/akinomyoga/ble.sh/issues/547

bash ではカーソルを行末などに置いても勝手に調整を行う。

vi_nmap における complete の時には一体どうしていたか。d7ec488a を見ると特に
core-complete.sh の側では vi_nmap に対する特別の処理は実装していない様に見
える。menu_complete に入った後で行末で menu_complete を抜けた後のカーソル位
置を何処で調節しているのかが分からない。

* 実際に挿入が起こった時には ble/keymap:vi/complete/insert.hook 経由で調整
しているのかと思ったが、よく見ると別にカーソル位置の調整はここでは行って
いない様に見える。

* と思ったが、実は core-complete.sh の側でちゃんと vi_nmap に対する特別の処
理を含めている。c106239a で実はその処理が既に追加されていたのだった。

本当は edit.sh の中に特定の keymap に対する特別処理を余り追加したくないが、
その為には色々と hook を定義しなければならず微妙である。例えば "bind -x の
処理が終了した時に呼び出される hook" という物をわざわざ実装する必要があるか
という話になる。

因みに現在 edit.sh の中で明示的に vi_nmap を参照している箇所はどれぐらいあ
るだろうか。結構たくさんある。filter-word.impl, undo,
{start,end,print}-keyboard-macro, history-search, nsearch, ... うーん。もう
実装の分離は諦めて良い気がする。

次に直接 needs-eol-fix を呼び出すべきか、adjust-command-mode を一括で呼び出
すべきか。adjust-command-mode の処理の中には C-o に対する処理も含まれている。
つまり、単にカーソル位置を調節するだけでは不十分という事ではないだろうか。

? 一方で、vi_nmap の中で C-o に抗う様な物を実装する可能性はあるだろうか。普
通に考えたら C-o の vi_nmap の中で何か実行してそれに抗うというのは考えに
くい。と思ったが、例えば引数を追加するコマンドを bind -x の中で実行した場
合には、引数をそのままにして抜けてしまったら問題になる。うーん。そもそも
vi_nmap の中の全ての widget が最後に adjust-command-mode を実行するわけで
はないのだ。

やはり下手な調整は実行せずにカーソル位置の調整だけに留めるべきなのだろう
か。

* 然し現在の実装では実のところ clear-arg をコマンド実行後に呼び出している
のでそもそも bind -x の中で引数を追加する等のことは想定していない。その
様な事をしたいのであれば、ble.sh 専用の widget を使うべきという事。そも
そも Bash では (ble.sh の widget を呼び出して) 引数を調整することはでき
ないので Bash の bind -x 経由でそれを実行しようとするべきではない。Bash
でも ble.sh でも動く兼用の設定にしたいとしても、ble.sh では引数を設定し
て Bash ではそれをしないという様な機能というのも考えにくい。

という事なので、bind -x の中で引数を設定するだけとか register を設定する
だけとかそういう物を実装することは想定しないとして、無条件に
adjust-command-mode を呼び出すという形で良い事にする。

? 或いは、C-o に突入する様な機能を実装した時にそれが即座にキャンセルされる
様な振る舞いになって不都合ではないか。然し、C-o を自前で実装するという事
があるだろうか…。と思ったが、bind -x の関数の中で ble.sh の widget を順
番に呼び出して処理をするという可能性もある。という事を考えると C-o に突入
した直後はそのままという可能性もある。うーん。

もう一つの可能性は vi_nmap で始まって vi_nmap で終わったら
adjust-command-mode を呼び出すという事。それ以外の場合には何もしないかカー
ソル位置の調整だけ行うという可能性。うーん。取り敢えずこれで実装してみた。

2025-01-07

* integration/zoxide: compopt -o noquote は余分 (reported by tessus) [#D2316]
Expand Down
16 changes: 16 additions & 0 deletions src/edit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11950,6 +11950,22 @@ function ble/widget/.EDIT_COMMAND {
local N=${#_ble_edit_str}
((_ble_edit_ind<0?_ble_edit_ind=0:(_ble_edit_ind>N&&(_ble_edit_ind=N))))
((_ble_edit_mark<0?_ble_edit_mark=0:(_ble_edit_mark>N&&(_ble_edit_mark=N))))
if [[ $_ble_decode_keymap == vi_nmap ]]; then
if [[ $KEYMAP == vi_nmap ]]; then
# Note: If the command did not change the current keymap, we perform the
# adjustment for a normal command in vi_nmap. For example, this exits
# the single command mode by C-o. We do not support the shell commands
# that tries to adjust arguments and registers without exiting the single
# command mode. There are already limitations with such a command
# because we clear the arguments after evaluating the shell command.
ble/keymap:vi/adjust-command-mode
else
# If the current keymap became vi_nmap due to the command, we do not
# perform extra adjustment for C-o, etc. We just fix the current cursor
# position.
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))
fi
fi

return "$ext"
}
Expand Down

0 comments on commit 36ab934

Please sign in to comment.