# Option adjustments
unsetopt nomatch

# Swap CAPSLOCK and ESC
setxkbmap -option caps:swapescape

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# Use powerline
USE_POWERLINE="true"
# Source manjaro-zsh-configuration
if [[ -e /usr/share/zsh/manjaro-zsh-config ]]; then
  source /usr/share/zsh/manjaro-zsh-config
fi

# Use a prompt, depending on what is available:
#   1. starship
#   2. p10k
#   3. manjaro
if (( $+commands[starship] )); then
  eval "$(starship init zsh)"
else
  if [[ -s ~/.p10k.zsh ]]; then
    source ~/.p10k.zsh
  else
    # Use manjaro zsh prompt
    if [[ -e /usr/share/zsh/manjaro-zsh-prompt ]]; then
      source /usr/share/zsh/manjaro-zsh-prompt
    fi
  fi
fi

# Autosuggestions
if [[ -s /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh ]]; then
  source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
fi

# Intelligent and fast autojump
if (( $+commands[zoxide] )); then
  eval "$(zoxide init zsh)"
fi

# Cheat my way through the CLI
if (( $+commands[navi] )); then
  eval "$(navi widget zsh)"
fi

# Per-directory env
if (( $+commands[direnv])); then
  eval "$(direnv hook zsh)"
fi

# Use $FINDER and $EDITOR
if (( $+commands[$FINDER] )); then
  e?() {
    $EDITOR $($FINDER)
  }
fi

# Use fd for fzf
if (( $+commands[fd] && $+commands[fzf] )); then
  _fzf_compgen_path() {
    fd --hidden --follow --exclude ".git" . "$1"
  }

  _fzf_compgen_dir() {
    fd --type d --hidden --follow --exclude ".git" . "$1"
  }
fi

# Custom aliases
if [[ -s ~/.aliases ]]; then
  source ~/.aliases
fi
if [[ -s ~/.aliases.local ]]; then
  source ~/.aliases.local
fi

# NVM
if [[ -s "$NVM_DIR/nvm.sh" ]]; then
  source "$NVM_DIR/nvm.sh"  # This loads nvm
fi
if [[ -s "$NVM_DIR/bash_completion" ]]; then
  source "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
fi

# Virtualenvwrapper
if [[ -s "$HOME/.local/bin/virtualenvwrapper_lazy.sh" ]]; then
  source "$HOME/.local/bin/virtualenvwrapper_lazy.sh"
elif [[ -s "$HOME/.local/bin/virtualenvwrapper.sh" ]]; then
  source "$HOME/.local/bin/virtualenvwrapper.sh"
fi

# broot
if [[ -s ~/.config/broot/launcher/bash/br ]]; then
  source ~/.config/broot/launcher/bash/br
fi

# Source machine-specific setup
if [[ -s ~/.zshrc.local ]]; then
  source ~/.zshrc.local
fi