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
1 changed files with 44 additions and 26 deletions

View File

@ -32,7 +32,7 @@
#
# Copyright (c) 2014 Jim Hester
#
# This software is provided 'as-is', without any express or implied warranty.
# This software is provided 'as-is', without any express or implied warranty.
# In no event will the authors be held liable for any damages arising from the
# use of this software.
#
@ -57,6 +57,7 @@
#-------------------------------------------------------------------------------
[[ -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'
#-------------------------------------------------------------------------------
@ -66,9 +67,11 @@
function per-directory-history-toggle-history() {
if [[ $_per_directory_history_is_global == true ]]; then
_per-directory-history-set-directory-history
_per_directory_history_is_global=false
print -n "\nusing local history"
else
_per-directory-history-set-global-history
_per_directory_history_is_global=true
print -n "\nusing global history"
fi
zle .push-line
@ -114,43 +117,58 @@ function _per-directory-history-addhistory() {
true
else
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
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() {
if [[ $_per_directory_history_is_global == true ]]; then
fc -AI $HISTFILE
local original_histsize=$HISTSIZE
HISTSIZE=0
HISTSIZE=$original_histsize
if [[ -e "$_per_directory_history_directory" ]]; then
fc -R "$_per_directory_history_directory"
fi
fc -AI $HISTFILE
local original_histsize=$HISTSIZE
HISTSIZE=0
HISTSIZE=$original_histsize
if [[ -e "$_per_directory_history_directory" ]]; then
fc -R "$_per_directory_history_directory"
fi
_per_directory_history_is_global=false
}
function _per-directory-history-set-global-history() {
if [[ $_per_directory_history_is_global == false ]]; then
fc -AI $_per_directory_history_directory
local original_histsize=$HISTSIZE
HISTSIZE=0
HISTSIZE=$original_histsize
if [[ -e "$HISTFILE" ]]; then
fc -R "$HISTFILE"
fi
fi
_per_directory_history_is_global=true
}
function _per-directory-history-set-global-history() {
fc -AI $_per_directory_history_directory
local original_histsize=$HISTSIZE
HISTSIZE=0
HISTSIZE=$original_histsize
if [[ -e "$HISTFILE" ]]; then
fc -R "$HISTFILE"
fi
}
mkdir -p ${_per_directory_history_directory:h}
#add functions to the exec list for chpwd and zshaddhistory
autoload -U add-zsh-hook
add-zsh-hook chpwd _per-directory-history-change-directory
add-zsh-hook zshaddhistory _per-directory-history-addhistory
add-zsh-hook precmd _per-directory-history-precmd
#start in directory mode
mkdir -p ${_per_directory_history_directory:h}
_per_directory_history_is_global=true
_per-directory-history-set-directory-history
# set initialized flag to false
_per_directory_history_initialized=false