zsh-workers
 help / color / mirror / code / Atom feed
* completer for file extensions
@ 2014-02-28 13:34 Oliver Kiddle
  2014-02-28 21:14 ` Mikael Magnusson
  0 siblings, 1 reply; 5+ messages in thread
From: Oliver Kiddle @ 2014-02-28 13:34 UTC (permalink / raw)
  To: Zsh workers

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%%.[^/]##}'
+
+if zstyle -t ":completion:${curcontext}:extensions" prefix-hidden; then
+  files=( ${files#.} )
+else
+  PREFIX=".$PREFIX"
+  IPREFIX="${IPREFIX%.}"
+fi
+
+zstyle -T ":completion:${curcontext}:extensions" add-space ||
+  suf=( -S '' )
+
+_description extensions expl 'file extension'
+
+# for an exact match, fail so as to give _expand or _match a chance.
+compadd -O mfiles "$expl[@]" -a files
+[[ $#mfiles -gt 1 || ${mfiles[1]} != $PREFIX ]] &&
+    compadd "$expl[@]" "$suf[@]" -a files &&
+    [[ -z $compstate[exact_string] ]]
diff --git a/Doc/Zsh/compsys.yo b/Doc/Zsh/compsys.yo
index c304461..603a5ad 100644
--- a/Doc/Zsh/compsys.yo
+++ b/Doc/Zsh/compsys.yo
@@ -3038,6 +3038,15 @@ This function is also a bindable command, see
 ifzman(the section `Bindable Commands' below)\
 ifnzman(noderef(Bindable Commands)).
 )
+findex(_extensions)
+item(tt(_extensions))(
+If the cursor follows the string `tt(*.)', filename extensions are
+completed. The extensions are taken from files in current directory or a
+directory specified at the beginning of the current word. For exact matches,
+completion continues to allow other completers such as tt(_expand) to
+expand the pattern. The standard tt(add-space) and tt(prefix-hidden)
+styles are observed.
+)
 findex(_history)
 item(tt(_history))(
 Complete words from the shell's command  history.  This completer 


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

* Re: completer for file extensions
  2014-02-28 13:34 completer for file extensions Oliver Kiddle
@ 2014-02-28 21:14 ` Mikael Magnusson
  2014-02-28 22:08   ` Oliver Kiddle
  0 siblings, 1 reply; 5+ messages in thread
From: Mikael Magnusson @ 2014-02-28 21:14 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Zsh workers

On 28 February 2014 14:34, Oliver Kiddle <okiddle@yahoo.co.uk> 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


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

* Re: completer for file extensions
  2014-02-28 21:14 ` Mikael Magnusson
@ 2014-02-28 22:08   ` Oliver Kiddle
  2014-03-01 22:54     ` Mikael Magnusson
  0 siblings, 1 reply; 5+ messages in thread
From: Oliver Kiddle @ 2014-02-28 22:08 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh workers

Mikael Magnusson wrote:
> This - needs to be a .. or BRACE_CCL needs to be set locally. Even

Thanks. It also needs to be .. to work for a number greater than 9.

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

I checked and it works for me starting from zsh -f.
If even mfiles contains a list of more than one extension then I can't
see why the compadd would fail to add matches. Have you got any matching
controls specified? What's your full completer list?

Oliver


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

* Re: completer for file extensions
  2014-02-28 22:08   ` Oliver Kiddle
@ 2014-03-01 22:54     ` Mikael Magnusson
  2014-03-03 15:33       ` Oliver Kiddle
  0 siblings, 1 reply; 5+ messages in thread
From: Mikael Magnusson @ 2014-03-01 22:54 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Zsh workers

On 28 February 2014 23:08, Oliver Kiddle <okiddle@yahoo.co.uk> wrote:
> Mikael Magnusson wrote:
>> This - needs to be a .. or BRACE_CCL needs to be set locally. Even
>
> Thanks. It also needs to be .. to work for a number greater than 9.
>
>> 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.
>
> I checked and it works for me starting from zsh -f.
> If even mfiles contains a list of more than one extension then I can't
> see why the compadd would fail to add matches. Have you got any matching
> controls specified? What's your full completer list?
>
> Oliver

Okay, my problem was just that I have a stupid
command_not_found_handler that tries to autoload and execute things,
and of course that is run in a subshell so no completions were added
in the parent shell. (eg, I forgot to rm .zcompdump).

I have a number of screenshots with . in the name and it causes these
completions to be added;
.00.png     .09.16.png  .13580      .16.png     .36.04.png  .58.png
   .letter     .wav
.04.png     .09.18.png  .14.png     .18.png     .36.45.png
.6.33.config  .log
.06.png     .09.20.png  .14460      .20.png     .37.04.png  .bin
   .log.13580
.09.11.png  .11.png     .14460-1    .27.00.png  .37.06.png  .cfg
   .log.14460
.09.12.png  .12.png     .14460-2    .32.18.png  .45.png     .conf
   .png
.09.14.png  .12863-1    .16.58.png  .33.config  .49.11.png  .config
   .tar

I guess you go for the first . to get stuff like .tar.gz? Tarballs
often have . in the name for version numbers so I have the same issue
in that directory;
.0.1.zip
.0.14.tar.gz
.0.16.tar.gz
.0.17-patches-0.1.tar.bz2
.0.2.tar.bz2
.0.2.tgz
.0.21.tgz


-- 
Mikael Magnusson


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

* Re: completer for file extensions
  2014-03-01 22:54     ` Mikael Magnusson
@ 2014-03-03 15:33       ` Oliver Kiddle
  0 siblings, 0 replies; 5+ messages in thread
From: Oliver Kiddle @ 2014-03-03 15:33 UTC (permalink / raw)
  To: Zsh workers

Mikael Magnusson wrote:
> I have a number of screenshots with . in the name and it causes these
> completions to be added;
> .00.png     .09.16.png  .13580      .16.png     .36.04.png  .58.png

> I guess you go for the first . to get stuff like .tar.gz? Tarballs

It takes all of them, so for foo-1.0.tar.gz, it will add .gz .tar.gz and
.0.tar.gz. Incidentally, that's what the {1.. brace expansion that was
mistakenly {1- is for (along with the I flag).

> often have . in the name for version numbers so I have the same issue
> in that directory;

Numbers alone are I think a rare choice for file extensions so I'll add
in:
  files=( ${files:#.<->(.*|)} )

You might still find cases where dots are used but not meant as
extensions: something like .rc2 or .5-dev-0 but you wouldn't want, e.g.
.mp3 filtered. You can always use an ignored-patterns style. The change
above is equivalent to doing:
  zstyle ':completion:*:extensions:*' ignored-patterns '.<->(.*|)'

Oliver


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

end of thread, other threads:[~2014-03-03 15:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-28 13:34 completer for file extensions Oliver Kiddle
2014-02-28 21:14 ` Mikael Magnusson
2014-02-28 22:08   ` Oliver Kiddle
2014-03-01 22:54     ` Mikael Magnusson
2014-03-03 15:33       ` Oliver Kiddle

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