zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <Peter.Stephenson@csr.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: Using the same completion function for various commands
Date: Mon, 6 Dec 2010 16:10:20 +0000	[thread overview]
Message-ID: <20101206161020.693f3f4a@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <AANLkTin8Kn88NXsK4V4Toos0-ZB2hj4RvhX5QxLarq1N@mail.gmail.com>

On Mon, 6 Dec 2010 16:18:47 +0100
Mikael Magnusson <mikachu@gmail.com> 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@csr.com>            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


  reply	other threads:[~2010-12-06 16:10 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-06 14:12 Martin Richter
2010-12-06 14:54 ` Mikael Magnusson
2010-12-06 15:01   ` Jérémie Roquet
2010-12-06 15:03     ` Mikael Magnusson
2010-12-06 15:11       ` Peter Stephenson
2010-12-06 15:18         ` Mikael Magnusson
2010-12-06 16:10           ` Peter Stephenson [this message]
2010-12-06 16:21             ` Peter Stephenson
2010-12-06 16:44               ` Bart Schaefer
2010-12-06 17:08                 ` Peter Stephenson
2010-12-06 19:39           ` Oliver Kiddle
2010-12-07 11:52             ` Peter Stephenson
2010-12-06 15:23         ` Martin Richter
2010-12-06 15:31         ` Jérémie Roquet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101206161020.693f3f4a@pwslap01u.europe.root.pri \
    --to=peter.stephenson@csr.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).