# recommended by VTE
source /etc/zsh/zprofile

bindkey -e # emacs mode

alias ls="ls --classify --color --group-directories-first --quoting-style=literal"

which eza > /dev/null
if [[ $? -eq 0 ]]; then
	alias l="eza --group --long --git --group-directories-first --binary --no-quotes"
	alias ll="l --all"
	
	alias lt="l --tree"
	alias llt="ll --tree"
	
	alias lt2="lt --level=2"
	alias llt2="llt --level=2"
	
	alias lt3="lt --level=3"
	alias llt3="llt --level=3"
	
	alias lt4="lt --level=4"
	alias llt4="llt --level=4"
	
	alias lt5="lt --level=5"
	alias llt5="llt --level=5"
else
	alias l="ls --human-readable -l"
	alias ll="l --almost-all"
fi

alias cp="cp --reflink=auto"
alias grep="grep --text --color=auto"
alias pull_website="wget --no-parent --recursive --page-requisites --convert-links --html-extension --no-clobber"
alias ..="cd .."
alias htop="htop --delay=3"
alias O=xdg-open
alias gits="git status"
alias gita="git add"
alias gitc="git commit"
alias gitp="git push"
alias gitl="git log --graph --oneline --decorate --all"
alias gitd="git diff --word-diff"
alias Y="noglob yt-dlp --no-mtime --add-metadata --embed-subs -f 'bestvideo[height<=1080]+bestaudio/best[height<=1080]'"
alias Ymkv="Y -no-container --merge-output-format mkv --remux-video mkv"
alias Ymp4="Y -no-container --merge-output-format mp4 --remux-video mp4"
alias Ysound="noglob yt-dlp --no-mtime -f bestaudio --remux-video opus"
alias p=python
alias ppp=python3
alias Ju="journalctl --unit"
alias Jue="journalctl --pager-end --unit"
alias Juf="journalctl --follow --unit"
alias Ss="systemctl status --no-pager"
alias Sc="systemctl cat"
alias SE="systemctl enable"
alias SD="systemctl disable"
alias SS="systemctl start"
alias SR="systemctl restart"
alias SL="systemctl reload"
alias SX="systemctl stop"
alias uJu="journalctl --user --unit"
alias uJue="journalctl --user --pager-end --unit"
alias uJuf="journalctl --user --follow --unit"
alias uSs="systemctl --user status --no-pager"
alias uSE="systemctl --user enable"
alias uSD="systemctl --user disable"
alias uSS="systemctl --user start"
alias uSR="systemctl --user restart"
alias uSL="systemctl --user reload"
alias uSX="systemctl --user stop"
alias dmesg="dmesg -e"

# Edit the current command line in $EDITOR
export VISUAL="nano"
autoload -U edit-command-line
zle -N edit-command-line
bindkey '\C-e' edit-command-line

notify () {
	notify-send -u critical "Attention required on $(tty)" "${1}"
}

mkcd () {
	mkdir -p "$1" && cd "$1"
}

psg () { ps axu | grep -v grep | grep -i $* }

pskill () {
	pids=($(psg $1|awk '{print $2}'))
	
	for pid in $pids; do
		echo "Killing $pid..."
		kill $pid
	done
}



# ╭─────────────────────────────────────╮
# │  ❤️ FZF ❤️ config and integrations  │ 
# ╰─────────────────────────────────────╯

# don't follow symlinks due to Wine's love for symlink loops (dosdevices/z: -> /)
export FZF_DEFAULT_OPTS='--walker=file,dir,hidden --preview "bat --style=plain" --bind alt-a:select-all'
export FZF_CTRL_R_OPTS='--preview=""'
source <(fzf --zsh)

# Helix and Neovim launchers
alias hf='fzf -m --bind "enter:become(hx {+})"'
alias nf='fzf -m --bind "enter:become(nvim {+})"'

# get one or more PIDs using fzf
function p_pids {
	ps -eo user,pid,ppid,start,time,command | fzf -m --header-lines=1 --no-preview --wrap | awk '{print $2}'
}

# p_kill is not necessary, you can just kill -SIGNAL **<tab>

# attach scanmem to a PID
function p_scanmem {
	pids=($(p_pids))
	scanmem -p ${pids[1]}
}

function zres {
	. /etc/zsh/zshrc
	[[ -f ~/.zshrc ]] && . ~/.zshrc
	hash -r
}

function http-dir {
	echo "Serving $PWD on 0.0.0.0:55555"
	python3 -m http.server 55555
}

function failed_while {
	echo "failed while ${1}"
	exit 1
}

# `loop-shell COMMAND` creates a shell that takes user_input and runs COMMAND user_input
# e.g. `loop-shell grep -ri` creates a grepping shell
function loop-shell {
	while true
	do
		echo -n "-> "
		read Q
		$@ $Q
	done
}

# history including search
HISTFILE=~/.history
HISTSIZE=10000000
SAVEHIST=10000000
setopt HIST_IGNORE_SPACE # Don't record an entry starting with a space.
setopt INC_APPEND_HISTORY_TIME # Append to file immediately.
bindkey '\e[A' history-beginning-search-backward # Completion from history
bindkey '\e[B' history-beginning-search-forward  # with Up/Down keys.

function precmd() {
	## zsh hook called before the prompt is printed.  See zshmisc(1).
	
	# over-prompt tie in
	if [[ ! -v OVER_PROMPT_DISABLED ]]; then
		__over_prompt_precmd || export OVER_PROMPT_DISABLED=1
	fi
}

function preexec () {
	if [[ ! -v OVER_PROMPT_DISABLED ]]; then
		__over_prompt_preexec "$1" || export OVER_PROMPT_DISABLED=1
	fi
}

# general settings
setopt EXTENDED_GLOB
setopt NO_CLOBBER # prevents cat < file > file
unsetopt CORRECTALL # no
setopt NO_BEEP # hell no
unsetopt equals # allows me to use strings like =this without escaping

# File completion
export ZLE_REMOVE_SUFFIX_CHARS=$' \t\n;&' # don't remove these chars after completion
setopt AUTOLIST
setopt NO_LIST_AMBIGUOUS # list files immediately
setopt LIST_PACKED
zmodload -i zsh/complist # colors
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion::complete:*' use-cache 1
zstyle ':completion:*' special-dirs true
autoload -U compinit
compinit
autoload colors zsh/terminfo

# app-shells/over-env
source /usr/share/over-env/prompt-init
export PYTHONSTARTUP="/usr/share/over-env/python-startup.py"

# keyboard stuff
bindkey "\e[3~" delete-char # Delete
bindkey "\e[1~" beginning-of-line # Home
bindkey "\e[H" beginning-of-line # Home
bindkey "\e[4~" end-of-line # End
bindkey "\e[F" end-of-line # End
bindkey "\e[1;5D" backward-word # Ctrl ←
bindkey "\e[1;5C" forward-word # Ctrl →

# load syntax highlighting if available; Gentoo and Arch paths are tried
for HPATH in "/usr/share/zsh/site-functions/zsh-syntax-highlighting.zsh" "/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
do
	if [[ -e "${HPATH}" ]]; then
		source "${HPATH}"
		break
	fi
done
