zsh-users
 help / color / mirror / code / Atom feed
* auto-quoting inside braces in arguments
@ 2021-02-18 13:40 Vincent Lefevre
  2021-02-18 17:35 ` Daniel Shahaf
  0 siblings, 1 reply; 16+ messages in thread
From: Vincent Lefevre @ 2021-02-18 13:40 UTC (permalink / raw)
  To: zsh-users

Is there a way to get auto-quoting inside braces in arguments?
For instance,

  echo {a b}

would give 2 words "echo" and "{a b}" (which is different from what
a POSIX shell would give, but the behavior is already different
anyway, as one gets a parse error otherwise). Obviously this rule
should be applied only for arguments.

Alternatively, one could have a feature like url-quote-magic.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-18 13:40 auto-quoting inside braces in arguments Vincent Lefevre
@ 2021-02-18 17:35 ` Daniel Shahaf
  2021-02-18 18:30   ` Bart Schaefer
  0 siblings, 1 reply; 16+ messages in thread
From: Daniel Shahaf @ 2021-02-18 17:35 UTC (permalink / raw)
  To: zsh-users

Vincent Lefevre wrote on Thu, 18 Feb 2021 13:40 +00:00:
> Is there a way to get auto-quoting inside braces in arguments?
> For instance,
> 
>   echo {a b}
> 

Proof of concept:

f() {
  local l=\{ r=\}
  CURSOR=${BUFFER[(i)$l]} MARK=${BUFFER[(i)$r]}
  (( -- CURSOR ))
  zle quote-region
}

> would give 2 words "echo" and "{a b}" (which is different from what
> a POSIX shell would give, but the behavior is already different
> anyway, as one gets a parse error otherwise). Obviously this rule
> should be applied only for arguments.
> 
> Alternatively, one could have a feature like url-quote-magic.
> 
> -- 
> Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
> Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
> 
>


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-18 17:35 ` Daniel Shahaf
@ 2021-02-18 18:30   ` Bart Schaefer
  2021-02-18 18:52     ` Bart Schaefer
                       ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Bart Schaefer @ 2021-02-18 18:30 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Zsh Users

On Thu, Feb 18, 2021 at 9:35 AM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
>
> Vincent Lefevre wrote on Thu, 18 Feb 2021 13:40 +00:00:
> > Is there a way to get auto-quoting inside braces in arguments?
>
> Proof of concept:
>
> f() {
>   local l=\{ r=\}
>   CURSOR=${BUFFER[(i)$l]} MARK=${BUFFER[(i)$r]}
>   (( -- CURSOR ))
>   zle quote-region
> }

It needs to be a bit more complicated than that, because you first
have to be sure that the braces are not already quoted and do not form
a valid comma-separated (or ".." range) brace expression.  Also,
should anything special happen if there is a valid brace expression
inside the invalid brace pair?   Is this single- or double-quote
equivalent, or something new?

As an aside to the latter, it might be nice if providing a
prefix-argument to quote-region caused it to use double instead of
single quotes.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-18 18:30   ` Bart Schaefer
@ 2021-02-18 18:52     ` Bart Schaefer
  2021-02-19 12:24       ` Vincent Lefevre
  2021-02-18 18:57     ` Ray Andrews
  2021-02-19 12:17     ` Vincent Lefevre
  2 siblings, 1 reply; 16+ messages in thread
From: Bart Schaefer @ 2021-02-18 18:52 UTC (permalink / raw)
  To: Zsh Users

On Thu, Feb 18, 2021 at 10:30 AM Bart Schaefer
<schaefer@brasslantern.com> wrote:
>
> It needs to be a bit more complicated than that

There's one additional problem:

  { echo what does {this mean}

??  Currently the final brace matches the initial brace, not the brace
before "this".


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-18 18:30   ` Bart Schaefer
  2021-02-18 18:52     ` Bart Schaefer
@ 2021-02-18 18:57     ` Ray Andrews
  2021-02-18 19:07       ` Bart Schaefer
  2021-02-19 12:17     ` Vincent Lefevre
  2 siblings, 1 reply; 16+ messages in thread
From: Ray Andrews @ 2021-02-18 18:57 UTC (permalink / raw)
  To: zsh-users

On 2021-02-18 10:30 a.m., Bart Schaefer wrote:
>
> As an aside to the latter, it might be nice if providing a
> prefix-argument to quote-region caused it to use double instead of
> single quotes.
>
It reminds me of my issue just past of trying to send exactly:

'one string' 'second string'

to grep.  Given the deep desire of shells to strip and interpret quotes 
there's not
much that can be done via syntax, but some prefix or some option should 
make it
doable such that you don't lose one level of quotes at each interpretation.
Mind, I'm presuming Vincent needs those quotes further down stream to
protect something.  Anyway, some prefix or option should make it possible
to do whatever might be required.  setopt LITERAL_STRING.  Dunno, but most
of my problems with zsh involve quoting.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-18 18:57     ` Ray Andrews
@ 2021-02-18 19:07       ` Bart Schaefer
  2021-02-18 21:49         ` Ray Andrews
  2021-02-18 22:12         ` Daniel Shahaf
  0 siblings, 2 replies; 16+ messages in thread
From: Bart Schaefer @ 2021-02-18 19:07 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

On Thu, Feb 18, 2021 at 10:57 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> Given the deep desire of shells to strip and interpret quotes

You're still misinterpreting this.  It's pretty straightforward:
-- quotes are interpreted ONCE when the command line is processed.
-- the only time quotes are ever interpreted AGAIN is with "eval".

Stop "eval"-ing things and you'll stop having problems.

If for some reason "eval" is absolutely necessary, THEN you need to
assess what quoting interpretation you WOULD have used if the
argument(s) to eval were the original command line, and re-apply those
quotes, which zsh makes possible with ${(q)...} or ${(qqq)...} or
whatever.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-18 19:07       ` Bart Schaefer
@ 2021-02-18 21:49         ` Ray Andrews
  2021-02-18 22:12         ` Daniel Shahaf
  1 sibling, 0 replies; 16+ messages in thread
From: Ray Andrews @ 2021-02-18 21:49 UTC (permalink / raw)
  To: zsh-users

On 2021-02-18 11:07 a.m., Bart Schaefer wrote:
> On Thu, Feb 18, 2021 at 10:57 AM Ray Andrews <rayandrews@eastlink.ca> wrote:
>> Given the deep desire of shells to strip and interpret quotes
> You're still misinterpreting this.  It's pretty straightforward:
> -- quotes are interpreted ONCE when the command line is processed.
> -- the only time quotes are ever interpreted AGAIN is with "eval".
>
> Stop "eval"-ing things and you'll stop having problems.
As I was saying, my whole zsh universe revolves around it.  Might have been
a mistake at the getgo.  Still, every problem has been solved one way or
another and it's me trying to make the shell do what it doesn't want to do
so if it isn't easy that's my problem.
>
> If for some reason "eval" is absolutely necessary, THEN you need to
> assess what quoting interpretation you WOULD have used if the
> argument(s) to eval were the original command line, and re-apply those
> quotes, which zsh makes possible with ${(q)...} or ${(qqq)...} or
> whatever.
>
(q) gets close to my philosophical instinct of how the thing should 
work.  But if
there was some way of providing a string with absolute protection then I 
could
proceed naively -- the string is always going to be exactly as I typed 
it.  Nevermind
tho, my functions work 90% as close to my theoretical model as I could want.
Remaining issues are whiny not substantive.




^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-18 19:07       ` Bart Schaefer
  2021-02-18 21:49         ` Ray Andrews
@ 2021-02-18 22:12         ` Daniel Shahaf
  2021-02-18 22:44           ` Bart Schaefer
  1 sibling, 1 reply; 16+ messages in thread
From: Daniel Shahaf @ 2021-02-18 22:12 UTC (permalink / raw)
  To: Zsh Users

Bart Schaefer wrote on Thu, 18 Feb 2021 19:07 +00:00:
> Stop "eval"-ing things and you'll stop having problems.

+1.  You might still run into null elision, though.

> If for some reason "eval" is absolutely necessary, THEN you need to
> assess what quoting interpretation you WOULD have used if the
> argument(s) to eval were the original command line, and re-apply those
> quotes, which zsh makes possible with ${(q)...} or ${(qqq)...} or
> whatever.

Hang on.  Why would it matter which style of quotes would have been
used?  Considering «eval "$foo${(q)bar}$baz"», normally $foo would be
constructed to end on inter-token whitespace, and then the number of q's
wouldn't matter, whether it's 1, 2, 3, 4, q+, or q-.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-18 22:12         ` Daniel Shahaf
@ 2021-02-18 22:44           ` Bart Schaefer
  2021-02-19 16:10             ` Daniel Shahaf
  0 siblings, 1 reply; 16+ messages in thread
From: Bart Schaefer @ 2021-02-18 22:44 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Zsh Users

On Thu, Feb 18, 2021 at 2:13 PM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
>
> Hang on.  Why would it matter which style of quotes would have been
> used?

In your example, what if $foo and $baz have an opening and closing
quote in their values?


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-18 18:30   ` Bart Schaefer
  2021-02-18 18:52     ` Bart Schaefer
  2021-02-18 18:57     ` Ray Andrews
@ 2021-02-19 12:17     ` Vincent Lefevre
  2021-02-19 12:29       ` Vincent Lefevre
  2 siblings, 1 reply; 16+ messages in thread
From: Vincent Lefevre @ 2021-02-19 12:17 UTC (permalink / raw)
  To: zsh-users

On 2021-02-18 10:30:50 -0800, Bart Schaefer wrote:
> On Thu, Feb 18, 2021 at 9:35 AM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> >
> > Vincent Lefevre wrote on Thu, 18 Feb 2021 13:40 +00:00:
> > > Is there a way to get auto-quoting inside braces in arguments?
> >
> > Proof of concept:
> >
> > f() {
> >   local l=\{ r=\}
> >   CURSOR=${BUFFER[(i)$l]} MARK=${BUFFER[(i)$r]}
> >   (( -- CURSOR ))
> >   zle quote-region
> > }
> 
> It needs to be a bit more complicated than that, because you first
> have to be sure that the braces are not already quoted and do not form
> a valid comma-separated (or ".." range) brace expression.  Also,
> should anything special happen if there is a valid brace expression
> inside the invalid brace pair?   Is this single- or double-quote
> equivalent, or something new?

I think that { would just appear as a quote mechanism until a
matching }. This should probably similar to double-quote.
For instance

  echo {ab cd"ef }" $ZSH_VERSION {0..2} }

should be equivalent to

  echo "{ab cdef } $ZSH_VERSION {0..2} }"

though in practice, there would be only something simple inside { }.
For instance:

  svn diff -r{1 hour ago}

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-18 18:52     ` Bart Schaefer
@ 2021-02-19 12:24       ` Vincent Lefevre
  0 siblings, 0 replies; 16+ messages in thread
From: Vincent Lefevre @ 2021-02-19 12:24 UTC (permalink / raw)
  To: zsh-users

On 2021-02-18 10:52:01 -0800, Bart Schaefer wrote:
> On Thu, Feb 18, 2021 at 10:30 AM Bart Schaefer
> <schaefer@brasslantern.com> wrote:
> >
> > It needs to be a bit more complicated than that
> 
> There's one additional problem:
> 
>   { echo what does {this mean}
> 
> ??  Currently the final brace matches the initial brace, not the brace
> before "this".

This happens to be just because of the space before "mean", so that
the two braces are not in the same word. With

  { echo what does {this\ mean}

it doesn't.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-19 12:17     ` Vincent Lefevre
@ 2021-02-19 12:29       ` Vincent Lefevre
  2021-02-19 22:15         ` Bart Schaefer
  0 siblings, 1 reply; 16+ messages in thread
From: Vincent Lefevre @ 2021-02-19 12:29 UTC (permalink / raw)
  To: zsh-users

On 2021-02-19 13:17:05 +0100, Vincent Lefevre wrote:
> On 2021-02-18 10:30:50 -0800, Bart Schaefer wrote:
> > It needs to be a bit more complicated than that, because you first
> > have to be sure that the braces are not already quoted and do not form
> > a valid comma-separated (or ".." range) brace expression.  Also,
> > should anything special happen if there is a valid brace expression
> > inside the invalid brace pair?   Is this single- or double-quote
> > equivalent, or something new?
> 
> I think that { would just appear as a quote mechanism until a
> matching }. This should probably similar to double-quote.
> For instance
> 
>   echo {ab cd"ef }" $ZSH_VERSION {0..2} }
> 
> should be equivalent to
> 
>   echo "{ab cdef } $ZSH_VERSION {0..2} }"

though this would introduce a difference. Currently:

zira% echo {{a..c}
{a {b {c
zira% echo {{a..c}}
{a} {b} {c}
zira% echo {a..c}}
zsh: parse error near `}'

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-18 22:44           ` Bart Schaefer
@ 2021-02-19 16:10             ` Daniel Shahaf
  2021-02-19 16:50               ` Daniel Shahaf
  0 siblings, 1 reply; 16+ messages in thread
From: Daniel Shahaf @ 2021-02-19 16:10 UTC (permalink / raw)
  To: Zsh Users

Bart Schaefer wrote on Thu, Feb 18, 2021 at 14:44:29 -0800:
> On Thu, Feb 18, 2021 at 2:13 PM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> >
> > Hang on.  Why would it matter which style of quotes would have been
> > used?
> 
> In your example, what if $foo and $baz have an opening and closing
> quote in their values?

As I wrote in the part you'd snipped:

> > > normally $foo would be constructed to end on inter-token whitespace,

It's of course possible to write code where the number of q's in bar matters
(for instance, «eval ${:-\$}${(q…):-lorem}»), but that falls under "don't do
that".


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-19 16:10             ` Daniel Shahaf
@ 2021-02-19 16:50               ` Daniel Shahaf
  2021-02-19 21:53                 ` Bart Schaefer
  0 siblings, 1 reply; 16+ messages in thread
From: Daniel Shahaf @ 2021-02-19 16:50 UTC (permalink / raw)
  To: Zsh Users

Daniel Shahaf wrote on Fri, 19 Feb 2021 16:10 +00:00:
> Bart Schaefer wrote on Thu, Feb 18, 2021 at 14:44:29 -0800:
> > On Thu, Feb 18, 2021 at 2:13 PM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> > >
> > > Hang on.  Why would it matter which style of quotes would have been
> > > used?
> > 
> > In your example, what if $foo and $baz have an opening and closing
> > quote in their values?

By the way, the scenario you're implying is this:
.
    eval ${:-'"one'}${(qq):-two}${:-'three"'}
.
where (qq) was deliberately used so as not to interact with the literal
double-quote marks already in the value.

This sort of construct means there are _two_ levels of parsing that are
going to happen — one for each type of quotes (for instance, two
«eval»s, or an «eval» that runs «sh -c») — and therefore, $two should be
quoted twice, as in «zsh -c 'eval '${(q)${(q)lorem}» (where the outer
(q) is only correct because the command name happens to be «zsh»).

> As I wrote in the part you'd snipped:
> 
> > > > normally $foo would be constructed to end on inter-token whitespace,
> 
> It's of course possible to write code where the number of q's in bar matters
> (for instance, «eval ${:-\$}${(q…):-lorem}»), but that falls under "don't do
> that".


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-19 16:50               ` Daniel Shahaf
@ 2021-02-19 21:53                 ` Bart Schaefer
  0 siblings, 0 replies; 16+ messages in thread
From: Bart Schaefer @ 2021-02-19 21:53 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Zsh Users

On Fri, Feb 19, 2021 at 8:50 AM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
>
> This sort of construct means there are _two_ levels of parsing that are
> going to happen — one for each type of quotes

I can think of examples where e.g. the second level of parsing is not
the shell and therefore different quoting is required, but I concede
that this is not a common case.


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: auto-quoting inside braces in arguments
  2021-02-19 12:29       ` Vincent Lefevre
@ 2021-02-19 22:15         ` Bart Schaefer
  0 siblings, 0 replies; 16+ messages in thread
From: Bart Schaefer @ 2021-02-19 22:15 UTC (permalink / raw)
  To: Zsh Users

On Fri, Feb 19, 2021 at 4:17 AM Vincent Lefevre <vincent@vinc17.net> wrote:
>
> For instance
>
>   echo {ab cd"ef }" $ZSH_VERSION {0..2} }
>
> should be equivalent to
>
>   echo "{ab cdef } $ZSH_VERSION {0..2} }"

I think the other problems with this scheme are going to scuttle it
anyway, but I would point out that for every other quoting mechanism,
the quotes themselves are removed (leaving only the string that's
between them), but here you are suggesting that the braces remain in
the result.


On Fri, Feb 19, 2021 at 4:24 AM Vincent Lefevre <vincent@vinc17.net> wrote:
>
> On 2021-02-18 10:52:01 -0800, Bart Schaefer wrote:
> >
> >   { echo what does {this mean}
>
> This happens to be just because of the space before "mean",

Obviously, but isn't the point of your suggestion that said space
would no longer be significant?  Which is a problem for current usage.


On Fri, Feb 19, 2021 at 4:29 AM Vincent Lefevre <vincent@vinc17.net> wrote:
>
> though this would introduce a difference. Currently:
>
> zira% echo {{a..c}
> {a {b {c
> zira% echo {{a..c}}
> {a} {b} {c}
> zira% echo {a..c}}
> zsh: parse error near `}'

The first two are more problematic than the third.  In fact, the third
one wouldn't be changed by your suggestion at all, as far as I can
tell.


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2021-02-19 22:16 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-18 13:40 auto-quoting inside braces in arguments Vincent Lefevre
2021-02-18 17:35 ` Daniel Shahaf
2021-02-18 18:30   ` Bart Schaefer
2021-02-18 18:52     ` Bart Schaefer
2021-02-19 12:24       ` Vincent Lefevre
2021-02-18 18:57     ` Ray Andrews
2021-02-18 19:07       ` Bart Schaefer
2021-02-18 21:49         ` Ray Andrews
2021-02-18 22:12         ` Daniel Shahaf
2021-02-18 22:44           ` Bart Schaefer
2021-02-19 16:10             ` Daniel Shahaf
2021-02-19 16:50               ` Daniel Shahaf
2021-02-19 21:53                 ` Bart Schaefer
2021-02-19 12:17     ` Vincent Lefevre
2021-02-19 12:29       ` Vincent Lefevre
2021-02-19 22:15         ` Bart Schaefer

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).