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

Re: which-command help



Francisco Borges wrote:
> Hello,
> 
> I can't get which-command ("^[?") to work as I would like:
> 
>  ~ % whence -c ls
> ls: aliased to /bin/ls -h -N --color=auto -F -v
> 
>  ~ % alias which-command='whence -c'
> # If I just run the command I get
> 
>  ~ % which-command ls
> ls: aliased to /bin/ls -h -N --color=auto -F -v
> 
> # but if I use "^[?" (is which-command) I get:
> 
>  ~ % which-command /bin/ls
> /bin/ls
> 
> How can I get ^[? to behave the same as a command line "which-command"?

The problem is that the processing of which-command within the editor
expands the alias, so that the command line that which-command sees
already has the /bin/ls expanded.

I think this is a bug, fixed below: we don't want to expand aliases when
getting the name of the command.

For now, you can use a workaround along the lines of:

  unalias which-command
  zle -N which-command
  which-command() { zle -I; whence -c ${${(zQ)LBUFFER}[1]}; }

That should cover almost any case where the builtin editor function
works.  (Don't forget the "unalias": the interaction between aliases and
function definitions isn't pretty.)

I note that quoted forms of the command don't work with the builtin
implementation of which-command, i.e. you can't expand 'ls' or \ls.
That's a bug, but it's rather harder to fix---probably not *that* hard,
though I haven't worked out the best way.

That's one area where the function works better.  However, it's still
not smart enough to realise that if the word is quoted it's not subject to
alias expansion.  Hmm, actually...

which-command() {
  zle -I
  local -a wds
  wds=(${(z)LBUFFER})
  local wd=${wds[1]}
  local rawwd=${(Q)wd}

  # replace "whence -c" with your favourite function
  # (but NOT which-command!)
  if [[ $rawwd != $wd ]]; then
    # quoted, don't expand alias
    (unalias -- $rawwd 2>/dev/null; whence -c $rawwd)
  else
    # turn on globsubst for =ls etc.
    whence -c ${~rawwd}
  fi
}

It also handles =cmd and even baroque constructions like /bin/l[sS]
correctly.

Unless anyone can see a flaw, I think I can probably recommend this over
the builtin editor function.

Index: Src/Zle/zle_tricky.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_tricky.c,v
retrieving revision 1.51
diff -u -r1.51 zle_tricky.c
--- Src/Zle/zle_tricky.c	23 Feb 2005 13:50:45 -0000	1.51
+++ Src/Zle/zle_tricky.c	17 May 2005 14:18:31 -0000
@@ -2354,8 +2354,10 @@
 getcurcmd(void)
 {
     int curlincmd;
+    int onoaliases = noaliases;
     char *s = NULL;
 
+    noaliases = 1;
     zleparse = 2;
     lexsave();
     metafy_line();
@@ -2381,6 +2383,7 @@
     inpop();
     errflag = zleparse = 0;
     lexrestore();
+    noaliases = onoaliases;
 
     return s;
 }

-- 
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