###########
# OPTIONS #
###########
#-> https://github.com/Chrysostomus/manjaro-zsh-config/blob/master/manjaro-zsh-config

setopt correct			# Auto correct mistakes
setopt extendedglob		# Extended globbing. Allows using regular expressions with *
setopt nocaseglob		# Case insensitive globbing
setopt rcexpandparam		# Array expension with parameters
setopt nocheckjobs		# Don't warn about running processes when exiting
setopt numericglobsort		# Sort filenames numerically when it makes sense
setopt nobeep			# No beep
setopt appendhistory		# Immediately append history instead of overwriting
setopt histignorealldups	# If a new command is a duplicate, remove the older one
setopt autocd			# If only directory path is entered, cd there.
setopt histignorespace		# Don't save commands that start with space

# Command completion
zstyle ':completion:*' matcher-list 'm:{[:lower:][:upper:]}={[:upper:][:lower:]}' # Case insensitive tab completion
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"	# Colored completion (different colors for dirs/files/etc)
zstyle ':completion:*' rehash true			# Automatically find new executables in path
zstyle ':completion:*' menu select			# Arrow-key driven autocompletion menu
## Speed up completions
zstyle ':completion:*' accept-exact '*(N)'
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.zsh/cache
## Directories first
zstyle ':completion:*:matches'         group 'yes'
zstyle ':completion:*'                 group-name ''
zstyle ':completion:*' list-dirs-first true

WORDCHARS=${WORDCHARS//\/[&.;]}				# Don't consider certain characters part of the word

## Command completion
autoload -Uz compinit
compinit

## History search
autoload -Uz history-substring-search-up history-substring-search-down
zle -N history-substring-search-up
zle -N history-substring-search-down

## History in cache directory
HISTSIZE=1000
SAVEHIST=500
HISTFILE=~/.cache/zsh-history


################
# KEY BINDINGS #
################
#-> https://wiki.archlinux.org/title/Zsh#Key_bindings
# Create a zkbd compatible hash;
# to add other keys to this hash, see: man 5 terminfo
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]}"

key[Control-Left]="${terminfo[kLFT5]}"
key[Control-Right]="${terminfo[kRIT5]}"

# Setup key accordingly
[[ -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]}"         history-substring-search-up	#->Plugin: zsh-history-substring-search
[[ -n "${key[Down]}"      ]] && bindkey -- "${key[Down]}"       history-substring-search-down	#->Plugin: zsh-history-substring-search
[[ -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

[[ -n "${key[Control-Left]}"  ]] && bindkey -- "${key[Control-Left]}"  backward-word
[[ -n "${key[Control-Right]}" ]] && bindkey -- "${key[Control-Right]}" forward-word

# Finally, make sure the terminal is in application mode, when zle is
# active. Only then are the values from $terminfo valid.
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


###########
# ALIASES #
###########

## Color
alias diff='diff --color=auto'
alias grep='grep --colour=auto'
alias egrep='egrep --colour=auto'
alias ip='ip -color=auto'
alias ls='ls --color=auto --human-readable --group-directories-first --classify'

## Interactive & Verbose
alias cp='cp -iv'
alias mv='mv -iv'
alias rm='rm -iv'
alias ln='ln -v'
alias mkdir='mkdir -v'
alias rmdir='rmdir -v'
alias rcp='rsync -v --progress'
alias rmv='rsync -v --progress --remove-source-files'
alias chmod='chmod -c'
alias chown='chown -c'


###########
# PLUGINS #
###########

## Fish-like: autosuggestions, syntax highlighting & history search
# -> https://wiki.archlinux.org/title/Zsh#Fish-like_syntax_highlighting_and_autosuggestions
source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source /usr/share/zsh/plugins/zsh-history-substring-search/zsh-history-substring-search.zsh


###################
# Theme: Starship #
###################

eval "$(starship init zsh)"
