zsh-users
 help / color / mirror / code / Atom feed
* activate alias inside subshell
@ 2018-04-06 16:29 Ray Andrews
  2018-04-07 19:57 ` Ray Andrews
  0 siblings, 1 reply; 8+ messages in thread
From: Ray Andrews @ 2018-04-06 16:29 UTC (permalink / raw)
  To: Zsh Users

Sourcing this:

    mag=$'\e[35;1m'
    cyn=$'\e[36;1m'
    nrm=$'\e[0m'

    alias msg='echo $cyn alias outside $nrm'
    whence -v msg
    msg "called outside"

    function test1 ()
    {
    (
    msg "called inside"
    unalias msg
    alias msg='echo $mag alias inside $nrm'
    whence -v msg
    msg "called inside function"
    )
    }
    echo "\nnow test:\n"
    test1

    whence -v msg
    msg "called outside function"

    echo "\n==================================\n"

I get:

    15 /aWorking/Zsh/Source/Wk 7 $ . test1
    msg is an alias for echo $cyn alias outside $nrm << fine
      alias outside  called outside << fine

    now test:

      alias outside  called inside << fine
    msg is an alias for echo $mag alias inside $nrm << whence is correct
      alias outside  called inside function << but the old alias is used
    anyway.
    msg is an alias for echo $cyn alias outside $nrm << original alias
    undisturbed, good.
      alias outside  called outside function   << fine

    ==================================

... so I have an apparent localization of the alias as far as whence is 
concerned but it is not acted upon.  Can that be rectified?  I tried a 
few variations, one succeeds in having it exactly backwards, the 
external alias is used inside and the redefined alias is used 
externally, but a second run fixes that, it seems the alias must always 
be defined before the function is sourced.  Can we have instant 
gratification with an alias change inside a function?  I'd have thought 
that the subshell would make it easy.  In any case one would think that 
the alias that whence reports would be the alias in effect.

What I'm actually trying to do is reduce verbosity of a function by 
redefining various message printer functions as null.  It works fine 
with functions, but if the message printers are aliases (which seem to 
be the only way to get:  ${(%):-%x %I} ... line information printed, it 
seems that can't be done in a function) ... then it goes sour.  For example:

     [[ "$vverbose" < 3 ]] &&
     {
         warningmsg () { ; }
      }

... I kill the message by nullifying the function that prints it (it's 
nothing but a colorized line).  In the subshell it's all local, no 
damage outside the function.  But when debugging I like the line 
numbers, so 'warningmsg' becomes an alias:

     alias warningmsg=' echo -en "${(%):-%x %I}: " && magline ' # Just 
colorizes.  I wish we could do that in a function.

... but now, when I want to kill messages, as above, efforts to neuter 
the alias fail.  The whole thing is very suspect.  I had thought to 
redefine the alias like this:

         local alias warningmsg='#'

... which is probably outrageous, but the idea is that it would turn the 
text of the message into a comment.  I'm probably best to forget the 
whole thing, but I do love the line numbers and killing messages by 
nullifying message functions works like a charm ... but not with 
aliases.  Can I have my cake and eat it too?  Some elegant solution?





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

end of thread, other threads:[~2018-04-09  4:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-06 16:29 activate alias inside subshell Ray Andrews
2018-04-07 19:57 ` Ray Andrews
2018-04-08  7:39   ` Bart Schaefer
2018-04-08 14:56     ` Ray Andrews
2018-04-08 20:18       ` Bart Schaefer
2018-04-08 20:54         ` Ray Andrews
2018-04-08 22:38       ` Ray Andrews
2018-04-09  4:11         ` Ray Andrews

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