zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Blast from the past: workers/18841
Date: Mon, 15 Jun 2020 15:25:01 -0700	[thread overview]
Message-ID: <CAH+w=7atm6r8eqvptwb1vYqRLy=9teoOr2NQaT7BQ4PbtiHu_g@mail.gmail.com> (raw)

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

On Sun, Jun 14, 2020 at 5:37 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> Incidentally the _prefix behavior with the unambiguous prefix is
> itself a compromise; before workers/18841 (2003-07-09) it would
> alarmingly delete the entire word from the line.

That's this bit of code (line numbers because gmail will probably line
wrap this):

     51     if [[ "$tmp" != _prefix ]] && "$tmp"; then
     52       [[ compstate[nmatches] -gt 1 ]] && return 0
     53       compadd -U -i "$IPREFIX" -I "$ISUFFIX" -
"${compstate[unambiguous]%$suf}x"
     54       compstate[list]=
     55       if [[ -n $compstate[unambiguous] ]]; then
     56         compstate[insert]=unambiguous
     57       else
     58         compstate[insert]=0
     59       fi
     60       return 0
     61     fi

The doc says:
     The completion code breaks the string to complete into seven fields
     in the order:
          <IPRE><APRE><HPRE><WORD><HSUF><ASUF><ISUF>
     The first field is an ignored prefix taken from the command line,
     the contents of the IPREFIX parameter plus the string given with
     the -i option.  With the -U option, only the string from the -i
     option is used.
Unless this doc is inaccurate, it's useless to even pass the -I option
on line 53.

Diagnosing this a bit further, I find that the reason parts of the
word vanish from the command line is because _path_files has already
added a completion for the directory prefix.  Consequently the compadd
performed by _prefix is a second one, which adds an ambiguity that was
not previously present, and compadd -U apparently selects the wrong
match to insert.

The below patch ALMOST fixes this.  The remaining problem is that in the example

% ls dir1/di_r2/dir3

The result is

% ls dir1/dir2_/r2/dir3

where the "2" immediately left of the cursor is highlighted.  The
slash isn't wanted, and any insertion at this point (including another
slash) appears after the 2; any cursor motion removes the 2.  This is
all side-effects of the way that _path_files has done its compadd, and
I haven't found a way to retroactively disable it.

[I'm sure gmail is going to line-wrap this.]

diff --git a/Completion/Base/Completer/_prefix
b/Completion/Base/Completer/_prefix
index 74be5f4..e256079 100644
--- a/Completion/Base/Completer/_prefix
+++ b/Completion/Base/Completer/_prefix
@@ -50,13 +50,19 @@ for tmp in "$comp[@]"; do

     if [[ "$tmp" != _prefix ]] && "$tmp"; then
       [[ compstate[nmatches] -gt 1 ]] && return 0
-      compadd -U -i "$IPREFIX" -I "$ISUFFIX" -
"${compstate[unambiguous]%$suf}x"
-      compstate[list]=
-      if [[ -n $compstate[unambiguous] ]]; then
-        compstate[insert]=unambiguous
-      else
+      # Does the following need pattern quoting on $PREFIX?
+      if [[ ${compstate[unambiguous]} = "$PREFIX"* ]]; then
+        # We completed something following the original prefix
         compstate[insert]=0
+        compstate[to_end]=
+      else
+        # We completed something inside the original prefix
+        compstate[insert]=unambiguous
+      fi
+      if [[ $compstate[nmatches] -eq 0 ]]; then
+        compadd -U -i "$IPREFIX" -S "$ISUFFIX" -
"${compstate[unambiguous]%$suf}"
       fi
+      compstate[list]=
       return 0
     fi
     (( _matcher_num++ ))

[-- Attachment #2: _prefix.txt --]
[-- Type: text/plain, Size: 1208 bytes --]

diff --git a/Completion/Base/Completer/_prefix b/Completion/Base/Completer/_prefix
index 74be5f4..e256079 100644
--- a/Completion/Base/Completer/_prefix
+++ b/Completion/Base/Completer/_prefix
@@ -50,13 +50,19 @@ for tmp in "$comp[@]"; do
 
     if [[ "$tmp" != _prefix ]] && "$tmp"; then
       [[ compstate[nmatches] -gt 1 ]] && return 0
-      compadd -U -i "$IPREFIX" -I "$ISUFFIX" - "${compstate[unambiguous]%$suf}x"
-      compstate[list]=
-      if [[ -n $compstate[unambiguous] ]]; then
-        compstate[insert]=unambiguous
-      else
+      # Does the following need pattern quoting on $PREFIX?
+      if [[ ${compstate[unambiguous]} = "$PREFIX"* ]]; then
+        # We completed something following the original prefix
         compstate[insert]=0
+        compstate[to_end]=
+      else
+        # We completed something inside the original prefix
+        compstate[insert]=unambiguous
+      fi
+      if [[ $compstate[nmatches] -eq 0 ]]; then
+        compadd -U -i "$IPREFIX" -S "$ISUFFIX" - "${compstate[unambiguous]%$suf}"
       fi
+      compstate[list]=
       return 0
     fi
     (( _matcher_num++ ))

                 reply	other threads:[~2020-06-15 22:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=7atm6r8eqvptwb1vYqRLy=9teoOr2NQaT7BQ4PbtiHu_g@mail.gmail.com' \
    --to=schaefer@brasslantern.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).