#!/usr/bin/env zsh

_plugin() {
  _perf_timer_start "plugin $1"
  plugin "$@"
  _perf_timer_stop "plugin $1"
}

_plugin completions 'zsh-users/zsh-completions'

# Oh My Zsh {{{

  omz_features=(key-bindings termsupport)
  omz_plugins=(git command-not-found)

  _plugin ohmyzsh 'ohmyzsh/ohmyzsh' \
    load='lib/'${^omz_features}'.zsh' \
    before_load='ZSH="$plugin_dir"' \
    after_load='plugin-cfg-path fpath prepend plugins/'${^omz_plugins} \
    after_load='plugin-cfg-path fpath prepend plugins/rust' # completion script for `rustc`

# }}}

# directory jumping {{{
  # <https://github.com/clvv/fasd>
  # <https://github.com/agkozak/zsh-z>
  # <https://github.com/skywind3000/z.lua>
  # <https://github.com/rupa/z>
  # <https://github.com/wting/autojump>
  # <https://github.com/ajeetdsouza/zoxide>

  ZSHZ_CASE=smart
  ZSHZ_DATA="${XDG_DATA_HOME:-$HOME/.local/share}/zshz_db.txt"
  ZSHZ_UNCOMMON=1

  # The plugin sometimes leaves behind the temporary files it creates for
  # atomically updating its database, so we find and delete them ourselves. The
  # glob selects files which were modified older than a week ago.
  for match in "${ZSHZ_DATA}".*(N.mw+1); do
    rm -f "$match"
  done; unset match

  _plugin zsh-z 'agkozak/zsh-z'

# }}}

# _plugin fzf 'junegunn/fzf' \
#   build='plugin-cfg-git-checkout-version "*"' \
#   build='./install --bin' \
#   after_load='plugin-cfg-path path prepend bin' \
#   after_load='plugin-cfg-path manpath prepend man'

FAST_WORK_DIR="$ZSH_CACHE_DIR"
if [[ "$TERM" != "linux" ]]; then
  _plugin fast-syntax-highlighting 'zdharma-continuum/fast-syntax-highlighting'
  set-my-syntax-theme() { fast-theme "$ZSH_DOTFILES/my-syntax-theme.ini" "$@"; }
  if [[ "$FAST_THEME_NAME" != "my-syntax-theme" && -z "$DOTFILES_DISABLE_MY_SYNTAX_THEME" ]]; then
    set-my-syntax-theme
  fi
fi

if (( _is_macos )); then
  _plugin retina 'https://raw.githubusercontent.com/lunixbochs/meta/master/utils/retina/retina.m' from=url \
    build='mkdir -p bin && gcc retina.m -framework Foundation -framework AppKit -o bin/retina' \
    after_load='plugin-cfg-path path prepend "bin"'
fi
