zsh-users
 help / color / mirror / code / Atom feed
* Re: real time alias?
       [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
  0 siblings, 1 reply; 18+ messages in thread
From: Stephane Chazelas @ 2018-03-17 22:47 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

2018-03-15 10:51:47 -0700, Ray Andrews:
> 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?
[...]

You can always use eval:

   $ f() { eval 'myalias 1'; echo done; }
   $ alias myalias=echo; f
   1
   done
   $ alias myalias=return; f
   $ echo $?
   1

-- 
Stephane


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-17 22:47 ` real time alias? Stephane Chazelas
@ 2018-03-18  3:20   ` Ray Andrews
  0 siblings, 0 replies; 18+ messages in thread
From: Ray Andrews @ 2018-03-18  3:20 UTC (permalink / raw)
  To: zsh-users

On 17/03/18 03:47 PM, Stephane Chazelas wrote:
> 2018-03-15 10:51:47 -0700, Ray Andrews:
>
> You can always use eval:
>
>     $ f() { eval 'myalias 1'; echo done; }
>     $ alias myalias=echo; f
>     1
>     done
>     $ alias myalias=return; f
>     $ echo $?
>     1
>

That's exactly what I had in mind:

Script:

alias alias1="echo one"
function test1 () { eval alias1 }

alias alias1="echo two"
test1
alias alias1="echo three"
test1
alias alias1="echo four"
test1

Output:

two
three
four

... so, bearing in mind what Bart said about aliases being able to carry 
any sort of foul up of syntax, one must be careful since whatever is in 
the alias at parsing will be checked for sanity, but after that I'd 
expect one is on one's own.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-18 23:01             ` Bart Schaefer
@ 2018-03-19  0:05               ` Ray Andrews
  0 siblings, 0 replies; 18+ messages in thread
From: Ray Andrews @ 2018-03-19  0:05 UTC (permalink / raw)
  To: zsh-users

On 18/03/18 04:01 PM, Bart Schaefer wrote:
> On Mar 16, 11:56am, Ray Andrews wrote:
> }
>
> Sure, but it doesn't keep track of whether "c" is the ONLY function
> that came from that file.  It wouldn't be safe to automatically re-
> source just any such file.  Telling zsh which files can be sourced
> that way is the job of "autoload".
I sorta see that.  When I tried autoload a while back it was a bit of a 
disaster because my functions mostly have subsidiary functions in the 
same file and I wanted them all autoloaded at the same time, which IIRC 
autoload does not do.  But, given that a file is sourced or not sourced 
I get a glimmer now of why it better only have one function given what 
you've just said.  The logical level of the file is relevant to sourcing 
but not relevant to anything else. And the bureaucracy of trying to keep 
it all straight would be onerous.
>
> Although it does seem ripe for unintended side-effects.
Yeah, and I guess any user of aliases ( or macros as in C ) knows that, 
and knows it's their hide if they are not careful.   Anyway 'eval alias' 
is the exact and intuitive answer to my original, and it's cool that we 
can have it either way.



^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-16 18:56           ` Ray Andrews
@ 2018-03-18 23:01             ` Bart Schaefer
  2018-03-19  0:05               ` Ray Andrews
  0 siblings, 1 reply; 18+ messages in thread
From: Bart Schaefer @ 2018-03-18 23:01 UTC (permalink / raw)
  To: zsh-users

On Mar 16, 11:56am, Ray Andrews wrote:
}
} I get it.  The function can't 'grab' a new version of the alias without 
} recompiling itself which ends up being far more trouble than it's 
} worth.  Mind, my functions do know where they came from:
} 
}      (1)TYPE: c is a shell function from /aWorking/Zsh/Source/c:

Sure, but it doesn't keep track of whether "c" is the ONLY function
that came from that file.  It wouldn't be safe to automatically re-
source just any such file.  Telling zsh which files can be sourced
that way is the job of "autoload".

It would be possible for every function to know whether it originally
came in via "autoload" and have some criteria for whether to discard
the current definition and re-load again.  However, given the possible
chain of dependencies (this alias depends on that function depends on
some other alias depends on ...) it isn't practical to keep track of
everything that might have gone into the definition of a function.

Besides, we generally recommend NOT depending on aliases in autoloaded
functions, and therefore using "autoload -U ...".

Maybe this is a feature that Sebastian is interested in tackling in his
plugin manager:  Remembering functions that are autoloaded *without* -U
in plugins that define aliases, and reloading them if the aliases are
updated.  Although it does seem ripe for unintended side-effects.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-15 21:44     ` Marc Chantreux
  2018-03-15 21:46       ` Marc Chantreux
  2018-03-16 16:47       ` Ray Andrews
@ 2018-03-17 23:17       ` Bart Schaefer
  2 siblings, 0 replies; 18+ messages in thread
From: Bart Schaefer @ 2018-03-17 23:17 UTC (permalink / raw)
  To: Zsh Users

I know this is from a couple of days ago, but:

On Thu, Mar 15, 2018 at 2:44 PM, Marc Chantreux <eiro@phear.org> wrote:
>
>     rip () {
>         print $1 wrote
>         "$@"
>         print then $1 died
>     }
>     alias stephen='print a brief history of time'
>     rip stephen hawkings

This isn't a very good example because "rip" would not use the alias
even if "stephen" had been explicitly mentioned in the function body.
You'd have to define them in the opposite order.

Also this:

> warn_ () { local r=$?; print -u2 "$*"; return $r }
> alias warn='warn_ at $0 line $LINENO, warning:'

Can be done directly like this:

alias warn='() { local r=$?; print -u2 "$*"; return $r } "at $0 line
$LINENO, warning: "'

in any zsh from the past several years that supports anonymous function scopes.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-16 17:41           ` Ray Andrews
@ 2018-03-17 16:00             ` Marc Chantreux
  0 siblings, 0 replies; 18+ messages in thread
From: Marc Chantreux @ 2018-03-17 16:00 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

> What's 'warn_ ()'? No such command here, nor any matching file.

it was a function from the example:

    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:'

regards
marc


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-16 18:13         ` Bart Schaefer
@ 2018-03-16 18:56           ` Ray Andrews
  2018-03-18 23:01             ` Bart Schaefer
  0 siblings, 1 reply; 18+ messages in thread
From: Ray Andrews @ 2018-03-16 18:56 UTC (permalink / raw)
  To: zsh-users

On 16/03/18 11:13 AM, Bart Schaefer wrote:
> The result of expanding an alias might include parens, braces, pipes,
> etc. - it can actually change the parsed syntax of the function body.
> So to "grab aliases again" you have to recompile the whole body of the
> function.

Right, because it might not be a self contained block of code, it could 
have syntactic effects on the rest of the function.

> The original (unparsed) body of the function is not stored anywhere,
> so to recompile it you have to get it back from wherever it originally
> appeared.

I get it.  The function can't 'grab' a new version of the alias without 
recompiling itself which ends up being far more trouble than it's 
worth.  Mind, my functions do know where they came from:

     (1)TYPE: c is a shell function from /aWorking/Zsh/Source/c:

... still I get the point, the sort of thing I'm speculating about is 
square circles, best to leave aliases as the strings of characters that 
they are.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  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
  1 sibling, 1 reply; 18+ messages in thread
From: Bart Schaefer @ 2018-03-16 18:13 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

On Fri, Mar 16, 2018 at 9:47 AM, Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> $ function test1 ()
> {
>     test2 # unchanged since last sourcing of this function.
>     setopt GRAB_ALIASES_AGAIN test2
>     test2 # Up-to-date.
>     test3
> }

The result of expanding an alias might include parens, braces, pipes,
etc. - it can actually change the parsed syntax of the function body.
So to "grab aliases again" you have to recompile the whole body of the
function.

The original (unparsed) body of the function is not stored anywhere,
so to recompile it you have to get it back from wherever it originally
appeared.  That's easy if the function was autoloaded from your
$fpath:

% unfunction test1; autoload test1

If the function came from some larger file (such as .zshrc), or was
defined at the command line, or was loaded from a plugin manager like
Sebastian's, the details of how to reparse it will vary.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-16 17:19         ` Justin M Wozniak
  2018-03-16 17:41           ` Ray Andrews
@ 2018-03-16 17:51           ` Marc Chantreux
  1 sibling, 0 replies; 18+ messages in thread
From: Marc Chantreux @ 2018-03-16 17:51 UTC (permalink / raw)
  To: Justin M Wozniak; +Cc: zsh-users

Hi Justin,

On Fri, Mar 16, 2018 at 12:19:03PM -0500, Justin M Wozniak wrote:
> Is there a way to have aliases work as macros?

the point i was trying to emphasize in my last mail is that
aliases really are macros (as preprocessor macros ...
not hygenic ones ...) that can be redefined
(from a user perspective: i rarely dove into the zsh code).

if you want to see that in action, `which` can help

    alias foo='echo this is it'
    bar () {
        print hello
        foo
    }
    alias foo=booog
    which bar

outputs the content of the function where you can see that
* there is no more mention of 'foo'
* foo is substitued with the value of foo at the time the function was
  defined

    bar () {
        print hello
        echo this is it
    }

is this what you asked for ?

> For example, can some
> modification of warn_ () be used to return from the calling function?

can you give an exemple of what you would like to get ?


hth
marc


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-16 17:19         ` Justin M Wozniak
@ 2018-03-16 17:41           ` Ray Andrews
  2018-03-17 16:00             ` Marc Chantreux
  2018-03-16 17:51           ` Marc Chantreux
  1 sibling, 1 reply; 18+ messages in thread
From: Ray Andrews @ 2018-03-16 17:41 UTC (permalink / raw)
  To: zsh-users

On 16/03/18 10:19 AM, Justin M Wozniak wrote:
>
> 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?

What's 'warn_ ()'? No such command here, nor any matching file.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-16 16:47       ` Ray Andrews
@ 2018-03-16 17:41         ` Marc Chantreux
  2018-03-16 18:13         ` Bart Schaefer
  1 sibling, 0 replies; 18+ messages in thread
From: Marc Chantreux @ 2018-03-16 17:41 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

hello,

> $ function test1 ()
> {
>     test2 # unchanged since last sourcing of this function.
>     setopt GRAB_ALIASES_AGAIN test2
>     test2 # Up-to-date.
>     test3
> }

you need to stack the alias definitions foreach aliases.
what you need is an associative array of arrays ... and
this is one of the boring limit of zsh (AFAIK): nested
structures needs files.

the best i have is: you cannot localize an alias but you can localize an
array

    alias my@='typeset -a'

    my@ test2=( echo this is it )
    () {
        my@ test2=( echo this is it )
        $test2
    }
    $test2


regards
marc












^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-15 21:46       ` Marc Chantreux
@ 2018-03-16 17:19         ` Justin M Wozniak
  2018-03-16 17:41           ` Ray Andrews
  2018-03-16 17:51           ` Marc Chantreux
  0 siblings, 2 replies; 18+ messages in thread
From: Justin M Wozniak @ 2018-03-16 17:19 UTC (permalink / raw)
  To: zsh-users


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


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-15 21:44     ` Marc Chantreux
  2018-03-15 21:46       ` 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-17 23:17       ` Bart Schaefer
  2 siblings, 2 replies; 18+ messages in thread
From: Ray Andrews @ 2018-03-16 16:47 UTC (permalink / raw)
  To: zsh-users

On 15/03/18 02:44 PM, Marc Chantreux wrote:
> 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.
Yeah, I know what to expect, I'm mostly just curious about possibilities.


$ alias test2="echo this is the original alias test2"
$ function test3 () { echo this is function test3 }

$ function test1 ()
{
     test2
     test3
}


$ alias test2="echo changed alias test2"

$ function test3 () { echo "test3 is now changed" }

$ test1
this is the original alias test2
test3 is now changed

... to be honest I can't even think of a reason to want anything more 
than what we have,  but I was curious.  One might imagine:

$ function test1 ()
{
     test2 # unchanged since last sourcing of this function.
     setopt GRAB_ALIASES_AGAIN test2
     test2 # Up-to-date.
     test3
}

Nevermind.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-15 21:44     ` Marc Chantreux
@ 2018-03-15 21:46       ` Marc Chantreux
  2018-03-16 17:19         ` Justin M Wozniak
  2018-03-16 16:47       ` Ray Andrews
  2018-03-17 23:17       ` Bart Schaefer
  2 siblings, 1 reply; 18+ messages in thread
From: Marc Chantreux @ 2018-03-15 21:46 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

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


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-15 18:13   ` Ray Andrews
@ 2018-03-15 21:44     ` Marc Chantreux
  2018-03-15 21:46       ` Marc Chantreux
                         ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: Marc Chantreux @ 2018-03-15 21:44 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

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


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-15 17:57 ` Jérémie Roquet
@ 2018-03-15 18:13   ` Ray Andrews
  2018-03-15 21:44     ` Marc Chantreux
  0 siblings, 1 reply; 18+ messages in thread
From: Ray Andrews @ 2018-03-15 18:13 UTC (permalink / raw)
  To: zsh-users

On 15/03/18 10:57 AM, Jérémie Roquet wrote:
> 2018-03-15 18:51 GMT+01:00 Ray Andrews <rayandrews@eastlink.ca>:
>> 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?
> You can use a function instead of an alias:
>
Sure, I was just wondering if it was possible at all with an alias. I 
expect not.  That's rather like asking for square circles but you don't 
know until 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.  It sounds like something 
that could be doable even it it isn't actually implemented right now.  
With interpreted code I'd expect it is theoretically possible.


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: real time alias?
  2018-03-15 17:51 Ray Andrews
@ 2018-03-15 17:57 ` Jérémie Roquet
  2018-03-15 18:13   ` Ray Andrews
  0 siblings, 1 reply; 18+ messages in thread
From: Jérémie Roquet @ 2018-03-15 17:57 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

2018-03-15 18:51 GMT+01:00 Ray Andrews <rayandrews@eastlink.ca>:
> 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?

You can use a function instead of an alias:

With an alias:

$ alias foo="echo first"
$ bar() { foo }
$ bar
first
$ alias foo="echo second"
$ bar
first

With a function:

$ foo() { echo first }
$ bar() { foo }
$ bar
first
$ foo() { echo second }
$ bar
second

Best regards,

-- 
Jérémie


^ permalink raw reply	[flat|nested] 18+ messages in thread

* real time alias?
@ 2018-03-15 17:51 Ray Andrews
  2018-03-15 17:57 ` Jérémie Roquet
  0 siblings, 1 reply; 18+ messages in thread
From: Ray Andrews @ 2018-03-15 17:51 UTC (permalink / raw)
  To: Zsh Users

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?

BTW, $IFS now under control, thanks all.


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2018-03-19  0:06 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <98aa0638-97ed-dfe0-8dd2-39129d02c084__39937.2481661432$1521136270$gmane$org@eastlink.ca>
2018-03-17 22:47 ` real time alias? Stephane Chazelas
2018-03-18  3:20   ` Ray Andrews
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
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

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).