help / color / mirror / code / Atom feed
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: How to set exit trap from inner function?
Date: Fri, 24 Feb 2023 02:08:35 +0000	[thread overview]
Message-ID: <CAKc7PVDooYnQ1hb-HU70rWD8oRpgQ0pR13dyPsY79BwFmqY5dw@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 968 bytes --]

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"
    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

[-- Attachment #2: Type: text/html, Size: 2088 bytes --]

             reply	other threads:[~2023-02-24  2:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-24  2:08 Sebastian Gniazdowski [this message]
2023-02-24 23:41 ` Bart Schaefer
2023-02-26  9:56   ` Sebastian Gniazdowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAKc7PVDooYnQ1hb-HU70rWD8oRpgQ0pR13dyPsY79BwFmqY5dw@mail.gmail.com \
    --to=sgniazdowski@gmail.com \
    --cc=zsh-workers@zsh.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).