diff --git a/config/zsh/.zprofile b/config/zsh/.zprofile index d9f0c0e..df2d615 100644 --- a/config/zsh/.zprofile +++ b/config/zsh/.zprofile @@ -1,4 +1,26 @@ -# General environment settings. +have() { + command -v "$1" >/dev/null 2>&1 +} + +# Prepend $2 to variable $1 using $3 as separator, but only if $2 is not already +# contained in $1. This is useful for prepending to PATH while avoiding +# duplicates. +prepend_unique() { + local var="$1" new="$2" sep="${3-:}" # Default separator is colon. + local old="${(P)var}" + + case "${sep}${old}${sep}" in + *"${sep}${new}${sep}"*) # Skip duplicate. + ;; + "${sep}${sep}") # Variable was empty, set without separator. + typeset -g $var="${new}" + ;; + *) # Prepend with separator. + typeset -g $var="${new}${sep}${old}" + ;; + esac +} + setup_environment() { export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}" export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}" @@ -14,91 +36,72 @@ setup_environment() { export LESS="-i -j.49 -M -R -z-2" export LESSHISTFILE="$XDG_DATA_HOME/less/history" export LESSHISTSIZE=1000 - export LOCAL_PREFIX="/usr/local" export PAGER=less export PYTHONSTARTUP="$XDG_CONFIG_HOME/python/startup.py" } -have() { - command -v "$1" >/dev/null 2>&1 -} - -# Prevent path_helper from messing with the PATH when starting tmux. -# -# Clearing PATH before path_helper executes (from /etc/profile) will prevent it -# from prepending the default PATH to our (previously) chosen PATH, and will -# allow the rest of this file to set up PATH and MANPATH correctly. -# -# For details see: https://superuser.com/a/583502 -# -setup_macos_path_helper() { +setup_path_and_manpath() { + # Prevent path_helper from messing with the PATH when starting tmux. + # + # Clearing PATH before path_helper executes (from /etc/profile) will prevent it + # from prepending the default PATH to our (previously) chosen PATH, and will + # allow the rest of this file to set up PATH and MANPATH correctly. + # + # For details see: https://superuser.com/a/583502 + # [ "$(uname -s)" = "Darwin" ] && { PATH=""; source /etc/profile; } + + local prefix="/usr/local" + have brew && prefix="$(brew --prefix)" + + local custom_paths=( + "$prefix/opt/curl/bin" + "$prefix/bin" + "$HOME/.local/bin" + "$HOME/.bin" + ) + + local bindir="" + for bindir in $custom_paths; do + [ -d "$bindir" ] && prepend_unique PATH "$bindir" + done + export PATH + + # Now that PATH is set, we should pick up all relevant man page locations, as + # for all directories ending in `/bin` found in PATH, `manpath` will + # print the first existing directory of: + # - `/man` + # - `/../share/man` + # - `/../man` + have manpath && { MANPATH="$(unset MANPATH; manpath)"; export MANPATH; } } -# Prepend $2 to variable $1 using $3 as separator, but only if $2 is not already -# contained in $1. This is useful for prepending to PATH while avoiding -# duplicates. -prepend_unique() { - local var="$1" - local new="$2" - local sep="${3-:}" # Default separator is colon. - local old="${(P)var}" +# Some packages are installed by Homebrew in non-standard locations, in order to +# prevent conflicts with their system versions. +setup_brew_keg_only_packages() { + have brew || return - case "${sep}${old}${sep}" in - *"${sep}${new}${sep}"*) # Skip duplicate. - ;; - "${sep}${sep}") # Variable was empty, set without separator. - typeset -g $var="${new}" - ;; - *) # Prepend with separator. - typeset -g $var="${new}${sep}${old}" - ;; - esac + local packages=( + make + grep + gnu-tar + gnu-sed + findutils + coreutils + ) + + local prefix="$(brew --prefix)" + local pkg="" bindir="" mandir="" + + for pkg in $packages; do + bindir="$prefix/opt/$pkg/libexec/gnubin" + [ -d "$bindir" ] && prepend_unique PATH "$bindir" + + mandir="$prefix/opt/$pkg/libexec/gnuman" + [ -d "$mandir" ] && prepend_unique MANPATH "$mandir" + done } -# Add custom bin dirs to PATH if they exist and are not already in PATH. -setup_path() { - local dir="" - while read -r dir; do - [ -d "$dir" ] && prepend_unique PATH "${dir}" - done <