unsetopt beep
# unsetopt AUTO_MENU
# setopt MENU_COMPLETE
# setopt AUTO_CD

# History
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt HIST_IGNORE_ALL_DUPS
setopt EXTENDED_HISTORY
setopt APPEND_HISTORY

if [ -e "/usr/share/fzf" ]; then
    source /usr/share/fzf/key-bindings.zsh
    source /usr/share/fzf/completion.zsh
fi

### Aliases ---------------------------------------------
alias vim="nvim"
alias rm="rm -i"
alias mv="mv -i"
alias ls="exa --group-directories-first"
alias ll="ls -lh"
alias la="ls -lha"
alias grep="grep --color=auto"
alias less="less -r"
alias nnn="nnn -e"
[ "$TERM" = "xterm-kitty" ] && alias ssh="kitty +kitten ssh"
alias gs="git status"
alias gap="git add -p"
alias gc="git commit"
alias lg="lazygit"
### -----------------------------------------------------

# Fix keys
bindkey '^[[H'      beginning-of-line
bindkey '^[[F'      end-of-line
bindkey '^H'        backward-delete-word
bindkey '^[[1;5D'   backward-word
bindkey '^[[1;5C'   forward-word
bindkey '^[[3~'     delete-char
bindkey '^[[3;5~'   delete-word

### zsh-autosuggestions
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=#a6a6a6"
ZSH_AUTOSUGGEST_USE_ASYNC=1
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=20
zle -N autosuggest-accept
bindkey '^ ' autosuggest-accept

export ZISH_HOME=$HOME/.local/share/zish
export RUSTUP_HOME="$XDG_DATA_HOME"/rustup
export PATH="$PATH:$HOME/.local/bin"

install_plugin() {
    IFS='/' read group project <<< "$1"
    repo=$1
    dir="$ZISH_HOME/repos/$repo"
    shift

    while [ $# -gt 0 ]; do
        IFS=':' read action value <<< "$1"
        case $action in
            "build")
                build=$value
                ;;
            "use")
                use=$dir/$value
                ;;
        esac
        shift
    done

    if [[ ! -d $dir ]]; then
        mkdir -p $dir && cd $dir && git clone --depth=1 "https://github.com/$repo" .
        [ -v build ] && eval $build
    fi

    [ -v use ] && source $use || source $dir/$project.plugin.zsh
}

install_plugin "zsh-users/zsh-syntax-highlighting"
install_plugin "zsh-users/zsh-autosuggestions"
install_plugin "zsh-users/zsh-completions"
install_plugin "trapd00r/LS_COLORS" build:"dircolors -b LS_COLORS > c.zsh" use:"c.zsh"

zstyle ":completion:*" list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
zstyle ':completion:*' menu select

autoload -U compinit

eval "$(zoxide init zsh)"

# starship prompt: https://github.com/starship/starship
# Must be at end of file
eval "$(starship init zsh)"