summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2005-05-23 10:11:18 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2005-05-23 10:11:18 +0000
commit8a24c5a5ee55ddec6896540de2e042abe009ed1e (patch)
tree53f3afed4752e85ac762c4c878e82f99d47c8fea
parentdc3668dd20133285416b6a3269c1d7ea81cdd57e (diff)
downloadzsh-8a24c5a5ee55ddec6896540de2e042abe009ed1e.tar.gz
zsh-8a24c5a5ee55ddec6896540de2e042abe009ed1e.tar.xz
zsh-8a24c5a5ee55ddec6896540de2e042abe009ed1e.zip
users/8856, users/8863: which-command stuff
-rw-r--r--ChangeLog8
-rw-r--r--Doc/Zsh/contrib.yo12
-rw-r--r--Functions/Zle/which-command42
-rw-r--r--Src/Zle/zle_tricky.c5
4 files changed, 66 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index eb4a019eb..e49570469 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2005-05-23  Peter Stephenson  <pws@csr.com>
+
+	* users/8863: Doc/Zsh/contrib.yo, Functions/Zle/which-command:
+	enhanced which-command that traces the final command better.
+
+	* users/8856: Src/Zle/zle_tricky.c: which-command shouldn't expand
+	aliases before passing first argument.
+
 2005-05-22  Bart Schaefer  <schaefer@zsh.org>
 
 	* unposted: Completion/Base/Widget/_complete_debug: add a colon
diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index dfa8efa3b..2303dccfc 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -858,6 +858,18 @@ example(zle -N insert-last-assignment smart-insert-last-word
 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)
diff --git a/Functions/Zle/which-command b/Functions/Zle/which-command
new file mode 100644
index 000000000..6e15c32a4
--- /dev/null
+++ b/Functions/Zle/which-command
@@ -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
diff --git a/Src/Zle/zle_tricky.c b/Src/Zle/zle_tricky.c
index 7d4fbd641..778305d18 100644
--- a/Src/Zle/zle_tricky.c
+++ b/Src/Zle/zle_tricky.c
@@ -2390,9 +2390,12 @@ int
 processcmd(UNUSED(char **args))
 {
     char *s;
-    int m = zmult;
+    int m = zmult, na = noaliases;
 
+    if (!strcmp(bindk->nam, "which-command"))
+	noaliases = 1;
     s = getcurcmd();
+    noaliases = na;
     if (!s)
 	return 1;
     zmult = 1;