zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Defining function based on alias
Date: Sun, 8 Jan 2017 12:14:54 -0800	[thread overview]
Message-ID: <170108121454.ZM10678@torch.brasslantern.com> (raw)
In-Reply-To: <20170108191922.3d3de59f@ntlworld.com>

On Jan 8,  7:19pm, Peter Stephenson wrote:
} Subject: Re: Defining function based on alias
}
} I am about to commit the following, which I hope will hope will stop
} people coming to grief with this common confusion.  Please say if you
} see any problems.

No problems, but -- if it's possible to detect this situation, why not
simply suppress the alias expansion and define the function as named,
rather than trigger an error?

Etc/FAQ should be updated too.

I never remember how to rebuild FAQ from FAQ.yo ... can someone check
my yodl syntax?


diff --git a/Etc/FAQ.yo b/Etc/FAQ.yo
index 6b635ea..9eb23db 100644
--- a/Etc/FAQ.yo
+++ b/Etc/FAQ.yo
@@ -574,7 +574,8 @@ tt(EXTENDED_GLOB).
   it() Aliases and functions:
   itemize(
     it()  The order in which aliases and functions are defined is significant:
-        function definitions with () expand aliases -- see question \
+        in zsh versions 5.3.1 and earlier, function definitions with ()
+        expand aliases -- see question \
 link(2.3)(23).
     it()  Aliases and functions cannot be exported.
     it()  There are no tracked aliases: command hashing replaces these.
@@ -704,6 +705,12 @@ label(23)
   the manual). Note also that the mytt(;) at the end of the function is
   optional in zsh, but not in ksh or sh (for sh's where it exists).
 
+  Recent versions of zsh support anonymous functions with argument lists,
+  so you can create an alias that acts like a function.
+  verb(
+    alias cd='() { builtin cd "$@"; print -D $PWD; }'
+  )
+
   Here is Bart Schaefer's guide to converting csh aliases for zsh.
 
   enumerate(
@@ -711,6 +718,9 @@ label(23)
      then in zsh you need a function (referencing tt($1), tt($*) etc.).
      Otherwise, you can use a zsh alias.
 
+     In zsh version 5.0 and later you can use an anonymous zsh function in
+     combination with a zsh alias.
+
   myeit() If you use a zsh function, you need to refer _at_least_ to
      tt($*) in the body (inside the tt({ })).  Parameters don't magically
      appear inside the tt({ }) the way they get appended to an alias.
@@ -772,13 +782,17 @@ label(23)
     alias l='/bin/ls -F'
     l() { /bin/ls -la "$@" | more }
   )
-  mytt(l) in the function definition is in command position and is expanded
+  mytt(l) in the function definition is in command position and may expand
   as an alias, defining mytt(/bin/ls) and mytt(-F) as functions which call
   mytt(/bin/ls), which gets a bit recursive.  This can be avoided if you use
-  mytt(function) to define a function, which doesn't expand aliases.  It is
-  possible to argue for extra warnings somewhere in this mess.
+  mytt(function) to define a function, which doesn't expand aliases.
+
+  For zsh versions after 5.3.1, alias expansion of function names is an
+  error by default and the option tt(ALIAS_FUNC_DEF) must be active to
+  expand aliases in function names.  However, this option em(is) active
+  in compatibility modes where aliases are supported.
 
-  One workaround for this is to use the "function" keyword instead:
+  Here is an example using the "function" keyword instead:
   verb(
     alias l='/bin/ls -F'
     function l { /bin/ls -la "$@" | more }


  reply	other threads:[~2017-01-08 20:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-07 22:16 Peter Stephenson
2017-01-08 19:19 ` Peter Stephenson
2017-01-08 20:14   ` Bart Schaefer [this message]
2017-01-08 20:23     ` Peter Stephenson
2017-01-09  0:09   ` Bart Schaefer
2017-01-09  4:02     ` Daniel Shahaf
2017-01-09 10:33       ` Peter Stephenson
2017-01-09  1:55   ` Daniel Shahaf

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=170108121454.ZM10678@torch.brasslantern.com \
    --to=schaefer@brasslantern.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).