#compdef glr
# vim: ft=zsh:sw=2:ts=2:et:noml:

# `glr [<flags>] searchterm [<searchpath> | (<grep args>...)]`
#     glr -h
#     glr searchterm
#     glr searchterm /search/path
#     glr -t 'name ~~ "*.js"' searchterm ./searchpath
#     glr searchterm file1 file2 file3
# Flag | Description
# ---- | -----------
# -E   | Use extended regular expression search
# -I   | Force case-sensitve search.
# -V   | Show version.
# -X   | Shorthand for adding a test condition for a file extension.
# -h   | Usage and help text.
# -p   | Do not open search results in a pager.
# -t   | Pass additional test conditions to lr; multiple occurences ok.
# -x   | Enable xtrace debug logging.

autoload -U is-at-least

_glr() {
  local curcontext="$curcontext" ret=1
  local -a context line state state_descr glr_types glr_args
  local -A opt_args

  # glr_types=(
  #   f'\:"regular files"'
  # )
    # {l,symlink}'\:"symbolic links"'
    # {e,empty}'\:"empty files or directories"'
    # {x,executable}'\:"executable (files)"'
    # {b,block-device}'\:"block devices"'
    # {c,char-device}'\:"character devices"'
    # {s,socket}'\:"sockets"'
    # {p,pipe}'\:"named pipes (FIFOs)"'

  # Do not complete rare options unless either the current prefix
  # matches one of those options or the user has the `complete-all`
  # style set. Note that this prefix check has to be updated manually to account
  # for all of the potential negation options listed below!
  # if
  #   # (--[bpsu]* => match all options marked with '$no')
  #   [[ $PREFIX$SUFFIX == --[bopsun]* ]] ||
  #   zstyle -t ":complete:$curcontext:*" complete-all
  # then
  #   no=
  # fi

  glr_args=(
    + '(regex-pattern)' # regex-based search pattern
    '(regex-pattern)-E[perform an extended regular expression search]'

    + '(case)' # case-sensitivity
    '(case)-I[perform a case-sensitive search]'

    + filter-misc # filter search
    # '*'-t+"[filter search by type]:type:(($glr_types))"
    '*'-t+"[test expression]:test:"
    '*'-X+'[filter search by file extension]:extension'
    # '*'{-S+,--size=}'[limit search by file size]:size limit:->size'

    + '(pager)' # page output
    '(pager)-p[disable paging]'

    # + '(exec-cmds)' # execute command
    # '(long-listing max-results)'{-x+,--exec=}'[execute command for each search result]:command: _command_names -e:*\;::program arguments: _normal'
    # '(long-listing max-results)'{-X+,--exec-batch=}'[execute command for all search results at once]:command: _command_names -e:*\;::program arguments: _normal'
    # '(long-listing max-results)--batch-size=[max number of args for each -X call]:size'

    + '(debug)'
    # $no'-x[enable the display of filesystem errors]'
    '(debug)-x[enable xtrace debug logging]'

    + '(about)' # about flags
    '(: * -)-h[display help message]'
    '(: * -)-V[display version information]'

    + args # positional arguments
    '1: :_guard "^-*" pattern'
    '(--search-path)*:directory:_files -/'
  )

  # Strip out argument groups where unsupported (see above)
  is-at-least 5.4 ||
  glr_args=( ${(@)args:#(#i)(+|[a-z0-9][a-z0-9_-]#|\([a-z0-9][a-z0-9_-]#\))} )

  _arguments -S : $glr_args && ret=0

  # case ${state} in
  #   size)
  #     if compset -P '[-+][0-9]##'; then
  #       local -a suff=(
  #         'B:bytes'
  #         'K:kilobytes  (10^3  = 1000   bytes)'
  #         'M:megabytes  (10^6  = 1000^2 bytes)'
  #         'G:gigabytes  (10^9  = 1000^3 bytes)'
  #         'T:terabytes  (10^12 = 1000^4 bytes)'
  #         'Ki:kibibytes  ( 2^10 = 1024   bytes)'
  #         'Mi:mebibytes  ( 2^20 = 1024^2 bytes)'
  #         'Gi:gigibytes  ( 2^30 = 1024^3 bytes)'
  #         'Ti:tebibytes  ( 2^40 = 1024^4 bytes)'
  #       )
  #       _describe -t units 'size limit units' suff -V 'units'
  #     elif compset -P '[-+]'; then
  #       _message -e 'size limit number (full format: <+-><number><unit>)'
  #     else
  #       _values 'size limit prefix (full format: <prefix><number><unit>)' \
  #         '\+[file size must be greater or equal to]'\
  #         '-[file size must be less than or equal to]' && ret=0
  #     fi
  #     ;;
  # esac
  # return ret
}

_glr "$@"
