#compdef info tkinfo install-info makeinfo texi2dvi texindex

local curcontext="$curcontext" state line ret=1
local -A opt_args
local -a info

case $service in
  info)
    info=( _call_program info ${words[1]} )
    _arguments -C -s \
      '(: -)--apropos=[look up string in indices]:search string: ' \
      '(-f --file)'{\*-d,\*--directory=}'[add directory to infopath]:info dir:_files -/' \
      '--dribble=[record keystrokes]:file with keystrokes:_files' \
      '(-f --file -d --directory)'{-f,--file=}'[info file to show]:info file:_files -g "*.info(|.gz|.bz2)(-.)"' \
      '(: - -h --help)'{-h,--help}'[display usage]' \
      '(-o --output -O)--index-search=[go directly to node if found]:search string: ' \
      '(--index-search -o --output -O)'{-o,--output=}'[dump selected nodes to filename]:filename:_files -g "*(-.)"' \
      '(-R --raw-escapes)'{-R,--raw-escapes}'[do not remove ANSI escapes from man pages]' \
      '--restore=[read keystrokes from file]:filename:_files -g "*(-.)"' \
      '(--index-search -o --output -O --show-options --usage)'{-O,--show-options,--usage}'[go to command-line options node]' \
      '--subnodes[recursively output menu items]' \
      '--vi-keys[use Vi-like key bindings]' \
      '(: -)--version[display version information]' \
      '(--apropos -h --help -O --output --version)*::menu item:->item' && ret=0

    info=( $info {(kv)opt_args[(I)(-d|--directory|-f|--file)]} )
  ;;
  install-info)
    if _pick_variant debian=Debian gnu --version; then
      _arguments -S \
        '(* -)--help[display help information]' \
        '(* -)--version[display version information]' \
        '(--maxwidth --align --calign)--remove[delete existing entries]' \
        '(--maxwidth --align --calign)--remove-exactly[delete existing entries taking exact entry name]' \
        '--section[specify section to add entry in]:regexp::title' \
        '--infodir=[specify directory containing dir file]:info directory:_files -/' \
        '--align=[specify minimum indentation for description]:indentation' \
        '--calign=[specify minimum indentation for continuation lines of description]:indentation' \
        '--maxwidth=[specify maximum width of dir file]:width' \
        '--quiet[suppress informational output]' \
        '--description=[specify description for menu entry]:description' \
        '--menuentry=[specify the menu entry for dir file]:menu entry' \
        '--keep-old=[inhibit replacement of existing entries/removal of empty sections]' \
        '--test[do not update dir file]' \
        '--debug[enable debugging]' \
        '*:info file:_files -g "*.info(-.)"'
	return
    else
      _arguments \
        '(--delete --remove)'{--delete,--remove}'[delete existing entries]' \
        '(2)--dir-file=[specify dir file]:dir file:_files -g "dir(-.)"' \
        \*{--entry,--info}'[specify directory entry to add]:text' \
        '(- 1 2)--help[display help information]' \
        '(1)--info-file=[specify info file to install in the directory]:info file:_files -g "*.info(-.)"' \
        '(2)--info-dir=[specify directory containing dir file]:info directory:_files -/' \
        '--quiet[suppress warnings]' \
        '(--delete --remove)--section=[put new entries in specified section]' \
        '(- 1 2)--version[display version information]' \
        '1:info file:_files -g "*.info(-.)"' \
        '2:dir file:_files -g "dir(-.)"'
	return
    fi
  ;;
  makeinfo)
    _arguments -s \
      '--error-limit=[specify number of errors to quit after]:number' \
      '--force[preserve output even if errors]' \
      '(* -)--help[display help]' \
      '--no-validate[suppress node cross-reference validation]' \
      '--no-warn[suppress warnings]' \
      '--reference-limit=[warn about at most specified number of references]:number' \
      '(-v --verbose)'{-v,--verbose}'[explain what is being done]' \
      '(* -)--version[display version info]' \
      '(--html --xml)--docbook[output in DocBook format]' \
      '(--docbook --xml)--html[output in HTML format]' \
      '--no-headers[suppress node separators and menus]' \
      '(--docbook --html)--xml[output in XML (TexinfoML) format]' \
      '(-E --macro-expand)'{-E,--macro-expand}'[output macro-expanded source to specified file]:file:_files' \
      '(--docbook --xml)--no-split[suppress splitting of output]' \
      '--number-sections[output chapter and section numbers]' \
      '(-o --output=)'{-o,--output}'[specify output file]:output file:_files' \
      '(--docbook --html --xml)--enable-encoding[output special characters]' \
      '(--docbook --html --xml)--fill-column=[specify width to break lines at]:width' \
      '(--docbook --html --xml)--footnote-style=[specify style for output of footnotes]:((separate\:in\ own\ node end\:at\ end\ of\ node))' \
      '(--docbook --html --xml)--paragraph-indent=[specify spaces to indent paragraphs by]' \
      '(--docbook --html --xml)--split-size=[split Info files at specified size]:size' \
      '--commands-in-node-names[allow @ commands in node names]' \
      '-D[define variable]:variable' \
      '-I[specify directory to append to @include search path]:directory:_files -/' \
      '-P[specify directory to prepend to @include search path]:directory:_files -/' \
      '-U[undefine variable]:variable' \
      '(--html --no-ifhtml)--ifhtml[process @ifhtml and @html when not generating HTML]' \
      '(--no-ifinfo)--ifinfo[process @ifinfo even when not generating Info]' \
      '(--no-iftex)--iftex[process @iftex and @tex text]' \
      '(--no-ifplaintext)--ifplaintext[process @ifplaintext even when not generating plain text]' \
      '(--ifhtml)--no-ifhtml[do not process @ifhtml and @html text]' \
      '(--ifinfo)--no-ifinfo[do not process @ifinfo text]' \
      '(--ifplaintext)--no-ifplaintext[do not process @ifplaintext text]' \
      '(--iftex)--no-iftex[do not process @iftex and @tex text]' \
      '*:texinfo file:_texi'
      return
  ;;
  texi2dvi)
    _arguments -s \
      '(-b --batch)'{-b,--batch}'[no interaction]' \
      '(-c --clean)'{-c,--clean}'[remove all auxiliary files]' \
      '(-D --debug)'{-D,--debug}'[turn on shell debugging (set -x)]' \
      '(- *)'{-h,--help}'[display help information]' \
      '(-o --output)'{-o,--output=}'[specify output file]:output file:_files' \
      '(-q -s --quiet --silent -V --verbose)'{-q,-s,--silent,--quiet}'[no output except errors]' \
      '(-q -s --quiet --silent -V --verbose)'{-V,--verbose}'[report on what is done]' \
      '(- *)'{-v,--version}'[display version information]' \
      '-@[use @input instead of \input; for preloaded Texinfo]' \
      '(-e --expand)'{-e,--expand}'[force macro expansion using makeinfo]' \
      '-I[specify search dir for texinfo files]:directory:_files -/' \
      '(-l --language)'{-l,--language}'[specify language of input file]:language:(LaTeX Texinfo)' \
      '(-p --pdf)'{-p,--pdf}'[use pdftex or pdflatex for processing]' \
      \*{-t,--texinfo}'[specify command to insert after @setfilename]:command' \
      '*:file:_texi'
      return
  ;;
  texindex)
    _arguments \
      '(* -)--help[display help]' \
      '(* -)--version[display version info]' \
      '(--no-keep)--keep[keep temporary files around after processing]' \
      '(--keep)--no-keep[do not keep temporary files (default)]' \
      '(-o --output)'{-o,--output=}'[specify output file]:file:_files' \
      '*:texinfo file:_texi'
      return
  ;;
  tkinfo)
    _x_arguments -C \
      '-+'{headers,buttons,balloons,scrollthrough,pagesep,showdir} \
      '-linklook:menu display:(color font underline)' \
      '-highlight:link display:(color inverse underline)' \
      '-searchlook:search match display:(color inverse underline)' \
      '-iconic' \
      '-dir:info path:_dir_list' \
      '-file:info file:_files -g "*.info(|.gz|.bz2)(-.)"' \
      '-node:info node:->item' \
      '*::menu items:->item' && ret=0
    info=( _call_program info info ${(kv)opt_args[(I)-file]/-file/-f} )
  ;;
esac

case $state in
  item )
    local -a items tags expl infopath
    (( $#INFOPATH )) && infopath=( -W ${s/:/INFOPATH} )

    tags=(items)
    if [[ $CURRENT -eq 1 ]]; then
	tags+=files
    fi

    _tags $tags
    while _tags; do
	if _requested files expl 'info file'; then
	    _files "$expl[@]" $infopath -g '*.info' && ret=0
	fi
	if _requested items; then
	    items=(${${(M)${${(f)"$(${info} --output - ${words[1,CURRENT-1]} 2>/dev/null)"}[1,(r)[[:space:]]#--- The Detailed Node Listing ---[[:space:]]#]}:#\* *~\* Menu:*}:/(#b)\*[[:space:]]##([^:]##):(#B)( \(?##\)[^.]#.|(:|))[[:space:]]#(#b)(*)[[:space:]]#/$match[1]${match[2]:+:}$match[2]})

	    _describe -t items "menu item" items && ret=0
	fi
    done
  ;;
esac

return ret
