From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 7805 invoked from network); 24 Mar 2022 17:56:08 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 24 Mar 2022 17:56:08 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1648144568; b=cUTC4WGLfqojFmxyklhy89BzlzXuyS2ibbPpbMjl/sRVFrtKUt2axpd6gwfzQEoaXBEHSg9XHF G/uusoiDN0f0Hh1ijHBOpVkFp+ajsWkd9WumqmBWqMixgb/+iN8LqH0KydNqEavFifrEyBffw7 ndgBmZP+AO1LRrhLdrlXe2YniSLzwlzZscNnv/mDaK9xYUejvSUGxhQP9fzeQ4RsN9E6MyNMrX //neJGlTvR9fkQSMwqka2718ufPc/OjwD1gj6qDweIW9dgF93UWaHtc6pOJOAA4JZtDJbbsI7y iCkQaWYjtqojl4/2SWxNQNfN7ArtNxIISgAz25YRrc44XQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-yb1-f171.google.com) smtp.remote-ip=209.85.219.171; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1648144568; bh=oqeUSQiMI8uSDZ2KH0ir5G2/N9Lr1UBG73s0+wJQS6I=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=kobSSbHukleTcn8TxNGUmUhbWfhjF8RgHecfboi1IV7U5FIYyxLa4GmuklmkVS74wWB07xiU8x Drgpmg3RTElucIVNShS4ZKFZnFwQWyfwQm5BVrZzWMJ7F+I43I2NTEby2TUhqNzYDEwDA17k+J zzUGWJ34TBTYz2judW5LLdd5t6LGhJBLxeEAmEUwxftCwtgvdb2crPl0vvJel62fw3O61/ys70 l/vYcs9EOA5hoQ1oApV8yLDsqx6qX7JAC/fPPG0LDBtfdwjGIn8DMqMrB6P3RWqZ+nOKqjNyo+ qybh/46bInwv8qkTmhvOTSJr1pvWm9gmxKr4RP/HkL9Tag==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:To:Subject:Message-ID: Date:From:In-Reply-To:References:MIME-Version:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=GS1rOLtu7Ns2Rkr4B80buHUfOT0bWilRPQDcpGO70z8=; b=gYba0uVqBLlBH739cR/ZD4eWHl fP5ZuxkWTF/nPz6LKD7p7wUaVXyiBcg021LGgdvEA4bpc5fk6idEwOlPC0BgQHZYcorU5y0OITziC 3A/CxBQWe2NeKD026RUCg0nK+YOragBWUy5IJJeI9wWDLJIOpXB47fnLIFPOFgvevKxBVFGcDk4DG NPwttsM5XvCYw3xcSFESIy+m3RnT9Ed+IB3QE7JO3OVYXgBfEVrJHAbiWpk5DbsF2xiXDdvmgIe9L tyUMF0repcsjQ4Xukl+OD9kfzftRu3m0TwT4uyEwusVH573NiIbDoaQBwi4NXq2v6BhdvmCvYNtu/ GIV77BVg==; Received: from authenticated user by zero.zsh.org with local id 1nXRgu-0006rG-7v; Thu, 24 Mar 2022 17:56:08 +0000 Authentication-Results: zsh.org; iprev=pass (mail-yb1-f171.google.com) smtp.remote-ip=209.85.219.171; dkim=pass header.d=brasslantern-com.20210112.gappssmtp.com header.s=20210112 header.a=rsa-sha256; dmarc=none header.from=brasslantern.com; arc=none Received: from mail-yb1-f171.google.com ([209.85.219.171]:36863) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1nXRcK-000616-JT; Thu, 24 Mar 2022 17:51:25 +0000 Received: by mail-yb1-f171.google.com with SMTP id f38so9817565ybi.3 for ; Thu, 24 Mar 2022 10:51:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=GS1rOLtu7Ns2Rkr4B80buHUfOT0bWilRPQDcpGO70z8=; b=Gg9RRanAF8eP1FqtewahE29iqsyMZsWjgHOmSk8/E3qpoOcLzKWxOaW9rb67/os6aE KceOmGIkE2X0NBIdllVt8GhL4Wf8h/k4++YbcBScyYlQMMGsPEr/cPF8QcnIV7hcyakU EBInvXgbo9GdqQ4pnwmdBHz+RACYCDTO+5bp0LAvMTRTC0uWzfhzxOZQFp7jiztdQjU/ qlE8q7YtCo1frVsD3XRwoGRot2YiyHEE0bKQaL9ul71uBOr46VJIbr+fVMi0y0QSusl2 mJsr2uV3U+r99bn0WSaFaceJ7rz47O238ND3GlAhONRQHe7iOBJIst20epVX/1ypuoEW 03fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=GS1rOLtu7Ns2Rkr4B80buHUfOT0bWilRPQDcpGO70z8=; b=bn+znsqd4eKshEqu13s8YTxtzHFsrcybqcpvSgOiRG8wVzoxOF1HB57M/YuWpF7rWX hQ8XgodsJ4W3yxAf4Djkrb9XBPMuU8k3YXe5iRZ2WzxKhXemaXvPz+vhP6lA6iCKppRV 0atsok0n3D3Y+VhL/3ZAmoasKQWsqIX7r2b3nymP83AzNYxOC1YDElfl2QQg9s6A/Kx+ Sdkzr+yKxRnh83UNjydMDxjoejaJdxT3lE4S76ob617S+Clp0ExFlvnKPgHYD2M2tOES 29wrP61RBCbg541gVzP+Q4x1GY6tKJX48GuIqHDATuN+0+WOUiThDILi9A890oFFwAQb DOgQ== X-Gm-Message-State: AOAM53236PY5RoQ7KIqwlyKCS/fA9bO61xunRauaKh7PJpRg+F19+pXa DCVip187k4JUJclDhXLpXanGWlnSUZl9ImQ6DWvqHQXhF+dPqA== X-Google-Smtp-Source: ABdhPJxcHn6XVrSj8Dwo/IX5aLB+I/AY2J5VjSMy5gOWf4NWEcnU+JpkzekLCwgxnLWoEOSWzwdndZB4dBDgS2mZ5IE= X-Received: by 2002:a25:8109:0:b0:633:73fc:208d with SMTP id o9-20020a258109000000b0063373fc208dmr5747275ybk.462.1648144283011; Thu, 24 Mar 2022 10:51:23 -0700 (PDT) MIME-Version: 1.0 References: <306931674.638268.1648141537809@mail2.virginmedia.com> In-Reply-To: <306931674.638268.1648141537809@mail2.virginmedia.com> From: Bart Schaefer Date: Thu, 24 Mar 2022 10:51:12 -0700 Message-ID: Subject: Re: Load autoloadable function, but only in enclosing function scope To: Zsh Users Content-Type: text/plain; charset="UTF-8" X-Seq: 27612 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: On Thu, Mar 24, 2022 at 10:06 AM Peter Stephenson wrote: > > With that key limitation --- if you've already got functions with the > names of the ones you're creating, you're stuffed --- you can automate > it like this. > > fn() { > local -a oldfuncs=(${(k)functions}) It actually can be even shorter than that, depending on how carefully the context needs to be preserved. Peter's example keeps the definitions of any functions that were previously defined, even if those definitions were replaced (by autoload execution or explicit redefinition). The example below restores all the function definitions to their previous state, even when replaced. (Native zsh mode semantics assumed below.) foo() { print original foo } bar() { local funcs=(${(kv)functions} { foo() { print the bar foo } foo } always { functions=($funcs) } } % foo; bar; foo original foo the bar foo original foo This does have side-effects for autoloaded functions; they become defined with "builtin autoload -X" as the body, which doesn't always work the same way as a true autoload.