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

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  7:03 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 [this message]
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='CAH+w=7Zw050rLmJWN+ON27AZ=V7Qhace8ms8F-PYCp0biaUH5Q@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=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).