zsh-users
 help / color / mirror / code / Atom feed
From: dana <dana@dana.is>
To: Zsh Users <zsh-users@zsh.org>
Cc: Bart Schaefer <schaefer@brasslantern.com>
Subject: Re: Determining whether a function is used in an arithmetic context
Date: Tue, 25 Jul 2017 16:20:29 -0500	[thread overview]
Message-ID: <CA+_8RBD5-P4Ej6LDc++mmZkEjV3ZhPrgg98QWpTdYN-VsASNEA@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7Zw050rLmJWN+ON27AZ=V7Qhace8ms8F-PYCp0biaUH5Q@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1243 bytes --]

I suppose that'll do. Thanks!


On 25 July 2017 at 02:03, Bart Schaefer <schaefer@brasslantern.com> wrote:

> On Mon, Jul 24, 2017 at 8:47 PM, dana <dana@dana.is> wrote:
> >
> > 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
>
> I guess I'd do it like this:
>
> myfunc() {
>   if [[ ${funcstack[2]} == myfunc_math ]]
>   then print In math context
>   else print Not in math context
>   fi
> }
> myfunc_math() {
>   myfunc "$@"
> }
>
> functions -M myfunc 1 1 myfunc_math
>
> > 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?)
>
> Yes, it's expected, because it's the parser state -- by the time the
> function is
> actually executing, the parser is done.  %_ is only meaningful during
> program
> input (typically interactively) or in an execution trace, not during
> execution itself.
> I think it's accidental that it has a value during arithmetic commands.
>

      reply	other threads:[~2017-07-25 21:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-25  3:47 dana
2017-07-25  7:03 ` Bart Schaefer
2017-07-25 21:20   ` dana [this message]

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+_8RBD5-P4Ej6LDc++mmZkEjV3ZhPrgg98QWpTdYN-VsASNEA@mail.gmail.com \
    --to=dana@dana.is \
    --cc=schaefer@brasslantern.com \
    --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).