zsh-workers
 help / color / mirror / code / Atom feed
* expand-and-quote-alias
@ 2007-10-10 22:58 Jörg Sommer
  2007-10-11  8:59 ` expand-and-quote-alias Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Jörg Sommer @ 2007-10-10 22:58 UTC (permalink / raw)
  To: zsh-workers

Hi,

sometimes I want to not the whole alias. But expand-alias is useless,
because the expansion still uses the alias.

% alias ls='ls --color -lA'

I want it without -A

% ls<expand-alias>
% ls --color -lA

But this is

% ls --color -lA --color -lA

How can I do something like the following with one key, e.g. ^Xa

% ls<backward-word set-mark-command forward-word _expand-alias quote-region>
% 'ls --color -lA'

But, mhh, this is still not what I want. I want this:

% 'ls' --color -lA

How can I get it?

Bye, Jörg.
-- 
Computer Science is no more about Computers than astronomy is about
telescopes.
            (Edsger Wybe Dijkstra)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: expand-and-quote-alias
  2007-10-10 22:58 expand-and-quote-alias Jörg Sommer
@ 2007-10-11  8:59 ` Peter Stephenson
  2007-10-11 13:36   ` expand-and-quote-alias Jörg Sommer
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2007-10-11  8:59 UTC (permalink / raw)
  To: zsh-workers

On Wed, 10 Oct 2007 22:58:41 +0000 (UTC)
Jörg Sommer <joerg@alea.gnuu.de> wrote:
> sometimes I want to not the whole alias. But expand-alias is useless,
> because the expansion still uses the alias.
> 
> % alias ls='ls --color -lA'
> 
> I want it without -A
> 
> % ls<expand-alias>
> % ls --color -lA
> 
> But this is
> 
> % ls --color -lA --color -lA
> 
> How can I do something like the following with one key, e.g. ^Xa
> 
> % ls<backward-word set-mark-command forward-word _expand-alias quote-region>
> % 'ls --color -lA'
> 
> But, mhh, this is still not what I want. I want this:
> 
> % 'ls' --color -lA

You can safely set the whole alias to something along these lines,
  alias ls='\ls --color -lA'
say (a single "\" is enough to inhibit alias expansion).

With a little more work you can make _expand_alias do that.  You probably
want to base it on the latest version from the archive since there was a
bug that it used to remove quoting (although that doesn't quite impinge on
the problem in question).  Care is needed since if the alias expansion
starts with a special character you definitely don't want to add a
backslash.

With even more effort you can split it into words, see if the first word
is the same as the original first word, and only add the backslash in that
case.  This is non-invasive enough to be worth applying to the archive.
Here's the complete result.  It's not perfect but it handles normal
cases like the present one.


#compdef -K _expand_alias complete-word \C-xa

local word expl tmp pre sel what
local -a tmpa

eval "$_comp_setup"

if [[ -n $funcstack[2] ]]; then
  if [[ "$funcstack[2]" = _prefix ]]; then
    word="$IPREFIX$PREFIX$SUFFIX"
  else
    word="$IPREFIX$PREFIX$SUFFIX$ISUFFIX"
  fi
  pre=()
else
  local curcontext="$curcontext"

  if [[ -z "$curcontext" ]]; then
    curcontext="expand-alias-word:::"
  else
    curcontext="expand-alias-word:${curcontext#*:}"
  fi

  word="$IPREFIX$PREFIX$SUFFIX$ISUFFIX"
  pre=(_main_complete - aliases)
fi

zstyle -s ":completion:${curcontext}:" regular tmp || tmp=yes
case $tmp in
always) sel=r;;
yes|1|true|on) [[ CURRENT -eq 1 ]] && sel=r;;
esac
zstyle -T ":completion:${curcontext}:" global && sel="g$sel"
zstyle -t ":completion:${curcontext}:" disabled && sel="${sel}${(U)sel}"

tmp=
[[ $sel = *r* ]] && tmp=$aliases[$word]
[[ -z $tmp && $sel = *g* ]] && tmp=$galiases[$word]
[[ -z $tmp && $sel = *R* ]] && tmp=$dis_aliases[$word]
[[ -z $tmp && $sel = *G* ]] && tmp=$dis_galiases[$word]

if [[ -n $tmp ]]; then
  # We used to remove the quoting from the value in the parameter.
  # That was probably just an oversight: an alias is always replaced
  # literally.
  tmp=${tmp%%[[:blank:]]##}
  if [[ $tmp[1] = [[:alnum:]_] ]]; then
    tmpa=(${(z)tmp})
    if [[ $tmpa[1] = $word && $tmp = $aliases[$word] ]]; then
      # This is an active regular alias and the first word in the result
      # is the same as what was on the line already.  Quote it so
      # that it doesn't get reexanded on execution.
      #
      # Strictly we also need to check if the original word matches
      # a later word in the expansion and the previous words are
      # all aliases where the expansion ends in " ", but I'm
      # too lazy.
      tmp="\\$tmp"
    fi
  fi
  $pre _wanted aliases expl alias compadd -UQ ${tmp%%[[:blank:]]##}
elif (( $#pre )) && zstyle -t ":completion:${curcontext}:" complete; then
  $pre _aliases -s "$sel" -S ''
else
  return 1
fi


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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: expand-and-quote-alias
  2007-10-11  8:59 ` expand-and-quote-alias Peter Stephenson
@ 2007-10-11 13:36   ` Jörg Sommer
  0 siblings, 0 replies; 3+ messages in thread
From: Jörg Sommer @ 2007-10-11 13:36 UTC (permalink / raw)
  To: zsh-workers

Hallo Peter,

Peter Stephenson <pws@csr.com> wrote:
> You can safely set the whole alias to something along these lines,
>   alias ls='\ls --color -lA'
> say (a single "\" is enough to inhibit alias expansion).
>
> With a little more work you can make _expand_alias do that.  You probably
> want to base it on the latest version from the archive since there was a
> bug that it used to remove quoting (although that doesn't quite impinge on
> the problem in question).  Care is needed since if the alias expansion
> starts with a special character you definitely don't want to add a
> backslash.
>
> With even more effort you can split it into words, see if the first word
> is the same as the original first word, and only add the backslash in that
> case.  This is non-invasive enough to be worth applying to the archive.
> Here's the complete result.  It's not perfect but it handles normal
> cases like the present one.

Thanks, it works like a charm. Really cool.

bye, Jörg.
-- 
“UNIX was not designed to stop people from doing stupid things, because
 that would also stop them from doing clever things.”
            (Doug Gwyn)


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-10-11 14:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-10 22:58 expand-and-quote-alias Jörg Sommer
2007-10-11  8:59 ` expand-and-quote-alias Peter Stephenson
2007-10-11 13:36   ` expand-and-quote-alias Jörg Sommer

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