zsh-users
 help / color / mirror / code / Atom feed
* bash convert:  new completion system skeleton?
@ 2003-01-17 13:16 clemens fischer
  2003-01-24 10:37 ` Peter Stephenson
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: clemens fischer @ 2003-01-17 13:16 UTC (permalink / raw)
  To: zsh-users

hi.

i have lots of bash completers for my daily needs that i can't live
without.  but bash-style completers resemble zsh's old completion
style, which is inferiour to zsh's new system.

my completers have the following properties:  the program which's
arguments have to be completed have some help or list function which
i can use to enumerate the possible completions, then i just grep(1)
out the likely candidates.

example:  there's an anti-spam system called qconfirm
<URL:http://smarden.org/qconfirm/>, the bash completer for which is:

_qconfirm ()
{
	local i cur qopts qcmd
	COMPREPLY=()
	qopts=""
	qcmd=${COMP_WORDS[1]}
        # collect possible options to hand over 1:1
	for (( i=1 ; i<=COMP_CWORD ; i++ ))
	do
		cur="${COMP_WORDS[$i]}"
		#echo $cur >&2
		case $cur in
		    -*)
			qopts="$qopts $cur"
			;;
		    *)
			qcmd=${cur}
			break
			;;
		esac
	done
        # select the database depending on the subcommand used
	case $qcmd in
	    ac*|b[ao]*|dr*)
		which=pending
		;;
	    re*|pe*)
		which=ok
		;;
	    *)
		which=ok
		;;
	esac
        # here's the meat:  list the relevant database and pick the
        # right completion
	cur=${COMP_WORDS[COMP_CWORD]}
	COMPREPLY=( $(qconfirm $qopts list "${which}" |
		cut -d" " -s -f2 |
		egrep -i "${cur}" ))
	return 0
}
# tell the completion system which function is responsible for qconfirm
complete -F _qconfirm qconfirm

similiar code exists for gnu-make, autoconf's configure, openssl etc.

could somebody please offer the skeleton of a completer for the new
system, complete with which share/zsh/4.0.6/function/_<FUNC>s to
call, how to name the zstyle and the contexts?

i'm simply lost in all the files of the distribution, especially
those _<helper> functions.  any pointers appreciated.  is there
already a wiki for zsh specialists and users?

  clemens


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

* Re: bash convert: new completion system skeleton?
  2003-01-17 13:16 bash convert: new completion system skeleton? clemens fischer
@ 2003-01-24 10:37 ` Peter Stephenson
  2003-01-24 10:42 ` Borzenkov Andrey
  2003-01-24 11:12 ` Oliver Kiddle
  2 siblings, 0 replies; 8+ messages in thread
From: Peter Stephenson @ 2003-01-24 10:37 UTC (permalink / raw)
  To: zsh-users

clemens fischer wrote:
> example:  there's an anti-spam system called qconfirm
> <URL:http://smarden.org/qconfirm/>, the bash completer for which is:

For most ordinary UNIX commands the point to start is from one of the
files in the Completion/Unix/Command directory of the source
distribution.  If it's simple enough you can use _arguments.
Unfortunately the _arguments syntax is somewhat non-intuitive.

I need to add something more explicitly constructive to the user guide
for cases like this.  I really only started major hacking with
_arguments quite recently.

> similiar code exists for gnu-make, autoconf's configure, openssl etc.

A lot of these are already done in the latest code.

> i'm simply lost in all the files of the distribution, especially
> those _<helper> functions.  any pointers appreciated.  is there
> already a wiki for zsh specialists and users?

http://www.zshwiki.org/
http://zsh.sunsite.dk/Guide/

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************


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

* RE: bash convert:  new completion system skeleton?
  2003-01-17 13:16 bash convert: new completion system skeleton? clemens fischer
  2003-01-24 10:37 ` Peter Stephenson
@ 2003-01-24 10:42 ` Borzenkov Andrey
  2003-01-24 11:12 ` Oliver Kiddle
  2 siblings, 0 replies; 8+ messages in thread
From: Borzenkov Andrey @ 2003-01-24 10:42 UTC (permalink / raw)
  To: 'clemens fischer', zsh-users


> i have lots of bash completers for my daily needs that i can't live
> without.  but bash-style completers resemble zsh's old completion
> style, which is inferiour to zsh's new system.
> 
> my completers have the following properties:  the program which's
> arguments have to be completed have some help or list function which
> i can use to enumerate the possible completions, then i just grep(1)
> out the likely candidates.
> 

First, _arguments has direct support for some simple cases when a command is
using --help; look in Completion/Unix/Command/_configure for direct example.
In short, you describe patterns how to interpret command help output and
what functions to use to complete specific parameters.

This works for simple case but does not allow you to supply description text
for a particular argument.

Speaking in general, if you need just to complete a list of command
arguments, you simply call _arguments passing it array of arguments names.
There are many examples, look in _texinfo (at the end, case $state item) )
for a perverse example of using shell substitutions :)) or _cvs where it
completes available commands.

But preferred way (if you know command arguments in advance) is to use
_arguments together with table of command arguments and descriptions. Like


#compdef foo
_arguments \
  'foo-arg-1[Argument 1]' \
  'foo-arg-2[Argument that takes some
parameters]::_function_to_complete_parameters'

in the simplest case it is just 

_arguments foo-arg-1 foo-arg-2

but then you won't ever get descriptions :)

> example:  there's an anti-spam system called qconfirm
> <URL:http://smarden.org/qconfirm/>, the bash completer for which is:
> 

For you specific function, look at _cvs or _rpm completion, i.e. completion
of command with subcommands. In general this would be like something like
following (unverified), see _cvs for excellent example and technique how to
dynamically call subcommands.

=========================
#compdef qconfirm

_qconfirm_subcommand ()
{
   local which
   case $line[2] in
     ac*|b[ao]*|dr*)
		which=pending
	;;
    re*|pe*)
 		which=ok
	;;
    *)
 		which=ok
	;;
  esac
  compadd "$@" -- $(qconfirm $opt_args list $which | sed ... | egrep ...)
  # note that both sed and grep cam be replaced
  # with internal Zsh substitution. See _texinfo (near bottom) for
absolutely
  # braindamage example that really works :)
}

_qconfirm ()
{
  _arguments \
     '-arg1[It is arg1 that does useful thing]' \
     '-arg2[It is arg 2 that does even more useful thing]' \
     '::possible subcommands:(cmd1:"description 1" cmd2:"Description 2"
...)' \
     '*::completin subcommand:_qconfirm_subcommand'
}

_qconfirm "$@"

======================

and call _qconfirm and put somewhere in your fpath. Note that _arguments
already extracts options so you need not worry about it and it will format
output according to your styles etc etc. In general, _arguments is what you
need for your own custom completion.

Of course, if subcommand may take flags you may need write completion for
each one separately etc Again look in _cvs for excellent example.

> {
> 	local i cur qopts qcmd
 	local -a COMPREPLY
> 	qopts=""
 	qcmd=${words[2]}
>         # collect possible options to hand over 1:1
> 	for (( i=1 ; i<=COMP_CWORD ; i++ ))
> 	do
> 		cur="${COMP_WORDS[$i]}"
> 		#echo $cur >&2
> 		case $cur in
> 		    -*)
> 			qopts="$qopts $cur"
> 			;;
> 		    *)
> 			qcmd=${cur}
> 			break
> 			;;
> 		esac
> 	done
>         # select the database depending on the subcommand used
> 	case $qcmd in
> 	    ac*|b[ao]*|dr*)
> 		which=pending
> 		;;
> 	    re*|pe*)
> 		which=ok
> 		;;
> 	    *)
> 		which=ok
> 		;;
> 	esac
>         # here's the meat:  list the relevant database and pick the
>         # right completion
> 	cur=${COMP_WORDS[COMP_CWORD]}
> 	COMPREPLY=( $(qconfirm $qopts list "${which}" |
> 		cut -d" " -s -f2 |
> 		egrep -i "${cur}" ))
> 	return 0
> }
> # tell the completion system which function is responsible for qconfirm
> complete -F _qconfirm qconfirm
> 
> similiar code exists for gnu-make, autoconf's configure, openssl etc.

All of them are already included with zsh.

> 
> could somebody please offer the skeleton of a completer for the new
> system, complete with which share/zsh/4.0.6/function/_<FUNC>s to
> call, how to name the zstyle and the contexts?
> 
> i'm simply lost in all the files of the distribution, especially
> those _<helper> functions.  any pointers appreciated.  is there
> already a wiki for zsh specialists and users?
>

the only one known to me is at zsh site, it is a good start anyway.

 
>   clemens


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

* Re: bash convert: new completion system skeleton?
  2003-01-17 13:16 bash convert: new completion system skeleton? clemens fischer
  2003-01-24 10:37 ` Peter Stephenson
  2003-01-24 10:42 ` Borzenkov Andrey
@ 2003-01-24 11:12 ` Oliver Kiddle
  2003-01-24 11:19   ` Borzenkov Andrey
  2 siblings, 1 reply; 8+ messages in thread
From: Oliver Kiddle @ 2003-01-24 11:12 UTC (permalink / raw)
  To: clemens fischer; +Cc: zsh-users

On 17 Jan, you wrote:
> hi.
> 
> i have lots of bash completers for my daily needs that i can't live
> without.  but bash-style completers resemble zsh's old completion
> style, which is inferiour to zsh's new system.
> 
> my completers have the following properties:  the program which's
> arguments have to be completed have some help or list function which
> i can use to enumerate the possible completions, then i just grep(1)
> out the likely candidates.

> similiar code exists for gnu-make, autoconf's configure, openssl etc.

zsh has all of these (except openssl) and a lot more besides already
written. If you've done:
  autoload -U compinit; compinit
they should work automatically.

> could somebody please offer the skeleton of a completer for the new
> system, complete with which share/zsh/4.0.6/function/_<FUNC>s to
> call, how to name the zstyle and the contexts?

To convert your bash completion for zsh, at a simple level, all you
should need to do is change the $COMP_WORDS variable to $words,
$COMP_CWORD to $CURRENT and perhaps a few others. Bash returns the
matches in the $COMPREPLY array whilst zsh uses a builtin - compadd. So
putting:
  compadd - "${COMPREPLY[@]}"
at the end of your bash function will handle that.

> i'm simply lost in all the files of the distribution, especially
> those _<helper> functions.  any pointers appreciated.  is there

The helpers are just a better way of doing what bash does with the
compgen builtin. You just call _users to complete usernames or _files
to complete files. Much more readable than `compgen -f' and much more
powerful when you look in more detail.

I could easily write a function to allow zsh to use completions written
for bash:

local COMP_REPLY
local COMP_CWORD=$CURRENT
local -a COMP_WORDS
COMP_WORDS=( $words )
setopt localoptions
emulate sh
"$@"
compadd - "${COMPREPLY[@]}"

Would be enough for _qconfirm (I think). Would that be useful? Or do we
have a perl guru who can write a program to convert the function? Of
course it is better to rewrite the functions properly to make full use
of zsh's greater power.

Oliver

This e-mail and any attachment is for authorised use by the intended recipient(s) only.  It may contain proprietary material, confidential information and/or be subject to legal privilege.  It should not be copied, disclosed to, retained or used by, any other party.  If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.  Thank you.


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

* RE: bash convert: new completion system skeleton?
  2003-01-24 11:12 ` Oliver Kiddle
@ 2003-01-24 11:19   ` Borzenkov Andrey
  2003-01-24 19:37     ` clemens fischer
  2003-01-24 21:19     ` John Buttery
  0 siblings, 2 replies; 8+ messages in thread
From: Borzenkov Andrey @ 2003-01-24 11:19 UTC (permalink / raw)
  To: 'Oliver Kiddle', 'clemens fischer'; +Cc: zsh-users


> 
> I could easily write a function to allow zsh to use completions written
> for bash:
> 
> local COMP_REPLY
> local COMP_CWORD=$CURRENT
> local -a COMP_WORDS
> COMP_WORDS=( $words )
> setopt localoptions
> emulate sh
> "$@"
> compadd - "${COMPREPLY[@]}"
> 

cool. I guess it could be really useful. 

> Would be enough for _qconfirm (I think). Would that be useful? Or do we
> have a perl guru who can write a program to convert the function? 

Or scan bash_profile/bashrc for complete -F and compdef all commands to use
wrapper unless already defined. It would allow almost transparent change
between bash and zsh. It could be added to compinstall then or even to
compinit with style to turn it off/on.

Would it be useful?

Of
> course it is better to rewrite the functions properly to make full use
> of zsh's greater power.
>

Sure.


-andrey


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

* Re: bash convert: new completion system skeleton?
  2003-01-24 11:19   ` Borzenkov Andrey
@ 2003-01-24 19:37     ` clemens fischer
  2003-01-24 21:19     ` John Buttery
  1 sibling, 0 replies; 8+ messages in thread
From: clemens fischer @ 2003-01-24 19:37 UTC (permalink / raw)
  To: Borzenkov Andrey; +Cc: 'Oliver Kiddle', zsh-users

Borzenkov Andrey <Andrey.Borzenkov@siemens.com>:

>> I could easily write a function to allow zsh to use completions written
>> for bash:

this would get you into a hell of troubles you were leaving with the
invention of the new zsh-completion-system.  think about special
cases popping up every place and all the work of keeping it!

> Or scan bash_profile/bashrc for complete -F and compdef all commands to use
> wrapper unless already defined. It would allow almost transparent change
> between bash and zsh. It could be added to compinstall then or even to
> compinit with style to turn it off/on.
>
> Would it be useful?

i don't think so.  bash completers resemble the old completion style
with compctl much more.

>> Of course it is better to rewrite the functions properly to make
>> full use of zsh's greater power.

exactly.  this is more a problem of documentation then programming,
because the facilities are already there.  i just got lost in all the
stuff.  the freebsd port maintainers put all the `_*' functions
containing zsh features at various levels into one single, flat
directory, so for me, who couldn't differentiate between widgets,
low- and high-level functions the interface layers were not clear.

but Borzenkov was nice enough to send me some pointers, so i think i
can take it from here.  thanks, Borzenkov!

zsh needs more documentation with elaborate examples, that's all.  i
know, it's the hardest part of it all ...

  clemens


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

* Re: bash convert: new completion system skeleton?
  2003-01-24 11:19   ` Borzenkov Andrey
  2003-01-24 19:37     ` clemens fischer
@ 2003-01-24 21:19     ` John Buttery
  2003-01-27 10:08       ` Oliver Kiddle
  1 sibling, 1 reply; 8+ messages in thread
From: John Buttery @ 2003-01-24 21:19 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 760 bytes --]

* Borzenkov Andrey <Andrey.Borzenkov@siemens.com> [2003-01-24 14:19:04 +0300]:
> cool. I guess it could be really useful. 

  I second that; I realize that zsh's new zstyle stuff is more powerful
inherently than bash's system, but still I see almost-daily updates to
the "bash-completion" project on freshmeat with new stuff and it would
be great to be able to use it.  That is, as long as it doesn't generate
too much unwanted work for someone and doesn't interfere with zsh's own
completion of course.  :)

-- 
------------------------------------------------------------------------
 John Buttery
                                     (Web page temporarily unavailable)
------------------------------------------------------------------------

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

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

* Re: bash convert: new completion system skeleton?
  2003-01-24 21:19     ` John Buttery
@ 2003-01-27 10:08       ` Oliver Kiddle
  0 siblings, 0 replies; 8+ messages in thread
From: Oliver Kiddle @ 2003-01-27 10:08 UTC (permalink / raw)
  To: John Buttery; +Cc: zsh-users

On 24 Jan, you wrote:
> 
> > cool. I guess it could be really useful.=20
> 
>   I second that; I realize that zsh's new zstyle stuff is more powerful
> inherently than bash's system, but still I see almost-daily updates to
> the "bash-completion" project on freshmeat with new stuff and it would
> be great to be able to use it.  That is, as long as it doesn't generate
> too much unwanted work for someone and doesn't interfere with zsh's own
> completion of course.  :)

They make a release for every tiny change to bash-completion though.
There aren't that many programs it handles that zsh doesn't.

I've just got a few issues to clear up and I'll post something to do this
to -workers in a day or two. Making use of the bash-completion project
still won't be entirely easy. It takes a bit of tweaking before I can
source the file. Making use of the regular bash_completion updates will
require a lot of effort. But for getting your own collection of bash
completions to work, it will work fairly well.

It was interesting to see in more detail how the bash system works. It is
quite a bit less powerful even than compctl and in some respects lacking
relative to tcsh. Though using shell code to find out where in the line
the cursor is is better than those systems. It can only complete whole
arguments at a time and the way it can only fall back on a default glob
for half decent file completion is fairly unpleasant.

Oliver

This e-mail and any attachment is for authorised use by the intended recipient(s) only.  It may contain proprietary material, confidential information and/or be subject to legal privilege.  It should not be copied, disclosed to, retained or used by, any other party.  If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender.  Thank you.


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

end of thread, other threads:[~2003-01-27 10:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-17 13:16 bash convert: new completion system skeleton? clemens fischer
2003-01-24 10:37 ` Peter Stephenson
2003-01-24 10:42 ` Borzenkov Andrey
2003-01-24 11:12 ` Oliver Kiddle
2003-01-24 11:19   ` Borzenkov Andrey
2003-01-24 19:37     ` clemens fischer
2003-01-24 21:19     ` John Buttery
2003-01-27 10:08       ` Oliver Kiddle

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