about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Stephenson <pws@zsh.org>2015-08-21 16:55:10 +0100
committerPeter Stephenson <pws@zsh.org>2015-08-21 16:55:10 +0100
commitf4c37a78b150005b59801b0a2fdbb36b50a293be (patch)
tree1adefe6b9a35852e38878979d6a66be698a6e851
parentc0df3440a44ac74a09115b536019b8ca6076245f (diff)
downloadzsh-f4c37a78b150005b59801b0a2fdbb36b50a293be.tar.gz
zsh-f4c37a78b150005b59801b0a2fdbb36b50a293be.tar.xz
zsh-f4c37a78b150005b59801b0a2fdbb36b50a293be.zip
36265 plus FAQ: fix alias expansion after "function"
Owing to interesting historical parsing, names after the first
were treated as command words so had non-global aliases expanded.
Add an FAQ note that use of the function keyword works around
other alias problems
-rw-r--r--Etc/FAQ.yo21
-rw-r--r--Src/parse.c5
2 files changed, 24 insertions, 2 deletions
diff --git a/Etc/FAQ.yo b/Etc/FAQ.yo
index 25a837ee7..9b7e558a6 100644
--- a/Etc/FAQ.yo
+++ b/Etc/FAQ.yo
@@ -778,6 +778,27 @@ label(23)
   mytt(function) to define a function, which doesn't expand aliases.  It is
   possible to argue for extra warnings somewhere in this mess.
 
+  One workaround for this is to use the "function" keyword instead:
+  verb(
+    alias l='/bin/ls -F'
+    function l { /bin/ls -la "$@" | more }
+  )
+  The mytt(l) after mytt(function) is not expanded.  Note you don't need
+  the mytt(LPAR()RPAR()) in this case, although it's harmless.
+
+  You need to be careful if you are defining a function with multiple
+  names; most people don't need to do this, so it's an unusual problem,
+  but in case you do you should be aware that in versions of the shell
+  before 5.1 names after the first em(were) expanded:
+  verb(
+    function a b c { ... }
+  )
+  Here, mytt(b) and mytt(c), but not mytt(a), have aliases expanded.
+  This oddity was fixed in version 5.1.
+
+  The rest of this item assumes you use the (more common,
+  but equivalent) mytt(LPAR()RPAR()) definitions.
+
   Bart Schaefer's rule is:  Define first those aliases you expect to
   use in the body of a function, but define the function first if the
   alias has the same name as the function.
diff --git a/Src/parse.c b/Src/parse.c
index 1a7416449..09317610b 100644
--- a/Src/parse.c
+++ b/Src/parse.c
@@ -1600,9 +1600,9 @@ par_funcdef(int *cmplx)
     p = ecadd(0);
     ecadd(0);
 
-    incmdpos = 1;
     while (tok == STRING) {
-	if (*tokstr == Inbrace && !tokstr[1]) {
+	if ((*tokstr == Inbrace || *tokstr == '{') &&
+	    !tokstr[1]) {
 	    tok = INBRACE;
 	    break;
 	}
@@ -1615,6 +1615,7 @@ par_funcdef(int *cmplx)
     ecadd(0);
 
     nocorrect = 0;
+    incmdpos = 1;
     if (tok == INOUTPAR)
 	zshlex();
     while (tok == SEPER)