zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: Zsh User <zsh-users@sunsite.dk>
Subject: Re: which-command help
Date: Tue, 17 May 2005 15:47:28 +0100	[thread overview]
Message-ID: <200505171447.j4HElTUj011344@news01.csr.com> (raw)
In-Reply-To: <20050517132000.GB9627@let.rug.nl>

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

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


  reply	other threads:[~2005-05-17 14:47 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 [this message]
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       ` PATCH: " Peter Stephenson

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=200505171447.j4HElTUj011344@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).