Skip to content

Commit

Permalink
history: refactor history hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Aug 24, 2022
1 parent 1ca87a9 commit c393c93
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 95 deletions.
2 changes: 2 additions & 0 deletions docs/ChangeLog.md
Expand Up @@ -427,6 +427,8 @@
- util (`fd#alloc`): limit the search range of free fds `#D1813` 43be0e4 4c90072
- github/workflows: define an action for the nightly builds (contributed by uyha) `#D1814` a3082a0
- global: quote numbers for unexpected `IFS` `#D1835` 0179afc
- history: refactor hooks `history_{{delete,clear,insert} => change}` `#D1860` XXXXXXX
- history: rename the hook `history_{on => }leave` `#D1860` XXXXXXX

## Contrib

Expand Down
110 changes: 58 additions & 52 deletions keymap/vi.sh
Expand Up @@ -2416,7 +2416,7 @@ ble/array#push _ble_textarea_local_VARNAMES \
#

ble/array#push _ble_edit_dirty_observer ble/keymap:vi/mark/shift-by-dirty-range
blehook history_onleave+=ble/keymap:vi/mark/history-onleave.hook
blehook history_leave+=ble/keymap:vi/mark/history-onleave.hook

## @fn ble/keymap:vi/mark/history-onleave.hook
function ble/keymap:vi/mark/history-onleave.hook {
Expand Down Expand Up @@ -2454,63 +2454,69 @@ function ble/keymap:vi/mark/update-mark-history {
_ble_keymap_vi_mark_hindex=$h
fi
}
blehook history_clear+=ble/keymap:vi/mark/history-clear.hook
blehook history_delete+=ble/keymap:vi/mark/history-delete.hook
blehook history_insert+=ble/keymap:vi/mark/history-insert.hook
function ble/keymap:vi/mark/history-clear.hook {
_ble_keymap_vi_mark_global=()
_ble_keymap_vi_mark_history=()
_ble_keymap_vi_mark_hindex=
}
## @fn ble/keymap:vi/mark/history-delete.hook index...
blehook history_change+=ble/keymap:vi/mark/history-change.hook
## @fn ble/keymap:vi/mark/history-change.hook 'delete' index...
## @fn ble/keymap:vi/mark/history-change.hook 'clear'
## @fn ble/keymap:vi/mark/history-change.hook 'insert' beg len
## @param[in] index...
## 昇順に並んでいる事と重複がない事を仮定する。
function ble/keymap:vi/mark/history-delete.hook {
# update _ble_keymap_vi_mark_global
for imark in "${!_ble_keymap_vi_mark_global[@]}"; do
local value=${_ble_keymap_vi_mark_global[imark]}
local h=${value%%:*} v=${value#*:}
local idel shift=0
for idel; do
if [[ $idel == *-* ]]; then
local b=${idel%-*} e=${idel#*-}
((b<=h&&h<e)) && shift= # delete
((h<e)) && break
((shift+=e-b))
else
((idel==h)) && shift= # delete
((idel>=h)) && break
((shift++))
fi
## 削除する項目の番号を指定します。昇順に並んでいる事と重複がない事を仮定します。
## @param[in] beg len
## 挿入位置と挿入項目の個数を指定します。
function ble/keymap:vi/mark/history-change.hook {
local kind=$1; shift
case $kind in
(delete)
# update _ble_keymap_vi_mark_global
local imark
for imark in "${!_ble_keymap_vi_mark_global[@]}"; do
local value=${_ble_keymap_vi_mark_global[imark]}
local h=${value%%:*} v=${value#*:}
local idel shift=0
for idel; do
if [[ $idel == *-* ]]; then
local b=${idel%-*} e=${idel#*-}
((b<=h&&h<e)) && shift= # delete
((h<e)) && break
((shift+=e-b))
else
((idel==h)) && shift= # delete
((idel>=h)) && break
((shift++))
fi
done
[[ $shift ]] &&
_ble_keymap_vi_mark_global[imark]=$((h-shift)):$v
done
[[ $shift ]] &&
_ble_keymap_vi_mark_global[imark]=$((h-shift)):$v
done

# update _ble_keymap_vi_mark_history
ble/builtin/history/array#delete-hindex _ble_keymap_vi_mark_history "$@"
# update _ble_keymap_vi_mark_history
ble/builtin/history/array#delete-hindex _ble_keymap_vi_mark_history "$@"

# reset _ble_keymap_vi_mark_hindex
_ble_keymap_vi_mark_hindex=
}
## @fn ble/keymap:vi/mark/history-insert.hook beg len
## @param[in] beg len
function ble/keymap:vi/mark/history-insert.hook {
local beg=$1 len=$2
# reset _ble_keymap_vi_mark_hindex
_ble_keymap_vi_mark_hindex= ;;

# update _ble_keymap_vi_mark_global
for imark in "${!_ble_keymap_vi_mark_global[@]}"; do
local value=${_ble_keymap_vi_mark_global[imark]}
(clear)
_ble_keymap_vi_mark_global=()
_ble_keymap_vi_mark_history=()
_ble_keymap_vi_mark_hindex= ;;

local h=${value%%:*} v=${value#*:}
((h>=beg)) && _ble_keymap_vi_mark_global[imark]=$((h+len)):$v
done
(insert)
local beg=$1 len=$2

# update _ble_keymap_vi_mark_history
ble/builtin/history/array#insert-range _ble_keymap_vi_mark_history "$@"
# update _ble_keymap_vi_mark_global
local imark
for imark in "${!_ble_keymap_vi_mark_global[@]}"; do
local value=${_ble_keymap_vi_mark_global[imark]}

# reset _ble_keymap_vi_mark_hindex
_ble_keymap_vi_mark_hindex=
local h=${value%%:*} v=${value#*:}
((h>=beg)) && _ble_keymap_vi_mark_global[imark]=$((h+len)):$v
done

# update _ble_keymap_vi_mark_history
ble/builtin/history/array#insert-range _ble_keymap_vi_mark_history "$@"

# reset _ble_keymap_vi_mark_hindex
_ble_keymap_vi_mark_hindex= ;;
esac
}

function ble/keymap:vi/mark/shift-by-dirty-range {
Expand Down Expand Up @@ -3448,7 +3454,7 @@ function ble/widget/vi_nmap/pagedown {
ble/widget/vi-command/bell
return 1
fi

# 行き先を決定
local vheight=$((height-_ble_textmap_begy-1))
local ybase=$((_ble_textarea_scroll_new+height-1))
Expand Down Expand Up @@ -8082,7 +8088,7 @@ function ble/keymap:vi/async-commandline-mode {

# edit/undo
ble-edit/undo/clear-all

# edit/history
ble/history/set-prefix _ble_keymap_vi_cmap

Expand Down
2 changes: 1 addition & 1 deletion lib/core-complete.sh
Expand Up @@ -6168,7 +6168,7 @@ function ble/complete/menu/clear {
return 0
}
blehook widget_bell+=ble/complete/menu/clear
blehook history_onleave+=ble/complete/menu/clear
blehook history_leave+=ble/complete/menu/clear

## @fn ble/complete/menu/get-footprint
## @var[out] footprint
Expand Down
10 changes: 10 additions & 0 deletions note.txt
Expand Up @@ -6636,6 +6636,16 @@ bash_tips

2022-08-21

* history: blehook history_* を blehook history_onchange に統合する [#D1860]

history_{delete,insert,clear} は何れも同じ箇所でセットで登録している。だと
したらわざわざ別の hook にしている意味もない。逆に一貫性を考えたら一つの登
録で全てを一貫した方法で処理しなければならない。

統合した hook の名前については結局 history_change にする事にした。結局 on
をつけ始めたら殆ど全てに on を付けなければならなく成るので余り付けても意味
がない。同様に history_onleave も history_leave に改名する事にする。

* util (ble-import): 調整する [#D1859]

* done: source 時の引数の継承についてチェックする
Expand Down
6 changes: 2 additions & 4 deletions src/def.sh
Expand Up @@ -40,10 +40,8 @@ blehook/declare color_setface_load

blehook/declare ADDHISTORY
blehook/declare history_reset_background
blehook/declare history_onleave
blehook/declare history_delete
blehook/declare history_insert
blehook/declare history_clear
blehook/declare history_leave
blehook/declare history_change
blehook/declare history_message

# edit.sh
Expand Down
32 changes: 16 additions & 16 deletions src/edit.sh
Expand Up @@ -7241,23 +7241,23 @@ function ble-edit/undo/clear-all {
_ble_edit_undo_history=()
_ble_edit_undo_hindex=
}
function ble-edit/undo/history-delete.hook {
ble/builtin/history/array#delete-hindex _ble_edit_undo_history "$@"
_ble_edit_undo_hindex=
}
function ble-edit/undo/history-clear.hook {
ble-edit/undo/clear-all
}
function ble-edit/undo/history-insert.hook {
ble/builtin/history/array#insert-range _ble_edit_undo_history "$@"
local beg=$1 len=$2
[[ $_ble_edit_undo_hindex ]] &&
((_ble_edit_undo_hindex>=beg)) &&
((_ble_edit_undo_hindex+=len))
function ble-edit/undo/history-change.hook {
local kind=$1; shift
case $kind in
(delete)
ble/builtin/history/array#delete-hindex _ble_edit_undo_history "$@"
_ble_edit_undo_hindex= ;;
(clear)
ble-edit/undo/clear-all ;;
(insert)
ble/builtin/history/array#insert-range _ble_edit_undo_history "$@"
local beg=$1 len=$2
[[ $_ble_edit_undo_hindex ]] &&
((_ble_edit_undo_hindex>=beg)) &&
((_ble_edit_undo_hindex+=len)) ;;
esac
}
blehook history_delete+=ble-edit/undo/history-delete.hook
blehook history_clear+=ble-edit/undo/history-clear.hook
blehook history_insert+=ble-edit/undo/history-insert.hook
blehook history_change+=ble-edit/undo/history-change.hook

## @fn ble-edit/undo/.get-current-state
## @var[out] str ind
Expand Down
42 changes: 20 additions & 22 deletions src/history.sh
Expand Up @@ -523,7 +523,7 @@ function ble/history:bash/initialize {

# Note: 追加読み込みをした際に対応するデータを shift (history_share)
local delta=$((new_count-old_count))
((delta>0)) && blehook/invoke history_insert "$old_count" "$delta"
((delta>0)) && blehook/invoke history_change insert "$old_count" "$delta"
}

#------------------------------------------------------------------------------
Expand Down Expand Up @@ -879,9 +879,7 @@ function ble/builtin/history/.touch-histfile {
}

# in def.sh
# @hook history_delete
# @hook history_clear
# @hook history_message
# @hook history_change

# Note: #D1126 一度置き換えたら戻せない。二回は初期化しない。
if [[ ! ${_ble_builtin_history_initialized+set} ]]; then
Expand Down Expand Up @@ -1022,7 +1020,7 @@ function ble/builtin/history/.load-recent-entries {
((_ble_history_index==_ble_history_count)) && _ble_history_index=$ncount
_ble_history_count=$ncount
ble/history/.update-position
blehook/invoke history_insert "$ocount" "$delta"
blehook/invoke history_change insert "$ocount" "$delta"
}
## @fn ble/builtin/history/.read file [skip [fetch]]
function ble/builtin/history/.read {
Expand Down Expand Up @@ -1181,18 +1179,18 @@ function ble/builtin/history/array#insert-range {
for i in "${!out[@]}"; do ARR[i]=${out[i]}; done'
builtin eval -- "${script//ARR/$array_name}"
}
blehook history_delete+=ble/builtin/history/delete.hook
blehook history_clear+=ble/builtin/history/clear.hook
blehook history_insert+=ble/builtin/history/insert.hook
function ble/builtin/history/delete.hook {
ble/builtin/history/array#delete-hindex _ble_history_dirt "$@"
}
function ble/builtin/history/clear.hook {
_ble_history_dirt=()
}
function ble/builtin/history/insert.hook {
# Note: _ble_history, _ble_history_edit は別に更新される
ble/builtin/history/array#insert-range _ble_history_dirt "$@"
blehook history_change+=ble/builtin/history/change.hook
function ble/builtin/history/change.hook {
local kind=$1; shift
case $kind in
(delete)
ble/builtin/history/array#delete-hindex _ble_history_dirt "$@" ;;
(clear)
_ble_history_dirt=() ;;
(insert)
# Note: _ble_history, _ble_history_edit は別に更新される
ble/builtin/history/array#insert-range _ble_history_dirt "$@" ;;
esac
}
## @fn ble/builtin/history/option:c
function ble/builtin/history/option:c {
Expand All @@ -1212,7 +1210,7 @@ function ble/builtin/history/option:c {
_ble_history_count=
fi
ble/history/.update-position
blehook/invoke history_clear
blehook/invoke history_change clear
fi
}
## @fn ble/builtin/history/option:d index
Expand Down Expand Up @@ -1242,7 +1240,7 @@ function ble/builtin/history/option:d {
if [[ $_ble_history_load_done ]]; then
local N=${#_ble_history[@]}
local b=$((beg-1+N-max)) e=$((end+N-max))
blehook/invoke history_delete "$b-$e"
blehook/invoke history_change delete "$b-$e"
if ((_ble_history_index>=e)); then
((_ble_history_index-=e-b))
elif ((_ble_history_index>=b)); then
Expand Down Expand Up @@ -1644,7 +1642,7 @@ function ble/builtin/history/erasedups {
fi

if ((${#delete_indices[@]})); then
blehook/invoke history_delete "${delete_indices[@]}"
blehook/invoke history_change delete "${delete_indices[@]}"
((_ble_builtin_history_wskip-=shift_wskip))
[[ ${HISTINDEX_NEXT+set} ]] && ((HISTINDEX_NEXT-=shift_histindex_next))
else
Expand Down Expand Up @@ -1900,10 +1898,10 @@ function ble/history/update-position {
ble/history/.update-position
}

## @hook history_onleave (defined in def.sh)
## @hook history_leave (defined in def.sh)

function ble/history/onleave.fire {
blehook/invoke history_onleave "$@"
blehook/invoke history_leave "$@"
}

## called by ble-edit/initialize in Bash 3
Expand Down

0 comments on commit c393c93

Please sign in to comment.