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

Re: Using the same completion function for various commands



On Mon, 6 Dec 2010 16:18:47 +0100
Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
> Ah, er, right. But. Is there any way to combine -p and saying foo=du ?
> When I tried compdef _du -p 'foo_*=du' nothing happened.

The current syntax isn't really powerful enough for that.  The
"foo_*=du" gets stored in _patcomps and then gets compared against the
word on the command line.  You'd need to put foo_* in the key and
squirrel the "=du" away somewhere else.

You could do it by (gasp) hacking the value, for example.

(I'm surprised we don't already sanitise the option settings within
compdef, seeing as you can call it from anywhere.  I suppose we've
got away with it because most people just have it running implicitly
from within compinit.)

Index: Completion/compinit
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/compinit,v
retrieving revision 1.25
diff -p -u -r1.25 compinit
--- Completion/compinit	8 Feb 2010 11:53:35 -0000	1.25
+++ Completion/compinit	6 Dec 2010 16:06:47 -0000
@@ -230,6 +230,10 @@ comppostfuncs=()
 
 compdef() {
   local opt autol type func delete new i ret=0 cmd svc
+  local -a match mbegin mend
+
+  emulate -L zsh
+  setopt extendedglob
 
   # Get the options.
 
@@ -364,10 +368,18 @@ compdef() {
         else
           case "$type" in
           pattern)
-            _patcomps[$1]="$func"
+	    if [[ $1 = (#b)(*)=(*) ]]; then
+	      _patcomps[$match[1]]="=$match[2]=$func"
+	    else
+	      _patcomps[$1]="$func"
+	    fi
             ;;
           postpattern)
-            _postpatcomps[$1]="$func"
+	    if [[ $1 = (#b)(*)=(*) ]]; then
+	      _postpatcomps[$match[1]]="=$match[2]=$func"
+	    else
+	      _postpatcomps[$1]="$func"
+	    fi
             ;;
           *)
             if [[ "$1" = *\=* ]]; then
Index: Completion/Base/Core/_dispatch
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Base/Core/_dispatch,v
retrieving revision 1.4
diff -p -u -r1.4 _dispatch
--- Completion/Base/Core/_dispatch	3 Jun 2004 15:14:59 -0000	1.4
+++ Completion/Base/Core/_dispatch	6 Dec 2010 16:06:47 -0000
@@ -2,6 +2,7 @@
 
 local comp pat val name i ret=1 _compskip="$_compskip"
 local curcontext="$curcontext" service str noskip
+local -a match mbegin mend
 
 # If we get the option `-s', we don't reset `_compskip'.
 
@@ -24,6 +25,10 @@ if [[ "$_compskip" != (all|*patterns*) ]
     [[ -n "$str" ]] || continue
     service="${_services[$str]:-$str}"
     for i in "${(@)_patcomps[(K)$str]}"; do
+      if [[ $i = (#b)"="([^=]#)"="(*) ]]; then
+	service=$match[1]
+	i=$match[2]
+      fi
       eval "$i" && ret=0
       if [[ "$_compskip" = *patterns* ]]; then
         break

-- 
Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom



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