zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Marlon Richert <marlon.richert@gmail.com>,
	Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Bug report: Completion for dynamically named dirs fails when $SUFFIX is not empty
Date: Mon, 25 Jan 2021 15:07:18 +0000 (GMT)	[thread overview]
Message-ID: <1857440792.3267285.1611587238431@mail2.virginmedia.com> (raw)
In-Reply-To: <CAHLkEDsban8XbhMmn4nHHWAN2sxMS+b5vANamu-41Cr2zghRwA@mail.gmail.com>

> On 21 January 2021 at 14:37 Marlon Richert <marlon.richert@gmail.com> wrote:
> % cd $(mktemp -d)
> % ZDOTDIR=$PWD HOME=$PWD zsh -f
> % autoload -Uz compinit
> % compinit
> % zsh_directory_name() { [[ $1 == c ]] && compadd -S ']' foo }
>
> Given the above setup, when
> 
>    1. $LBUFFER is `cd ~[` or `cd ~[fo`,
>    2. $RBUFFER is empty, and
>    3. I press ^I,
>    4. then $LBUFFER becomes `cd ~[foo]`.
>  
>    1. $LBUFFER is `cd ~[`,
>    2. $RBUFFER is `fo` or `]`, and
>    3. I press ^I,
>    4. then completion beeps and the buffer remains unchanged.
> 
> `functions -t _complete` shows that the problem is caused by
> $compstate[context] becoming `tilde` instead of `subscript` as soon as
> $SUFFIX is non-empty, which causes _complete() to not call _subscript(),
> which is the only point of entry to _dynamic_directory_name().

The following is somewhere near, but this is quite complex and somewhat
at odds with completion in other contexts --- it's not clear to me
whether or not I should need that special diversion in _main_complete
but currently I do.  (Your compadd -S ']' is going to add too many
']'s in this case, I think, but that's a separate problem.)

pws

diff --git a/Completion/Base/Core/_main_complete b/Completion/Base/Core/_main_complete
index 6b2cf2bcf..2bcbd2118 100644
--- a/Completion/Base/Core/_main_complete
+++ b/Completion/Base/Core/_main_complete
@@ -94,8 +94,18 @@ if [[ -z "$compstate[quote]" ]]; then
   if [[ -o equals ]] && compset -P 1 '='; then
     compstate[context]=equal
   elif [[ "$PREFIX" != */* && "$PREFIX[1]" = '~' ]]; then
-    compset -p 1
-    compstate[context]=tilde
+    if [[ "$PREFIX" = '~['[^\]]## ]]; then
+      # Inside ~[...] should be treated as a subscript.
+      compset -p 2
+      # To be consistent, we ignore all but the contents of the square
+      # brackets.
+      compset -S '\]*'
+      compstate[context]=subscript
+      [[ -n $_comps[-subscript-] ]] && $_comps[-subscript-] && return
+    else
+      compset -p 1
+      compstate[context]=tilde
+    fi
   fi
 fi
 
diff --git a/Completion/Zsh/Context/_subscript b/Completion/Zsh/Context/_subscript
index 0c9a89ad5..0d9632864 100644
--- a/Completion/Zsh/Context/_subscript
+++ b/Completion/Zsh/Context/_subscript
@@ -1,6 +1,8 @@
 #compdef -subscript-
 
-local expl ind osuf=']' flags sep
+local expl ind osuf flags sep
+
+[[ $ISUFFIX = *\]* ]] || osuf=\]
 
 if [[ "$1" = -q ]]; then
   compquote osuf
diff --git a/Functions/Chpwd/zsh_directory_name_cdr b/Functions/Chpwd/zsh_directory_name_cdr
index cb72e4600..b653e7c38 100644
--- a/Functions/Chpwd/zsh_directory_name_cdr
+++ b/Functions/Chpwd/zsh_directory_name_cdr
@@ -16,8 +16,10 @@ elif [[ $1 = c ]]; then
     typeset -a keys values
     values=(${${(f)"$(cdr -l)"}/ ##/:})
     keys=(${values%%:*})
+    local addsuffix
+    [[ $ISUFFIX = *\]* ]] || addsuffix='-S]'
     _describe -t dir-index 'recent directory index' \
-      values -V unsorted -S']'
+      values -V unsorted $addsuffix
     return
   fi
 fi
diff --git a/Src/Zle/zle_main.c b/Src/Zle/zle_main.c
index 8c0534708..1622d8a6b 100644
--- a/Src/Zle/zle_main.c
+++ b/Src/Zle/zle_main.c
@@ -1067,6 +1067,7 @@ redrawhook(void)
 	int old_incompfunc = incompfunc;
 	char *args[2];
 	Thingy lbindk_save = lbindk, bindk_save = bindk;
+	struct modifier zmod_save = zmod;
 
 	refthingy(lbindk_save);
 	refthingy(bindk_save);
@@ -1094,6 +1095,7 @@ redrawhook(void)
 	 * restore lastcmd manually so that we don't mess up the global state
 	 */
 	lastcmd = lastcmd_prev;
+	zmod = zmod_save;
     }
 }


  reply	other threads:[~2021-01-25 15:07 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-21 14:37 Marlon Richert
2021-01-25 15:07 ` Peter Stephenson [this message]
2021-03-31  8:10   ` Marlon Richert
2021-03-31  8:25     ` Peter Stephenson
2021-03-31  8:29       ` Marlon Richert
2021-03-31  8:37         ` Marlon Richert
2021-03-31  8:56           ` Peter Stephenson
2021-05-07 14:08   ` [BUG] COMPLETE_IN_WORD fails to recognize brace_parameter context Marlon Richert
2021-05-08  0:04     ` Bart Schaefer
2021-05-08 13:04       ` Marlon Richert
2021-05-08  4:37     ` Bart Schaefer
2023-02-22 11:55   ` Bug report: Completion for dynamically named dirs fails when $SUFFIX is not empty Marlon Richert

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=1857440792.3267285.1611587238431@mail2.virginmedia.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=marlon.richert@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).