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

completer for file extensions



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 



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