From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
To: Sebastian Gniazdowski <sgniazdowski@gmail.com>,
Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [PATCH] Support the mksh's ${|func;} substitution
Date: Sat, 7 Sep 2019 20:09:57 +0200 [thread overview]
Message-ID: <CAKc7PVDC6NULei7FCKXvHu1YRzhmk9ZJK4BECRRxpWNQpHCw9w@mail.gmail.com> (raw)
In-Reply-To: <20190907150741.jwztdoslrvk5j7nk@chaz.gmail.com>
On Sat, 7 Sep 2019 at 17:07, Stephane Chazelas
<stephane.chazelas@gmail.com> wrote:
> Note that mksh's operator can do ${|REPLY=value}, it's not
> limited to functions.
Ok, true, it can also run binary commands.
> The ; is also not necessary
I think that this is undocumented feature, as the docs say:
"Another variant of substitution are the valsubs (value substitutions)
${|command;} which are also executed in the current environment, like
funsubs, but share their I/O with the parent; instead, they evaluate
to whatever the, initially empty, expression-local variable REPLY is
set to within the commands."
> Note that in zsh anything is a valid function name, just like
> just about anything is a valid command name. So your operator
> would be incompatible with mksh's if it accepted arbitrary
> function names unless you handled quoting in there:
>
> $ ./Src/zsh -c '"REPLY=value"() { REPLY=x; echo done; }; REPLY\=value; echo ${|REPLY=value;}'
> done
> zsh:1: no such function: REPLY=value
That's a valid and nice point, and it does yield some concerns,
however for any problems to appear the user would have to have at
least a function with "targetvariable=..." in name, i.e. a coincidence
of two places in code, the targetvar= in the ${|...;} and in the
function name, and this can be controlled and seems unlikely to occur
by accident.
> With those fixed, i.e. when it's really like mksh's ${|code},
> I'd agree the feature could be useful, but I suspect that would
> be harder to implement as it would mean changing the parsing.
The parsing would have to be changed to prevent the "=" in function names?
I think that I've chosen an initial wrong direction: to implement the
substitution "as-is", with it's form, treating it as a model. Instead,
I should have implemented the feature, not the substitution. Zsh has
its own ways to set-up complex substitutions and this is done via the
parens flags.
Would you consider such method, i.e. to not impose mksh's substitution
ways on Zsh, but instead assign a flag, like e.g.: ${(|)funct}, still
useful?
> Note that beside the math functions, zsh already has something
> similar with its "dynamic named directory" framework (a feature
> I always found quite obscure/far fetched myself).
>
> echo "${| REPLY=value}"
>
> could be done in zsh with:
>
> zsh_directory_name() {
> [[ $1 = d ]] && [[ $2 = //* ]] || return
> eval " ${2#//}"
> reply=("$REPLY" ${#2})
> }
>
> echo ${${${(D):-//REPLY=value}#\~\[}%\]}
>
> (even more convoluted than your math function approach).
That's interesting, it actually allows to do:
arr=( val1 val2 abc1 abc3 )
funct() { REPLY="${(C)MATCH}"; }
zsh_directory_name() { ... }
print -rl ${arr[@]//(#m)*/${${${(D):-//funct}#\~\[}%\]}}
Output:
Val1
Val2
Abc1
Abc3
--
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org
next prev parent reply other threads:[~2019-09-07 18:11 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-06 0:52 Sebastian Gniazdowski
2019-09-06 0:54 ` Sebastian Gniazdowski
2019-09-06 23:16 ` Sebastian Gniazdowski
2019-09-07 12:16 ` Daniel Shahaf
2019-09-07 15:07 ` Stephane Chazelas
2019-09-07 18:09 ` Sebastian Gniazdowski [this message]
2019-09-07 20:19 ` Stephane Chazelas
2019-09-07 21:19 ` Sebastian Gniazdowski
2019-09-10 2:20 ` Sebastian Gniazdowski
2019-09-10 5:29 ` Bart Schaefer
2019-09-10 18:21 ` Sebastian Gniazdowski
2019-09-10 19:38 ` Bart Schaefer
2019-09-12 0:08 ` Sebastian Gniazdowski
2019-09-12 1:03 ` Bart Schaefer
2019-09-12 2:06 ` Sebastian Gniazdowski
2019-09-12 5:35 ` Bart Schaefer
2019-09-12 6:00 ` Sebastian Gniazdowski
2019-09-12 6:55 ` Bart Schaefer
2019-09-13 20:28 ` Sebastian Gniazdowski
2019-09-13 21:33 ` Bart Schaefer
2019-09-13 21:36 ` Bart Schaefer
2019-09-14 0:41 ` Sebastian Gniazdowski
2019-09-14 0:44 ` Sebastian Gniazdowski
2023-05-03 14:46 Sebastian Gniazdowski
2023-07-04 1:55 ` Bart Schaefer
2023-07-04 18:53 ` Lawrence Velázquez
2023-07-05 5:32 ` Bart Schaefer
2023-07-05 6:30 ` Bart Schaefer
2023-07-06 1:39 ` Bart Schaefer
2023-07-06 2:27 ` Mikael Magnusson
2023-07-06 4:00 ` Bart Schaefer
2023-07-18 3:14 ` Bart Schaefer
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=CAKc7PVDC6NULei7FCKXvHu1YRzhmk9ZJK4BECRRxpWNQpHCw9w@mail.gmail.com \
--to=sgniazdowski@gmail.com \
--cc=zsh-workers@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).