# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.config/zsh/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# History
HISTFILE=$ZDOTDIR/zsh_history
HISTSIZE=1001
SAVEHIST=1000
setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_FIND_NO_DUPS
setopt HIST_SAVE_NO_DUPS

# Various
export LANG=en_GB.UTF-8
setopt autocd
unsetopt beep
bindkey -v
zstyle :compinstall filename "$HOME/.zshrc"

# Other general user config
autoload -U colors && colors
autoload -U promptinit && promptinit

########## Reducing clutter in home directory ##########
alias feh="feh --no-fehbg"
alias newsboat="prime-run newsboat -u ~/.config/newsboat/urls"
alias mpv="prime-run mpv"
alias gpg2="gpg2 --homedir $XDG_DATA_HOME/gnupg"
alias wget='wget --hsts-file="$XDG_CACHE_HOME/wget-hsts"'
alias svn="svn --config-dir \"$XDG_CONFIG_HOME\"/subversion"

########## Configuration #############
if [[ ! -d ~/.config/zsh/zsh-autopair ]]; then
  git clone https://github.com/hlissner/zsh-autopair ~/.config/zsh/zsh-autopair
fi

source ~/.config/zsh/zsh-autopair/autopair.zsh
autopair-init

########## Aliases / Functions ##########
# Allows declaring abbreviations:
# declare a list of expandable aliases to fill up later
typeset -a ealiases
ealiases=()

# write a function for adding an alias to the list mentioned above
function abbrev-alias() {
    alias $1
    ealiases+=(${1%%\=*})
}

# expand any aliases in the current line buffer
function expand-ealias() {
    if [[ $LBUFFER =~ "\<(${(j:|:)ealiases})\$" ]]; then
        zle _expand_alias
        zle expand-word
    fi
    zle magic-space
}
zle -N expand-ealias

# Bind the space key to the expand-alias function above, so that space will expand any expandable aliases
bindkey ' '        expand-ealias
bindkey '^ '       magic-space     # control-space to bypass completion
bindkey -M isearch " "      magic-space     # normal space during searches

# A function for expanding any aliases before accepting the line as is and executing the entered command
expand-alias-and-accept-line() {
    expand-ealias
    zle .backward-delete-char
    zle .accept-line
}
zle -N accept-line expand-alias-and-accept-line
#
# allow sudo to make use of aliases
alias sudo="sudo "

# various temporary
pvd() {
    mpv *$1*
}

# ls / exa 
alias ls='ls -a --color=always'
alias exa="exa -a --header --long --git --time-style=long-iso --group"
abbrev-alias e="exa"
abbrev-alias xc="clear; exa"
abbrev-alias c="clear"
abbrev-alias t="exa --tree"

# vim / nvim
abbrev-alias v="vim"
abbrev-alias n="nvim"

# music: mpd, ncmpcpp, mpc
abbrev-alias m="ncmpcpp"

abbrev-alias mps="mpc pause"
abbrev-alias mpl="mpc play"
abbrev-alias mst="mpc stop"
abbrev-alias mtg="mpc toggle"
abbrev-alias mnx="mpc next"
abbrev-alias mpr="mpc prev"

# backgrounds, wal...
alias rbg="feh --no-fehbg --bg-fill $(shuf -n1 -e ~/bgs/*)"

# cargo 
abbrev-alias cb="cargo build"
abbrev-alias cbr="cargo build --release"

# todos...
abbrev-alias td="mn td/todos"
abbrev-alias tdcmp="mn td/todos-cmp"
abbrev-alias tdntd="mn td/todos-ntd"

abbrev-alias cld="mn cld/calendar" 
abbrev-alias cldcmp="mn cld/calendar-cmp"
abbrev-alias cldrec="mn cld/calendar-recurring"

# other 
abbrev-alias r="newsboat"

# documents
abbrev-alias p="zathura" 
abbrev-alias o="libreoffice --writer" 
abbrev-alias b="bat"

# file manipulation
defix() {
    for i in  "$1"*;do mv "$i" "${i#"$1"}";done
}

# pulseaudio - this is a temporary solution
sv() {
    pactl set-sink-volume 0 $1%
}

# git
# TODO: add command to push to all remote repos, e.g. github, origin and gitlab
abbrev-alias gb="git branch"
abbrev-alias gco="git checkout" 
abbrev-alias gcom="git checkout master"
abbrev-alias gbd="git branch -d"
abbrev-alias gp="git push"
abbrev-alias gpa="git push origin && git push github && git push gitlab"
abbrev-alias gpl="git pull" 
abbrev-alias gs="git status"
abbrev-alias ga="git add"
abbrev-alias gc="git commit"
abbrev-alias gcm="git commit -m"
abbrev-alias gf="git fetch"
abbrev-alias gcl="git clone"
git-add-remotes() { # Add remotes
    git remote add origin gitea@git.gabbott.dev:george/$1.git
    git remote add github git@github.com:GeorgeAbbott/$1.git
    git remote add gitlab git@gitlab.com:GeorgeAbbott/$1.git
}
git-rename-remotes() { # Rename all remotes 
    git remote set-url origin gitea@git.gabbott.dev:george/$1.git
    git remote set-url github git@github.com:GeorgeAbbott/$1.git
    git remote set-url gitlab git@gitlab.com:GeorgeAbbott/$1.git
}
git-print-remotes() { # Print all remotes
    git remote get-url origin 
    git remote get-url github 
    git remote get-url gitlab 
}
git-clone-add-remotes() { # Git clone from origin url and then add in the rest of remotes
    git clone gitea@git.gabbott.dev:george/$1.git
    cd ./$1
    gar $1
    cd ..
}
git-update-all() { # Update all - recursively iterates through dir and runs git pull 
    # TODO implement
}
abbrev-alias gar="git-add-remotes"
abbrev-alias grr="git-rename-remotes"
abbrev-alias gpr="git-print-remotes"
abbrev-alias gcr="git-clone-add-remotes"



# misc
alias pkg-query="pacman -Qq | fzf --preview 'pacman -Qil {}' --layout=reverse --bind 'enter:execute(pacman -Qil {} | less)'"

lle() { # Life Log Entry: L~
    if [ -z $1 ] ; then DT="today" else DT="$1" fi
    mkdir -p "$HOME/docs/wr/lle/$(date +%Y-%m -d $DT)"
    nvim "$HOME/docs/wr/lle/$(date +%Y-%m -d $DT)/L$(date --iso-8601 -d $DT).txt"
}

holc () { # Holiday Entry: H~
    if [ -z $1 ] ; then DT="today" else DT="$1" fi
    mkdir -p "$HOME/docs/wr/lle/$(date +%Y-%m -d $DT)"
    nvim "$HOME/docs/wr/lle/$(date +%Y-%m -d $DT)/H$(date --iso-8601 -d $DT).txt"
}

mer() { # Month End Recap: M~
    if [ -z $1 ] ; then DT="yesterday" else DT="$1" fi
    mkdir -p "$HOME/docs/wr/lle/$(date +%Y-%m -d $DT)"
    nvim "$HOME/docs/wr/lle/$(date +%Y-%m -d $DT)/M$(date +%Y-%m -d $DT).txt" 
}

yer() { # Year End Recap: Y~
    if [ -z $1 ] ; then DT="yesterday" else DT="$1" fi
    mkdir -p "$HOME/docs/wr/lle/$(date +%Y -d $DT)-12"
    nvim "$HOME/docs/wr/lle/$(date +%Y -d $DT)-12/Y$(date +%Y -d $DT).txt"
}

mkdream() { # Dream Record: D~
    if [ -z $1 ] ; then DT="yesterday" else DT="$1" fi 
    mkdir -p "$HOME/docs/wr/dream"
    nvim "$HOME/docs/wr/dream/D$(date --iso-8601 -d $DT).txt"
}

mn() { # make note
    nvim "$HOME/docs/wr/mn/$1"
}


autoload -Uz compinit
compinit
# End of lines added by compinstall

# z - must go after compinit
eval "$(zoxide init zsh)"


# Sourcing for zsh
source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
source /usr/share/zsh-theme-powerlevel10k/powerlevel10k.zsh-theme

# To customize prompt, run `p10k configure` or edit ~/.config/zsh/.p10k.zsh.
[[ ! -f ~/.config/zsh/.p10k.zsh ]] || source ~/.config/zsh/.p10k.zsh