zsh-users
 help / color / mirror / code / Atom feed
From: Justin M Wozniak <wozniak@mcs.anl.gov>
To: <zsh-users@zsh.org>
Subject: Re: real time alias?
Date: Fri, 16 Mar 2018 12:19:03 -0500	[thread overview]
Message-ID: <0d176b63-3e79-ddd3-dcfa-03bed94db0e2@mcs.anl.gov> (raw)
In-Reply-To: <20180315214618.5exweubdq3d5akzo@prometheus.u-strasbg.fr>


Is there a way to have aliases work as macros?  For example, can some 
modification of warn_ () be used to return from the calling function?

On 03/15/2018 04:46 PM, Marc Chantreux wrote:
> On Thu, Mar 15, 2018 at 10:44:50PM +0100, Marc Chantreux wrote:
>> hello,
>>
>>>>> If a function calls an alias, if the alias changes, the function must be
>>>>> resourced, yes?  That makes nothing but sense sincethe alias is what it is
>>>>> at sourcing.  An executed script uses the alias in 'real time'.  But, is
>>>>> there a way to make a function also use the real time value of an alias?
>>> Sure, I was just wondering if it was possible at all with an alias.
>> my advice is: see aliases as macro: don't use it as long as you can
>> achieve things with functions. one of the reasons is aliases can be used
>> after a variable expansion and so you can get some surprising behaviors.
>>
>>      rip () {
>>          print $1 wrote
>>          "$@"
>>          print then $1 died
>>      }
>>      alias stephen='print a brief history of time'
>>      rip stephen hawkings
>>
>> then your universe colapse
>>
>>      stephen wrote
>>      rip:2: command not found: stephen
>>      then stephen died
>>
>> but
>>
>>      rip () {
>>          print $1 wrote
>>          "$@"
>>          print then $1 died
>>      }
>>      stephen () print a brief history of time
>>      rip stephen hawkings
>>
>> gives you a good reading advice
>>
>>      stephen wrote
>>      a brief history of time
>>      then stephen died
>>
>> so when are aliases useful? well ... basically when you need something
>> like a preprocessor. for example if you have a set of functions where
>> the first arguments are always the same, you can write an alias for it:
>>
>>      alias user_='local ns=${1?namespace of the user expected} \
>>      id=${2?the id of the user expected}'
>>
>>      showid () { user_; print user id is $id }
>>      showns () { user_; print user ns is $ns }
>>      showid
>>
>> warns you
>>
>>      showid: 1: namespace of the user expected
>>
>> another example from uze.zsh (https://github.com/zsh-uze)
>>
>>      warn_ () { local r=$?; print -u2 "$*"; return $r }
>>      die_  () { local r=$?; print -u2 "$*"; exit   $r }
>>      alias warn='warn_ at $0 line $LINENO, warning:'
>>      alias ...='{warn unimplemented; return 255}'
>>      alias die='die_  died at $0 line $LINENO:'
>>
>> now i can write
>>
>>      allowed () false
>>
>>      do_stuff () {
>>          if {allowed} {
>>              print i do stuff
>>          } else {
>>              # what to do then ?
>>              ...
>>          }
>>      }
>>
>>      do_stuff
>>
>> so i have this message:
>>
>>      at do_stuff line 5, warning: unimplemented
>>
>> hth,
>> marc
>>
>>> you ask, there might be some option or something whereby a function is
>>> instructed to grab an alias at runtime sorta the way the value of a variable
>>> is grabbed at runtime.
>> you can use both functions and arrays
>>
>>      show () {
>>          print "hello, i'm a particle and i'm"
>>          position
>>      }
>>      position () print here and there
>>      show
>>      position () print elsewhere
>>      show
>>
>> gives
>>
>>      hello, i'm a particle and i'm
>>      here and there
>>      hello, i'm a particle and i'm
>>      elsewhere
>>
>> also
>>
>>      show () print -l "hello, i'm a particle and i'm" $position
>>      position=( here and there )
>>      show
>>      position=( elsewhere )
>>      show
>>
>> gives
>>
>>      hello, i'm a particle and i'm
>>      here
>>      and
>>      there
>>      hello, i'm a particle and i'm
>>      elsewhere
>>
>> don't forget that in crontrary of other shells, zsh takes arrays and
>> word spliting seriously so you can store a whole command into an array.
>> this is lovely when you build one step by step
>>
>>      academic () true
>>      faster   () true
>>      dryrun   () true
>>
>>      dryrun && compiles=( print cc )
>>             || compiles=( cc )
>>
>>      academic && compiles+=( -Wall -std=c99 )
>>      faster   && compiles+=( -O2 )
>>      compiles+=( $src $dest )
>>      $compiles
>>
>> hth
>> marc

-- 
Justin M Wozniak


  reply	other threads:[~2018-03-16 17:25 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-15 17:51 Ray Andrews
2018-03-15 17:57 ` Jérémie Roquet
2018-03-15 18:13   ` Ray Andrews
2018-03-15 21:44     ` Marc Chantreux
2018-03-15 21:46       ` Marc Chantreux
2018-03-16 17:19         ` Justin M Wozniak [this message]
2018-03-16 17:41           ` Ray Andrews
2018-03-17 16:00             ` Marc Chantreux
2018-03-16 17:51           ` Marc Chantreux
2018-03-16 16:47       ` Ray Andrews
2018-03-16 17:41         ` Marc Chantreux
2018-03-16 18:13         ` Bart Schaefer
2018-03-16 18:56           ` Ray Andrews
2018-03-18 23:01             ` Bart Schaefer
2018-03-19  0:05               ` Ray Andrews
2018-03-17 23:17       ` Bart Schaefer
     [not found] <98aa0638-97ed-dfe0-8dd2-39129d02c084__39937.2481661432$1521136270$gmane$org@eastlink.ca>
2018-03-17 22:47 ` Stephane Chazelas
2018-03-18  3:20   ` Ray Andrews

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=0d176b63-3e79-ddd3-dcfa-03bed94db0e2@mcs.anl.gov \
    --to=wozniak@mcs.anl.gov \
    --cc=zsh-users@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).