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

Re: ordering of file-patterns completions



On 11/6/13, 9:03 AM, Bart Schaefer wrote:
On Nov 6,  8:24am, John wrote:
}
} I have a tool which needs one file-patterns for the first file matched,
} then the second and the rest should be of a different file-patterns.  I
} don't see a way to do this.

You probably want to write a custom completer with the _arguments utility
function, and to leave file-patterns unspecified.


     _foo() {
       _arguments '1:The first file:_files -g "XXX.*"' \
		 '*:All other files:_files -g "*.${words[2]:x}"'
     }

     compdef _foo foo

Here "1:" refers to the first argument (after skipping the command name
and any option flags) whereas "$words[2]" refers to the second word
of the entire command line (first word after the command name), so this
doesn't quite generalize if there may be options (words starting with
a hyphen) between the command name and the first file.  If that is the
case, you'll need to do some pre-processing of $words before calling
_arguments to determine which position to reference instead of [2].
Yeah, unfortunately I do. I was using _arguments to process those, and then zstyle to deal with the files. But I'm not quite understanding how to do what you're describing to pre-process them instead of using _arguments.

(You can skip the "compdef" command if you put the body of _foo in a file
named _foo in a directory referenced in your $fpath, and begin that file
with the line "#compdef foo".)

} Are there any examples of something similar to this around?

There are lots of examples of using _arguments in the default set of
completion functions.  If there is a standard tool that you know of that
accepts arguments something like your custom one, try looking at the
completion function for that.  You can usually find the name of that
function by looking at ${_comps[command-name-here]}.

I searched through a number of them, but haven't found anything which has helped yet.

John



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