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

Re: help for a function and its completion



On Thu, 19 Mar 2009 22:58:39 +0100
ugaciaka <ugaciaka@xxxxxxxxx> wrote:
> ##################
> #extract files eg: ex tarball.tar#
> ##################
> ex () {
>...
> }
> 
> ##################
> #compress directory eg: cm tarball.tar tarball#
> ##################
> cm () {
> ...
> }

(I don't think your "compress" and "gzip" items will work quite the way
you've written them.  Also, "ex" is the name of an editor, although you
probably wouldn't want to run it directly, so that may not matter.
However, that's not what you're asking about.)

> I tried to understand how to write zstyle completion to make your life
> easier but are not very good.
> 
> I only wish that when I enter, eg,
> 
> cm directory directory.tar
> 
> postfix tar appear on a list where there are tar, tar.gz, etc.

You can use zstyle to tell the system that you only want certain files
completed after a command.  However, since you want something a little
bit more than that, to complete an archive as the first argument and a
directory as the second, it's best to use a new function.  You can call
the following "_cm" and put it in your function path (make sure the
#compdef is in the first line of the file):


#compdef cm ex

local expl

if [[ CURRENT -eq 3 && $service = cm ]]; then
  # Complete directory
  _wanted directories expl directory _path_files -/
else
  # Complete archive
  _wanted archives expl archive \
      _files -g '*.(tar.gz|bz2|rar|gz|tar|tbz2|tgz|zip|Z|7z)'
fi


Here's a summary of what's going on.

- The "#compdef cm ex" means that when you run "compinit" and it
  searches all your functions beginning with "_", it will find that this
  function does completion for the commands "cm" and "ex".

- The local variable "expl" is used by the completion system for
  temporarily saving explanations to display.  You'll see it's the
  second argument after "_wanted" later on.

- If the current argument is 3 (where the command counts a s 1), and the
  command is "cm" ($service stores the name of the command), then complete
  directories.

- Else complete a pattern giving one of the forms of compressed file or
  archive you want.

- The "_wanted <tag> expl <description>" is there to help the
  completion system know how to deal with the completion you're giving
  it.  It adds a description to the completion, as well as a tag that
  can be used if you want to make a zstyle for this completion.

- The important bit of the completions are these: for directories,
    _path_files -/
  This says complete files, with an option saying that the files must
  be directories.

- For the archives or compressed files,
    _files -g '*.(tar.gz|bz2|rar|gz|tar|tbz2|tgz|zip|Z|7z)'
  says complete files, and gives as an option the pattern that the files
  must match.

- The difference betewen _path_files and _files is that _files will
  also complete directories.  So if your archive is in another
  directory, you can still use completion to find it.  If you don't want
  that, you can change it to
    _path_files -g '*.(tar.gz|bz2|rar|gz|tar|tbz2|tgz|zip|Z|7z)'
  and it will still complete the archives, but not directories.

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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