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

Re: autoload -X inside an anonymous function



On Fri, 5 Oct 2012 22:32:28 +0100
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx> wrote:
> On the whole I'd be inclined to reverse my decision not to propagate
> stickiness to autoloads, and to document that -z only affects the
> autoload style and that
> 
>   emulate zsh -c 'autoload -Uz ...'
> 
> is the way to ensure functions are loaded with appropriate options.

This would be the corresponding patch, but I'll wait for reaction.

One slight subtlety is that I only changed the use of sticky emulation
at the point where the function is marked for autoload; this causes the
sticky emulation to be in effect during the autoload itself, and it
remains in effect while the function is being defined so propagates to
the new function definition which retains the same sticky emulation for
execution.  If you don't believe me, believe the test...

Index: README
===================================================================
RCS file: /cvsroot/zsh/zsh/README,v
retrieving revision 1.77
diff -p -u -r1.77 README
--- README	15 Jun 2012 21:23:42 -0000	1.77
+++ README	5 Oct 2012 22:18:58 -0000
@@ -30,8 +30,38 @@ Zsh is a shell with lots of features.  F
 file FEATURES, and for the latest changes see NEWS.  For more
 details, see the documentation.
 
-Possible incompatibilities
----------------------------
+Incompatibilities between 5.0.0 and 5.0.1
+-----------------------------------------
+
+In 5.0.0, the new "sticky" emulation feature was applied to functions
+explicitly declared within an expression following `emulate ... -c', but
+did not apply to functions marked for autoload in that expression.  This
+was not documented and experience suggests it was inconvenient, so in
+5.0.1 autoloads also have the sticky property.
+
+In other words,
+
+  emulate zsh -c 'func() { ... }'
+
+behaves the same way in 5.0.0 and 5.0.1, with the function func always being
+run in native zsh emulation regardless of the current option settings.
+However,
+
+  emulate zsh -c 'autoload -Uz func'
+
+behaves differently: in 5.0.0, func was loaded with the options in
+effect at the point where it was first run, and subsequently run with
+whatever options were in effect at that point; in 5.0.1, func is loaded
+with native zsh emulation options and run with those same options.  This
+is now the recommended way of ensuring a function is loaded and run with
+a consistent set of options.
+
+Note that the `autoload -z' has never affected the options applied when
+the function is loaded or run, only the effect of the KSH_AUTOLOAD
+option at the point the function is loaded.
+
+Possible incompatibilities between 4.2 and 5.0
+----------------------------------------------
 
 Here are some incompatibilities in the shell since the 4.2 series of
 releases.  It is hoped most users will not be adversely affected by these.
Index: Doc/Zsh/builtins.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v
retrieving revision 1.143
diff -p -u -r1.143 builtins.yo
--- Doc/Zsh/builtins.yo	21 Sep 2012 19:08:36 -0000	1.143
+++ Doc/Zsh/builtins.yo	5 Oct 2012 22:18:58 -0000
@@ -137,10 +137,19 @@ With the tt(-w) flag, the var(name)s are
 with the tt(zcompile) builtin, and all functions defined in them are
 marked for autoloading.
 
-The flags tt(-z) and tt(-k) mark the function to be autoloaded in
-native or ksh emulation, as if the option tt(KSH_AUTOLOAD) were
-unset or were set, respectively.  The flags override the setting of
-the option at the time the function is loaded.
+The flags tt(-z) and tt(-k) mark the function to be autoloaded using the
+zsh or ksh style, as if the option tt(KSH_AUTOLOAD) were unset or were
+set, respectively.  The flags override the setting of the option at the
+time the function is loaded.
+
+Note that the tt(autoload) command makes no attempt to ensure the
+shell options set during the loading or execution of the file have
+any particular value.  For this, the tt(emulate) command can be used:
+
+example(emulate zsh -c 'autoload -Uz var(func)')
+
+arranges that when var(func) is loaded the shell is in native tt(zsh)
+emulation, and this emulation is also applied when var(func) is run.
 )
 findex(bg)
 cindex(jobs, backgrounding)
@@ -393,6 +402,7 @@ ifnzman(noderef(Invocation))\
 ifzman(the section INVOCATION in zmanref(zsh)),
 except that `tt(-o EMACS)' and `tt(-o VI)' may not be used.  Flags such
 as `tt(+r)'/`tt(+o RESTRICTED)' may be prohibited in some circumstances.
+
 If tt(-c) var(arg) appears in var(flags), var(arg) is evaluated while the
 requested emulation is temporarily in effect.  In this case the emulation
 mode and all options are restored to their previous values before
@@ -409,7 +419,10 @@ If the function is called when the stick
 effect, either within an `tt(emulate) var(shell) tt(-c)' expression or
 within another function with the same sticky emulation, entry and exit
 from the function do not cause options to be altered (except due to
-standard processing such as the tt(LOCAL_OPTIONS) option).
+standard processing such as the tt(LOCAL_OPTIONS) option).  This also
+applies to functions marked for autoload within the sticky emulation;
+the appropriate set of options will be applied at the point the
+function is loaded as well as when it is run.
 
 For example:
 
Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.263
diff -p -u -r1.263 builtin.c
--- Src/builtin.c	21 Sep 2012 19:08:36 -0000	1.263
+++ Src/builtin.c	5 Oct 2012 22:18:58 -0000
@@ -2944,8 +2944,7 @@ bin_functions(char *name, char **argv, O
 	    shf = (Shfunc) zshcalloc(sizeof *shf);
 	    shf->node.flags = on;
 	    shf->funcdef = mkautofn(shf);
-	    /* No sticky emulation for autoloaded functions */
-	    shf->emulation = 0;
+	    shf->emulation = sticky_emulation;
 	    shfunctab->addnode(shfunctab, ztrdup(*argv), shf);
 
 	    if (signum != -1) {
Index: Test/C04funcdef.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/C04funcdef.ztst,v
retrieving revision 1.10
diff -p -u -r1.10 C04funcdef.ztst
--- Test/C04funcdef.ztst	3 Aug 2011 18:45:19 -0000	1.10
+++ Test/C04funcdef.ztst	5 Oct 2012 22:18:58 -0000
@@ -251,6 +251,23 @@
 >foo1
 >bar2
 
+  (
+  setopt ignorebraces
+  fpath=(.)
+  print "{ echo OK }\n[[ -o ignorebraces ]] || print 'ignorebraces is off'" \
+      >emufunctest
+  (autoload -z emufunctest; emufunctest) 2>&1
+  emulate zsh -c 'autoload -Uz emufunctest'
+  emufunctest
+  [[ -o ignorebraces ]] && print 'ignorebraces is still on here'
+  )
+0:sticky emulation applies to autoloads and autoloaded function execution
+>emufunctest:3: parse error near `\n'
+>OK
+>ignorebraces is off
+>ignorebraces is still on here
+
+
 %clean
 
  rm -f file.in file.out

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



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