#!/usr/bin/env zsh
#
# zm - search zshall more efficiently

setopt extendedglob

man() {
  env \
    LESS_TERMCAP_md=$(tput bold; tput setaf 4) \
    LESS_TERMCAP_me=$(tput sgr0) \
    LESS_TERMCAP_mb=$(tput blink) \
    LESS_TERMCAP_us=$(tput setaf 2) \
    LESS_TERMCAP_ue=$(tput sgr0) \
    LESS_TERMCAP_so=$(tput smso) \
    LESS_TERMCAP_se=$(tput rmso) \
    PAGER="${commands[less]:-$PAGER}" \
    man "$@"
}

# mansearch() {
#   man -P "less -p'^$1'" $2;
# }

# zp() { man -P "less -p'^       "$1" '" zshexpn; }
# zs() { man -P "less -p'^       "$1" '" zshparam; }
# # zmc() { man -P "less -p'^${@}'" zshcompsys; }
# # zmp() { man -P "less -p'^${@}'" zshall; }
# # zmo() { man -P "less -p'^${@}'" zshmodules; }
# # zlm() { man -P "less -p'^${@}'" zshzle; }
# # zd()  { man -P "less -p'^THE ZSH/$1 MODULE$'" zshmodules; }

zcompsys_h()   { man -P "less -p'^${@}'" zshcompsys; }
zshall_h()     { man -P "less -p'^${@}'" zshall; }
zmodules_h()   { man -P "less -p'^${@}'" zshmodules; }
zmodules_mod() { man -P "less -p'^THE ZSH/$1 MODULE$'" zshmodules; }
zzle_h()       { man -P "less -p'^${@}'" zshzle; }

zbuiltins() { man -P "less -p'^       "$1" '" zshbuiltins; }
zoptions()  { man -P "less -p'^       "$1"'" zshoptions; }
zzle()   { man -P "less -p'^       "$1"'" zshzle; }
zexpn()  { man -P "less -p'^       ${1/#[ewspfrRiInbkK]/"${1}"} '" zshexpn; }
zparam() { man -P "less -p'^       ${1/#[AacCefFikLnoOPqQtuUvVwWXzpjslrSIBEMNR]/"${1}"} '" zshparam; }

usage() {
  print -Pr -- "%F{2}-t, --test -- [[ -e ]]%f"
  print -Pr -- "%F{4}-e, --example%f"
  print -Pr -- "%F{2}--pf, --pexpf -- \"\$ {(f)foo}\"%f"
  print -Pr -- "%F{4}--pexp, --paramexp -- \"\$ {~foo}\"%f"
  print -Pr -- "%F{2}--he, --histexp -- $ {var:h3}%f"
  print -Pr -- "%F{4}--hf, histflags%f"
  print -Pr -- "%F{2}-a, --arrparam%f"
  print -Pr -- "%F{4}--ars, --arrsubscript%f"
  print -Pr -- "%F{2}--arsf, --arrflag -- $ {array[(i)pattern]}%f"
  print -Pr -- "%F{4}-c, --complex%f"
  print -Pr -- "%F{2}-g, --glob -- (#X)%f"
  print -Pr -- "%F{4}-o, --opts -- AUTO_CD%f"
  print -Pr -- "%F{2}--sopts, --1opts%f"
  print -Pr -- "%F{4}--prompt, --promptchars -- %%B%f"
  print -Pr -- "%F{2}-r, --redir%f"
  print -Pr -- "%F{4}--wid --widget -- vi-backward-kill%f"
  print -Pr -- "%F{2}-z, --zparse%f"
  print -Pr -- "%F{4}--ru, --rules%f"
  print -Pr -- "%F{2}--vared, --bindkey%f"
  print -Pr -- "%F{4}-u, --used%f -- Parameters set by shell"
  print -Pr -- "%F{2}-b, --built -- echoti%f"
  print -Pr -- "%F{4}-p, --param -- $ {var:h}, $ {(f)foo}%f"
  print -Pr -- "%F{2}-s, --subscript -- $ {foo[(re)bar]}%f"
  print -Pr -- "%F{4}-m, --module -- ZSH/ZUTIL%f"
}

lopts=(test example pf pexpf pexp paramexp he histexp hf histflags arrparam arrsubscript help)
lopts+=(arsf arrflag complex glob shopts sopts 1opts prompt promptchars redir wid widget zparse)
lopts+=(ars ru rules bindkey vared opts used zst zstyle cf ctrlfunc ss stdstyles)

zparseopts -D -E -a opts ${(s::):-teacgrzhu} \
  ${lopts//(#m)*/\-${MATCH}} \
  ${${(@)${(s::):-bpsmow}//(#m)*/${MATCH}\:\=opts}}

  # b:=opts p:=opts s:=opts
  # ${(j.:=opts .)${(s::):-bps}}:=opts
  # "${${(@)${(s::):-bps}//(#m)*/${MATCH}\:\=opts}}"

  # 'b' 'built' \
  # 'p' 'param' \
  # 's' 'subscript' \
  # 'm' 'module' \
  # 'w' 'widget'
  # 'h' 'help'

[[ -z "${opts[1]}" && -z "${1}" ]] && { usage; exit 1 }

builtin set -- ${@:#--}

case ${opts[1]} in
  -t|--test) zshall_h "CONDITIONAL EXPRESSIONS$" ;;
  -e|--exmple) zshall_h "   Examples$" ;;
  --pf|--pexpf) zshall_h "   Parameter Expansion Flags$" ;;
  --pexp|--paramexp) zshall_h "PARAMETER EXPANSION$" ;;
  --he|--histexp) zshall_h "HISTORY EXPANSION$" ;;
  --hf|--histflags) zshall_h "   Modifiers$" ;;
  -a|--arrparam) zshall_h "ARRAY PARAMETERS$" ;;
  --ars|--arrsubscript|"array subscripts") zshall_h "   Array Subscripts$" ;;
  --arsf|-a-rrflag|"arr flags"|"array flags") zshall_h "   Subscript Flags$" ;;
  -c|--complex) zshall_h "COMPLEX COMMANDS$" ;;
  -g|--glob) zshall_h "   Globbing Flags$" ;;
  --opts) zshall_h "DESCRIPTION OF OPTIONS$" ;;
  --sopts|--1opts) zshall_h "SINGLE LETTER OPTIONS$" ;;
  --prompt|--promptchars) zshall_h "SIMPLE PROMPT ESCAPES$" ;;
  -r|--redir) zshall_h "REDIRECTION$" ;;
  --wid|--widget) zshall_h "STANDARD WIDGETS$" ;;
  --bindkey|--vared) zzle_h "ZLE BUILTINS$" ;;
  -z|--zparse) zmodules_h "       zparseopts$" ;;
  --ru|--rules) zshall_h "   Rules$" ;;
  -u|--used) zshall_h "PARAMETERS USED BY THE SHELL$" ;;
  --zst|--zstyle) zmodules_h "THE ZSH/ZUTIL MODULE$" ;;
  --cf|--ctrlfunc) zcompsys_h "CONTROL FUNCTIONS$" ;;
  --ss|--stdstyles) zcompsys_h "   Standard Styles$" ;;
  -m) zmodules_mod "${(U)opts[2]}" ;;
  -b) zbuiltins "${(qq)opts[2]}" ;;
  -o) zoptions "${(qq)opts[2]}" ;;
  -p) zexpn "${(qq)opts[2]}" ;;
  -s) zparam "${(qq)opts[2]}" ;;
  -w) zzle "${(qq)opts[2]}" ;;
  -h|--help) usage ;;
  *) man -P "less -p'^ *$@ '" zshall ;;
esac
