Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: completion as if.
On Wed, Oct 30, 2024 at 04:21:12PM -0400, Eric Cook wrote:
> Hey,
>
> For years now people have come to irc and asked a question along the lines of
> `how can i get my command to complete like this other command?' and over time
> the given answer became to mess with the $words and $CURRENT (and times $service)
> parameters before calling the completer that has the desired completion.
>
> _othercommand() { words[1]=(docker run); (( CURRENT++ )); _docker }
> compdef _othercommand othercommand
>
> at some point noticing that the _normal function also works at tricking
> compsys into doing so.
>
> matthew martin floated the idea of shipping a function with zsh to easily
> facilitate this, with something like:
>
> #autoload
> local service=$1 words=("$words[@]") CURRENT=$CURRENT
> words[1]=("$@")
> (( CURRENT += $# - 1 ))
> _normal
>
>
> Does anyone (in particular oliver) see a problem with this approach or
> including a potential function with zsh?
Haven't gotten around to finding a way to check the yodl, but this is
the diff I had (with an addition of local). $service is set and local'd
by _dispatch which _normal calls, so I don't think _as_if needs to mess
with it. I'm also curious if there are any issues with this.
diff --git a/Completion/Base/Utility/_as_if b/Completion/Base/Utility/_as_if
new file mode 100644
index 000000000..84777ee32
--- /dev/null
+++ b/Completion/Base/Utility/_as_if
@@ -0,0 +1,6 @@
+#autoload
+
+local words=("$words[@]") CURRENT=$CURRENT
+words[1]=("$@")
+(( CURRENT += $# - 1 ))
+_normal
diff --git a/Doc/Zsh/compsys.yo b/Doc/Zsh/compsys.yo
index 77627bacc..9b7f91148 100644
--- a/Doc/Zsh/compsys.yo
+++ b/Doc/Zsh/compsys.yo
@@ -4290,6 +4290,14 @@ arguments. The first describes the first argument as a
be completed. The last description gives all other arguments the
description `var(page number)' but does not offer completions.
)
+findex(_as_if)
+item(tt(_as_if) var(command) [var(arg) ... ])(
+This function is useful when one command should be completed as if it were
+another command with particular arguments. For example to complete tt(foo) as
+if it were tt(bar --baz), use
+
+example(compdef '_as_if bar --baz' foo)
+)
findex(_cache_invalid)
item(tt(_cache_invalid) var(cache_identifier))(
This function returns status zero if the completions cache corresponding to
Messages sorted by:
Reverse Date,
Date,
Thread,
Author