dotfiles/.config/zsh/.zprofile

229 lines
8.2 KiB
Bash

function() # Keep local variables in here.
{
############################## Paths ###########################################
export PATH="${HOME}/bin:${HOME}/.local/bin:${PATH}"
export HISTFILE="${ZDOTDIR}/.zsh_history"
export ZSH_CACHE_DIR="${XDG_CACHE_HOME:-${HOME}/.cache}/zsh"
mkdir -p "${ZSH_CACHE_DIR}"
if [[ -d "${XDG_DATA_HOME:-${HOME}/.local/share}/man/" ]]; then
export MANPATH="${MANPATH}:${XDG_DATA_HOME:-${HOME}/.local/share}/man"
fi
############################ Editor ############################################
# Set preferred editor.
# Add Defaults env_keep += "SSH_CONNECTION" to /etc/sudoers.d/ssh_vars
if command -v emacsclient > /dev/null; then
export EMACS_SOCKET_NAME="${HOME}/.emacs.d/server/server"
if pgrep -f "emacs --daemon" > /dev/null; then
VISUAL="$(command -v emacsclient) -c"
else
VISUAL="$(command -v emacs)"
fi
if [[ ${EUID} -eq 0 ]]; then
# Edit files as root in the Emacs instance run by the current user.
VISUAL="$(command -v emacsremote) --sudo --local"
fi
elif command -v emacs > /dev/null; then
VISUAL="$(command -v emacs)"
elif command -v nano > /dev/null; then
VISUAL="$(command -v nano)"
elif command -v vim > /dev/null; then
VISUAL="$(command -v vim)"
fi
export VISUAL
export EDITOR="${VISUAL}"
export ALTERNATE_EDITOR="$(command -v nano)"
############################# Plugins ##########################################
zmodload zsh/stat
zmodload zsh/datetime
if command -v fzf > /dev/null; then
# Only load these plugins if fzf was found.
local _depends_on_fzf="Aloxaf/fzf-tab"
fi
if command -v antibody > /dev/null; then
local plugin_cache_dir="$(antibody home)"
export ZSH_PLUGIN_SOURCE="${plugin_cache_dir}/antibody-plugins.zsh"
if [[ -f "${ZSH_PLUGIN_SOURCE}" ]] \
&& [[ $(zstat +size "${ZSH_PLUGIN_SOURCE}") -ne 0 ]]; then
local _plugins_modified=$(zstat +mtime "${ZSH_PLUGIN_SOURCE}")
local _now=$(strftime '%s')
# Update plugins every 10 days
if [[ $(( ${_now} - ${_plugins_modified} - 60 * 60 * 24 * 10)) -gt 0 ]]; then
print "\e[3;95mUpdating plugins…\e[0m"
antibody update --parallelism=$(nproc --ignore=1) \
&& touch "${ZSH_PLUGIN_SOURCE}"
fi
else
print "\e[3;95mInstalling plugins…\e[0m"
mkdir -p "${plugin_cache_dir}"
antibody bundle --parallelism=$(nproc --ignore=1) <<EOF > "${ZSH_PLUGIN_SOURCE}"
${_depends_on_fzf}
zsh-users/zsh-autosuggestions
mafredri/zsh-async
zsh-users/zsh-syntax-highlighting
# Must come after zsh-syntax-highlighting
zsh-users/zsh-history-substring-search
EOF
fi
else
print -u 2 "\e[7;95mantibody not found.\e[0m"
fi
############################# Variables ########################################
# Stop at chars not in there with backward-word and so on
export WORDCHARS="${WORDCHARS//(\/|_|.)}"
# minicom color on.
export MINICOM="-c on -R utf-8"
export PASSWORD_STORE_GENERATED_LENGTH="43"
export LESS="--tabs=4 --RAW-CONTROL-CHARS --LONG-PROMPT"
if command -v highlight > /dev/null; then
# ~/.lessfilter handles the highlighting
LESSOPEN="| $(command -v lesspipe) %s"
export CATOPEN="highlight --validate-input --force --out-format=truecolor --style=base16/unikitty-reversible --stdout %s"
elif command -v pygmentize > /dev/null; then
LESSOPEN="| $(command -v pygmentize) -f terminal16m -g -O style=paraiso-dark %s"
elif command -v source-highlight > /dev/null; then
LESSOPEN="| $(command -v source-highlight) --failsafe --infer-lang --out-format=esc --style-file=esc.style --input=%s"
elif command -v lesspipe > /dev/null; then
LESSOPEN="| $(command -v lesspipe) %s"
elif command -v lesspipe.sh > /dev/null; then
LESSOPEN="| $(command -v lesspipe.sh) %s"
fi
export LESSOPEN
# Show AS for each hop.
export MTR_OPTIONS="-z"
# Set SDL soundfont.
if [[ -f "/usr/share/sounds/sf2/FluidR3_GM.sf2" ]]; then
export SDL_SOUNDFONTS="/usr/share/sounds/sf2/FluidR3_GM.sf2"
fi
# Make virsh use the local hypervisor by default.
if command -v virsh > /dev/null; then
export LIBVIRT_DEFAULT_URI="qemu:///system"
fi
# Use ISO 8601 date & time.
if command -v locale > /dev/null; then
locale -a | grep -q en_DK && export LC_TIME="en_DK.utf8"
fi
export CCACHE_DIR="/var/cache/ccache"
# Set terminal
if command -v alacritty > /dev/null; then
export TERMINAL="alacritty"
elif command -v xfce4-terminal > /dev/null; then
export TERMINAL="xfce4-terminal"
elif command -v urxvt > /dev/null; then
export TERMINAL="urxvt"
elif command -v xterm > /dev/null; then
export TERMINAL="xterm"
fi
# Add some nice colors to ls output
function _gen_lscolor()
{
local color="${1}"
shift
local -a extensions=("${@}")
for ext in ${extensions}; do
print -n ":*.${ext}=${color}"
done
}
unset LS_COLORS
# Program code (rose)
LS_COLORS="${LS_COLORS}$(_gen_lscolor "38;5;177" cpp cxx c++ c go rs)"
LS_COLORS="${LS_COLORS}$(_gen_lscolor "38;5;177" py rb el ebuild zsh sh bash pl php phtml qml)"
# Stuff that gets included (violet + italic)
LS_COLORS="${LS_COLORS}$(_gen_lscolor "3;38;5;141" hpp hxx h++ h qss qrc)"
LS_COLORS="${LS_COLORS}$(_gen_lscolor "3;38;5;141" css sass scss tmpl csp inc)"
# Build systems and adjacent (green)
LS_COLORS="${LS_COLORS}$(_gen_lscolor "38;5;112" patch diff in cmake build mk ac m4 am)"
# Images, audio, video, playlists (yellow, orange, green)
LS_COLORS="${LS_COLORS}$(_gen_lscolor "38;5;184" webp png jpg jpeg gif svg svgz xcf bmp ico icns tiff tif xpm xbm tga ppm)"
LS_COLORS="${LS_COLORS}$(_gen_lscolor "38;5;172" flac opus ogg oga mp3 m4a wav mid midi wma ac3 au aac spx)"
LS_COLORS="${LS_COLORS}$(_gen_lscolor "38;5;172" mkv webm ogv ogm mp4 m4v avi mpeg mpg wmv flv mov 3gp asf)"
LS_COLORS="${LS_COLORS}$(_gen_lscolor "38;5;112" m3u m3u8 pls xspf)"
# Text (cyan)
LS_COLORS="${LS_COLORS}$(_gen_lscolor "38;5;117" txt md adoc org rst html xhtml shtml htm)"
# Config files (ochre)
LS_COLORS="${LS_COLORS}$(_gen_lscolor "38;5;100" cfg json ini conf yml yaml xml toml)"
# Compressed files and archives (red)
LS_COLORS="${LS_COLORS}$(_gen_lscolor "38;5;124" tar gz tgz zst bz2 xz lzma ar zip rar 7z Z arc lha cpio zoo)"
# Special cases
LS_COLORS="${LS_COLORS}:*Makefile=38;5;112:*CMakeLists.txt=38;5;112:*meson_options.txt=38;5;112"
LS_COLORS="${LS_COLORS}:*go.mod=38;5;112:*go.sum=38;5;112"
export LS_COLORS
unfunction _gen_lscolor
# Host specific settings.
case ${HOST} in
schnibble | gaffer | localhost)
export GIT_SSH_COMMAND="ssha" # Termux command for ssh with key-add.
export CPATH="/data/data/com.termux/files/usr/local/include"
export LIBRARY_PATH="/data/data/com.termux/files/usr/local/lib"
;;
esac
[[ -f "${ZDOTDIR}"/zsecrets ]] && source "${ZDOTDIR}"/zsecrets
############################# Daemons ##########################################
# Run SSH/GPG agents if they are installed and not already started. If they are
# started, attempt to inherit them.
if command -v keychain > /dev/null; then
if [[ $(pgrep --uid ${EUID} ssh-agent)
&& $(pgrep --uid ${EUID} gpg-agent) ]]; then
# Set environment variables from files.
if [[ -z "${SSH_AUTH_SOCK}" ]]; then
source "${HOME}/.keychain/$(hostname)-sh"
fi
if [[ -z "${GPG_AGENT_INFO}" ]]; then
source "${HOME}/.keychain/$(hostname)-sh-gpg"
fi
else
# Start SSH/GPG agents and write environment variables to files.
eval $(keychain --agents "ssh,gpg" \
--timeout $((60*16)) \
--inherit local \
--quiet --eval)
# <https://github.com/funtoo/keychain/issues/126>
eval $(keychain --agents "ssh,gpg" \
--timeout $((60*16)) \
--inherit local \
--quiet --eval)
fi
else
# keychain not installed, start SSH/GPG agents if they are not running.
if command -v ssh-agent > /dev/null \
&& [[ ! $(pgrep --uid ${EUID} ssh-agent) ]]; then
eval $(ssh-agent -t 16H 2> /dev/null)
fi
if command -v gpg-agent > /dev/null \
&& [[ ! $(pgrep --uid ${EUID} gpg-agent) ]]; then
eval $(gpg-agent --daemon --quiet --default-cache-ttl $((60*16)) \
--max-cache-ttl $((60*16)))
fi
fi
return 0
}