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

Re: PATCH: Don't define internal params directly in hook function scope

On Thu, 30 Apr 2015 09:44:04 +0100
Peter Stephenson <p.stephenson@xxxxxxxxxxx> wrote:
> > (I believe the answer about adding a new level of scope around the
> > "local -h" has appeared on zsh-users before, though I can't think of
> > a good search term to find it.)
> Documenting this is probably a reasonable approach.  Adding a nested
> function in the odd case where you need it probably isn't a big issue,
> as long as people know.

For example.


diff --git a/Doc/Zsh/zle.yo b/Doc/Zsh/zle.yo
index ffce54c..653678e 100644
--- a/Doc/Zsh/zle.yo
+++ b/Doc/Zsh/zle.yo
@@ -743,6 +743,21 @@ local scope, like parameters created in a function using tt(local).
 Inside completion widgets and traps called while ZLE is active, these
 parameters are available read-only.
+Note that the parameters appear as local to any ZLE widget in
+which they appear.  Hence if it is desired to override them this needs
+to be done within a nested function:
+example(widget-function+LPAR()+RPAR() {
+  # $WIDGET here refers to the special variable
+  # that is local inside widget-function
+  +LPAR()+RPAR() {
+     # This anonymous nested function allows WIDGET
+     # to be used as a local variable.  The -h
+     # removes the special status of the variable.
+     local -h WIDGET
+  }
 item(tt(BUFFER) (scalar))(

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