zsh-workers
 help / color / mirror / code / Atom feed
* completion as if.
@ 2024-10-30 20:21 Eric Cook
  2024-10-30 23:51 ` Matthew Martin
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Cook @ 2024-10-30 20:21 UTC (permalink / raw)
  To: zsh-workers

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?


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

* Re: completion as if.
  2024-10-30 20:21 completion as if Eric Cook
@ 2024-10-30 23:51 ` Matthew Martin
  2024-11-07  0:28   ` Oliver Kiddle
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Martin @ 2024-10-30 23:51 UTC (permalink / raw)
  To: zsh-workers

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


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

* Re: completion as if.
  2024-10-30 23:51 ` Matthew Martin
@ 2024-11-07  0:28   ` Oliver Kiddle
  0 siblings, 0 replies; 3+ messages in thread
From: Oliver Kiddle @ 2024-11-07  0:28 UTC (permalink / raw)
  To: zsh-workers

On 30 Oct, Matthew Martin wrote:
> On Wed, Oct 30, 2024 at 04:21:12PM -0400, Eric Cook wrote:
> > 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

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

I don't see any obvious issue with this and I can believe that users
have a need for an easy way to do this.

I would be inclined to call _dispatch directly. The first two big blocks
of _normal aren't needed but would be harmless other than wasting time
on redundant checks. The use of _set_command possibly is needed, however.
As far as I'm concerned, feel free to commit it in either form.

The nearest thing we have already is.

  compdef command=context

But that does take a context which could be a -value- or -redirect-
or something. And that can't be used as a helper function.

Oliver


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

end of thread, other threads:[~2024-11-07  0:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-30 20:21 completion as if Eric Cook
2024-10-30 23:51 ` Matthew Martin
2024-11-07  0:28   ` Oliver Kiddle

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