zsh-users
 help / color / mirror / code / Atom feed
* matching commands by pattern
@ 2006-10-13  9:04 Frank Terbeck
  2006-10-13  9:30 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Frank Terbeck @ 2006-10-13  9:04 UTC (permalink / raw)
  To: zsh users

Howdy list!

Due to a question in comp.unix.shell, I recently thought about the
following:

I'm trying to be able to use compsys to find all completions, that are
possible as the first word of the command-line _and_ match a given
pattern. Ideally, these matches should be accessible via a completion
menu.

E.g.:

% *com*<KEY-SEQUENCE>
- external command -
combine                  compose
comm                     composeglyphs
compare                  composite
compile_et               debcommit
- builtin command -
command                  comparguments
compadd                  compcall
- shell function -
compaudit                compdef
- alias -
which-command
- parameter -
commands
- original -
*com*

The solution I found is this:
[snip]
zmodload zsh/complist
autoload -U compinit && compinit
zstyle ':completion:::::' completer _complete _match
zstyle ':completion:*:descriptions' format "- %d -"
zstyle ':completion:*:default' list-prompt '%S%M matches%s'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' menu select
bindkey "^xd" menu-complete
[snap]

That way, I can enter a pattern (like *com*) and hit 'ctrl-x d' to get
into a nice menu with possible completions.

My questions are:
Is this the way one would do it?
Is there a possibility, to get this going with 'expand-or-complete'
instead of 'menu-complete'? That way you could use <TAB> to trigger
the menu. I think this could be done by not expanding filenames, if
completing the first word in the command-line. But I'm not sure, how
to achieve this.

Any comments are welcome.

Regards, Frank


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

* Re: matching commands by pattern
  2006-10-13  9:04 matching commands by pattern Frank Terbeck
@ 2006-10-13  9:30 ` Peter Stephenson
  2006-10-13 10:45   ` Frank Terbeck
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2006-10-13  9:30 UTC (permalink / raw)
  To: zsh users

Frank Terbeck wrote:
> I'm trying to be able to use compsys to find all completions, that are
> possible as the first word of the command-line _and_ match a given
> pattern. Ideally, these matches should be accessible via a completion
> menu.

It's easy: just set the option "globcomplete", that's what it's designed
for.  This makes pattern characters active during normal completion, so
you don't have to do anything else special to get command completion in
command position.

You need to ensure the *'s aren't being grabbed by non-compsys expansion
but that's probably already the case.  If there were files in your
directory matching the pattern and you had the _expand completer before
_complete then it would still try to do globbing first.

> Is there a possibility, to get this going with 'expand-or-complete'
> instead of 'menu-complete'? That way you could use <TAB> to trigger
> the menu. I think this could be done by not expanding filenames, if
> completing the first word in the command-line. But I'm not sure, how
> to achieve this.

Well, with globcomplete you should have the full range of completion
options, so autolist etc. etc. should work as normal.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

* Re: matching commands by pattern
  2006-10-13  9:30 ` Peter Stephenson
@ 2006-10-13 10:45   ` Frank Terbeck
  2006-10-13 17:20     ` Andrey Borzenkov
  0 siblings, 1 reply; 4+ messages in thread
From: Frank Terbeck @ 2006-10-13 10:45 UTC (permalink / raw)
  To: zsh users

Peter Stephenson <pws@csr.com>:
> Frank Terbeck wrote:
> > I'm trying to be able to use compsys to find all completions, that are
> > possible as the first word of the command-line _and_ match a given
> > pattern. Ideally, these matches should be accessible via a completion
> > menu.
> 
> It's easy: just set the option "globcomplete", that's what it's designed
> for.  This makes pattern characters active during normal completion, so
> you don't have to do anything else special to get command completion in
> command position.

Thank you, Peter.

This works. But it this way, glob_complete is in effect for every glob
on the command-line when trying to complete.

% ls *.txt<TAB>

drops into a menu, rather than expanding the glob into .txt files.
Would it be possible to enable glob_complete only for the 1st word on
the line other then the following code?

[snip]
function _mycomp () {
  setopt localoptions;
  local buf
  buf=(${(s: :)LBUFFER})                                                                                                                                        if [[ ${#buf} -eq 1 ]] ; then
    setopt globcomplete;
  else
    setopt noglobcomplete;
  fi
  zle expand-or-complete
}
zle -N _mycomp
bindkey "^I" _mycomp
[snap]

Regards, Frank


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

* Re: matching commands by pattern
  2006-10-13 10:45   ` Frank Terbeck
@ 2006-10-13 17:20     ` Andrey Borzenkov
  0 siblings, 0 replies; 4+ messages in thread
From: Andrey Borzenkov @ 2006-10-13 17:20 UTC (permalink / raw)
  To: zsh users

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Friday 13 October 2006 14:45, Frank Terbeck wrote:
> Peter Stephenson <pws@csr.com>:
> > Frank Terbeck wrote:
> > > I'm trying to be able to use compsys to find all completions, that are
> > > possible as the first word of the command-line _and_ match a given
> > > pattern. Ideally, these matches should be accessible via a completion
> > > menu.
> >
> > It's easy: just set the option "globcomplete", that's what it's designed
> > for.  This makes pattern characters active during normal completion, so
> > you don't have to do anything else special to get command completion in
> > command position.
>

wow, I did not even know this one exists :)

Personally I'm using these completers:

zstyle ':completion:*' completer _oldlist _complete _match

and it does the same, but with added bonus that all other goodies (like 
matchers) work too.

> Thank you, Peter.
>
> This works. But it this way, glob_complete is in effect for every glob
> on the command-line when trying to complete.
>
> % ls *.txt<TAB>
>
> drops into a menu, rather than expanding the glob into .txt files.

Well, you can always expand using expand-word (I believe, by default bound to 
^X*). Or you could add _all_matches completer, see man page:

_all_matches
     This completer can be used to add a string consisting of all other
     matches.  As it influences later completers it must appear as the
     first completer in the list.  The list of all matches is affected
     by the avoid-completer and old-matches styles described above.

So you have choice to include either all generated matches or only part 
thereof. It really is more flexible than old way.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)

iD8DBQFFL8r9R6LMutpd94wRAirFAKCgroYaPJPcQDiO4L69qMORmKkNQQCeMW9U
u9w4zgMfO26VH51uVuxCL18=
=JmGt
-----END PGP SIGNATURE-----


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

end of thread, other threads:[~2006-10-13 17:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-10-13  9:04 matching commands by pattern Frank Terbeck
2006-10-13  9:30 ` Peter Stephenson
2006-10-13 10:45   ` Frank Terbeck
2006-10-13 17:20     ` Andrey Borzenkov

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