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

Re: dynamic reset of completion widget



Francisco Borges wrote:
> 2. Have a default widget for my python script, say _optparse, that makes
> a new custom widget and rebinds the completion of foo.py to _foo.py

This sounds quite a reasonable way of doing it.

You might want to make it a bit more flexible about possible
enchancements by providing a few extra support functions in parallel to
_optparse (just put #autoload at the top of each function) and then make
_foo.py use those.  It gives you a little bit more decoupling between
the versions of zsh and the versions of the python script.  It depends
how complicated _foo.py is going to be; if you're simply going to call
_arguments, for example, maybe there's no point.  However, having your
own _optparse_arguments as a front-end whose initial implementation is
simply

_optparse_arguments() { _arguments "$@"; }

allows you a bit of future-proofing.

> # I start with
> compdef _optparse foo.py 
> compdef _optparse bozo.py
> [etc]
> 
> # I'm not a zsh programmer, so please have patience here...
> _optparse(){
> 
> # discover the name of the command we are completing
> set bar=`give the command name I'm completing`

$service ought to give you this immediately.  In general it's
a completion context, but if you know you're complete for
command arguments, as you will here, it's the name of the command
you're completing for.

> eval "`$bar --make-zsh-widget`"
> # which would return something like:
> # "_foo.py() { compadd Scooby Dooby Doo; }"
> 
> compdef _$bar $bar
> }
> 
> Is this possible? Can I just rebind like this?

Yes, you can, however the newly defined function won't get called the
first time.  So you should add

_$bar "$@"

to the end of _optparse.

Before that you might want to check that the specific completion
function has actually been defined for safety, e.g.

if [[ -z $functions[_$bar] ]]; the
  _message "$bar --make-zsh-widget didn't define _$bar"
  return 1
fi

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

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************



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