zsh-users
 help / color / mirror / code / Atom feed
* Command Completion
@ 2012-09-11 12:55 Nick Cross
  2012-09-11 14:15 ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Nick Cross @ 2012-09-11 12:55 UTC (permalink / raw)
  To: zsh-users


Hi,

Would anyone be able to help with fixing a completion script? Or would 
the IRC channel be better>

The https://github.com/defunkt/hub project provides extra functions to 
git and supplies the following completion script:

https://github.com/defunkt/hub/blob/master/etc/hub.zsh_completion

Ideally I would like to use the hub binary command to complete standard 
git commands plus the extra commands it provides (e.g. pull-request). 
While I can see how to write a basic completion for a command what I'm 
not sure about is how to include and add to another completion set.

Thanks!

Nick



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

* Re: Command Completion
  2012-09-11 12:55 Command Completion Nick Cross
@ 2012-09-11 14:15 ` Bart Schaefer
  0 siblings, 0 replies; 11+ messages in thread
From: Bart Schaefer @ 2012-09-11 14:15 UTC (permalink / raw)
  To: zsh-users

On Sep 11,  1:55pm, Nick Cross wrote:
}
} https://github.com/defunkt/hub/blob/master/etc/hub.zsh_completion
} 
} Ideally I would like to use the hub binary command to complete standard 
} git commands plus the extra commands it provides (e.g. pull-request). 

I suggest you start by looking at Completion/Unix/Command/_git, more
specifically the function _git_commands.  I don't know the answer to
the comment it poses at the end (about _alternative vs. the stack of
calls to _describe) but I suspect you could add one more _describe -t
with the hub commands.

To avoid mucking with _git_commands, just use a wrapper that ends with
something like

local ret=1
_git_commands && ret=0
_describe -t hub-extra-commands 'extra command from hub project' ... \
	&& ret=0
return ret

(where "..." is what you fill in).


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

* Re: command completion
  2011-08-15  4:13         ` Bart Schaefer
@ 2011-08-15  9:18           ` Vincent Lefevre
  0 siblings, 0 replies; 11+ messages in thread
From: Vincent Lefevre @ 2011-08-15  9:18 UTC (permalink / raw)
  To: zsh-users

On 2011-08-14 21:13:41 -0700, Bart Schaefer wrote:
> Most people are probably already have
> 
> zstyle ':completion:*:complete:*' group-name ''
> 
> and are therefore content with
> 
> zstyle ':completion:*:complete:-command-:*' group-order commands
> 
> which makes sure that the command names are offered first, rather
> than mixed with the directory names.

However the number of keys typed is not reduced by that. I mean,
instead of getting the command completion with one TAB, one may
need more because matching directories are in the way.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)


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

* Re: command completion
  2011-08-15  0:57       ` Vincent Lefevre
@ 2011-08-15  4:13         ` Bart Schaefer
  2011-08-15  9:18           ` Vincent Lefevre
  0 siblings, 1 reply; 11+ messages in thread
From: Bart Schaefer @ 2011-08-15  4:13 UTC (permalink / raw)
  To: zsh-users

On Aug 15,  2:57am, Vincent Lefevre wrote:
}
} > [...] because there are two passes and there are no
} > *additional* completions for the "command" tag added inside _cd, all
} > the defaults for _cd get added as well.
} 
} OK, but this behavior is a bit strange: if I use
} 
}   zstyle ':completion:*:complete:-command-:*' tag-order 'commands'
} 
} on my example, then I get:
}   % prefix-
}   prefix-cmd1  prefix-dir/
} from the "dir" directory. And from the parent directory "temp_dir",
} prefix[TAB] gives prefix-cmd1 only (i.e. prefix-dir/ has mysteriously
} disappeared though it is a cd target).

To understand that you have to read comments in _cd, explaining that
it assumes (incorrectly because of your tag-order) that _command_names
has already added all the names of local subdirectories, so _cd should
not do so again.

Wheels within wheels ...
 
} However wouldn't it be cleaner if there were a specific
} tag for autocd?

Yes, but to do that would require abstracting the guts of both _cd
and _command_names somehow so they could be combined into a single
_tags loop in _autocd.  As you're the only person who has complained
about this in many years, there's been no impetus to make that effort.

Most people are probably already have

zstyle ':completion:*:complete:*' group-name ''

and are therefore content with

zstyle ':completion:*:complete:-command-:*' group-order commands

which makes sure that the command names are offered first, rather
than mixed with the directory names.


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

* Re: command completion
  2011-08-14 23:56     ` Bart Schaefer
@ 2011-08-15  0:57       ` Vincent Lefevre
  2011-08-15  4:13         ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Vincent Lefevre @ 2011-08-15  0:57 UTC (permalink / raw)
  To: zsh-users

On 2011-08-14 16:56:50 -0700, Bart Schaefer wrote:
> As I said before, it's really got to do with how _autocd insinuates itself
> into the process.
> 
> Autocd makes itself the handler for -command-.  It then calls both
> _command_names and _cd, each of which takes a spin through _alternative,
> so it goes around the _tags loop twice as well.
> 
> If there were only one pass through _alternative, I think you'd get what
> you're looking for; but because there are two passes and there are no
> *additional* completions for the "command" tag added inside _cd, all
> the defaults for _cd get added as well.

OK, but this behavior is a bit strange: if I use

  zstyle ':completion:*:complete:-command-:*' tag-order 'commands'

on my example, then I get:
  % prefix-
  prefix-cmd1  prefix-dir/
from the "dir" directory. And from the parent directory "temp_dir",
prefix[TAB] gives prefix-cmd1 only (i.e. prefix-dir/ has mysteriously
disappeared though it is a cd target).

> So, unfortunately, I don't believe you can get what you want with
> zstyles.  You'll have to rewrite _autocd, something like so:
> 
> #compdef -command-
> _command_names && return 0
> [[ -o autocd ]] && _cd && return 0
> return 1

This works. However wouldn't it be cleaner if there were a specific
tag for autocd?

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)


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

* Re: command completion
  2011-08-14 23:07   ` Vincent Lefevre
@ 2011-08-14 23:56     ` Bart Schaefer
  2011-08-15  0:57       ` Vincent Lefevre
  0 siblings, 1 reply; 11+ messages in thread
From: Bart Schaefer @ 2011-08-14 23:56 UTC (permalink / raw)
  To: zsh-users

On Aug 15,  1:07am, Vincent Lefevre wrote:
} Subject: Re: command completion
}
} On 2011-08-14 15:14:53 -0700, Bart Schaefer wrote:
} > Here we have the key.  In order to stop "remaining tags" from being tried,
} > the value "-" must appear somewhere in the completion style.  So your
} > problem is solved by:
} > 
} >     zstyle ':completion:*:complete:-command-:*' tag-order 'commands' -
} 
} Not really. I still wish to generate a completion for directories in
} the cdpath if there are no matches.

Ah, I see.

As I said before, it's really got to do with how _autocd insinuates itself
into the process.

Autocd makes itself the handler for -command-.  It then calls both
_command_names and _cd, each of which takes a spin through _alternative,
so it goes around the _tags loop twice as well.

If there were only one pass through _alternative, I think you'd get what
you're looking for; but because there are two passes and there are no
*additional* completions for the "command" tag added inside _cd, all
the defaults for _cd get added as well.

So, unfortunately, I don't believe you can get what you want with
zstyles.  You'll have to rewrite _autocd, something like so:

#compdef -command-
_command_names && return 0
[[ -o autocd ]] && _cd && return 0
return 1


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

* Re: command completion
  2011-08-14 22:14 ` Bart Schaefer
@ 2011-08-14 23:07   ` Vincent Lefevre
  2011-08-14 23:56     ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Vincent Lefevre @ 2011-08-14 23:07 UTC (permalink / raw)
  To: zsh-users

On 2011-08-14 15:14:53 -0700, Bart Schaefer wrote:
> Here we have the key.  In order to stop "remaining tags" from being tried,
> the value "-" must appear somewhere in the completion style.  So your
> problem is solved by:
> 
>     zstyle ':completion:*:complete:-command-:*' tag-order 'commands' -
> 
> (note trailing dash).

Not really. I still wish to generate a completion for directories in
the cdpath if there are no matches. Consider the following example:

autoload -U compinit
compinit
zstyle ':completion:*:complete:-command-:*' tag-order 'commands executables builtins functions aliases suffix-aliases reserved-words jobs parameters' -
mkdir temp_dir
cd temp_dir
mkdir bin dir prefix-dir
touch bin/prefix-cmd1
chmod 755 bin/prefix-cmd1
touch dir/prefix-cmd2
chmod 755 dir/prefix-cmd2
path=($PWD/bin $path)
echo $path
rehash
cdpath=(. $PWD)
setopt AUTO_CD
cd dir

If I type prefix[TAB][TAB], I get:

% prefix-cmd
prefix-cmd1   prefix-cmd2*

as more or less expected (I don't know why prefix-cmd2 as an asterisk
but not prefix-cmd1).

However, if I type prefix-d[TAB], I get no completions, while I would
like "prefix-dir".

If I remove the trailing dash from the zstyle line and I type
prefix[TAB][TAB], I get:

% prefix-
prefix-cmd1   prefix-cmd2*  prefix-dir/

which is not what I want either.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)


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

* Re: command completion
  2011-08-14 20:59 command completion Vincent Lefevre
  2011-08-14 21:09 ` Mikael Magnusson
@ 2011-08-14 22:14 ` Bart Schaefer
  2011-08-14 23:07   ` Vincent Lefevre
  1 sibling, 1 reply; 11+ messages in thread
From: Bart Schaefer @ 2011-08-14 22:14 UTC (permalink / raw)
  To: zsh-users

On Aug 14, 10:59pm, Vincent Lefevre wrote:
}
} I asked a question several years ago about command/function/etc.
} completion, but never had an answer. I'd like command completion
} to be preferred over directory completion.
} 
} % mkdir ~/blah-hdir
} % cdpath=(. ~)
} % setopt AUTO_CD
} % blah[TAB]
} 
} then both blah-cmd and blah-hdir/ are proposed. I don't see why
} blah-hdir/ should be regarded as a command.

This is because the _autocd completer in the zsh distribution has
explictly inserted itself as a completion function for the context
named -command-, via its #compdef line.

This is further complicated by what may be an inaccuracy in the zsh
manual description of the tag-order style.  It says:

     The values for the style are sets of space-separated lists of tags.
     The tags in each value will be tried at the same time; if no match
     is found, the next value is used. [...]
     [...example elided...]  Remaining tags will be
     tried if no completions are found.

What it really means, though, is that remaining tags (even those not
explicitly listed in the tag-order style) are tried if no UNAMBIGUOUS
completion is found.  That last sentence really belongs before the
example, and should be clarified.

The doc then goes on:

     In addition to tag names, each string in the value may take one of
     the following forms:

    -
          If any value consists of only a hyphen, then _only_ the tags
          specified in the other values are generated.  Normally all
          tags not explicitly selected are tried last if the specified
          tags fail to generate any matches.  This means that a single
          value consisting only of a single hyphen turns off completion.

Here we have the key.  In order to stop "remaining tags" from being tried,
the value "-" must appear somewhere in the completion style.  So your
problem is solved by:

    zstyle ':completion:*:complete:-command-:*' tag-order 'commands' -

(note trailing dash).


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

* Re: command completion
  2011-08-14 21:09 ` Mikael Magnusson
@ 2011-08-14 21:54   ` Vincent Lefevre
  0 siblings, 0 replies; 11+ messages in thread
From: Vincent Lefevre @ 2011-08-14 21:54 UTC (permalink / raw)
  To: zsh-users

On 2011-08-14 23:09:37 +0200, Mikael Magnusson wrote:
> On 14 August 2011 22:59, Vincent Lefevre <vincent@vinc17.net> wrote:
[...]
> > Now, if I move blah-cmd somewhere else in $PATH, and use
> >
> > % zstyle ':completion:*:complete:-command-:*' tag-order 'commands'
> > % blah[TAB]
> >
> > then only blah-cmd is proposed, as expected. However if I do:
> >
> > % mkdir ~/blah-hdir
> > % cdpath=(. ~)
> > % setopt AUTO_CD
> > % blah[TAB]
> >
> > then both blah-cmd and blah-hdir/ are proposed. I don't see why
> > blah-hdir/ should be regarded as a command.
> 
> Because it might contain executable files?

Then what?

First, zsh does not complete to such executable files, so that's
a wrong reason. Moreover such executable files cannot be executed
anyway, because they are not in the path!

And if the current directory happens to be $HOME, zsh does not
propose blah-hdir/ in the completions.

It seems the reason is that the directory (and possible subdirectories)
is a candidate for the autocd. But my point is that such a directory is
not a command ('commands' was specified first in tag-order).

> What happens when you only create the dir and don't do the other
> things?

Both the "cdpath=(. ~)" and "setopt AUTO_CD" are necessary to make
this (IMHO incorrect) behavior appear. Otherwise blah-hdir/ is not
proposed as a completion.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)


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

* Re: command completion
  2011-08-14 20:59 command completion Vincent Lefevre
@ 2011-08-14 21:09 ` Mikael Magnusson
  2011-08-14 21:54   ` Vincent Lefevre
  2011-08-14 22:14 ` Bart Schaefer
  1 sibling, 1 reply; 11+ messages in thread
From: Mikael Magnusson @ 2011-08-14 21:09 UTC (permalink / raw)
  To: zsh-users

On 14 August 2011 22:59, Vincent Lefevre <vincent@vinc17.net> wrote:
> I asked a question several years ago about command/function/etc.
> completion, but never had an answer. I'd like command completion
> to be preferred over directory completion.
>
> For instance, if I create an executable file blah-cmd and a directory
> blah-dir in the current directory ("." being in the $PATH), and do:
>
> % rehash
> % zstyle ':completion:*:complete:-command-:*' tag-order 'commands'
> % bl[TAB]
>
> then I can see that various commands are proposed, but not blah-cmd.
>
> ^Xh (_complete_help) at this point outputs:
>
> tags in context :completion::complete:-command-::
>    commands executables builtins functions aliases suffix-aliases reserved-words jobs parameters  (_alternative _command_names _autocd (eval))
>    commands                                                                                       (_path_commands _alternative _command_names _autocd (eval))
>    jobs                                                                                           (_jobs _alternative _command_names _autocd (eval))
>    parameters                                                                                     (_parameters _alternative _command_names _autocd (eval))
>
> Unfortunately not all the tags are described in the zshcompsys
> man page, and the description is quite short, so that I don't
> understand why blah-cmd hasn't been proposed.
>
> Then I've tried:
>
> % zstyle ':completion:*:complete:-command-:*' tag-order 'executables'
>
> but then both blah-cmd and blah-dir/ are proposed.
>
> Now, if I move blah-cmd somewhere else in $PATH, and use
>
> % zstyle ':completion:*:complete:-command-:*' tag-order 'commands'
> % blah[TAB]
>
> then only blah-cmd is proposed, as expected. However if I do:
>
> % mkdir ~/blah-hdir
> % cdpath=(. ~)
> % setopt AUTO_CD
> % blah[TAB]
>
> then both blah-cmd and blah-hdir/ are proposed. I don't see why
> blah-hdir/ should be regarded as a command.

Because it might contain executable files? What happens when you only
create the dir and don't do the other things?

-- 
Mikael Magnusson


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

* command completion
@ 2011-08-14 20:59 Vincent Lefevre
  2011-08-14 21:09 ` Mikael Magnusson
  2011-08-14 22:14 ` Bart Schaefer
  0 siblings, 2 replies; 11+ messages in thread
From: Vincent Lefevre @ 2011-08-14 20:59 UTC (permalink / raw)
  To: zsh-users

I asked a question several years ago about command/function/etc.
completion, but never had an answer. I'd like command completion
to be preferred over directory completion.

For instance, if I create an executable file blah-cmd and a directory
blah-dir in the current directory ("." being in the $PATH), and do:

% rehash
% zstyle ':completion:*:complete:-command-:*' tag-order 'commands'
% bl[TAB]

then I can see that various commands are proposed, but not blah-cmd.

^Xh (_complete_help) at this point outputs:

tags in context :completion::complete:-command-::
    commands executables builtins functions aliases suffix-aliases reserved-words jobs parameters  (_alternative _command_names _autocd (eval)) 
    commands                                                                                       (_path_commands _alternative _command_names _autocd (eval)) 
    jobs                                                                                           (_jobs _alternative _command_names _autocd (eval)) 
    parameters                                                                                     (_parameters _alternative _command_names _autocd (eval))

Unfortunately not all the tags are described in the zshcompsys
man page, and the description is quite short, so that I don't
understand why blah-cmd hasn't been proposed.

Then I've tried:

% zstyle ':completion:*:complete:-command-:*' tag-order 'executables'

but then both blah-cmd and blah-dir/ are proposed.

Now, if I move blah-cmd somewhere else in $PATH, and use

% zstyle ':completion:*:complete:-command-:*' tag-order 'commands'
% blah[TAB]

then only blah-cmd is proposed, as expected. However if I do:

% mkdir ~/blah-hdir
% cdpath=(. ~)
% setopt AUTO_CD
% blah[TAB]

then both blah-cmd and blah-hdir/ are proposed. I don't see why
blah-hdir/ should be regarded as a command.

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)


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

end of thread, other threads:[~2012-09-11 14:15 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-11 12:55 Command Completion Nick Cross
2012-09-11 14:15 ` Bart Schaefer
  -- strict thread matches above, loose matches on Subject: below --
2011-08-14 20:59 command completion Vincent Lefevre
2011-08-14 21:09 ` Mikael Magnusson
2011-08-14 21:54   ` Vincent Lefevre
2011-08-14 22:14 ` Bart Schaefer
2011-08-14 23:07   ` Vincent Lefevre
2011-08-14 23:56     ` Bart Schaefer
2011-08-15  0:57       ` Vincent Lefevre
2011-08-15  4:13         ` Bart Schaefer
2011-08-15  9:18           ` Vincent Lefevre

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