From 8812ef854493e55eac07f38359aa60e8f150aa95 Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Tue, 10 Sep 2024 07:31:17 -0700 Subject: [PATCH] Use XDG base dir vars instead of defaults --- .gitignore | 1 + autoenv/autoenv.zsh | 18 ++++++++++++------ prompt_fresh_setup | 13 ++++++++----- zshenv | 42 +++++++++++++++++++++++++++--------------- zshrc | 21 +++++++++++++-------- 5 files changed, 61 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 1248c5c..e986154 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ # Ignore all plugin files in subdirectories zsh-*/ local +zsh*.local diff --git a/autoenv/autoenv.zsh b/autoenv/autoenv.zsh index 1a0c302..e8b2992 100644 --- a/autoenv/autoenv.zsh +++ b/autoenv/autoenv.zsh @@ -137,16 +137,22 @@ zmodload -F zsh/stat b:zstat # Check if the given file is authorized, if not prompt the user to authorize, # ignore, or view the file. Authorized files and their modified times are -# stored in the ~/.cache/autoenv/authorized file to make authorization +# stored in the $XDG_STATE_HOME/autoenv/authorized file to make authorization # persistent. _autoenv_authorized() { local file=$1 yes=$2 - # If autoenv cache directory does not exist, create it. - ! [ -d ~/.cache/autoenv ] && mkdir -p ~/.cache/autoenv + # If autoenv state directory does not exist, create it. + ! [ -d ${XDG_STATE_HOME:-$HOME/.local/state}/autoenv ] && \ + mkdir -p ${XDG_STATE_HOME:-$HOME/.local/state}/autoenv + # Migrate from cache to state directory + [ -f $HOME/.cache/autoenv/authorized ] && \ + mv $HOME/.cache/autoenv/authorized \ + ${XDG_STATE_HOME:-$HOME/.local/state}/autoenv/authorized # If the authorized file does not exist, create it. - ! [ -f ~/.cache/autoenv/authorized ] && touch ~/.cache/autoenv/authorized + ! [ -f ${XDG_STATE_HOME:-$HOME/.local/state}/autoenv/authorized ] && \ + touch ${XDG_STATE_HOME:-$HOME/.local/state}/autoenv/authorized # Load the authorized file into a map of authorized key value pairs. - typeset -A authorized=(`cat ~/.cache/autoenv/authorized`) + typeset -A authorized=(`cat ${XDG_STATE_HOME:-$HOME/.local/state}/autoenv/authorized`) # If the file has been removed, return. ! [ -f $file ] && return 1 # If the given file has been authorized, i.e. the modified time matches that @@ -168,7 +174,7 @@ _autoenv_authorized() { # Add file to the authorized map. authorized[$file]=$modified_time # Store authorized map in authorized file. - echo ${(kv)authorized} > ~/.cache/autoenv/authorized + echo ${(kv)authorized} > ${XDG_STATE_HOME:-$HOME/.local/state}/autoenv/authorized } # Source an enter script and add its directory to the global entered diff --git a/prompt_fresh_setup b/prompt_fresh_setup index dada470..6b6a119 100644 --- a/prompt_fresh_setup +++ b/prompt_fresh_setup @@ -56,7 +56,8 @@ prompt_fresh_setup() { } prompt_cleanup() { - [ -f ~/.cache/zsh/git-prompt ] && rm ~/.cache/zsh/git-prompt + [ -f ${XDG_CACHE_HOME:-$HOME/.cache}/zsh/git-prompt ] && \ + rm ${XDG_CACHE_HOME:-$HOME/.cache}/zsh/git-prompt } fresh_line_one() { @@ -76,7 +77,7 @@ fresh_line_one() { local directory="%{%F{37}%}%~%{%f%}" # Check we are in a git repository - local git=`~/.cache/zsh/git-prompt` + local git=`${XDG_CACHE_HOME:-$HOME/.cache}/zsh/git-prompt` # If the last command failed, display its error code at the right if [[ $exit_code -ne 0 ]]; then @@ -178,10 +179,12 @@ fresh_almostontop_preexec() { fresh_compile_git_prompt() { # Compile a simple C program which parses the output of `git status # --procelain` to greatly decrease the time taken to draw the prompt - [ ! -d ~/.cache/zsh ] && mkdir -p ~/.cache/zsh - if [ ! -f ~/.cache/zsh/git-prompt ]; then + [ ! -d ${XDG_CACHE_HOME:-$HOME/.cache}/zsh ] && \ + mkdir -p ${XDG_CACHE_HOME:-$HOME/.cache}/zsh + if [ ! -f ${XDG_CACHE_HOME:-$HOME/.cache}/zsh/git-prompt ]; then cc -std=gnu99 -O3 -DNDEBUG -Wno-unused-result \ - ~/.config/zsh/git-prompt.c -o ~/.cache/zsh/git-prompt + ${XDG_CONFIG_HOME:-$HOME/.config}/zsh/git-prompt.c \ + -o ${XDG_CACHE_HOME:-$HOME/.cache}/zsh/git-prompt if [ $? -ne 0 ]; then echo "git-prompt was not compiled, is a C99 toolchain installed?" fi diff --git a/zshenv b/zshenv index 9d9829b..7862607 100644 --- a/zshenv +++ b/zshenv @@ -2,12 +2,24 @@ # contain commands that produce output or assume the shell is attached to a # tty. This file will always be sourced. +[ -f ~/.config/zsh/zshenv.local ] && source ~/.config/zsh/zshenv.local + +# Ensure cache and state directories exist +[ ! -d -${XDG_CACHE_HOME:-$HOME/.cache}/zsh ] && \ + mkdir -p ${XDG_CACHE_HOME:-$HOME/.cache}/zsh +[ ! -d -${XDG_STATE_HOME:-$HOME/.local/state}/zsh ] && \ + mkdir -p ${XDG_STATE_HOME:-$HOME/.local/state}/zsh + # Enable saving command history to file -[ ! -d $HOME/.cache/zsh ] && mkdir -p $HOME/.cache/zsh -HISTFILE=$HOME/.cache/zsh/histfile +HISTFILE=${XDG_STATE_HOME:-$HOME/.local/state}/zsh/histfile HISTSIZE=20000 SAVEHIST=20000 +# Migrate histfile from cache to state directory +[ -f $HOME/.cache/zsh/histfile ] && \ + mv $HOME/.cache/zsh/histfile \ + ${XDG_STATE_HOME:-$HOME/.local/state}/zsh/histfile + # Remove vi mode switch delay KEYTIMEOUT=1 @@ -40,8 +52,8 @@ elif [ -f /usr/bin/ccache ]; then PATH=/usr/lib/ccache:$PATH fi fi -export CCACHE_CONFIGPATH=$HOME/.config/ccache -export CCACHE_DIR=$HOME/.cache/ccache +export CCACHE_CONFIGPATH=${XDG_CONFIG_HOME:-$HOME/.config}/ccache +export CCACHE_DIR=${XDG_CACHE_HOME:-$HOME/.cache}/ccache # Add default CMake generator command -v ninja &> /dev/null && \ @@ -103,27 +115,27 @@ command -v pinentry-curses &> /dev/null && \ export LPASS_PINENTRY=pinentry-curses # Teach these some XDG Base Directory Spec manners -export IPYTHONDIR=$HOME/.config/ipython +export IPYTHONDIR=${XDG_CONFIG_HOME:-$HOME/.config}/ipython command -v cargo &> /dev/null && \ export CARGO_HOME=$HOME/.local/share/cargo if command -v ccache &> /dev/null; then - export CCACHE_CONFIGPATH=$HOME/.config/ccache.conf - export CCACHE_DIR=$HOME/.cache/ccache + export CCACHE_CONFIGPATH=${XDG_CONFIG_HOME:-$HOME/.config}/ccache.conf + export CCACHE_DIR=${XDG_CACHE_HOME:-$HOME/.cache}/ccache fi command -v conan &> /dev/null && \ export CONAN_USER_HOME=$HOME/.local/share/conan command -v docker &> /dev/null && \ export DOCKER_CONFIG=$HOME/.local/share/docker -export GTK_RC_FILES=$HOME/.config/gtk/gtkrc -export GTK2_RC_FILES=$HOME/.config/gtk-2.0/gtkrc -export PYLINTHOME=$HOME/.cache/pylint +export GTK_RC_FILES=${XDG_CONFIG_HOME:-$HOME/.config}/gtk/gtkrc +export GTK2_RC_FILES=${XDG_CONFIG_HOME:-$HOME/.config}/gtk-2.0/gtkrc +export PYLINTHOME=${XDG_CACHE_HOME:-$HOME/.cache}/pylint command -v rustup &> /dev/null && \ export RUSTUP_HOME=$HOME/.local/share/rustup -[ -f $HOME/.config/wget/rc ] && \ - export WGETRC=$HOME/.config/wget/rc +[ -f ${XDG_CONFIG_HOME:-$HOME/.config}/wget/rc ] && \ + export WGETRC=${XDG_CONFIG_HOME:-$HOME/.config}/wget/rc # TODO: terminfo export GOBIN=$HOME/.local/bin export GOPATH=$HOME/.local/share/go -export GOCACHE=$HOME/.cache/go/build -export GOMODCACHE=$HOME/.cache/go/pkg/mod -export GOTMPDIR=$HOME/.cache/go/tmp +export GOCACHE=${XDG_CACHE_HOME:-$HOME/.cache}/go/build +export GOMODCACHE=${XDG_CACHE_HOME:-$HOME/.cache}/go/pkg/mod +export GOTMPDIR=${XDG_CACHE_HOME:-$HOME/.cache}/go/tmp diff --git a/zshrc b/zshrc index ab01726..bd9d6dc 100644 --- a/zshrc +++ b/zshrc @@ -3,8 +3,8 @@ # Load plugin scripts source-plugin() { - local shared_plugin=~/.local/share/zsh/plugins/$1/$1.plugin.zsh - local local_plugin=~/.config/zsh/$1/$1.plugin.zsh + local shared_plugin=${XDG_DATA_HOME:-$HOME/.local/share}/zsh/plugins/$1/$1.plugin.zsh + local local_plugin=${XDG_CONFIG_HOME:-$HOME/.config}/zsh/$1/$1.plugin.zsh if [ -f $shared_plugin ]; then source $shared_plugin elif [ -f $local_plugin ]; then @@ -76,7 +76,7 @@ setopt completeinword # Initialize completions autoload -U compinit -compinit -d ~/.cache/zsh/compdump +compinit -d ${XDG_CACHE_HOME:-$HOME/.cache}/zsh/compdump # Add pip to the old completion engine if present if command -v pip &> /dev/null; then @@ -192,14 +192,19 @@ frequent-directory Documents "$HOME/Documents" frequent-directory Downloads "$HOME/Downloads" frequent-directory Projects "$HOME/Projects" frequent-directory Sandbox "$HOME/Sandbox" -frequent-directory cache "$HOME/.cache" -frequent-directory config "$HOME/.config" +frequent-directory cache "${XDG_CACHE_HOME:-$HOME/.cache}" +frequent-directory config "${XDG_CONFIG_HOME:-$HOME/.config}" frequent-directory local "$HOME/.local" # Load work related config -[ -f ~/.config/work/zshrc ] && source ~/.config/work/zshrc -[ -f ~/.config/private/zshrc ] && source ~/.config/private/zshrc -[ -f ~/.config/zsh/local ] && source ~/.config/zsh/local +[ -f ${XDG_CONFIG_HOME:-$HOME/.config}/work/zshrc ] && \ + source ${XDG_CONFIG_HOME:-$HOME/.config}/work/zshrc +[ -f ${XDG_CONFIG_HOME:-$HOME/.config}/private/zshrc ] && \ + source ${XDG_CONFIG_HOME:-$HOME/.config}/private/zshrc +[ -f ${XDG_CONFIG_HOME:-$HOME/.config}/zsh/local ] && \ + source ${XDG_CONFIG_HOME:-$HOME/.config}/zsh/local +[ -f ${XDG_CONFIG_HOME:-$HOME/.config}/zsh/zshrc.local ] && \ + source ${XDG_CONFIG_HOME:-$HOME/.config}/zsh/zshrc.local # Aliases alias grep='grep --color=always'