#!/bin/zsh

[ -n "$EAT_SHELL_INTEGRATION_DIR" ] && \
  source "$EAT_SHELL_INTEGRATION_DIR/zsh"

# ZSH Configurations
unsetopt autocd               # Change directory just by typing its name (hurts performance)
setopt interactivecomments    # Allow comments in interactive mode
setopt complete_aliases       # Allows auto-completion with aliases
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
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
unsetopt ksharrays # 0-indexing arrays breaks highlighting

# vi mode
bindkey -v
bindkey -M vicmd -r ":"
export KEYTIMEOUT=1

# Change cursor shape for different vi modes.
function zle-keymap-select {
  if [[ ${KEYMAP} == vicmd ]] ||
     [[ $1 = 'block' ]]; then
    echo -ne '\e[1 q'
  elif [[ ${KEYMAP} == main ]] ||
       [[ ${KEYMAP} == viins ]] ||
       [[ ${KEYMAP} = '' ]] ||
       [[ $1 = 'beam' ]]; then
    echo -ne '\e[5 q'
  fi
}
zle -N zle-keymap-select
zle-line-init() {
    zle -K viins # initiate `vi insert` as keymap (can be removed if `bindkey -V` has been set elsewhere)
    echo -ne "\e[5 q"
}
zle -N zle-line-init
echo -ne '\e[5 q' # Use beam shape cursor on startup.
preexec() { echo -ne '\e[5 q' ;} # Use beam shape cursor for each new prompt.

## ALIASES ##

# The rice repo
alias config='git --git-dir $HOME/repos/dotfiles/ --work-tree=$HOME'

alias pypr='sh -c pypr'
alias asciiquarium='asciiquarium -t'
alias ll='ls -l'
alias la='ls -A'
alias l='ls -CF'
alias python='python3'
alias xournal='xournalpp'
alias wget='wget --hsts-file="$XDG_CACHE_HOME/wget-hsts"'
alias sxiv='nsxiv'
alias blueman='blueman-manager'
alias spotify='dlkiller spotify'
alias zoom='dlkiller zoom'
alias tor='torbrowser-launcher'
alias emax="devour emacsclient -c -a 'emacs' 1>/dev/null"
alias em="emacsclient -nw -a 'emacs -nw'"

# Print out all colors
alias colors='for i in {0..255}; do print -Pn "%K{$i}  %k%F{$i}${(l:3::0:)i}%f " ${${(M)$((i%6)):#3}:+$"\n"}; done'

# Colorful output
alias ls='LC_COLLATE=C grc ls --color=auto --group-directories-first -hN -A'
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'
alias gcc='grc gcc'
alias ifconfig='grc ifconfig'
alias make='grc make'
alias netstat='grc netstat'
alias nmap='grc nmap'
alias ping='grc ping'
alias ping='grc ping'
alias uptime='grc uptime'

## FUNCTIONS ##

function lfcd {
    tmp="$(mktemp)"
    command lf -last-dir-path="$tmp" "$@"
    if [ -f "$tmp" ]; then
        dir="$(cat "$tmp")"
        rm -f "$tmp"
        if [ -d "$dir" ]; then
            if [ "$dir" != "$(pwd)" ]; then
                cd "$dir"
            fi
        fi
    fi
}

# This script was automatically generated by the broot program
# More information can be found in https://github.com/Canop/broot
# This function starts broot and executes the command
# it produces, if any.
# It's needed because some shell commands, like `cd`,
# have no useful effect if executed in a subshell.
function br {
    local cmd cmd_file code
    cmd_file=$(mktemp)
    if broot --outcmd "$cmd_file" "$@"; then
        cmd=$(<"$cmd_file")
        command rm -f "$cmd_file"
        eval "$cmd"
    else
        code=$?
        command rm -f "$cmd_file"
        return "$code"
    fi
}

function nix_shell_menu {
    shell_dir="$HOME/shells"
    shells=$(ls "$shell_dir")
    selection=$(echo "$shells" | rofi -dmenu)
    nix-shell "$shell_dir/$selection" --run "
        export name=${selection%.*};
        $([ "$1" = "fhs" ] && echo "fhs-run ")$([ "$1" = "sudo" ] && echo "sudo ")$SHELL
    "
}

# function man () {
#     emacsclient -nw --eval '(progn (man "'$1'"))'
# }

## KEYBINDS ##

bindkey -s '^e' 'lfcd\n'
bindkey -s '^n' 'nix_shell_menu'
bindkey -s '^[^M' 'nohup $TERMINAL -e $SHELL -c "cd $PWD && exec $SHELL" >/dev/null 2>&1 & \n'

# Enable completion features
autoload -Uz compinit
compinit -d ~/.cache/zcompdump
zstyle ':completion:*:*:*:*:*' menu select
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' # case insensitive tab completion

# Remove green background of simlinks
LS_COLORS+=':ow=01;33' 

# Don't consider certain characters part of the word
WORDCHARS=${WORDCHARS//\/} 

# History configurations
export HISTFILE=~/.local/share/history
HISTSIZE=1000
SAVEHIST=2000
alias history="history 0"     # force zsh to show the complete history

export GPG_TTY=$(tty)

if [ ! -z "$(grep nixos /etc/os-release)" ]; then
    # Shell niceties
    source /run/current-system/sw/share/zsh-autosuggestions/zsh-autosuggestions.zsh
    source /run/current-system/sw/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

    # Use zsh as default nix build shell
    source /run/current-system/sw/share/zsh-nix-shell/nix-shell.plugin.zsh
    nix-shell-name() {
        # If hopping into a pkg subshell, the shell name should reflect the
        # package linked. (i.e.: 'nix-shell -p nyancat' => 'export name=nyancat')
        if [ "$1" = "-p" ]; then
            nix-shell "$@" --command "export name='$2'"
        else
            nix-shell "$@"
        fi
    }
    alias nix-shell='nix-shell-name'
else
    source ~/.config/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.plugin.zsh
fi

source ~/.config/zsh/.zshhighlighting

if [ -f  ~/.config/zsh/.zshpersonalrc ]; then
    source ~/.config/zsh/.zshpersonalrc
fi

eval "$(starship init zsh)"

