diff --git a/zsh-z.plugin.zsh b/zsh-z.plugin.zsh index 59f889e..26575a4 100644 --- a/zsh-z.plugin.zsh +++ b/zsh-z.plugin.zsh @@ -149,9 +149,19 @@ zshz() { local REPLY local -a lines - # Allow the user to specify the datafile name in $ZSHZ_DATA (default: ~/.z) + # Allow the user to specify a custom datafile in $ZSHZ_DATA (or legacy $_Z_DATA) + local custom_datafile="${ZSHZ_DATA:-$_Z_DATA}" + + # If a datafile was provided as a standalone file without a directory path + # print a warning and exit + if [[ -n ${custom_datafile} && ${custom_datafile} != */* ]]; then + print "ERROR: You configured a custom Zsh-z datafile (${custom_datafile}), but have not specified its directory." >&2 + exit + fi + + # If the user specified a datafile, use that or default to ~/.z # If the datafile is a symlink, it gets dereferenced - local datafile=${${ZSHZ_DATA:-${_Z_DATA:-${HOME}/.z}}:A} + local datafile=${${custom_datafile:-$HOME/.z}:A} # If the datafile is a directory, print a warning and exit if [[ -d $datafile ]]; then @@ -161,7 +171,7 @@ zshz() { # Make sure that the datafile exists before attempting to read it or lock it # for writing - [[ -f $datafile ]] || touch "$datafile" + [[ -f $datafile ]] || { mkdir -p "${datafile:h}" && touch "$datafile" } # Bail if we don't own the datafile and $ZSHZ_OWNER is not set [[ -z ${ZSHZ_OWNER:-${_Z_OWNER}} && -f $datafile && ! -O $datafile ]] &&