Convert add-zsh-trap into a module

This commit is contained in:
Sorin Ionescu 2012-04-04 14:46:30 -04:00
parent d13b697792
commit c9b8ad569d
3 changed files with 53 additions and 40 deletions

View File

@ -64,43 +64,3 @@ function omodload {
done
}
# Trap signals were generated with 'kill -l'.
# DEBUG, EXIT, and ZERR are Zsh signals.
TRAP_SIGNALS=(
ABRT ALRM BUS CHLD CONT EMT FPE HUP ILL INFO INT IO KILL PIPE PROF QUIT
SEGV STOP SYS TERM TRAP TSTP TTIN TTOU URG USR1 USR2 VTALRM WINCH XCPU XFSZ
DEBUG EXIT ZERR
)
# Adds a function to a list to be called when a trap is triggered.
function add-zsh-trap {
if (( $# < 2 )); then
print "usage: $0 type function" >&2
return 1
fi
if [[ -z "$TRAP_SIGNALS[(r)$1]" ]]; then
print "$0: unknown signal: $1" >&2
return 1
fi
local trap_functions="TRAP${1}_FUNCTIONS"
if (( ! ${(P)+trap_functions} )); then
typeset -gaU "$trap_functions"
fi
eval "$trap_functions+="$2""
if (( ! $+functions[TRAP${1}] )); then
eval "
function TRAP${1} {
for trap_function in \"\$TRAP${1}_FUNCTIONS[@]\"; do
if (( \$+functions[\$trap_function] )); then
\"\$trap_function\" \"\$1\"
fi
done
return \$(( 128 + \$1 ))
}
"
fi
}

View File

@ -62,6 +62,9 @@
# zstyle ':omz:module:git:ignore' submodule ''
#
# Load dependencies.
omodload 'trap'
# Gets the path to the Git directory.
function _git-dir {
local git_dir="${$(git rev-parse --git-dir):A}"

View File

@ -0,0 +1,50 @@
#
# Provides for the trapping of UNIX signals and the calling of multiple
# registered functions when a trap is triggered.
#
# Authors:
# Sorin Ionescu <sorin.ionescu@gmail.com>
#
# Trap signals were generated with 'kill -l'.
# DEBUG, EXIT, and ZERR are Zsh signals.
_trap_signals=(
ABRT ALRM BUS CHLD CONT EMT FPE HUP ILL INFO INT IO KILL PIPE PROF QUIT
SEGV STOP SYS TERM TRAP TSTP TTIN TTOU URG USR1 USR2 VTALRM WINCH XCPU XFSZ
DEBUG EXIT ZERR
)
# Adds a function name to a list to be called when a trap is triggered.
function add-zsh-trap {
if (( $# < 2 )); then
print "usage: $0 type function" >&2
return 1
fi
if [[ -z "$_trap_signals[(r)$1]" ]]; then
print "$0: unknown signal: $1" >&2
return 1
fi
local trap_functions="TRAP${1}_FUNCTIONS"
if (( ! ${(P)+trap_functions} )); then
typeset -gaU "$trap_functions"
fi
eval "$trap_functions+="$2""
if (( ! $+functions[TRAP${1}] )); then
eval "
function TRAP${1} {
for trap_function in \"\$TRAP${1}_FUNCTIONS[@]\"; do
if (( \$+functions[\$trap_function] )); then
\"\$trap_function\" \"\$1\"
fi
done
return \$(( 128 + \$1 ))
}
"
fi
}
add-zsh-trap "$@"