From c9b8ad569d36e4a78d1ab1314c07a5b26aa7fcf1 Mon Sep 17 00:00:00 2001 From: Sorin Ionescu Date: Wed, 4 Apr 2012 14:46:30 -0400 Subject: [PATCH] Convert add-zsh-trap into a module --- helper.zsh | 40 ----------------------- modules/git/functions/git-info | 3 ++ modules/trap/functions/add-zsh-trap | 50 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 40 deletions(-) create mode 100644 modules/trap/functions/add-zsh-trap diff --git a/helper.zsh b/helper.zsh index 7714591f..f05eaa5b 100644 --- a/helper.zsh +++ b/helper.zsh @@ -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 -} - diff --git a/modules/git/functions/git-info b/modules/git/functions/git-info index 86d55b19..d236155e 100644 --- a/modules/git/functions/git-info +++ b/modules/git/functions/git-info @@ -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}" diff --git a/modules/trap/functions/add-zsh-trap b/modules/trap/functions/add-zsh-trap new file mode 100644 index 00000000..a147c689 --- /dev/null +++ b/modules/trap/functions/add-zsh-trap @@ -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 +# + +# 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 "$@" +