Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Determining whether a function is used in an arithmetic context
- X-seq: zsh-users 22782
- From: dana <dana@xxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Determining whether a function is used in an arithmetic context
- Date: Mon, 24 Jul 2017 22:47:39 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dana-is.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=bjYvcbYQZGHHdx9+ZMQlzwjpPyno/idduNPR85MH/ME=; b=MIFRqlYVt2AzMDVeQTSxhZLP/kxHO4Gkb2RfASL+z9Z2LWB9ZxXDo6j4dmPP/RvpFU MQG3l0egbVsyAJU1rczFoQDPJZLZiTl2dJo2at0xNtD/RUcVXpta+EiAp5ORzCAdOQqN nD0NP3fZHuRBqs/AsTAF4TYCvMAzD7uk8vhIj3koloaoZjEVPtd0TQlMy7/X3z9JY6sI KjFSanPSQLuWDutC7vKtiUzqPkWC78GnivSlnfyYHoRZdjmOAezxJLfjhslFA5ovhSO4 V7784IJjs/U98/NV6MGY18/95REkoeGboqqHhOVkjiXM5M6yEQDOUopBqdk+yfmNhy7X cL+A==
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author