# vim: filetype=zsh

# Hooks
function chpwd() {
  if [[ -n "$VIRTUAL_ENV" && -f "$VIRTUAL_ENV/bin/activate" ]]; then
    local venv_dir="${VIRTUAL_ENV:h}"
    if [[ "$PWD" != "$venv_dir"* ]] && (( $+functions[deactivate] )); then
      deactivate
    fi
  fi

  for v in .venv venv .env; do
    if [[ -f "$v/bin/activate" ]]; then
      source "$v/bin/activate"
      break
    fi
  done
}

# Funktionen
function source_file() {
  [[ ! -r "$1" ]] || source "$1"
}

# Optionen
setopt   AUTO_PARAM_SLASH
unsetopt CASE_GLOB
setopt   HIST_SAVE_NO_DUPS
setopt   INC_APPEND_HISTORY
setopt   SHARE_HISTORY
setopt   HIST_IGNORE_ALL_DUPS
setopt   HIST_IGNORE_SPACE

source_file "$HOME/.opam/opam-init/init.zsh"

case "$OSTYPE" in
  darwin*)
    source_file "/opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
    source_file "/opt/homebrew/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
    ;;
  freebsd*)
    source_file "/usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
    source_file "/usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
    ;;
esac

if (( ${+ZSH_HIGHLIGHT_STYLES} )); then
  ZSH_HIGHLIGHT_STYLES[path]='none'
  ZSH_HIGHLIGHT_STYLES[path_prefix]='none'
fi

bindkey -v
export KEYTIMEOUT=1

# Keybindings
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

autoload -Uz surround
zle -N delete-surround surround
zle -N add-surround surround
zle -N change-surround surround
bindkey -M vicmd cs change-surround
bindkey -M vicmd ds delete-surround
bindkey -M vicmd ys add-surround
bindkey -M visual S add-surround

bindkey '^A' vi-beginning-of-line
bindkey '^E' vi-end-of-line

# Aliase
alias mc='mc -u'
alias gdb='gdb -q'
alias pwgen='pwgen -s 48 1'
alias ls='ls -G'
alias nvim-update='nvim --headless "+Lazy! sync" "+qa"'

# Command completion
autoload -U compinit; compinit
_comp_options+=(globdots) # With hidden files

zstyle ':completion:*' completer _extensions _complete _approximate

zstyle ':completion:*' menu select

zstyle ':completion:*:*:*:*:descriptions' format '%F{green}-- %d --%f'
zstyle ':completion:*:*:*:*:corrections' format '%F{yellow}!- %d (errors: %e) -!%f'
zstyle ':completion:*:*:*:*:messages' format ' %F{purple} -- %d --%f'
zstyle ':completion:*:*:*:*:warnings' format ' %F{red}-- no matches found --%f'
zstyle ':completion:*' group-name ''

# Prompt
autoload -Uz vcs_info
precmd_vcs_info() { vcs_info }
precmd_functions+=( precmd_vcs_info )
setopt prompt_subst

RPROMPT=\$vcs_info_msg_0_
PROMPT=$'[%F{green}%8>..>%n%>>%F{white}@%F{green}%8>..>%m%>>%F{white}] %F{white}%(4~|.../%3~|%~) %B%F{blue}>%f%b '

zstyle ':vcs_info:*' unstagedstr ' *'
zstyle ':vcs_info:*' stagedstr ' +'
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' actionformats '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%a%u%c%F{3}|%F{1}%a%F{5}]%f'
zstyle ':vcs_info:*' formats       '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%u%c%F{5}]%f'
zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat '%b%F{1}:%F{3}%r'
