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

Re: completer for file extensions



On 28 February 2014 14:34, Oliver Kiddle <okiddle@xxxxxxxxxxx> wrote:
> This is a completer for filename extensions after *. (or ^*.). So, for
> example, you might have:
> % rm *.<tab>
> .patch  .pdf.gz   .tar.gz   .html   .gz
>
> File extensions are usually short so this probably doesn't seem
> particularly useful but it doesn't really interfere with anything else
> and it is actually sometimes useful. I think I originally wrote this
> with the idea of it being a simple example for the bash2zsh book but it
> didn't get used there and in the form below, it isn't quite as simple as
> it was.
>
> Directories before *. are handled, so /tmp/*. will complete extensions
> of files in /tmp but something like a*. is not: wouldn't be hard but I'm
> not sure it'd be useful.
>
> In the past I've always used this after _expand but I've attempted to
> make it better handle exact matches so it can be used before _expand or
> _match. Unfortunately, with compadd -O, you can't tell if any of the
> matches were exact.
>
> Oliver
>
> diff --git a/Completion/Base/Completer/_extensions b/Completion/Base/Completer/_extensions
> new file mode 100644
> index 0000000..2fcfe82
> --- /dev/null
> +++ b/Completion/Base/Completer/_extensions
> @@ -0,0 +1,32 @@
> +#autoload
> +
> +# This completer completes filename extensions when completing
> +# after *. or ^*. It can be used anywhere in the completer list
> +# but if used after _expand, patterns that already match a file
> +# will be expanded before it is called.
> +
> +compset -P '(#b)([~$][^/]#/|)(*/|)(\^|)\*.' || return 1
> +
> +local -aU files
> +local -a expl suf mfiles
> +
> +files=( ${(e)~match[1]}${match[2]}*.* ) || return 1
> +eval set -A files '${(MSI:'{1-${#${(O)files//[^.]/}[1]}}':)files%%.[^/]##}'

This - needs to be a .. or BRACE_CCL needs to be set locally. Even
with this fix, the completer does nothing for me. I tried printing
both $files and $mfiles and they contain more or less the correct list
of extensions, but nothing shows up through the completion system.

-- 
Mikael Magnusson



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