diff --git a/docs/ChangeLog.md b/docs/ChangeLog.md index 8a4adb98..e50e4980 100644 --- a/docs/ChangeLog.md +++ b/docs/ChangeLog.md @@ -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 diff --git a/note.txt b/note.txt index 30bc16de..1c1065ab 100644 --- a/note.txt +++ b/note.txt @@ -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] diff --git a/src/history.sh b/src/history.sh index 78f8b850..0cd11958 100644 --- a/src/history.sh +++ b/src/history.sh @@ -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 は発生し # ない様なのでそれに倣う。