# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.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

# zcomet install
if [[ ! -f ${ZDOTDIR:-${HOME}}/.zcomet/bin/zcomet.zsh ]]; then
  command git clone https://github.com/agkozak/zcomet.git ${ZDOTDIR:-${HOME}}/.zcomet/bin
fi

# zcomit + shell line activation
source ${ZDOTDIR:-${HOME}}/.zcomet/bin/zcomet.zsh
zcomet load romkatv/powerlevel10k 

# misc plugins
zcomet load zsh-users/zsh-syntax-highlighting
zcomet load ytakahashi/igit
#if [[ -n "${TMUX}" ]]; then
#    zcomet load mbenford/zsh-tmux-auto-title
#fi

# fzf
zcomet load junegunn/fzf shell completion.zsh key-bindings.zsh
(( ${+commands[fzf]} )) || ~[fzf]/install --bin
zcomet load Aloxaf/fzf-tab

# history
zcomet load larkery/zsh-histdb
zcomet load m42e/zsh-histdb-skim

# Run compinit and compile its cache
zcomet compinit

# zsh options
autoload run-help
setopt auto_pushd
setopt clobber
setopt complete_aliases
setopt correct
#setopt correct_all
setopt extended_glob
setopt extended_history
setopt hash_list_all
setopt hist_ignore_all_dups
setopt hist_ignore_space
setopt no_beep
setopt no_hist_allow_clobber
setopt no_rm_star_silent
setopt notify
setopt prompt_subst
setopt pushd_ignore_dups
setopt pushd_silent
setopt pushd_to_home
setopt rm_star_wait

export SAVEHIST=1000000
export HISTSIZE=1000000
export HISTFILE=~/.zsh_history
setopt append_history
setopt share_history

# zsh plugin options
if [[ -n "${TMUX}" ]]; then
    ZSH_TMUX_AUTO_TITLE_SHORT=true
    ZSH_TMUX_AUTO_TITLE_IDLE_TEXT="%last"
fi
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'exa -1 --color=always $realpath'

# misc / general
export EDITOR="nano"
export PAGER="most"
export PATH=~/.local/bin:${PATH}

# asdf
. $HOME/.asdf/asdf.sh

# global aliases (before interactive, just in case)
alias ls="exa"

# termux aliases
if grep -q "Android" <<< `uname -a`; then
    alias proot-distro-keeb-root="proot-distro login keeb"
    alias proot-distro-keeb="proot-distro login --user keeb --termux-home keeb -- /bin/bash"
    alias emacs="echo 'daemon mode -- use em [profile] instead'"
    alias em="/data/data/com.termux/files/home/.shortcuts/emacs "
fi

# dont allow mosh prediction (override in zshrc.local)
export MOSH_PREDICTION_DISPLAY="never"

# local overrides
test -f ~/.zshrc.local && source ~/.zshrc.local

# dont do anything more if non interactive
if [[ ! -o interactive ]]; then
    return
fi

# setup mode/status line for zsh
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh

# ensure histdb gets ctrl-r for history and skim widget can still be called
source ~/.zcomet/repos/larkery/zsh-histdb/histdb-interactive.zsh
alias hist="histdb-skim-widget"

# Fix home/end/del silly
if [[ "$TERM" != emacs ]]; then
[[ -z "$terminfo[kdch1]" ]] || bindkey -M emacs "$terminfo[kdch1]" delete-char
[[ -z "$terminfo[khome]" ]] || bindkey -M emacs "$terminfo[khome]" beginning-of-line
[[ -z "$terminfo[kend]" ]] || bindkey -M emacs "$terminfo[kend]" end-of-line
[[ -z "$terminfo[kich1]" ]] || bindkey -M emacs "$terminfo[kich1]" overwrite-mode
[[ -z "$terminfo[kdch1]" ]] || bindkey -M vicmd "$terminfo[kdch1]" vi-delete-char
[[ -z "$terminfo[khome]" ]] || bindkey -M vicmd "$terminfo[khome]" vi-beginning-of-line
[[ -z "$terminfo[kend]" ]] || bindkey -M vicmd "$terminfo[kend]" vi-end-of-line
[[ -z "$terminfo[kich1]" ]] || bindkey -M vicmd "$terminfo[kich1]" overwrite-mode

[[ -z "$terminfo[cuu1]" ]] || bindkey -M viins "$terminfo[cuu1]" vi-up-line-or-history
[[ -z "$terminfo[cuf1]" ]] || bindkey -M viins "$terminfo[cuf1]" vi-forward-char
[[ -z "$terminfo[kcuu1]" ]] || bindkey -M viins "$terminfo[kcuu1]" vi-up-line-or-history
[[ -z "$terminfo[kcud1]" ]] || bindkey -M viins "$terminfo[kcud1]" vi-down-line-or-history
[[ -z "$terminfo[kcuf1]" ]] || bindkey -M viins "$terminfo[kcuf1]" vi-forward-char
[[ -z "$terminfo[kcub1]" ]] || bindkey -M viins "$terminfo[kcub1]" vi-backward-char

# ncurses fogyatekos
[[ "$terminfo[kcuu1]" == "^[O"* ]] && bindkey -M viins "${terminfo[kcuu1]/O/[}" vi-up-line-or-history
[[ "$terminfo[kcud1]" == "^[O"* ]] && bindkey -M viins "${terminfo[kcud1]/O/[}" vi-down-line-or-history
[[ "$terminfo[kcuf1]" == "^[O"* ]] && bindkey -M viins "${terminfo[kcuf1]/O/[}" vi-forward-char
[[ "$terminfo[kcub1]" == "^[O"* ]] && bindkey -M viins "${terminfo[kcub1]/O/[}" vi-backward-char
[[ "$terminfo[khome]" == "^[O"* ]] && bindkey -M viins "${terminfo[khome]/O/[}" beginning-of-line
[[ "$terminfo[kend]" == "^[O"* ]] && bindkey -M viins "${terminfo[kend]/O/[}" end-of-line
[[ "$terminfo[khome]" == "^[O"* ]] && bindkey -M emacs "${terminfo[khome]/O/[}" beginning-of-line
[[ "$terminfo[kend]" == "^[O"* ]] && bindkey -M emacs "${terminfo[kend]/O/[}" end-of-line
fi
