Skip to content

Commit

Permalink
Correctly set and restore precmd, preexec, PROMPT_COMMAND and debug trap
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Apr 26, 2019
1 parent 05795cf commit 7336a8c
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/shellhistory/shellhistory.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# FUNCTIONS --------------------------------------------------------------------


if [ -n "${ZSH_VERSION}" ]; then
_shellhistory_command_type() {
whence -w "$1" | cut -d' ' -f2
Expand Down Expand Up @@ -29,7 +28,6 @@ elif [ -n "${BASH_VERSION}" ]; then
}
fi


# shellcheck disable=SC2120
_shellhistory_parents() {
local list pid
Expand Down Expand Up @@ -136,29 +134,49 @@ _shellhistory_after() {

_SHELLHISTORY_BEFORE_DONE=0
_SHELLHISTORY_AFTER_DONE=1

return ${_SHELLHISTORY_CODE}
}

_shellhistory_get_debug_trap() {
local trap
trap="$(trap -p | grep ' DEBUG$')" || return
trap=${trap:9}
trap=${trap:0:-7}
case ${trap} in
*;) ;;
*) trap+=";" ;;
esac
echo "${trap}"
}

_shellhistory_enable() {
_SHELLHISTORY_BEFORE_DONE=2
_SHELLHISTORY_AFTER_DONE=1
if [ "${ZSH_VERSION}" ]; then
# FIXME: don't override possible previous contents of precmd
preexec() { _shellhistory_before; }
precmd() { _shellhistory_after; }
preexec_functions=($preexec_functions _shellhistory_before)
precmd_functions=(_shellhistory_after $precmd_functions)
elif [ "${BASH_VERSION}" ]; then
PROMPT_COMMAND="_shellhistory_after; ${PROMPT_COMMAND}"
trap '_shellhistory_before' DEBUG
PROMPT_COMMAND="_shellhistory_after;${PROMPT_COMMAND}"
trap "$(_shellhistory_get_debug_trap)_shellhistory_before;" DEBUG
fi
}

_shellhistory_disable() {
local trap
_SHELLHISTORY_AFTER_DONE=1
if [ "${ZSH_VERSION}" ]; then
preexec() { :; }
precmd() { :; }
preexec_functions=(${preexec_functions:#_shellhistory_before})
precmd_functions=(${precmd_functions:#_shellhistory_after})
elif [ "${BASH_VERSION}" ]; then
trap - DEBUG
PROMPT_COMMAND="${PROMPT_COMMAND#_shellhistory_after; }"
trap="$(_shellhistory_get_debug_trap)"
trap=${trap//_shellhistory_before;}
if [ -n "${trap}" ]; then
trap "${trap}" DEBUG
else
trap - DEBUG
fi
PROMPT_COMMAND="${PROMPT_COMMAND//_shellhistory_after;}"
fi
}

Expand Down

0 comments on commit 7336a8c

Please sign in to comment.