Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

How to set exit trap from inner function?

I want to automatically clear all functions created from outer function.

auto-unset-funcs() {
    # Don't leak any functions
    typeset -ga ef
    ef=( ${(k)functions} )
    # No localtraps – sets in outside scope
    trap "unset -f -- \"\${(k)functions[@]:|ef}\" &>/dev/null; unset ef" EXIT
    trap "return 1" INT

Above code correctly clears new functions when pasted into the outer function. However, I would like to avoid pasting and simply do:

outer() {
    emulate -L zsh
    # freely define new functions…

However, the EXIT trap is executed when auto-unset-funcs exits, not when outer() does… I thought that lack of localoptions localtraps would declare the EXIT trap in the first function that has it (outer does, via emulate -L). Doing setopt localoptions nolocaltraps in auto-unset-funcs didn't help. Any ideas on having such auto-unset-funcs function?
Best regards,
Sebastian Gniazdowski

Messages sorted by: Reverse Date, Date, Thread, Author