Skip to content

Commit

Permalink
keymap.vi: handle history_default_point in vi motion
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Dec 31, 2024
1 parent 89c6907 commit 2a3351e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 26 deletions.
5 changes: 3 additions & 2 deletions blerc.template
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,10 @@
## is specified, te cursor is placed at the same column as before moving the
## history entry. When the versions without "graphical" or "logical" is used,
## a logical or graphical line is used based on "bleopt edit_line_type". The
## default is "end-of-line".
## default is "end-of-line". When "auto" is specified, the behavior is "end"
## by default, but it becomes similar to "near" when called by vi motion.

#bleopt history_default_point=
#bleopt history_default_point=auto


## The following setting controls the history sharing. If it has non-empty
Expand Down
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
- complete: support completion for `execute-named-command` `#D2288` 4fee44e6
- complete: support `ble-face menu_complete_{match,selected}` (requested by simonLeary42) `#D2291` 31f264ad
- edit: support `bleopt history_default_point={preserve,begin,end,near,far,{beginning,end}-of-line,preserve-column,...}` (requested by miltieIV2) `#D2297` 37291ff1
- edit: support `bleopt history_default_point=auto` (reported by miltieIV2) `#D2297` xxxxxxxx
- edit: support `bleopt undo_point={first,last,near,auto}` `#D2303` 99af0ece
- keymap/vi: add readline-compatible widgets for `vi_imap` and `vi_nmap` (requested by excited-bore) `#D2304` d7ec488a
- edit: support bash-5.2 readline bindable function `vi-edit-and-execute-command` `#D2306` c395eb33
Expand Down
41 changes: 21 additions & 20 deletions lib/keymap.vi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3111,36 +3111,37 @@ function ble/widget/vi-command/.history-relative-line {
local index histsize
ble/history/get-index
ble/history/get-count -v histsize
local index0=$index

local ret count=$((offset<0?-offset:offset)) exit=1
local ret count=$((offset<0?-offset:offset))
((count--))
while ((count>=0)); do
if ((offset<0)); then
((index>0)) || return "$exit"
ble/widget/history-prev
ret=${#_ble_edit_str}
ble/keymap:vi/needs-eol-fix "$ret" && ((ret--))
_ble_edit_ind=$ret
((--index))
((index>=0)) || break
else
((index<histsize)) || return "$exit"
ble/widget/history-next
_ble_edit_ind=0
((++index))
((index<=histsize)) || break
fi
exit=0
ble/string#count-char "$_ble_edit_str" $'\n'; local nline=$((ret+1))
local entry; ble/history/get-edited-entry "$index"
ble/string#count-char "$entry" $'\n'; local nline=$((ret+1))
((count<nline)) && break
((count-=nline))
done

if ((count)); then
if ((offset<0)); then
ble-edit/content/find-logical-eol 0 "$((nline-count-1))"
ble/keymap:vi/needs-eol-fix "$ret" && ((ret--))
else
ble-edit/content/find-logical-bol 0 "$count"
fi
_ble_edit_ind=$ret
((index!=index0)) || return 1

if ((index<0)); then
# reached the beginning of history
ble-edit/history/goto 0 point=none
_ble_edit_ind=0
elif ((index>histsize)); then
# reached the end of history
ble-edit/history/goto "$histsize" point=none
_ble_edit_ind=${#_ble_edit_str}
else
ble-edit/history/goto "$index" linewise:default-point=near
fi
ble/keymap:vi/needs-eol-fix && ((_ble_edit_ind--))

return 0
}
Expand Down
20 changes: 20 additions & 0 deletions note.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8097,6 +8097,8 @@ bash_tips
* edit: 履歴移動を行った時のカーソル位置の設定 (requested by miltieIV2) [#D2297]
https://github.com/akinomyoga/ble.sh/issues/537

Note: これにより history_preserve_point -> history_default_point に変更した。

* fixed: 何と今まで "bleopt edit_line_type" が動いていなかった様だ。

* fixed: 何と今まで bleopt/check:input_encoding のエラーメッセージの一部が
Expand Down Expand Up @@ -8158,6 +8160,24 @@ bash_tips

その他は大体動いている様だ。

2024-12-31 実は keymap.vi の vi motion 経由で呼び出される履歴移動は
ble/widget/history-{prev,next} を用いていた為に、今回の新しいカーソル位置の
設定が適用されていなかった? 取り敢えず対応した。

? done: と思ったが、本来は ble/widget/history-{prev,next} が正しく位置を設
定する筈だったのでは…? 何故動いていないのだろう? 後で確認する必要がある。

→確認した。history-{prev,next} の直後にカーソル位置を手動で設定している
ので折角 ble/widget/history-{prev,next} がカーソル位置を設定してもそれが
上書きされていた。その処理を削除されたら期待通りの振る舞いになる事も確認
できた。然し、更に引数付きで履歴項目内を移動した時には何れにしてもカーソ
ル位置を調整する必要がある。そもそも処理をよく見てみると
ble/widget/history-{prev,next} を呼び出す意味はない。単に index を追跡し
てその都度 get-edited-entry で取り出した文字列に含まれる改行を数えれば良
いだけである。

最後に ble-edit/history/goto を一回呼び出すだけにする事にした。

2024-12-12

* style: prefix:$name の形をしている定義関数の呼び出しのクォートを調整 [#D2296]
Expand Down
16 changes: 12 additions & 4 deletions src/edit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8738,10 +8738,10 @@ function ble/widget/print-keyboard-macro {
#------------------------------------------------------------------------------
# **** history **** @history

bleopt/declare -v history_default_point 'end'
bleopt/declare -v history_default_point 'auto'
function bleopt/check:history_default_point {
case $value in
(begin|end|near|far|preserve) return 0 ;;
(begin|end|near|far|preserve|auto) return 0 ;;
(beginning-of-line|end-of-line|preserve-column) return 0 ;;
(beginning-of-logical-line|end-of-logical-line|preserve-logical-column) return 0 ;;
(beginning-of-graphical-line|end-of-graphical-line|preserve-graphical-column) return 0;;
Expand All @@ -8754,7 +8754,7 @@ function bleopt/check:history_default_point {
bleopt/declare -o history_preserve_point history_default_point
function bleopt/check:history_preserve_point {
case $value in
(begin|end|near|far|preserve) ;;
(begin|end|near|far|preserve|auto) ;;
(beginning-of-line|end-of-line|preserve-column) ;;
(beginning-of-logical-line|end-of-logical-line|preserve-logical-column) ;;
(beginning-of-graphical-line|end-of-graphical-line|preserve-graphical-column) ;;
Expand Down Expand Up @@ -8849,6 +8849,15 @@ function ble-edit/history/goto/.prepare-point {
[[ $ret ]] || ret=$bleopt_history_default_point
point=$ret

if [[ $point == auto ]]; then
ble/opts#extract-last-optarg "$point_opts" default-point
if [[ $ret ]]; then
point=$ret
else
point=end
fi
fi

case $point in
(near)
if [[ :$point_opts: == *:backward:* ]]; then
Expand Down Expand Up @@ -8913,7 +8922,6 @@ function ble-edit/history/goto/.prepare-point {

## @fn ble-edit/history/goto/.set-point [delta]
## @param[in] delta
## @var[in] index0 index1
## @var[in] point point_x point_opts
##
## @var[in] _ble_edit_str
Expand Down

0 comments on commit 2a3351e

Please sign in to comment.