add-zsh-trap 923 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #
  2. # Provides for trapping UNIX signals and calling callback functions when a trap
  3. # is triggered.
  4. #
  5. # Authors:
  6. # Sorin Ionescu <sorin.ionescu@gmail.com>
  7. #
  8. # Adds a function name to a list to be called when a trap is triggered.
  9. function add-zsh-trap {
  10. if (( $# < 2 )); then
  11. print "usage: $0 type function" >&2
  12. return 1
  13. fi
  14. if [[ -z "$signals[(r)$1]" ]]; then
  15. print "$0: unknown signal: $1" >&2
  16. return 1
  17. fi
  18. local trap_functions="TRAP${1}_FUNCTIONS"
  19. if (( ! ${(P)+trap_functions} )); then
  20. typeset -gaU "$trap_functions"
  21. fi
  22. eval "$trap_functions+="$2""
  23. if (( ! $+functions[TRAP${1}] )); then
  24. eval "
  25. function TRAP${1} {
  26. for trap_function in \"\$TRAP${1}_FUNCTIONS[@]\"; do
  27. if (( \$+functions[\$trap_function] )); then
  28. \"\$trap_function\" \"\$1\"
  29. fi
  30. done
  31. return \$(( 128 + \$1 ))
  32. }
  33. "
  34. fi
  35. }
  36. add-zsh-trap "$@"