zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: Zsh User <zsh-users@sunsite.dk>
Subject: PATCH: Re: which-command help
Date: Wed, 18 May 2005 11:27:38 +0100	[thread overview]
Message-ID: <200505181027.j4IARchB003301@news01.csr.com> (raw)
In-Reply-To: <200505171555.j4HFtUOb012962@news01.csr.com>

Latest version of the which-command widget.  Added a style to give the
`whence' variant.  Also fixed up the correct behaviour in the case of
some abstruse aliases:  for example, after 'alias \ls=ls', you might get
something like

% \ls<Esc-?>
\ls: aliased to ls
ls: aliased to ls --color=tty
ls () {
        local ls
        if [[ -n $LS_COLORS ]]
        then
                ls=(ls --color=auto)
        else
                ls=(ls -F)
        fi
        command $ls $*
}

(The remainder is an example of what happens when your distributed
initialisation scripts are over-clever...)

"whence -c" might not be the best default for the type command seeing
that functions can be arbitrarily long.

You should be able to use this with any recent zsh by taking the second
hunk and stripping off the initial +'s.

[I meant to send this yesterday but didn't hit C-c C-c for some
reason... I hope I haven't forgotten something...]

Index: Doc/Zsh/contrib.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/contrib.yo,v
retrieving revision 1.41
diff -u -r1.41 contrib.yo
--- Doc/Zsh/contrib.yo	21 Feb 2005 14:40:35 -0000	1.41
+++ Doc/Zsh/contrib.yo	17 May 2005 18:04:24 -0000
@@ -858,6 +858,18 @@
 zstyle :insert-last-assignment match '[[:alpha:]][][[:alnum:]]#=*'
 bindkey '\e=' insert-last-assignment)
 )
+tindex(which-command)
+item(tt(which-command))(
+This function is a drop-in replacement for the builtin widget
+tt(which-command).  It has enhanced behaviour, in that it correctly
+detects whether or not the command word needs to be expanded as an
+alias; if so, it continues tracing the command word from the expanded
+alias until it reaches the command that will be executed.
+
+The style tt(whence) is available in the context tt(:zle:$WIDGET); this
+may be set to an array to give the command and options that will be used to
+investigate the command word found.  The default is tt(whence -c).
+)
 enditem()
 
 subsect(Styles)
Index: Functions/Zle/which-command
===================================================================
RCS file: Functions/Zle/which-command
diff -N Functions/Zle/which-command
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Functions/Zle/which-command	17 May 2005 18:04:24 -0000
@@ -0,0 +1,42 @@
+zmodload -i zsh/parameter zsh/zutil
+
+zle -I
+
+local -a whencecmd wds
+
+# Set the whence style to your favourite function
+# (but NOT which-command!)
+zstyle -a :zle:$WIDGET whence whencecmd || whencecmd=(whence -c --)
+
+wds=(${(z)LBUFFER})
+local wd barewd
+local -A seen
+
+while true; do
+  wd=${wds[1]}
+  barewd=${(Q)wd}
+
+  if [[ $barewd != $wd || -n $seen[$barewd] ]]; then
+    # quoted or already expanded, see if original word is an alias...
+    if [[ -z $seen[$barewd] && -n $aliases[$wd] ]]; then
+      # yes, so we need to decode that, with no extra expansion...
+      $whencecmd $wd
+      seen[$wd]=1
+      wds=(${(z)aliases[$wd]})
+      continue
+    else
+      # use unquoted word, don't expand alias
+      (unalias -- $barewd 2>/dev/null; $whencecmd $barewd)
+    fi
+  else
+    # turn on globsubst for =ls etc.
+    $whencecmd ${~barewd}
+    if [[ -n $aliases[$barewd] && -z $seen[$barewd] ]]; then
+      # Recursively expand aliases
+      seen[$barewd]=1
+      wds=(${(z)aliases[$barewd]})
+      continue
+    fi
+  fi
+  break
+done

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************


      parent reply	other threads:[~2005-05-18 10:28 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-17 13:20 Francisco Borges
2005-05-17 14:47 ` Peter Stephenson
2005-05-17 15:32   ` Bart Schaefer
2005-05-17 15:55     ` Peter Stephenson
2005-05-17 17:06       ` Bart Schaefer
2005-05-17 17:47         ` Peter Stephenson
2005-05-18 10:27       ` Peter Stephenson [this message]

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=200505181027.j4IARchB003301@news01.csr.com \
    --to=pws@csr.com \
    --cc=zsh-users@sunsite.dk \
    /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).