zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Zsh list <zsh-workers@zsh.org>
Subject: Re: Non-escaped special chars from var expansion
Date: Fri, 2 Nov 2018 22:06:57 +0000	[thread overview]
Message-ID: <20181102220657.396df24a@pws-HP.localdomain> (raw)
In-Reply-To: <CAHSx_Svj04+0L2Lcxymegscn9aG+fsuE6jnv7cQvD67oprE1mA@mail.gmail.com>

On Fri, 2 Nov 2018 13:39:26 -0700
Wayne Davison <wayne@opencoder.net> wrote:
> I have a particular compinit setup where special-chars from a var (other
> than spaces) are not backslash-escaped.  To duplicate:
> 
> zsh -f
> autoload -Uz compinit
> compinit
> zstyle ':completion:*' completer _oldlist _expand _complete _match _ignored
> _files _prefix
> zstyle ':completion:*:expand:*' tag-order all-expansions
> bindkey -e
> bindkey '\t' complete-word
> mkdir 'foo; bar (baz)'
> cd !:1
> cd $PWD<tab>
> 
> The result is "cd foo;\ bar\ (baz)" instead of "cd foo\;\ bar\ \(baz\)".

Don't breathe too loudly but I *think* I might have worked out what's
going on.  (Which the author of the code obviously wasn't banking on.)

Your file name is making the attempted glob fail; it's been
deliberately silenced, so you don't notice.  But that also has the
effect of abandoning the attempt to quote the expression.

So I think we need to apply the quoting without the "~" that turns on
active glob characters any time we didn't get globbing to work.

(Whether the globbing should actually have worked is a question I
haven't gone into.  But I suspect it's not a problem --- the _expand
function is trying lots of things not all of which will work.)

Maybe?

diff --git a/Completion/Base/Completer/_expand b/Completion/Base/Completer/_expand
index ee3681bad..f4909826a 100644
--- a/Completion/Base/Completer/_expand
+++ b/Completion/Base/Completer/_expand
@@ -103,9 +103,19 @@ subd=("$exp[@]")
 
 # Now try globbing.
 
-[[ "$force" = *g* ]] || zstyle -T ":completion:${curcontext}:" glob &&
-    eval 'exp=( ${~exp//(#b)\\([ 	\"'"\'"'
+# We need to come out of this with consistent quoting, by hook or by crook.
+integer done_quote
+local orig_exp=$exp
+if [[ "$force" = *g* ]] || zstyle -T ":completion:${curcontext}:" glob; then
+  eval 'exp=( ${~exp//(#b)\\([ 	\"'"\'"'
+])/$match[1]} ); exp=( ${(q)exp} )' 2>/dev/null && done_quote=1
+fi
+# If the globbing failed, or we didn't try globbing, we'll do
+# it again without the "~" so globbing is simply omitted.
+if (( ! done_quote )); then
+  eval 'exp=( ${orig_exp//(#b)\\([ 	\"'"\'"'
 ])/$match[1]} ); exp=( ${(q)exp} )' 2>/dev/null
+fi
 
 ### Don't remember why we once used this instead of the (q) above.
 #    eval 'exp=( ${~exp} ); exp=( ${exp//(#b)([][()|*?^#~<>\\=])/\\${match[1]}} )' 2>/dev/null

  reply	other threads:[~2018-11-02 22:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-02 20:39 Wayne Davison
2018-11-02 22:06 ` Peter Stephenson [this message]
2018-11-03 19:49   ` Peter Stephenson
2018-11-02 23:21 ` Wayne Davison
2019-11-17  6:00 ` Wayne Davison

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=20181102220657.396df24a@pws-HP.localdomain \
    --to=p.w.stephenson@ntlworld.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).