From 568584a9f5f42d51a08de8b71347f6463b9211e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 4 Oct 2021 17:14:34 +0200 Subject: [PATCH] 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 --- .../per-directory-history.zsh | 70 ++++++++++++------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/plugins/per-directory-history/per-directory-history.zsh b/plugins/per-directory-history/per-directory-history.zsh index 41de2f91d..7cd673cdb 100644 --- a/plugins/per-directory-history/per-directory-history.zsh +++ b/plugins/per-directory-history/per-directory-history.zsh @@ -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