autoload -U colors && colors
autoload -U complist

# Set vim mode
set -o vi
# Allow editing of current line in $EDITOR using command mode V
autoload -U edit-command-line
zle -N edit-command-line
bindkey -M vicmd v edit-command-line

# Use vim keys within completion
zmodload zsh/complist
bindkey -M menuselect 'h' vi-backward-char
bindkey -M menuselect 'k' vi-up-line-or-history
bindkey -M menuselect 'l' vi-forward-char
bindkey -M menuselect 'j' vi-down-line-or-history

# Add vim like text objects
autoload -Uz select-bracketed select-quoted
zle -N select-quoted
zle -N select-bracketed
for km in viopp visual; do
    bindkey -M $km -- '-' vi-up-line-or-history
    for c in {a,i}${(s..)^:-\'\"\`\|,./:;=+@}; do
        bindkey -M $km $c select-quoted
    done
    for c in {a,i}${(s..)^:-'()[]{}<>bB'}; do
        bindkey -M $km $c select-bracketed
    done
done

# Set options

# also complete defined alias
setopt completealiases
# Correct mistyped commands
setopt correctall
# Try to make the completion list smaller (occupying less lines) by printing the matches in columns with different widths.
setopt list_packed
# When listing files that are possible completions, show the type of each file with a trailing identifying mark.
setopt list_types

# Set Prompt
autoload -Uz add-zsh-hook
setopt promptsubst

nix_subshell () {
    prompt_color="blue"
    # check for IN_NIX_SHELL
    if [[ "$IN_NIX_SHELL" ]]; then
        prompt_color="yellow"
    fi
}
add-zsh-hook precmd nix_subshell

PROMPT=$'\n%{$fg[${prompt_color}]%}%1~ > %{$reset_color%}'

zstyle ':completion:*:descriptions' format '%F{blue}-- %d --%f'
zstyle ':completion:*:messages' format ' %F{purple} -- %d --%f'
zstyle ':completion:*:warnings' format '%F{yellow} -- Sorry no matches -- %d%b%f'
zstyle ':completion:*:*:*:*:corrections' format '%F{yellow}!- %d (errors: %e) -!%f'
zstyle ':completion:*' menu select
zstyle ':completion:*' use-cache on
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}

zstyle ':completion:*' completer _extensions _complete

zstyle ':completion:*' group-name ''
zstyle ':completion::complete:git-checkout:argument-rest:' group-order heads-local heads-remote commit-tags
zstyle ':completion::complete:git-checkout:argument-rest:commits' command 'echo'
zstyle ':completion::complete:git-checkout:argument-rest:valid-ref-names' command 'echo'
zstyle ':completion::complete:git-checkout:argument-rest:remote-branch-refs-noprefix' command 'echo'


# Configuration for fzf-tab, see: https://github.com/Aloxaf/fzf-tab#configure
# disable sort when completing `git checkout`
zstyle ':completion:*:git-checkout:*' sort false
# set descriptions format to enable group support
zstyle ':completion:*:descriptions' format '[%d]'
# set list-colors to enable filename colorizing
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
# switch group using `,` and `.`
zstyle ':fzf-tab:*' switch-group ',' '.'
