fix(per-directory-history): fix use of global history on shell start (#9008)

Updated from upstream: https://github.com/jimhester/per-directory-history/blob/d2e291d/per-directory-history.zsh

Fixes #9007
Closese #9008

Co-authored-by: Sebastian Marsching <sebastian-git-2016@marsching.com>
This commit is contained in:
Marc Cornellà 2021-10-04 17:14:34 +02:00
parent be4a952972
commit 568584a9f5
No known key found for this signature in database
GPG Key ID: 0314585E776A9C1B

View File

@ -57,6 +57,7 @@
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
[[ -z $HISTORY_BASE ]] && HISTORY_BASE="$HOME/.directory_history" [[ -z $HISTORY_BASE ]] && HISTORY_BASE="$HOME/.directory_history"
[[ -z $HISTORY_START_WITH_GLOBAL ]] && HISTORY_START_WITH_GLOBAL=false
[[ -z $PER_DIRECTORY_HISTORY_TOGGLE ]] && PER_DIRECTORY_HISTORY_TOGGLE='^G' [[ -z $PER_DIRECTORY_HISTORY_TOGGLE ]] && PER_DIRECTORY_HISTORY_TOGGLE='^G'
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
@ -66,9 +67,11 @@
function per-directory-history-toggle-history() { function per-directory-history-toggle-history() {
if [[ $_per_directory_history_is_global == true ]]; then if [[ $_per_directory_history_is_global == true ]]; then
_per-directory-history-set-directory-history _per-directory-history-set-directory-history
_per_directory_history_is_global=false
print -n "\nusing local history" print -n "\nusing local history"
else else
_per-directory-history-set-global-history _per-directory-history-set-global-history
_per_directory_history_is_global=true
print -n "\nusing global history" print -n "\nusing global history"
fi fi
zle .push-line zle .push-line
@ -114,13 +117,32 @@ function _per-directory-history-addhistory() {
true true
else else
print -Sr -- "${1%%$'\n'}" print -Sr -- "${1%%$'\n'}"
# instantly write history if set options require it.
if [[ -o share_history ]] || \
[[ -o inc_append_history ]] || \
[[ -o inc_append_history_time ]]; then
fc -AI $HISTFILE
fc -AI $_per_directory_history_directory
fi
fc -p $_per_directory_history_directory fc -p $_per_directory_history_directory
fi fi
} }
function _per-directory-history-precmd() {
if [[ $_per_directory_history_initialized == false ]]; then
_per_directory_history_initialized=true
if [[ $HISTORY_START_WITH_GLOBAL == true ]]; then
_per-directory-history-set-global-history
_per_directory_history_is_global=true
else
_per-directory-history-set-directory-history
_per_directory_history_is_global=false
fi
fi
}
function _per-directory-history-set-directory-history() { function _per-directory-history-set-directory-history() {
if [[ $_per_directory_history_is_global == true ]]; then
fc -AI $HISTFILE fc -AI $HISTFILE
local original_histsize=$HISTSIZE local original_histsize=$HISTSIZE
HISTSIZE=0 HISTSIZE=0
@ -128,11 +150,9 @@ function _per-directory-history-set-directory-history() {
if [[ -e "$_per_directory_history_directory" ]]; then if [[ -e "$_per_directory_history_directory" ]]; then
fc -R "$_per_directory_history_directory" fc -R "$_per_directory_history_directory"
fi fi
fi
_per_directory_history_is_global=false
} }
function _per-directory-history-set-global-history() { function _per-directory-history-set-global-history() {
if [[ $_per_directory_history_is_global == false ]]; then
fc -AI $_per_directory_history_directory fc -AI $_per_directory_history_directory
local original_histsize=$HISTSIZE local original_histsize=$HISTSIZE
HISTSIZE=0 HISTSIZE=0
@ -140,17 +160,15 @@ function _per-directory-history-set-global-history() {
if [[ -e "$HISTFILE" ]]; then if [[ -e "$HISTFILE" ]]; then
fc -R "$HISTFILE" fc -R "$HISTFILE"
fi fi
fi
_per_directory_history_is_global=true
} }
mkdir -p ${_per_directory_history_directory:h}
#add functions to the exec list for chpwd and zshaddhistory #add functions to the exec list for chpwd and zshaddhistory
autoload -U add-zsh-hook autoload -U add-zsh-hook
add-zsh-hook chpwd _per-directory-history-change-directory add-zsh-hook chpwd _per-directory-history-change-directory
add-zsh-hook zshaddhistory _per-directory-history-addhistory add-zsh-hook zshaddhistory _per-directory-history-addhistory
add-zsh-hook precmd _per-directory-history-precmd
#start in directory mode # set initialized flag to false
mkdir -p ${_per_directory_history_directory:h} _per_directory_history_initialized=false
_per_directory_history_is_global=true
_per-directory-history-set-directory-history