# For interactive shells
HISTFILE=~/.zhistory
HISTSIZE=10000
SAVEHIST=10000
setopt \
  ALWAYS_TO_END \
  APPEND_HISTORY \
  AUTO_CD \
  AUTO_LIST \
  AUTO_MENU \
  AUTO_PARAM_SLASH \
  AUTO_PUSHD \
  BANG_HIST \
  C_BASES \
  COMPLETE_IN_WORD \
  EXTENDED_GLOB \
  EXTENDED_HISTORY \
  HIST_EXPIRE_DUPS_FIRST \
  HIST_FIND_NO_DUPS \
  HIST_IGNORE_DUPS \
  HIST_IGNORE_SPACE \
  HIST_LEX_WORDS \
  HIST_SAVE_NO_DUPS \
  HIST_VERIFY \
  INTERACTIVE_COMMENTS \
  LONG_LIST_JOBS \
  MULTIOS \
  NO_CLOBBER \
  NO_HUP \
  NOMATCH \
  NOTIFY \
  PUSHD_IGNORE_DUPS \
  PUSHD_SILENT \
  PUSHD_TO_HOME \
  RC_QUOTES \
  SHARE_HISTORY
unsetopt \
  BEEP \
  CDABLE_VARS \
  HIST_BEEP \
  LIST_BEEP \
  FLOW_CONTROL \
  MAIL_WARNING \
  HUP \
  BG_NICE \
  CHECK_JOBS
# vi keybindings
bindkey -v

# Allow core files
ulimit -c unlimited

DIRSTACKSIZE=16
# Set terminal title
case $TERM in
  xterm*)
    precmd () {print -Pn "\e]0;%n@%m: %~\a"}
    ;;
esac

autoload -U colors && colors
PS1="%{$fg[black]%}[%{$fg[yellow]%}%h%{$fg[black]%}] %{%(!.$fg[red].$fg[green])%}%8>..>%n%>>%{$fg[white]%}@%{$fg[blue]%}%12>..>%m%>>%{$fg[white]%}:%{$fg[green]%}%32<...<%~%<<%{$fg[white]%}%#%{$reset_color%} "

zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion::complete:*' use-cache on
zstyle ':completion::complete:*' cache-path "${ZDOTDIR:-$HOME}/.zcompcache"

# .profile is universal
. ~/.profile
# Deduplicate the path
typeset -U path

# Additional Keybindings
bindkey '^[[A' history-search-backward
bindkey '^[[B' history-search-forward
# ctrl-arrow keys
bindkey '^[[1;5C' forward-word
bindkey '^[[1;5D' backward-word
bindkey '^P' up-history
bindkey '^N' down-history
bindkey '^?' backward-delete-char
bindkey '^h' backward-delete-char
# ok, a few emacs convenience bindings
bindkey '^w' backward-kill-word
bindkey '^r' history-incremental-search-backward
# delete really deletes
bindkey "^[[3~" delete-char

# Source extras and aliases if interactive
if [[ $- == *i* ]] ; then
  if [[ -e $HOME/.aliases ]] ; then source $HOME/.aliases ; fi
  if [[ -e $HOME/.aliases.local ]] ; then source $HOME/.aliases.local ; fi
  # zsh-only-ism to avoid error if glob doesn't expand
  for file in $HOME/.zshrc.d/[a-zA-Z0-9]*.zsh(N) ; do
    source "$file"
  done
  # extra completions, prompt
  fpath=(~/.zshrc.completions ~/.zshrc.d/matir_prompt ~/.zshrc.d/agnoster_prompt $fpath)
  # Completion
  zstyle ':compinstall' filename "${HOME}/.zshrc"
  zstyle ':completion:*' users root ${USER}
  # Modules after fpath
  autoload -Uz compinit && compinit -i
  autoload -Uz promptinit && promptinit
  # Virtualenvwrapper
  if test -f /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh ; then
    source /usr/share/virtualenvwrapper/virtualenvwrapper_lazy.sh
  fi
  if command ls --version >/dev/null 2>&1 ; then
    alias ls="$(whence -p ls) --color -C"
  fi
  # Syntax highlighting and substring search
  if test -f /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ; then
    source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
  fi
  if test -f ${HOME}/.zshrc.d/_zsh-history-substring-search.zsh ; then
    source ${HOME}/.zshrc.d/_zsh-history-substring-search.zsh
    bindkey '^[[A' history-substring-search-up
    bindkey '^[[B' history-substring-search-down
    bindkey -M vicmd 'k' history-substring-search-up
    bindkey -M vicmd 'j' history-substring-search-down
  fi
  # Suggestions
  if test -f /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh ; then
    # Works well for solarized
    ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=10"
    # Strategy -- note that 'completion' is slow AF
    ZSH_AUTOSUGGEST_STRATEGY=(history)
    source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
  fi
fi  # End interactive-only block

# In case ack is named ack-grep
if [ -x /usr/bin/ack-grep ] ; then
  alias ack='/usr/bin/ack-grep'
fi

# Got rust?
if test -d ${HOME}/.cargo/bin ; then
  PATH=${PATH}:${HOME}/.cargo/bin
fi

# Pip packages
if test -d ${HOME}/.local/bin ; then
  PATH=${PATH}:${HOME}/.local/bin
fi

# Most is nice, if we have it
if command -v most >/dev/null 2>&1; then
  export PAGER="most"
fi

# Load any local settings
if [ -e $HOME/.zshrc.local ] ; then source $HOME/.zshrc.local ; fi

# Set prompt based on local settings
prompt "${THEME:-matir}"
