Skip to content

Commit

Permalink
history: support "HISTCONTROL=strip"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Sep 16, 2022
1 parent 39efcf9 commit 021e033
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/ChangeLog.md
Expand Up @@ -108,6 +108,7 @@
- history: support `bleopt history_erasedups_limit` (motivated by SuperSandro2000) `#D1822` e4afb5a 3110967
- prompt: support `bleopt prompt_{emacs,vi}_mode_indicator` (motivated by ferdinandyb) `#D1843` 2b905f8
- util (`ble-import`): support option `-q` `#D1859` 1ca87a9
- history: support extension `HISTCONTROL=strip` `#D1874` xxxxxxx

## Changes

Expand Down
11 changes: 11 additions & 0 deletions note.txt
Expand Up @@ -6554,6 +6554,17 @@ bash_tips
Done (実装ログ)
-------------------------------------------------------------------------------

2022-09-13

* HISTCONTROL に trim 的な物を追加しても良いのではないか [#D1874]
https://github.com/akinomyoga/ble.sh/issues/226#issuecomment-1243759012

bash のソースを見たら stringlib.c に strip_{leading,trailing} というのがあ
るので trim ではなく strip を名前として使う事にする。

取り敢えず bash の patch も作ってみる事にする。
https://gitlab.com/akinomyoga/bash/-/commit/d43c167e9ec150d1fe4a730475a590d48a4cc9cd

2022-09-06

* decode: ble-bind --cursor で設定したカーソルが反映されない [#D1873]
Expand Down
38 changes: 27 additions & 11 deletions src/history.sh
Expand Up @@ -1666,22 +1666,38 @@ function ble/builtin/history/option:s {
for pat in "${pats[@]}"; do
[[ $cmd == $pat ]] && return 0
done
# Note: 以降の処理では HISTIGNORE は無視する。trim した後のコマンドに対して
# 改めて作用するのを防ぐ為。
local HISTIGNORE=
fi

# Note: ble/builtin/history/erasedups によって後の builtin history -s の為に
# 時的に erasedups を除去する場合がある為ローカル変数に変えておく。また、
# ignoreboth の処理の便宜の為にも内部的に書き換える。
local HISTCONTROL=$HISTCONTROL

# Note: HISTIGNORE 及び ignorespace は trim 前に処理する。何故なら行頭の空白
# などに意味を持たせたいから。ignoredups 及び erasedups は trim 後に作用させ
# る。何故なら実際に履歴に登録されたコマンドと比較したいから。
if [[ $HISTCONTROL ]]; then
[[ :$HISTCONTROL: == *:ignoreboth:* ]] &&
HISTCONTROL=$HISTCONTROL:ignorespace:ignoredups
if [[ :$HISTCONTROL: == *:ignorespace:* ]]; then
[[ $cmd == [' ']* ]] && return 0
fi

if [[ :$HISTCONTROL: == *:strip:* ]]; then
local ret
ble/string#rtrim "$cmd"
ble/string#match "$ret" $'^[ \t]*(\n([ \t]*\n)*)?'
cmd=${ret:${#BASH_REMATCH}}
[[ $BASH_REMATCH == *$'\n'* && $cmd == *$'\n'* ]] && cmd=$'\n'$cmd
fi
fi

local use_bash300wa=
if [[ $_ble_history_load_done ]]; then
if [[ $HISTCONTROL ]]; then
# Note: ble/builtin/history/erasedups によって後の builtin history -s の為
# に時的に erasedups を除去する場合がある為ローカル変数に変えておく。また、
# ignoreboth の処理の便宜の為にも内部的に書き換える。
local HISTCONTROL=$HISTCONTROL

[[ :$HISTCONTROL: == *:ignoreboth:* ]] &&
HISTCONTROL=$HISTCONTROL:ignorespace:ignoredups

if [[ :$HISTCONTROL: == *:ignorespace:* ]]; then
[[ $cmd == [' ']* ]] && return 0
fi
if [[ :$HISTCONTROL: == *:ignoredups:* ]]; then
# Note: plain Bash では ignoredups を検出した時には erasedups は発生し
# ない様なのでそれに倣う。
Expand Down

0 comments on commit 021e033

Please sign in to comment.