From: Ray Andrews <rayandrews@eastlink.ca>
To: Zsh Users <zsh-users@zsh.org>
Subject: activate alias inside subshell
Date: Fri, 6 Apr 2018 09:29:11 -0700 [thread overview]
Message-ID: <604319cb-d86f-686b-ac9b-00d21650edff@eastlink.ca> (raw)
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?
next reply other threads:[~2018-04-06 16:29 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-04-06 16:29 Ray Andrews [this message]
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
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=604319cb-d86f-686b-ac9b-00d21650edff@eastlink.ca \
--to=rayandrews@eastlink.ca \
--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).