dotfiles/.config/zsh/.zprofile
tastytea c8dce951d6
Zsh: Make / not be part of words
makes backward-word and so on stop at /.
2022-03-23 14:34:53 +01:00

191 lines
6.5 KiB
Bash

function() # Keep local variables in here.
{
############################## Paths ###########################################
export PATH="${HOME}/bin:${HOME}/.local/bin:${PATH}"
export ZDOTDIR="${XDG_CONFIG_HOME:-${HOME}/.config}/zsh"
export HISTFILE="${ZDOTDIR}/.zsh_history"
export ZSH_CACHE_DIR="${XDG_CACHE_HOME:-${HOME}/.cache}/zsh"
mkdir -p "${ZSH_CACHE_DIR}"
if [[ -d "${HOME}/.local/share/man/" ]]; then
export MANPATH="${MANPATH}:${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 [[ $(id -u) -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 ##########################################
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} ]] \
&& [[ $(stat --format="%s" ${ZSH_PLUGIN_SOURCE}) -ne 0 ]]; then
local mod_month="$(stat --format="%y" ${ZSH_PLUGIN_SOURCE} 2> /dev/null | cut -d\- -f2)"
# Update plugins every month.
if [[ ${mod_month} -ne $(date +'%m') ]]; 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}
# robbyrussell/oh-my-zsh path:lib
# robbyrussell/oh-my-zsh path:plugins/history-substring-search
${depends_on_fzf}
zsh-users/zsh-autosuggestions
zsh-users/zsh-history-substring-search
mafredri/zsh-async
zsh-users/zsh-syntax-highlighting
EOF
fi
else
echo "antibody not found." >&2
fi
############################# Variables ########################################
# Stop at / 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
# Host specific settings.
case $(hostname) 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
[[ -d "${ZDOTDIR}"/functions ]] && fpath+="${ZDOTDIR}"/functions && export fpath
############################# 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 $(id -u) ssh-agent)
&& $(pgrep --uid $(id -u) 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 $(id -u) ssh-agent) ]]; then
eval $(ssh-agent -t 16H 2> /dev/null)
fi
if command -v gpg-agent > /dev/null \
&& [[ ! $(pgrep --uid $(id -u) gpg-agent) ]]; then
eval $(gpg-agent --daemon --quiet --default-cache-ttl $((60*16)) \
--max-cache-ttl $((60*16)))
fi
fi
return 0
}