zsh-users
 help / color / mirror / code / Atom feed
* dynamic reset of completion widget
@ 2004-11-04 12:42 Francisco Borges
  2004-11-04 13:09 ` Peter Stephenson
  2004-11-04 18:39 ` Bart Schaefer
  0 siblings, 2 replies; 5+ messages in thread
From: Francisco Borges @ 2004-11-04 12:42 UTC (permalink / raw)
  To: Zsh User

I hope the subject wasn't that confusing...

I want to automatically create zsh widgets for python programs using
python's standard option parser framework (optparse) and I want also to
be able to make and set this widget on-the-fly.

----------------------------------------------------------------
Here is the full story...

Yesterday I made a small script that, called from inside my foo.py
script, outputs a zsh completion widget to a file _foo.py.

This gives me a fast completion widget but it involves some shell
configuration every time I change options.

[...]

This guy has done a python module (http://furius.ca/optcomplete/) that
will provide bash directly with option completion through bash's
'completion protocol', e.g. setting COMP_(CWORD|LINE|POINT|WORDS) and
COMPREPLY.

So appart from a stable _foo.py file, I think it would also be good to
have a on-the-fly completion widget for zsh, like the one done for
bash. 

Wondering how to do it, I saw two possibilities:

1. Do the same as optcomplete does for bash, e.g. talk to the shell by
setting the correspondent variables;

OR

(don't know if this is possible, but it seems easier) 
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

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

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?

I imagine I'm not taking care of all details yet...

Any ideas, comments or suggestions? All welcome.

Any example of something similar? (I must confess that I find zsh
documentation a bit overwhelming...)

Peace,
Francisco.


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

* Re: dynamic reset of completion widget
  2004-11-04 12:42 dynamic reset of completion widget Francisco Borges
@ 2004-11-04 13:09 ` Peter Stephenson
  2004-11-04 20:54   ` Francisco Borges
  2004-11-04 18:39 ` Bart Schaefer
  1 sibling, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2004-11-04 13:09 UTC (permalink / raw)
  To: Zsh User

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

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

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


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

* Re: dynamic reset of completion widget
  2004-11-04 12:42 dynamic reset of completion widget Francisco Borges
  2004-11-04 13:09 ` Peter Stephenson
@ 2004-11-04 18:39 ` Bart Schaefer
  1 sibling, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2004-11-04 18:39 UTC (permalink / raw)
  To: Zsh User

On Thu, 4 Nov 2004, Francisco Borges wrote:

> I want to automatically create zsh widgets for python programs using 
> python's standard option parser framework (optparse) and I want also to 
> be able to make and set this widget on-the-fly.

Seems to me the right way to do this would be to define _first to spot 
previously-unknown python scripts and hook them up.  Calling _first is 
already supported in the completion system, but the default implementation 
is a no-op.

E.g.

_first() {
  local command=$words[1]
  if (( CURRENT > 1 )) && [[ -z $_comps[$command] ]]; then
    if [[ $command = *.py ]]; then
      eval "$($command --make-zsh-widget)"
      compdef _$command $command
    fi
  fi
}


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

* Re: dynamic reset of completion widget
  2004-11-04 13:09 ` Peter Stephenson
@ 2004-11-04 20:54   ` Francisco Borges
  2004-11-05 12:11     ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Francisco Borges @ 2004-11-04 20:54 UTC (permalink / raw)
  To: Zsh User

» On Thu, Nov 04, 2004 at 01:09:41PM +0000, Peter Stephenson wrote:

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

Hum, I can't say I understood what you meant to say here. What I can
tell you is that so far, I'm only using _arguments. 

> $service ought to give you this immediately.  In general it's

It did, thanks :-)

I have another problem, everytime I complete on foo.py, everything seems
to go fine but _optparse is still the widget used to complete the
script, e.g. the python script is called everytime I need to complete.

So I guess I'm not actually rebinding... The function I'm using is:

Any hints on how to get this right?

#autoload
_optparse(){

    bar=$service
    eval "`$bar --optcomp-on-the-fly`"

    if [[ -z $functions[_$bar] ]]; then
    _message "$bar --optcomp-on-the-fly didn't define _$bar"
    return 1
    fi

    compdef _$bar $bar
    _$bar "$@"
}

BTW, how can I see which widget is being used to complete a command? 

Peace,
-- 
Francisco Borges
Alfa Informatica - RuG


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

* Re: dynamic reset of completion widget
  2004-11-04 20:54   ` Francisco Borges
@ 2004-11-05 12:11     ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2004-11-05 12:11 UTC (permalink / raw)
  To: Francisco Borges; +Cc: Zsh User

Francisco Borges wrote:
> > _optparse_arguments() { _arguments "$@"; }
> >
> > allows you a bit of future-proofing.
> 
> Hum, I can't say I understood what you meant to say here. What I can
> tell you is that so far, I'm only using _arguments.

If _arguments doesn't change, you don't need to worry.

> I have another problem, everytime I complete on foo.py, everything seems
> to go fine but _optparse is still the widget used to complete the
> script, e.g. the python script is called everytime I need to complete.
> 
> So I guess I'm not actually rebinding... The function I'm using is:
> 
> Any hints on how to get this right?
> 
> #autoload
> _optparse(){
> 
>     bar=3D$service
>     eval "`$bar --optcomp-on-the-fly`"
> 
>     if [[ -z $functions[_$bar] ]]; then
>     _message "$bar --optcomp-on-the-fly didn't define _$bar"
>     return 1
>     fi
> 
>     compdef _$bar $bar
>     _$bar "$@"
> }
> 
> BTW, how can I see which widget is being used to complete a command?

print $_comps[foo.py] will tell you what function the system will call
to handle foo.py.

Try adding

print "$bar $_comps[$bar]"  >/tmp/optparse.dbg

after the compdef to see what's going on.

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

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

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


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

end of thread, other threads:[~2004-11-05 12:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-04 12:42 dynamic reset of completion widget Francisco Borges
2004-11-04 13:09 ` Peter Stephenson
2004-11-04 20:54   ` Francisco Borges
2004-11-05 12:11     ` Peter Stephenson
2004-11-04 18:39 ` Bart Schaefer

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