# ________              _____
# ___  __ )_____ __________(_)_____________
# __  __  |  __ `/_  ___/_  /_  ___/_  ___/
# _  /_/ // /_/ /_(__  )_  / / /__ _(__  )
# /_____/ \__,_/ /____/ /_/  \___/ /____/

# Bundled modules
use unix
use re
use str
use math
use readline-binding

# Prompt
if ?(command -v starship >/dev/null) {
  eval (starship init elvish)
}

# Case-insensitive completion
edit:completion:matcher[''] = [p]{ edit:match-prefix &ignore-case $p }

# __________
# ___  ____/_________   __
# __  __/  __  __ \_ | / /
# _  /___  _  / / /_ |/ /
# /_____/  /_/ /_/_____/

# xdg stuff
E:XDG_CONFIG_HOME = "$E:HOME/.config"
E:XDG_CACHE_HOME = "$E:HOME/.cache"
E:XDG_DATA_HOME = "$E:HOME/.local/share"
E:XDG_DATA_DIRS = "$E:HOME/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share"

# alternative paths
E:LESSHISTFILE = -
E:GTK2_RC_FILES = "$E:XDG_CONFIG_HOME/gtk-2.0/gtkrc"
E:TERMINFO = "$E:XDG_DATA_HOME/terminfo"
E:TERMINFO_DIRS = "$E:XDG_DATA_HOME/terminfo:/usr/share/terminfo"
E:GOPATH = "$E:XDG_DATA_HOME/go"
E:GRADLE_USER_HOME = "$E:XDG_DATA_HOME/gradle"
E:_JAVA_OPTIONS = "-Djava.util.prefs.userRoot=$E:XDG_CONFIG_HOME/java"
E:NODE_REPL_HISTORY = "$E:XDG_CACHE_HOME/node_repl_history"
E:NPM_CONFIG_USERCONFIG = "$E:XDG_CONFIG_HOME/npm/npmrc"
E:NPM_CONFIG_PREFIX = "$E:XDG_DATA_HOME/npm-global"
E:CARGO_HOME = "$E:XDG_DATA_HOME/cargo"
E:RUSTUP_HOME = "$E:XDG_DATA_HOME/rustup"
E:BUNDLE_USER_CONFIG = "$E:XDG_CONFIG_HOME/bundle"
E:BUNDLE_USER_CACHE = "$E:XDG_CACHE_HOME/bundle"
E:BUNDLE_USER_PLUGIN = "$E:XDG_DATA_HOME/bundle"
E:GEM_HOME = "$E:XDG_DATA_HOME/gem"
E:GEM_SPEC_CACHE = "$E:XDG_CACHE_HOME/gem"
E:DOCKER_CONFIG = "$E:XDG_CONFIG_HOME/docker"
E:GNUPGHOME = "$E:XDG_DATA_HOME/gnupg"
E:IPYTHONDIR = "$E:XDG_CONFIG_HOME/ipython"
E:JUPYTER_CONFIG_DIR = "$E:XDG_CONFIG_HOME/jupyter"
E:XAUTHORITY = "$E:XDG_RUNTIME_DIR/Xauthority"
E:XINITRC = "$E:XDG_CONFIG_HOME/X11/xinitrc"
E:XSERVERRC = "$E:XDG_CONFIG_HOME/X11/xserverrc"

# env
E:EDITOR = nvim
E:VISUAL = nvim
E:PAGER = "less -R"
# E:SVDIR = "$E:HOME/.local/share/service"
E:_JAVA_AWT_WM_NONREPARENTING = 1
E:GPG_TTY = "$(tty)"
# fzf
E:FZF_DEFAULT_OPTS = "--multi --layout=reverse --inline-info
 --color fg:#D8DEE9,bg:#2E3440,hl:#A3BE8C,fg+:#D8DEE9,bg+:#434C5E,hl+:#A3BE8C
 --color pointer:#BF616A,info:#4C566A,spinner:#4C566A,header:#4C566A,prompt:#81A1C1,marker:#EBCB8B"
# E:FZF_DEFAULT_OPTS = "--multi --layout=reverse --inline-info
#  --color dark
#  --color fg:-1,bg:-1,hl:#c678dd,fg+:#ffffff,bg+:#4b5263,hl+:#d858fe
#  --color info:#98c379,prompt:#61afef,pointer:#be5046,marker:#e5c07b,spinner:#61afef,header:#61afef"
E:FZF_DEFAULT_COMMAND = "fd --type f --follow --hidden --exclude .git"
E:FZF_CTRL_T_OPTS = "--no-height --preview-window 'left:60%' --preview '$HOME/.local/bin/garbage/preview {} 2>/dev/null'"
E:FZF_CTRL_T_COMMAND = "fd --follow --hidden --exclude .git"
E:FZF_ALT_C_OPTS = "--preview 'exa -1a --color always --icons {} 2>/dev/null'"
E:FZF_ALT_C_COMMAND = "fd --type d --follow --hidden --exclude .git"
E:FZF_TMUX = 1
# ibus
E:GTK_IM_MODULE = ibus
E:XMODIFIERS = "@im=ibus"
E:QT_IM_MODULE = ibus
# qt5ct
E:QT_QPA_PLATFORMTHEME = qt5ct
E:QT_PLATFORM_PLUGIN = qt5ct
# .NET
E:DOTNET_CLI_TELEMETRY_OPTOUT = 1
# nnn
if ?(command -v nnn >/dev/null) {
  E:TERMINAL = alacritty
  E:NNN_OPTS = Hedx
  E:NNN_PLUG = 't:preview-tui;m:nmount;f:fzcd;c:chksum;n:nuke;z:fzz;b:bulknew;d:dups:h:hexview;o:organize;p:pdfview;s:suedit;r:renamer'
  E:NNN_FIFO = "/tmp/nnn.fifo"
  E:NNN_ARCHIVE = '\\.(7z|a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|rar|rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)$'
  # E:NNN_OPENER = "$E:XDG_CONFIG_HOME/nnn/plugins/nuke"
  E:USE_SCOPE = 1
  E:NNN_FCOLORS = 'c1e20402006033f7c6d6ab01'
  E:NNN_COLORS = '2345'
  E:NNN_BMS = "h:~;C:~/Code;D:~/Downloads;P:~/Pictures;V:~/Videos;A:~/Media;U:~/Music;f:~/.config;l:~/.local/share;e:/etc;u:/usr/share;o:/opt;b:/boot;m:/media;M:/mnt;i:/run/media/$USER;v:/var;t:/tmp;d:/dev;s:/srv;r:/;k:/bedrock/strata"
  E:NNN_SSHFS = "sshfs -o reconnect,idmap=user,follow_symlinks"
}

# Set path
use path
paths = [
	$E:HOME/.local/bin
	$E:HOME/.local/bin/fzf
	$E:CARGO_HOME/bin
	$E:GOPATH/bin
	$E:NPM_CONFIG_PREFIX/bin
	/bin
	/usr/bin
	/usr/local/bin
]
each [p]{
  if (not (path:is-dir (or (_ = ?(path:eval-symlinks $p)) $p))) {
    echo (styled "Warning: directory "$p" in PATH does not exist." yellow)
  }
} $paths

# ______  ___     _________      ______
# ___   |/  /___________  /___  ____  /____________
# __  /|_/ /_  __ \  __  /_  / / /_  /_  _ \_  ___/
# _  /  / / / /_/ / /_/ / / /_/ /_  / /  __/(__  )
# /_/  /_/  \____/\__,_/  \__,_/ /_/  \___//____/

#use epm

# _________________
# ___    |__  /__(_)_____ ____________________
# __  /| |_  /__  /_  __ `/_  ___/  _ \_  ___/
# _  ___ |  / _  / / /_/ /_(__  )/  __/(__  )
# /_/  |_/_/  /_/  \__,_/ /____/ \___//____/

# man page with colors
fn man [@a]{
  E:LESS_TERMCAP_mb = "\e[01;31m"
  E:LESS_TERMCAP_md = "\e[01;36m"
  E:LESS_TERMCAP_me = "\e[0m"
  E:LESS_TERMCAP_se = "\e[0m"
  E:LESS_TERMCAP_so = "\e[01;44;33m"
  E:LESS_TERMCAP_ue = "\e[0m"
  E:LESS_TERMCAP_us = "\e[01;32m"
  e:man $@a
}

# ____________              _____
# __  ___/_  /______ _________  /____  _________
# _____ \_  __/  __ `/_  ___/  __/  / / /__  __ \
# ____/ // /_ / /_/ /_  /   / /_ / /_/ /__  /_/ /
# /____/ \__/ \__,_/ /_/    \__/ \__,_/ _  .___/
#                                       /_/

$E:HOME/.local/bin/bunny

# gpg-agent
if ?(not pgrep -u "$E:USER" gpg-agent >/dev/null) {
  gpg-daemon --daemon --enable-ssh-support >/dev/null
}
if ?(test -z "$E:SSH_AUTH_SOCK") {
  E:SSH_AUTH_SOCK = "$(gpgconf --list-dirs agent-ssh-socket)"
}
gpg-connect-agent updatestartuptty /bye >/dev/null
