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

Re: GPG completion function



Bruno Bonfils wrote:
> 
> > > local context state line
> >
> i believe that some functions declared this var without using them. That's why
> i write this line

They are used internally to _arguments for calls which use states so
the functions probably do use them, just it isn't imediately apparent.

> > >     '-s[sign a file]:file attachment:_files' \

> it is possible to tell zsh there maybe more than 1 file after the
> option ?

Yes. '-s[sign a file]:*:file attachment:_files' I think it is. Though
that
isn't exactly what you want to do in this case.

> > >     '-o[write output to file]:_files attachment:_files' \
> > 
> > Your description on that line and others is messed up. I'd need to know
> > more about gpg usage but the file attachment maybe could be completed
> > with a '1' or '*' spec to _arguments.
> > 
> i don't understand completely. you mean that i delete all descriptions
> (which comes from gpg's manpage) ?

I was refering to the `_files attachment' description which is the
description used when completing the output file with _files. I might
use `output file' here. Perhaps you don't see these descriptions
because you won't by default.
  zstyle ':completion:*:descriptions' format '%B%d%b'
enables them (displayed in bold).

> _pub-keys-list ()
> {
>         local list
>         list=(${${(Mo)$(gpg --list-keys 2>/dev/null):%<*>}//(<|>)/})
>         compadd $list
> }

You could use `compadd -a list' as a slightly more efficient
alternative to that though in this case, you can just put the parameter
substitution directly as an argument to compadd. It is also useful to
use _wanted here. I've made this change in an attached _gpg and have
also added a few more gpg options.

I've now had a slightly closer look at gpg. You've not picked the
easiest command to start with.

gpg arguments are in this form:
  gpg [options] command [options] [args]

The commands look like options (e.g. --export) but affect the type of
the final [args]. You are also allowed things like `--export --armour
key' where another option (--armor) intervenes between the command and
the args. So basically, instead of completing public keys as an
argument to --export, we need to setup something for completing the
final args which looks back to see which command has been used.

To do this, I've added '*:args:->args' to the _arguments specs. This
says that for the non-option arguments enter the args state. The state
handler for `args' then checks which gpg command was used to select
between completing keys, secret keys and files. Other options, e.g.
--recipient can use the public-key and secret-key states to complete
single keys as arguments instead of the functions.

There is still a good deal to do on this: exclusion lists are needed
for incompatible commands (e.g. --export and --decrypt), any command
such as --gen-key which allows no args needs to exclude '*', more
commands need to use the states, and there are many options which this
still doesn't complete.

Oliver

_____________________________________________________________________
This message has been checked for all known viruses by the 
MessageLabs Virus Scanning Service. For further information visit
http://www.messagelabs.com/stats.asp
#compdef gpg

# Author : Bruno Bonfils <asyd@xxxxxxxxxxxxx>
# Date   : Sept 2001
# Thanks to Oliver Kiddle and Denis Bodor

local curcontext="$curcontext" state line expl
typeset -A opt_args

_arguments -C -s -S -A "-*" \
  '(-a --armor)'{-a,--armor}'[create ASCII armored output]' \
  '(-o --output)'{-o+,--output}'[write output to file]:output file:_files' \
  '(-u --local-user)'{-u+,--local-user}'[use name as the user ID to sign]:user attachment:_users'\
  '(-b --detach-sign)'{-b,--detach-sign}'[make a detached signature]' \
  '(-s --sign)'{-s,--sign}'[sign a file]' \
  '(-e --encrypt)'{-e,--encrypt}'[encrypt data. this option may be combined  with --sign]' \
  '(-c --symmetric)'{-c,--symmetric}'[encrypt with symmetric cypher only]' \
  '(-h --help)'{-h,--help}'[display usage information]' \
  '(-n --dry-run)'{-n,--dry-run}"[don't make any changes]" \
  '(-q --quiet -v --verbose)'{-q,--quiet}'[reduce amount of output]' \
  '(-q --quiet)*'{-v,--verbose}'[increase amount of output]' \
  '*'{-r+,--recipient}'[specify user to encrypt for]:recipient:->public-key' \
  '--clearsign[make a clear text signature]' \
  '--charset[specify native character set]:character set:(iso-8859-1 iso-8859-2 koi8-r utf-8)' \
  '--check-sigs[lists key, signatures and check them]:key attachment:_pub-keys-list' \
  '--decrypt[decrypt file or stdin]' \
  '--delete-key[remove key from public keyring]:key attachment:_pub-keys-list' \
  '--delete-secret-key[remove key from public & private keyring]:key attachment:_sec-keys-list' \
  '--delete-secret-and-public-key[remove key from private & public keyring]:key attachment:_sec-keys-list' \
  '--edit-key[a menu for edit yours keys]:key attachment:_pub-keys-list' \
  '--export[export all key from all keyrings]' \
  '--export-all[export all key and not OpenPGP compatible keys]' \
  '--export-ownertrust[list the assigned ownertrust values in ASCII format]' \
  '--export-secret-keys[export a list of secret keys]:key attachment:_sec-keys-list' \
  '--export-secret-subkeys[same as --export but export the secret keys instead]:key attachment:_sec-keys-list' \
  '--fast-import[import a file without build trustdb]:_files attachment:_files' \
  '--fingerprint[list all keys with their fingerprints]:key attachment:_pub-keys-list' \
  '(*)--gen-key[generate a new pair key]' \
  '--gen-random[emit random bytes of the given level quality]' \
  '--gen-prime[use the source, luke :-)]' \
  '--import[import a gpg key from a file]:_files attachment:_files' \
  '--import-ownertrust[update the trustdb with a file]:_files attachment:_files'\
  '--keyserver[use server for send/recv keys]:_hosts attachment:_hosts' \
  '--list-keys[list all keys]' \
  '--list-public-keys[list all public keys]' \
  '--list-secret-keys[list all secret keys]' \
  '--list-packets[list only the sequence of packets]' \
  '--list-sigs[lists keys and signatures]:key attachment:_pub-keys-list' \
  '--lsign-key[sign a key but mark not is as non-exportable]:key attachment:_pub-keys-list' \
  '(--no-options)--options[specify file to read options from]:options file:_files' \
  "(--options)--no-options[don't read options file]" \
  '--print-md[print message digest of the given algorithm for all given files]' \
  '--recv-keys[receive a list of keys from a keyserver]:key attachment:_pub-keys-list' \
  '--send-keys[send keys to a keyserver]:key attachment:_pub-keys-list' \
  '--sign-key[sign a key]:key attachment:_pub-keys-list '\
  '--store[store only]' \
  '--trusted-key[assume that the specified key is trustworthy]' \
  '--verify[verify a signature]:file attachment:_files' \
  '--verify-files[verify a list of files]:_files attachment_files' \
  '*:args:->args' && return

if [[ $state = args ]]; then
  if (( ${+opt_args[--export]} || ${+opt_args[--list-keys]} )); then
    state=public-key
  elif (( ${+opt_args[--list-secret-keys]} )); then
    state=secret-key
  else
    _files && return
  fi
fi

case "$state" in
  public-key)
    _wanted public-keys expl 'public key' \
	compadd ${${(Mo)$(gpg --list-keys 2>/dev/null):%<*>}//(<|>)/} && return
  ;;
  secret-key)
    _wanted secretkeys expl 'secret key' compadd \
	${${(Mo)$(gpg --list-secret-keys 2>/dev/null):%<*>}//(<|>)/} && return
  ;;
esac

return 1


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