#compdef kaimei

autoload -U is-at-least

_kaimei() {
    typeset -A opt_args
    typeset -a _arguments_options
    local ret=1

    if is-at-least 5.2; then
        _arguments_options=(-s -S -C)
    else
        _arguments_options=(-s -C)
    fi

    local context curcontext="$curcontext" state line
    _arguments "${_arguments_options[@]}" : \
'-t+[Template for the new name, e.g. '\''{n\:03}_{stem|lower}.{ext}'\'']:TEMPLATE:_default' \
'--template=[Template for the new name, e.g. '\''{n\:03}_{stem|lower}.{ext}'\'']:TEMPLATE:_default' \
'*-e+[sed expression\: s/RE/REPL/\[gix\] or y/abc/xyz/ (repeatable, run after the template)]:EXPR:_default' \
'*--expr=[sed expression\: s/RE/REPL/\[gix\] or y/abc/xyz/ (repeatable, run after the template)]:EXPR:_default' \
'--match=[Regex matched against each name; captures become {orig.1}, {orig.NAME}, ...]:REGEX:_default' \
'--start=[First value of the counter n \[default\: 1\]]:N:_default' \
'--step=[Step of the counter n \[default\: 1\]]:N:_default' \
'-j+[Files to read metadata from at once; 0 = automatic (1 on a spinning disk) \[default\: 0\]]:N:_default' \
'--jobs=[Files to read metadata from at once; 0 = automatic (1 on a spinning disk) \[default\: 0\]]:N:_default' \
'--missing=[When a template field has no value\: error (nothing renamed), skip the file, or use ""]:MISSING:(error skip empty)' \
'*--include=[Only names matching this glob (repeatable)]:GLOB:_default' \
'*--exclude=[Skip names matching this glob; with -R, also skips such directories (repeatable)]:GLOB:_default' \
'--from-encoding=[Read names that aren'\''t UTF-8 as ENC and rename them to UTF-8\: sjis, eucjp, gbk, big5, euckr, cp1252]:ENC:_default' \
'--sort=[Order of the preview, the renames and the counter \[default\: natural\]]:SORT:(name natural mtime size)' \
'--safe=[Sanitizing profile for new names \[default\: portable\]]:SAFE:(posix portable windows)' \
'--replace-char=[What unsafe characters become, may be empty \[default\: _\]]:STR:_default' \
'--romaji-long=[How romaji writes long vowels unless the template says (romaji\:macron) \[default\: wapuro\]]:ROMAJI_LONG:(wapuro macron double none)' \
'--color=[\[default\: auto\]]:COLOR:(auto always never)' \
'(--no-config)--config=[Read defaults from this file instead of ~/.config/kaimei/config.toml]:FILE:_files' \
'-y[Apply the renames (without it, only preview)]' \
'--yes[Apply the renames (without it, only preview)]' \
'--counter-per-dir[Restart the counter in each directory]' \
'-R[Descend into directories given as arguments]' \
'--recursive[Descend into directories given as arguments]' \
'(--mkdir)--dirs[Rename directories too]' \
'--mkdir[Let a / in the template or an expression move files into subfolders, creating them]' \
'-E[Edit the names in \$VISUAL / \$EDITOR, then preview and confirm]' \
'--edit[Edit the names in \$VISUAL / \$EDITOR, then preview and confirm]' \
'--hidden[With -R, include names starting with a dot]' \
'-i[Case-insensitive matching for every s expression and --match]' \
'--ignore-case[Case-insensitive matching for every s expression and --match]' \
'(--whole-name)--stem-only[Expressions see only the stem, never the extension]' \
'--whole-name[Expressions see the whole name including the extension]' \
'--fullwidth[Replace unsafe punctuation with full-width forms (：？／) instead]' \
'--no-fullwidth[Turn off fullwidth (e.g. when the config file turns it on)]' \
'--nfd[Write names in NFD instead of NFC]' \
'--no-nfd[Write names in NFC (the default)]' \
'--ci[Treat names differing only in case as colliding (vfat, exFAT targets)]' \
'--no-ci[Turn off ci]' \
'--trim-dot[Remove a dot left at the end of a new name ({stem}.{ext} with no extension)]' \
'--no-trim-dot[Turn off trim-dot]' \
'--show-unchanged[List files whose name stays the same]' \
'--no-config[Ignore the config file]' \
'-h[Print help (see more with '\''--help'\'')]' \
'--help[Print help (see more with '\''--help'\'')]' \
'-V[Print version]' \
'--version[Print version]' \
':files -- Files (and with --dirs or -R, directories) to rename:_files' \
":: :_kaimei_commands" \
"*::: :->kaimei" \
&& ret=0
    case $state in
    (kaimei)
        words=($line[2] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:kaimei-command-$line[2]:"
        case $line[2] in
            (undo)
_arguments "${_arguments_options[@]}" : \
'--color=[]:COLOR:(auto always never)' \
'-n[Show what would be restored, change nothing]' \
'--dry-run[Show what would be restored, change nothing]' \
'-h[Print help]' \
'--help[Print help]' \
'::run -- Run id from `kaimei history`:_default' \
&& ret=0
;;
(history)
_arguments "${_arguments_options[@]}" : \
'-v[Also list every rename in each run]' \
'--verbose[Also list every rename in each run]' \
'-h[Print help]' \
'--help[Print help]' \
&& ret=0
;;
(forget)
_arguments "${_arguments_options[@]}" : \
'--older-than=[Runs older than this\: 90d, 12w, 6m or 2y (a bare number is days)]:AGE:_default' \
'--gone[Runs with nothing left to undo\: none of their files are still at the names the run gave them]' \
'(--older-than --gone)--all[Every run]' \
'-y[Delete them (without this\: only list what would go)]' \
'--yes[Delete them (without this\: only list what would go)]' \
'-h[Print help]' \
'--help[Print help]' \
'*::runs -- Run ids from `kaimei history`:_default' \
&& ret=0
;;
(translit)
_arguments "${_arguments_options[@]}" : \
'--romaji-long=[Default long-vowel style for romaji]:ROMAJI_LONG:(wapuro macron double none)' \
'--list[List the transliteration filters and the tables found]' \
'--digraphs[Show the digraphs the compose filter understands]' \
'--test[Show step by step how a tr\:NAME table converts the text]' \
'-h[Print help]' \
'--help[Print help]' \
'::filter -- A filter as written in a template\: romaji\:macron, ascii\:de, tr\:ru-simple, ...:_default' \
'*::text -- Text to convert (default\: each line of standard input):_default' \
&& ret=0
;;
(completions)
_arguments "${_arguments_options[@]}" : \
'-h[Print help]' \
'--help[Print help]' \
':shell:(bash elvish fish powershell zsh)' \
&& ret=0
;;
(vars)
_arguments "${_arguments_options[@]}" : \
'--match=[Regex whose captures become {orig.1}, {orig.NAME}, ...]:REGEX:_default' \
'-j+[Files to read at once; 0 = automatic]:N:_default' \
'--jobs=[Files to read at once; 0 = automatic]:N:_default' \
'-i[Case-insensitive --match]' \
'--ignore-case[Case-insensitive --match]' \
'--hash[Also show {crc32}, {md5}, {sha1} and {sha256} (reads every file in full)]' \
'-h[Print help]' \
'--help[Print help]' \
'*::files:_files' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" : \
":: :_kaimei__subcmd__help_commands" \
"*::: :->help" \
&& ret=0

    case $state in
    (help)
        words=($line[1] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:kaimei-help-command-$line[1]:"
        case $line[1] in
            (undo)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(history)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(forget)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(translit)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(completions)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(vars)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
        esac
    ;;
esac
;;
        esac
    ;;
esac
}

(( $+functions[_kaimei_commands] )) ||
_kaimei_commands() {
    local commands; commands=(
'undo:Revert a run (default\: the newest rename run not yet undone)' \
'history:List journaled runs, oldest first' \
'forget:Delete old run journals (those runs can then no longer be undone)' \
'translit:Try a transliteration filter on some text, or list filters and tables' \
'completions:Print a shell completion script (bash, zsh, fish, elvish, powershell)' \
'vars:Show every template variable for some files' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'kaimei commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__completions_commands] )) ||
_kaimei__subcmd__completions_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei completions commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__forget_commands] )) ||
_kaimei__subcmd__forget_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei forget commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__help_commands] )) ||
_kaimei__subcmd__help_commands() {
    local commands; commands=(
'undo:Revert a run (default\: the newest rename run not yet undone)' \
'history:List journaled runs, oldest first' \
'forget:Delete old run journals (those runs can then no longer be undone)' \
'translit:Try a transliteration filter on some text, or list filters and tables' \
'completions:Print a shell completion script (bash, zsh, fish, elvish, powershell)' \
'vars:Show every template variable for some files' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'kaimei help commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__help__subcmd__completions_commands] )) ||
_kaimei__subcmd__help__subcmd__completions_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei help completions commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__help__subcmd__forget_commands] )) ||
_kaimei__subcmd__help__subcmd__forget_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei help forget commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__help__subcmd__help_commands] )) ||
_kaimei__subcmd__help__subcmd__help_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei help help commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__help__subcmd__history_commands] )) ||
_kaimei__subcmd__help__subcmd__history_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei help history commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__help__subcmd__translit_commands] )) ||
_kaimei__subcmd__help__subcmd__translit_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei help translit commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__help__subcmd__undo_commands] )) ||
_kaimei__subcmd__help__subcmd__undo_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei help undo commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__help__subcmd__vars_commands] )) ||
_kaimei__subcmd__help__subcmd__vars_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei help vars commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__history_commands] )) ||
_kaimei__subcmd__history_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei history commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__translit_commands] )) ||
_kaimei__subcmd__translit_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei translit commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__undo_commands] )) ||
_kaimei__subcmd__undo_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei undo commands' commands "$@"
}
(( $+functions[_kaimei__subcmd__vars_commands] )) ||
_kaimei__subcmd__vars_commands() {
    local commands; commands=()
    _describe -t commands 'kaimei vars commands' commands "$@"
}

if [ "$funcstack[1]" = "_kaimei" ]; then
    _kaimei "$@"
else
    compdef _kaimei kaimei
fi
