zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: PATCH: functions with redirections
Date: Wed, 1 Oct 2014 20:17:05 +0100	[thread overview]
Message-ID: <20141001201705.23ee198d@pws-pc.ntlworld.com> (raw)
In-Reply-To: <CAH+w=7Y=QjPSELAuXCGf_HoidypLt_3gNpfOi+RE419bSJkrng@mail.gmail.com>

On Mon, 29 Sep 2014 14:37:35 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> My first idea when this came up was to alter the wordcode for functions
> with redirections to explicitly add the implicit set of braces, e.g.,
> 
> foo () { echo foo } >&3
> 
> becomes
> 
> foo () { { echo foo } >&3 }
> 
> Then it would not be necessary to mess with printing/dumping etc. of the
> definition, or invent a new place to store the redirections.  Yeah, output
> of "functions" et al. becomes a little less faithful but so what?
> 
> Sadly I never managed to make that work, but maybe the concept is helpful
> for dump file creation.

This appears to be about the only way I could even conceivably get this
to work --- otherwise it means reengineering multiple different ways of
constructing and reading a dump file --- but even this appears too
difficult.  The main sticking point is where you need to add the wrapper
you have an Eprog, and it's hard to convert that back into the form you
need for adding additional components or glueing bits together ---
Eprogs are already glued together seamlessly.  I fixed it for the redir
on its own by writing a function to extract the simple redir wordcode
directly, but that's not feasible here.  It's not fatal that it doesn't
work, you just lose the redirections.

So I'll simply document around this and be satisfied with the fact that
it is possible to compile an autoload file like

foo() { echo foo } >&3

with or without the zsh kludge 'foo "$@"' stuck at the end.  I suspect
compiling existing autoload files is much more common than compiling
functions typed into the shell.

I've also documented the basic feature and verified for the first time
that

foo() echo foo >&3

also works as expected.  (As expected if you understand weird squiggles,
that is.)

diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo
index 9862c63..41c189f 100644
--- a/Doc/Zsh/builtins.yo
+++ b/Doc/Zsh/builtins.yo
@@ -2174,6 +2174,20 @@ match one of these patterns will be written. If no var(name) is given,
 the definitions of all functions currently defined or marked as
 autoloaded will be written.
 
+Note the second form cannot be used for compiling functions that
+include redirections as part of the definition rather than within
+the body of the function; for example
+
+example(fn1() { { ... } >~/logfile })
+
+can be compiled but
+
+example(fn1() { ... } >~/logfile)
+
+cannot.  It is possible to use the first form of tt(zcompile) to compile
+autoloadable functions that include the full function definition instead
+of just the body of the function.
+
 The third form, with the tt(-t) option, examines an existing
 compiled file.  Without further arguments, the names of the original
 files compiled into it are listed.  The first line of output shows
diff --git a/Doc/Zsh/grammar.yo b/Doc/Zsh/grammar.yo
index 77f0098..eb1edf7 100644
--- a/Doc/Zsh/grammar.yo
+++ b/Doc/Zsh/grammar.yo
@@ -352,6 +352,15 @@ If the option tt(SH_GLOB) is set for compatibility with other shells, then
 whitespace may appear between between the left and right parentheses when
 there is a single var(word);  otherwise, the parentheses will be treated as
 forming a globbing pattern in that case.
+
+In any of the forms above, a redirection may appear outside the
+function body, for example
+
+example(func() { ... } 2>&1)
+
+The redirection is stored with the function and applied whenever the
+function is executed.  Any variables in the redirection are expanded
+at the point the function is executed, but outside the function scope.
 )
 cindex(timing)
 findex(time)

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


  parent reply	other threads:[~2014-10-01 19:22 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-29 19:52 Peter Stephenson
2014-09-29 20:27 ` Peter Stephenson
2014-09-29 20:53 ` Bart Schaefer
2014-09-29 21:37   ` Bart Schaefer
2014-09-30  4:51     ` Mikael Magnusson
2014-10-01 19:17     ` Peter Stephenson [this message]
2014-10-02  4:07       ` Bart Schaefer
2014-10-02  8:35         ` Peter Stephenson
2014-10-02 14:55           ` Peter Stephenson
2014-10-02 15:54           ` Bart Schaefer
2014-10-02 15:56           ` Bart Schaefer
2014-10-02 16:03             ` Peter Stephenson
2014-09-30  1:02   ` Bart Schaefer
2014-09-30  8:48     ` Peter Stephenson
2014-09-30 11:52       ` Peter Stephenson

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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

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

  git send-email \
    --in-reply-to=20141001201705.23ee198d@pws-pc.ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* 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

	https://git.vuxu.org/mirror/zsh/

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).