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

Re: which-command help



Bart Schaefer wrote:
> On May 17,  3:47pm, Peter Stephenson wrote:
> } Subject: Re: which-command help
> }
> } > # but if I use "^[?" (is which-command) I get:
> } > 
> } >  ~ % which-command /bin/ls
> } > /bin/ls
> } 
> } 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.
> 
> Compare zsh-workers/20895 and surrounding thread.  Are we breaking one
> thing to fix another?

Well, your reply in that message was:

> Suppose you have
> 
>   alias LL='ls -lL'
> 
> Do you want run-help to display the man page for "ls", or do you want it
> to simply fail because there is no LL command?

Actually, it *doesn't* fail after the patch:

% alias LL='ls -lL'
% which-command LL
ls -lL

I would have said that's more useful, because it's clearer about what's
going on (even more so if which-command is aliased to something more
verbose).  However, I'm certainly not set on the patch.  I would be
happy to suggest using the editor widget instead.  That could even be
modified to expand the alias and lookup the first word as a command,
noting that aliases work recursively:

which-command() {
  zmodload -i zsh/parameter

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

  while true; do
    wd=${wds[1]}
    rawwd=${(Q)wd}

    # replace "whence -c" with your favourite function
    # (but NOT which-command!)
    if [[ $rawwd != $wd || -n $seen[$rawwd] ]]; then
      # quoted or already expanded, don't expand alias
      (unalias -- $rawwd 2>/dev/null; whence -c $rawwd)
    else
      # turn on globsubst for =ls etc.
      whence -c ${~rawwd}
      if [[ -n $aliases[$rawwd] && -z $seen[$rawwd] ]]; then
        # Recursively expand aliases
        seen[$rawwd]=1
        wds=(${(z)aliases[$rawwd]})
	continue
      fi
    fi
    break
  done
}

This might be worth sticking in Functions/Zle.

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