Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Adding array content to cd completion



Jean-Rene David <jrdavid@xxxxxxxx> wrote:
> I have an array of directories I frequently visit.
> I would like the completion of "cd" to include
> those directories, as a separate group.

It depends how much work you want to do.

If you don't mind them appearing as named directories you can do:

mydirs=(mydir1 mydir1)
zstyle -e ':completion::complete:cd:*:named-directories' fake '$mydirs'

If you really want a separate group it's harder.  The whole completion
system is written so that only a fixed set of tags are available for each
completion.

It's possible to use the tag-order to split a group into two, as described
in the manual.  You can use this with, say, the named-directories tag to
have a normal completion and your own context.  In your own context you
have to add your own matches using the fake style and then (the hairiest
part of the exercise) remove the normal matches with ignored-patterns.
Luckily in this case the matches to be ignored are parameter names
available from the associatiave array $nameddirs:

zstyle ':completion::complete:cd:*' tag-order \
  'named-directories:-mine:extra\ directories
named-directories:-normal:named\ directories *'
mydirs=(mydir1 mydir2)
zstyle -e ':completion::complete:cd:*:named-directories-mine' fake \
  'reply=($mydirs)'
zstyle -e ':completion::complete:cd:*:named-directories-mine' \
ignored-patterns 'reply=("(${(qkj.|.)nameddirs})")'

This seems to work.  It's not clear it's easier than redefining _cd.

Arguably there should be an easier way of saying only use the fake matches.
I think that's quite difficult since all the normal matches have been added
and are in the shell's internal structures by the time that style is
processed.

-- 
Peter Stephenson <pws@xxxxxxx>                  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



Messages sorted by: Reverse Date, Date, Thread, Author