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

Re: Completion Control (Novice qn)



Varadarajan wrote:
> 
> Hi all,
>    I need some help with completions. Basically what I want is
> this. If I want to talk to a person on local network, I want talk to
> complete the machine name.
> Typically this is what I want.
> talk person@`rwho | grep person | cut -d: -f1 | awk '{print $2}' |
> head -1`

The following seems to work.  Note you want the second word in the
array: the first is the command.  Also, you don't need the `head':  zsh
will simply make the results into different possible completions.  The
upshot of the compctl command is that you need to type the @ sign
yourself before it is recognised as a candidate for this behaviour.
This allows you to complete users as well if there is no @ (the -u).

talkcomp () {
  local line from

  read -Ac line
  from="${line[2]}"
  from=${from%%@*}

  reply=( `rwho | grep $from | cut -d: -f1 | awk '{print $2}'` )
# with perl
# reply=( `rwho | perl -ane '/^(\S+)\s*(\S+):/ && ($1 eq '$from') 
#          && print "$2\n";` )
}
compctl -u -x 'n[1,@]' -K talkcomp -- talk

(Note that with perl you can get away with two processes instead of
four; maybe you can do it in awk too.)

> I would like to know what is the solution, and if
> there is any ways of debugging such things.. (Like how to know the
> value of variables being used).

Miss out the `local' and you can check the values afterwards.  You can
put `set -x' inside the function, although that makes the display
harder to see.

-- 
Peter Stephenson <pws@xxxxxx>       Tel: +49 33762 77366
WWW:  http://www.ifh.de/~pws/       Fax: +49 33762 77413
Deutches Electronen-Synchrotron --- Institut fuer Hochenergiephysik Zeuthen
DESY-IfH, 15735 Zeuthen, Germany.



Messages sorted by: Reverse Date, Date, Thread, Author