Skip to content

Commit

Permalink
edit: revert edits with widget "discard-line"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Dec 20, 2024
1 parent 86cbf78 commit 3b2b4b8
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
- highlight (`ble/syntax/highlight/vartype`): reference the saved states of variables `#D2268` 063249b4
- complete: attempt pathname expansions of incomplete pattern for `COMPV` (reported by mcepl) `#D2278` 6a426954
- make: save commit id and branch name with `git archive` (requested by LecrisUT, blackteahamburger) `#D2290` 31f264ad
- edit: revert edits with widget `discard-line` (reported by dezza) `#D2301` xxxxxxxx

## Fixes

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

2024-12-18

* edit: C-c 後の履歴位置が変だ (reported by dezza)
https://github.com/akinomyoga/ble.sh/discussions/540

C-c を実行した後は C-m を実行した時と同様に全ての編集をクリアして、履歴位置
は履歴の末端に移動するべきである。

2024-12-09

* highlight: cd を bind -x 経由で実行するとコマンドキャッシュがクリアされないので
Expand Down Expand Up @@ -7711,6 +7703,12 @@ bash_tips

2024-12-20

* edit: C-c 後の履歴位置が変だ (reported by dezza) [#D2301]
https://github.com/akinomyoga/ble.sh/discussions/540

C-c を実行した後は C-m を実行した時と同様に全ての編集をクリアして、履歴位置
は履歴の末端に移動するべきである。

* global: rename "ret" not used as REPLY [#D2300]
Ref #T0009

Expand Down
1 change: 1 addition & 0 deletions src/edit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7853,6 +7853,7 @@ function ble/widget/discard-line {
[[ $bleopt_history_share ]] && ble/builtin/history/option:n
_ble_edit_line_disabled=1 ble/textarea#render leave
ble/widget/.newline keep-info
ble/history/revert-edits
ble/textarea#render
}

Expand Down
60 changes: 37 additions & 23 deletions src/history.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,42 @@ function ble/history/set-edited-entry {
builtin eval -- "${code//PREFIX/${_ble_history_prefix:-_ble}}"
}


## @fn ble/history/revert-edits
## This function reverts the temporary editing of the current history.
function ble/history/revert-edits {
if [[ $_ble_history_prefix ]]; then
local code='
# PREFIX_history_edit を未編集状態に戻す
local index
for index in "${!PREFIX_history_dirt[@]}"; do
PREFIX_history_edit[index]=${PREFIX_history[index]}
done
PREFIX_history_dirt=()
local topIndex=${#PREFIX_history[@]}
_ble_history_COUNT=$topIndex
_ble_history_INDEX=$topIndex'
builtin eval -- "${code//PREFIX/$_ble_history_prefix}"
else
if [[ $_ble_history_load_done ]]; then
# 登録・不登録に拘わらず取り敢えず初期化
_ble_history_index=${#_ble_history[@]}
ble/history/.update-position

# _ble_history_edit を未編集状態に戻す
local index
for index in "${!_ble_history_dirt[@]}"; do
_ble_history_edit[index]=${_ble_history[index]}
done
_ble_history_dirt=()

# 同時に _ble_edit_undo も初期化する。
ble-edit/undo/clear-all
fi
fi
}

## @fn ble/history/.add-command-history command
## @var[in,out] HISTINDEX_NEXT
## used by ble/widget/accept-and-next to get modified next-entry positions
Expand All @@ -2007,22 +2043,6 @@ function ble/history/.add-command-history {
# Note: mc (midnight commander) が初期化スクリプトを送ってくる #D1392
[[ $MC_SID == $$ && $_ble_edit_LINENO -le 2 && ( $1 == *PROMPT_COMMAND=* || $1 == *PS1=* ) ]] && return 1

if [[ $_ble_history_load_done ]]; then
# 登録・不登録に拘わらず取り敢えず初期化
_ble_history_index=${#_ble_history[@]}
ble/history/.update-position

# _ble_history_edit を未編集状態に戻す
local index
for index in "${!_ble_history_dirt[@]}"; do
_ble_history_edit[index]=${_ble_history[index]}
done
_ble_history_dirt=()

# 同時に _ble_edit_undo も初期化する。
ble-edit/undo/clear-all
fi

if [[ $bleopt_history_share ]]; then
ble/builtin/history/option:n
ble/builtin/history/option:s "$1"
Expand All @@ -2037,15 +2057,9 @@ function ble/history/add {
local command=$1
((bleopt_history_limit_length>0&&${#command}>bleopt_history_limit_length)) && return 1

ble/history/revert-edits
if [[ $_ble_history_prefix ]]; then
local code='
# PREFIX_history_edit を未編集状態に戻す
local index
for index in "${!PREFIX_history_dirt[@]}"; do
PREFIX_history_edit[index]=${PREFIX_history[index]}
done
PREFIX_history_dirt=()
local topIndex=${#PREFIX_history[@]}
PREFIX_history[topIndex]=$command
PREFIX_history_edit[topIndex]=$command
Expand Down

0 comments on commit 3b2b4b8

Please sign in to comment.