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

PATCH: Re: which-command help

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 ]]
                ls=(ls --color=auto)
                ls=(ls -F)
        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)
+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).
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 --)
+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

Peter Stephenson <pws@xxxxxxx>                  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.


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