# Enable vi mode - you can uncomment the line below to enable vim keybindings
#vim_mode=True

# Enable colors and change prompt:
autoload -U colors && colors    # Load colors
# Maia prompt
PROMPT="%B%{$fg[cyan]%}%(4~|%-1~/.../%2~|%~)%u%b >%{$fg[cyan]%}>%B%(?.%{$fg[cyan]%}.%{$fg[red]%})>%{$reset_color%}%b "
RPROMPT="%{$fg[red]%} %(?..[%?])"

setopt autocd        # Automatically cd into typed directory.
stty stop undef        # Disable ctrl-s to freeze terminal.
setopt interactive_comments

# Enable command history
HISTFILE=~/.zsh_history
HISTSIZE=10000000
SAVEHIST=10000000
setopt histignorealldups # Ignore duplicates in history
setopt HIST_IGNORE_SPACE # Ignore commands that begin with a space in history

# Online cheat sheet
chtsh () {
    ping -c 1 cheat.sh >/dev/null 2>&1 || { echo 'Cannot connect to cheat.sh !\nCheck your internet connection'; return 1 }
    curl -s "cheat.sh/$1" | less -P "\ Cheat\ sheet\ ?ltline\ %lt?L/%L.: byte\ %bB?s/%s..?\ (END):?pB\ %pB\\%.. (press h for help or q to quit)"
}

# Aliases

alias cp="cp -iv"                    # Prompt before overwrite
alias mv="mv -iv"                    # Prompt before overwrite
alias rm="rm -vI"                    # Prompt once before removing more than 3 files, or when removing recursively
alias ffmpeg="ffmpeg -hide_banner"    # Suppress printing banner
alias df="df -h"                    # human-readable sizes
alias du="du -h"                    # human-readable sizes

# Colorize commands when possible
alias ls="ls -h --color=auto --group-directories-first"    # human-readable sizes, group directories before files
alias grep="grep --color=auto"
alias egrep="egrep --colour=auto"
alias fgrep="fgrep --colour=auto"
alias diff="diff --color=auto"
alias pacman="pacman --color=auto"

# Basic auto/tab complete:
autoload -U compinit
zstyle ':completion:*' menu select
zmodload zsh/complist
compinit
_comp_options+=(globdots)        # Include hidden files.
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'    # Case insensitive tab completion
source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.plugin.zsh 2>/dev/null

# Change cursor shape for different vi modes.
function zle-keymap-select () {
    case $KEYMAP in
        vicmd) echo -ne '\e[2 q';;      # block
        viins|main) echo -ne '\e[6 q';; # beam
    esac
}
zle-line-init() {
    zle -K viins # initiate `vi insert` as keymap (can be removed if `bindkey -V` has been set elsewhere)
    echo -ne "\e[6 q"
}


[ "$vim_mode" = 'True' ] &&
    { bindkey -v
    export KEYTIMEOUT=1
    bindkey -v '^?' backward-delete-char
    zle -N zle-keymap-select
    zle -N zle-line-init
    echo -ne '\e[6 q' # Use beam shape cursor on startup.
    preexec() { echo -ne '\e[6 q' ;} # Use beam shape cursor for each new prompt.

    # Use vim keys in tab complete menu:
    bindkey -M menuselect 'h' vi-backward-char
    bindkey -M menuselect 'k' vi-up-line-or-history
    bindkey -M menuselect 'l' vi-forward-char
    bindkey -M menuselect 'j' vi-down-line-or-history
}
unset vim_mode

# History beginning search
bindkey '^[[A' history-beginning-search-backward
bindkey '^[[B' history-beginning-search-forward
bindkey '^[[1;2A' up-line-or-history
bindkey '^[[1;2B' down-line-or-history
bindkey -a '^[[A' history-beginning-search-backward
bindkey -a '^[[B' history-beginning-search-forward
bindkey -a 'k' history-beginning-search-backward
bindkey -a 'j' history-beginning-search-forward
bindkey -a 'K' vi-up-line-or-history
bindkey -a 'J' vi-down-line-or-history

bindkey '^[[3~' delete-char; bindkey -a '^[[3~' delete-char # Make delete key work as expected

# Load syntax highlighting; should be last.
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.plugin.zsh 2>/dev/null