# init
source /usr/share/zinit/zinit.zsh
autoload -Uz add-zsh-hook compinit promptinit vcs_info
compinit
promptinit
setopt prompt_subst
add-zsh-hook precmd vcs_info
zstyle ':vcs_info:git:*' formats ' (%b)'
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
setopt extendedglob
unsetopt beep autocd
TERM=kitty
TERMINAL=kitty



# history
HISTFILE=~/.zhist
HISTSIZE=1000
SAVEHIST=10000



# prompts
ZLE_RPROMPT_INDENT=0

get_ip() {
  LOCAL_IP=$(ip route get 1 2>/dev/null | awk '{print $7; exit}')
  [[ -z "$LOCAL_IP" ]] && LOCAL_IP="-"
}
add-zsh-hook precmd get_ip

### for personal systems
prompt_borealis_setup() {
  PS1='%n ${${PWD:#/}:+%~}/${vcs_info_msg_0_} > '
  RPS1=' [%?]'
}
prompt_themes+=( borealis )

### for server systems
prompt_industrial_setup() {
  PS1='[%n@%m]::[$LOCAL_IP]::[%~]${vcs_info_msg_0_} > '
  RPS1=' [%?]'
}
prompt_themes+=( industrial )

prompt borealis



# keybinds
bindkey -e

typeset -g -A key

key[Home]="${terminfo[khome]}"
key[End]="${terminfo[kend]}"
key[Insert]="${terminfo[kich1]}"
key[Backspace]="${terminfo[kbs]}"
key[Delete]="${terminfo[kdch1]}"
key[Up]="${terminfo[kcuu1]}"
key[Down]="${terminfo[kcud1]}"
key[Left]="${terminfo[kcub1]}"
key[Right]="${terminfo[kcuf1]}"
key[PageUp]="${terminfo[kpp]}"
key[PageDown]="${terminfo[knp]}"
key[Shift-Tab]="${terminfo[kcbt]}"

[[ -n "${key[Home]}"       ]] && bindkey -- "${key[Home]}"        beginning-of-line
[[ -n "${key[End]}"        ]] && bindkey -- "${key[End]}"         end-of-line
[[ -n "${key[Insert]}"     ]] && bindkey -- "${key[Insert]}"      overwrite-mode
[[ -n "${key[Backspace]}"  ]] && bindkey -- "${key[Backspace]}"   backward-delete-char
[[ -n "${key[Delete]}"     ]] && bindkey -- "${key[Delete]}"      delete-char
[[ -n "${key[Up]}"         ]] && bindkey -- "${key[Up]}"          up-line-or-history
[[ -n "${key[Down]}"       ]] && bindkey -- "${key[Down]}"        down-line-or-history
[[ -n "${key[Left]}"       ]] && bindkey -- "${key[Left]}"        backward-char
[[ -n "${key[Right]}"      ]] && bindkey -- "${key[Right]}"       forward-char
[[ -n "${key[PageUp]}"     ]] && bindkey -- "${key[PageUp]}"      beginning-of-buffer-or-history
[[ -n "${key[PageDown]}"   ]] && bindkey -- "${key[PageDown]}"    end-of-buffer-or-history
[[ -n "${key[Shift-Tab]}"  ]] && bindkey -- "${key[Shift-Tab]}"   reverse-menu-complete

bindkey '^[[1;5C' emacs-forward-word
bindkey '^[[1;5D' emacs-backward-word



if (( ${+terminfo[smkx]} && ${+terminfo[rmkx]} )); then
	autoload -Uz add-zle-hook-widget
	function zle_application_mode_start { echoti smkx }
	function zle_application_mode_stop { echoti rmkx }
	add-zle-hook-widget -Uz zle-line-init zle_application_mode_start
	add-zle-hook-widget -Uz zle-line-finish zle_application_mode_stop
fi



zstyle ':completion:*' completer _complete _ignored
zstyle :compinstall filename '/home/anon/.zshrc'



# aliases and functions

alias quit='exit'

alias cp='cp -iv'
alias mv='mv -iv'
alias rm='rm -Iv'
alias mkd='mkdir -pv'
alias rmd='rm -Ivrf'

alias ls='ls -Nph --color=always --group-directories-first'
alias la='ls -ANph --color=always --group-directories-first'
alias ll='ls -lANph --color=always --group-directories-first'

alias grep='grep --color=auto'

alias chown='chown -R'

alias mount='sudo mount'
alias mnt='sudo mount'
alias unmount='sudo umount'
alias unmnt='sudo umount'
alias umnt='sudo umount'

alias '..'='cd ../'
alias '...'='cd ../../'
alias '....'='cd ../../../'
alias '.....'='cd ../../../../'

alias vim='nvim'

alias calc='bc -lqi'

alias big='sort -hr'
alias small='sort -h'

cmkd() { mkdir -pv $1 && cd $1 }
mkcd() { mkdir -pv $1 && cd $1 }
mkdc() { mkdir -pv $1 && cd $1 }

+x() { chmod +x "$@" }

sp() {
  local dirs=()

  local no_tail=0

  for arg in $@; do
    case "$1" in
      -r) no_tail=1;;
      -h) echo "Usage: sp [{-r|-h}] [dirs]" >&2; return 1;;
      -*) echo "Incorrect usage. Run $ sp -h for help." >&2; return 1;;
      *) dirs+=("$arg");;
    esac
  done

  if [[ ${#dirs[@]} -eq 0 ]]; then
    df -h | head -2
  else
    for dir in "${dirs[@]}"; do
      if [[ $no_tail -eq 1 ]]; then
        du -h $dir
      else
        du -h $dir | tail -1
      fi
    done
  fi
}

extract() {
  for archive in "$@"; do
    if [ -f "$archive" ]; then
      case $archive in
        *.tar.gz) tar xvzf $archive ;;
        *.tar.xz) tar xvJf $archive ;;
        *.tar.bz2) tar xvjf $archive ;;
        *.bz2) bunzip2 $archive ;;
        *.rar) unrar x $archive ;;
        *.gz) gunzip $archive ;;
        *.tar) tar xvf $archive ;;
        *.tbz2) tar xvjf $archive ;;
        *.tgz) tar xvzf $archive ;;
        *.zip) unzip $archive ;;
        *.Z) uncompress $archive ;;
        *.7z) 7z x $archive ;;
        *) echo "unable to extract '$archive'" ;;
      esac
    else
      echo "'$archive' is not a valid file!"
    fi
  done
}



# custom behavior

# cd into directories without `cd` if followed by `/`
autoload -Uz add-zsh-hook
_auto_cd_slash() {
  # if the buffer ends with `/` and is a directory
  if [[ -d "$BUFFER" && "$BUFFER" == */ ]]; then
    local typed="$BUFFER"
    echo -ne "\n"    # move cursor to next line
    cd "$BUFFER"
    BUFFER=""        # prevent shell from trying to execute it
    zle reset-prompt
  else
    zle .accept-line # normal behavior
  fi
}
zle -N accept-line _auto_cd_slash

# run files without needing `./`
command_not_found_handler() {
  local cmd="$1"

  if [[ -f "./$cmd" && -x "./$cmd" ]]; then
    exec "./$cmd"
    return 0
  fi

  print -u2 "zsh: command not found: $cmd"
  return 127
}



export PATH="$HOME/.local/bin:$PATH"
