From: dana <dana@dana.is>
To: zsh-users@zsh.org
Subject: Determining whether a function is used in an arithmetic context
Date: Mon, 24 Jul 2017 22:47:39 -0500 [thread overview]
Message-ID: <CA+_8RBDSKtMkaQM+UXxavB5NCc1M=HbBhiJbboCMDtCH2e37yA@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1963 bytes --]
Hey there,
I was playing with arithmetic functions recently and i wanted to have one
of them behave differently depending on whether it's used in an arithmetic
context or not. I can't seem to figure out a *reliable* way to detect that,
though.
The %_ prompt expansion seemed like it might be the way to go — it produces
math when used in an arithmetic command... but not an arithmetic
*substitution*, strangely. (Is that expected?)
The best luck i had was giving the function a different name when used in
arithmetic context, and then checking to see if %N matches $0. However,
this only works when function_argzero is enabled, and unfortunately i think
i need that turned off, since ZSH_ARGZERO is not available in zsh <5.3.
See below for the result of my experiments. Have i missed something?
Cheers
dana
% zsh --version
zsh 5.3.1 (x86_64-apple-darwin16.6.0)
% cat /tmp/foo
myfunc() {
(( $1 ))
printf ' $zec %s\n' ${zsh_eval_context:-\?}
printf ' $ft %s\n' ${functrace:-\?}
printf ' $fs %s\n' ${funcstack:-\?}
printf ' $0 %s\n' ${0:-\?}
printf ' $_ %s\n' ${_:-\?}
printf ' $ZAZ %s\n' ${ZSH_ARGZERO:-\?}
printf ' %%N %s\n' ${${(%):-%N}:-\?}
printf ' %%_ %s\n' ${${(%):-%_}:-\?}
}
functions -M func 1 1 myfunc
print 'Normal context:'
myfunc 1
print 'Arithmetic substitution context:'
: $(( func(1) ))
print 'Arithmetic command context:'
(( func(1) ))
% zsh /tmp/foo
Normal context:
$zec toplevel
$zec shfunc
$ft /tmp/foo:16
$fs myfunc
$0 myfunc
$_ myfunc
$ZAZ /tmp/foo
%N myfunc
%_ ?
Arithmetic substitution context:
$zec toplevel
$zec shfunc
$ft /tmp/foo:19
$fs myfunc
$0 func
$_ func
$ZAZ /tmp/foo
%N myfunc
%_ ?
Arithmetic command context:
$zec toplevel
$zec shfunc
$ft /tmp/foo:22
$fs myfunc
$0 func
$_ func
$ZAZ /tmp/foo
%N myfunc
%_ math
next reply other threads:[~2017-07-25 3:48 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-25 3:47 dana [this message]
2017-07-25 7:03 ` Bart Schaefer
2017-07-25 21:20 ` dana
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='CA+_8RBDSKtMkaQM+UXxavB5NCc1M=HbBhiJbboCMDtCH2e37yA@mail.gmail.com' \
--to=dana@dana.is \
--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).