# Developer Zsh. Zsh 5.9 or newer. Antidote 2.3.0 static bundle.
# Copy to ${ZDOTDIR:-$HOME}/.zshrc and copy developer.zsh_plugins.txt to
# ${ZDOTDIR:-$HOME}/.zsh_plugins.txt. Do not also start Oh My Zsh.

HISTFILE=${ZDOTDIR:-$HOME}/.zsh_history
HISTSIZE=50000
SAVEHIST=50000
setopt EXTENDED_HISTORY
setopt SHARE_HISTORY
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_REDUCE_BLANKS
setopt HIST_FCNTL_LOCK
setopt INTERACTIVE_COMMENTS

# Used only when Starship is absent. Starship replaces PROMPT below.
PROMPT='%F{cyan}%~%f %# '

# post:fm_zsh_compinit runs after zsh-completions is added to fpath.
fm_zsh_compinit() {
  autoload -Uz compinit
  compinit -d "${ZDOTDIR:-$HOME}/.zcompdump"
}

# post:fm_zsh_history_keys runs after history-substring-search is sourced
# and before zsh-syntax-highlighting. Arrow sequences differ by terminal.
fm_zsh_history_keys() {
  bindkey '^[[A' history-substring-search-up
  bindkey '^[[B' history-substring-search-down
  bindkey -M emacs '^P' history-substring-search-up
  bindkey -M emacs '^N' history-substring-search-down
}

# Stat known paths. `brew --prefix` is a process, so it is not on this path.
antidote_zsh=
for candidate in \
  "${ZDOTDIR:-$HOME}/.antidote/antidote.zsh" \
  /opt/homebrew/opt/antidote/share/antidote/antidote.zsh \
  /usr/local/opt/antidote/share/antidote/antidote.zsh
do
  if [[ -r $candidate ]]; then
    antidote_zsh=$candidate
    break
  fi
done

plugins_txt=${ZDOTDIR:-$HOME}/.zsh_plugins.txt
if [[ -n $antidote_zsh && -r $plugins_txt ]]; then
  # antidote load writes a static file and sources it. On a failed rebuild
  # it keeps the previous static file. Do not also call antidote init.
  source "$antidote_zsh"
  antidote load "$plugins_txt"
else
  fm_zsh_compinit
fi

# fzf is a program. `fzf --zsh` prints the integration; it is not a plugin repo.
if command -v fzf >/dev/null 2>&1; then
  source <(fzf --zsh)
fi

if command -v zoxide >/dev/null 2>&1; then
  eval "$(zoxide init zsh)"
fi

if command -v starship >/dev/null 2>&1; then
  eval "$(starship init zsh)"
fi

# Atuin also binds Ctrl-R. Uncomment only if Atuin should own history search.
# if command -v atuin >/dev/null 2>&1; then
#   eval "$(atuin init zsh)"
# fi

# direnv runs an allowed .envrc on directory entry. Uncomment after you
# have read https://direnv.net/ and you will not allow a file you have not read.
# if command -v direnv >/dev/null 2>&1; then
#   eval "$(direnv hook zsh)"
# fi
