### ENVVARS
# zmodload zsh/zprof # for profiling, also uncomment last line

source "$HOME/.zprofile"
source "$HOME/.config/user-dirs.dirs"
export TIMEFMT=$'\nreal\t%E\nuser\t%U\nsys\t%S\ncpu\t%P'
export ZSH=$HOME/.zsh
export CLIPBOARD_NOGUI=0    # wayland only allows GUI apps to use the clipboard.
                            # cb would have to open every 2 seconds and steal
                            # focus. This sucks, so I will have to disable GUI
                            # integration (yes that means ctrl+v) for now.
                            #
                            # For wayland, set this to `1`
                            # see https://github.com/Slackadays/Clipboard/issues/171
export DATEFMT='+%a %Y-%m-%d %X'
export EDITOR=nvim
export VISUAL=nvim


### Aliases
alias l="lsd -lah   --date \"$DATEFMT\" --hyperlink=auto"
alias ll="lsd -lh   --date \"$DATEFMT\" --hyperlink=auto"
alias la="lsd -a    --date \"$DATEFMT\" --hyperlink=auto"
alias ls="lsd       --date \"$DATEFMT\" --hyperlink=auto"
alias grep="grep --color"
alias egrep="grep -E"
alias lgrep="find | grep"
alias lagrep="find -exec realpath {} \; | grep"
alias llgrep="find -type d -exec lsd -lah {} \;| grep"
alias psgrep="ps axu | grep"
alias plasmarestart="killall plasmashell; kstart5 plasmashell"
alias isotime='date +"%Y-%m-%dT%H:%M:%S%z"'
alias isodate='date +%Y-%m-%dT%H:%M:%S%z'
alias datefmt='date $DATEFMT'
alias gg=lazygit
alias sg=sourcegit
alias reload="source ~/.zshrc"
alias gls=/bin/ls
alias bat=batcat
alias accon="HOST=$(hostname) conda activate"
alias gotemp="cd $(mktemp -d)"
alias open=xdg-open
alias ipb="ip -brief"
alias psa="ps -eadf"
alias fsize="stat --printf='%s'"
alias rg="rg --no-ignore"
alias home="cd $HOME"
alias ff=firefox
alias bell="tput bel"
alias fd=fdfind
alias nobak="touch .nobackup"
# alias shutdown="confirm shutdown"

### Functions
function random() {
    if [ $# -ne 1 ] || ! [[ $1 =~ ^[0-9]+$ ]] || [ $1 -lt 1 ]; then
        echo "Error: Please provide one positive integer argument" >&2
        return 1
    fi
    echo $(( (RANDOM % $1) + 1 ))
}
function largefiles () { exec 2>/dev/null; du -ah "$@" | grep -P "^\d+(G|T|P|E)\s" }
function midfiles () { exec 2>/dev/null; du -ah "$@" | grep -P "^\d+(M|G|T|P|E)\s" }
function smallfiles () { exec 2>/dev/null; du -ah "$@" | grep -P "^\d+(B|K)\s" }
function cachekeys () { exec 2>/dev/null;
    ssh-agent-start # see ~/.zprofile
    ssh-add
}
function newpass() {
    LC_ALL=C tr -dc '[:alnum:]' < /dev/urandom | head -c${1:-40}
}
function condac() {
    conda activate $@
    export HOST=$(hostname)
}
# calculate on shell with `c 1+1`
function c() { printf "%s\n" "$@" | bc -l; }
# Call Python and execute multiple statements from args
function py() {
    python <<< "
from math import *

def evaluate_and_print(code):
    for expr in code.split(';'):
        expr = expr.strip()
        if '=' in expr:
            exec(expr)
        else:
            result = eval(expr)
            print(f\"{expr} => {result}\")

if __name__ == \"__main__\":
    expr = '$*'
    evaluate_and_print(expr)
"
}
function countlines() {
  find . -type f -name "$1" -exec wc -l {} \; | awk '{print $0} {total += $1} END {print "Total lines:", total}'
}
function git-verify-commit () {
git verify-commit $1
ret=$?
if [ $ret -ne 0 ]; then
    echo "Commit is not signed."
else
    echo "OK"
fi
return $ret
}
function confirm() {
	bash -c "
	echo \"To be executed: $@\";
	read -p \"Confirm (Y/N): \" -n 1 -r;
	case \"\$REPLY\" in
		y|Y ) echo \"es\"; eval $@;;
		n|N ) echo \"o\";;
		* ) echo -e \"\ninvalid\";;
	esac" $@
}
function confirm-important() {
	echo "Write 'yes do as i say' to confirm that you REALLY want to do this: $@"
	read
	if [[ "${REPLY,,}" = "yes do as i say" ]]
	then
		eval $@
	else
		echo "aborting"
	fi
}


### ---- zsh options -------------------------------------
setopt autocd
setopt HIST_FIND_NO_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_IGNORE_ALL_DUPS
setopt appendhistory
setopt INC_APPEND_HISTORY
setopt SHARE_HISTORY
setopt interactivecomments # allow comments in interactive mode
setopt magicequalsubst     # enable filename expansion for arguments of the form ‘anything=expression’
setopt nonomatch           # hide error message if there is no match for the pattern
setopt notify              # report the status of background jobs immediately
setopt numericglobsort     # sort filenames numerically when it makes sense
setopt promptsubst         # enable command substitution in prompt
WORDCHARS=${WORDCHARS//\/} # Don't consider certain characters part of the word
PROMPT_EOL_MARK=""

# History configurations
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=200000
setopt hist_expire_dups_first # delete duplicates first when HISTFILE size exceeds HISTSIZE
setopt hist_ignore_dups       # ignore duplicated commands history list
setopt hist_ignore_space      # ignore commands that start with space
setopt hist_verify            # show command with history expansion to user before running it

### --- Inputs Config ------------------------------------
# vim keys, then override stuff.
bindkey -v
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
bindkey -s "^X" 'n^M'

### ---- EXPAND DOTS -------------------------------------
function expand-dots() {
    local MATCH
    if [[ $LBUFFER =~ '(^| )\.\.\.+' ]]; then
        LBUFFER=$LBUFFER:fs%\.\.\.%../..%
    fi
}

function expand-dots-then-expand-or-complete() {
    zle expand-dots
    zle expand-or-complete
}

function expand-dots-then-accept-line() {
    zle expand-dots
    zle accept-line
}

zle -N expand-dots
zle -N expand-dots-then-expand-or-complete
zle -N expand-dots-then-accept-line
bindkey '^I' expand-dots-then-expand-or-complete
bindkey '^M' expand-dots-then-accept-line

### ---- THEMES -----------------------------------
PS1='%B%F{red}%n@%m%k %B%F{cyan}%(4~|...|)%3~%F{white} %# %b%f%k'

### ---- ZSH MODULES -----------------------------------
fpath+=$ZSH/zfunc
# enable completion features
autoload -Uz compinit
compinit -d ~/.cache/zcompdump
zstyle ':completion:*:*:*:*:*' menu select
zstyle ':completion:*' auto-description 'specify: %d'
zstyle ':completion:*' completer _expand _complete
zstyle ':completion:*' format 'Completing %d'
zstyle ':completion:*' group-name ''
zstyle ':completion:*' list-colors ''
zstyle ':completion:*' list-prompt %SAt %p: Hit TAB for more, or the character to insert%s
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
zstyle ':completion:*' rehash true
zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
zstyle ':completion:*' use-compctl false
zstyle ':completion:*' verbose true
zstyle ':completion:*:kill:*' command 'ps -u $USER -o pid,%cpu,tty,cputime,cmd'

# enable color support
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    export LS_COLORS="$LS_COLORS:ow=30;44:" # fix ls color for folders with 777 permissions

    alias dir='dir --color=auto'
    alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
    alias diff='diff --color=auto'
    alias ip='ip --color=auto'

    export LESS_TERMCAP_mb=$'\E[1;31m'     # begin blink
    export LESS_TERMCAP_md=$'\E[1;36m'     # begin bold
    export LESS_TERMCAP_me=$'\E[0m'        # reset bold/blink
    export LESS_TERMCAP_so=$'\E[01;33m'    # begin reverse video
    export LESS_TERMCAP_se=$'\E[0m'        # reset reverse video
    export LESS_TERMCAP_us=$'\E[1;32m'     # begin underline
    export LESS_TERMCAP_ue=$'\E[0m'        # reset underline

    # Take advantage of $LS_COLORS for completion as well
    zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
    zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
fi

### ---- PLUGINS -----------------------------------
source $ZSH/plugins/fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh
source $ZSH/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
source $ZSH/plugins/zsh-autoquoter/zsh-autoquoter.zsh
ZSH_HIGHLIGHT_HIGHLIGHTERS+=(zaq)
source ~/.local/share/fzf/key-bindings.zsh
source ~/.local/share/fzf/completion.zsh
fpath=($ZSH/plugins/zsh-completions/src $fpath)

ZAQ_PREFIXES+=('git commit( [^ ]##)# -[^ -]#m')
ZAQ_PREFIXES_GREEDY+=('py #')
ZAQ_PREFIXES_GREEDY+=('c #')
ZAQ_PREFIXES_GREEDY+=('countlines #')

### --- fzf Config ------------------------------------
export FZF_CTRL_T_OPTS="--preview '(highlight -O ansi -l {} 2> /dev/null || cat {} || tree -C {}) 2> /dev/null | head -200'"
export FZF_CTRL_R_OPTS='--no-sort --exact'

### --- nnn Config ------------------------------------
source ~/.local/share/nnn/quitcd/quitcd.bash_zsh
export NNN_PLUG='j:jump;z:autojump;'

### --- kitty Config ------------------------------------
# $KITTY_TERM is a custom envar I set in the kitty conf
# this stuff does not work nicely with tmux, just make them regular aliases. Shows an error if you use them in another terminal
#if [[ "$KITTY_TERM" -eq "TRUE" ]]
#then
#    alias ssh="kitty +kitten ssh"
#    alias tmux="export KITTY_TERM='TRUE'; tmux"
#fi
alias kssh="kitty +kitten ssh"
alias kimg="kitty +kitten icat"
# kdiff does not work on my system for some reason
#alias kdiff="kitty +kitten diff"

### --- zoxide Config ------------------------------------
if ! command -v zoxide &> /dev/null
then
    # zoxide not installed, skipping
    # do nothing
else
    eval "$(zoxide init zsh)"
fi

### --- pyenv Config -------------------------------------
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init - zsh)"

. "$HOME/.cargo/env"

zstyle ':completion:*' menu select
fpath+=~/.zfunc

export NVM_DIR="$HOME/.config/nvm"
# diese Scheiße läd so ewig, wer auch immer dachte dass das so eine gute idee
# ist gehört gefeuert. Das muss jeden Tag viele leute minuten kosten.
# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
# [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# on demand nvm
function load_nvm() {
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
}
alias nvim="load_nvm && nvim"
# zprof

### load unversioned zsh code
if [ -f ~/.zsh.local ]; then
    source ~/.zsh.local
else
    touch ~/.zsh.local
fi
