From e7f8f6ed9734b3fb765aa926c30ae223c0529774 Mon Sep 17 00:00:00 2001 From: Max Savchenko Date: Fri, 1 Nov 2024 18:55:11 +0100 Subject: [PATCH] take $XDG_CONFIG_HOME into account when searching for config files --- doc/reference.md | 9 ++++++--- src/zprint/config.cljc | 25 ++++++++++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/doc/reference.md b/doc/reference.md index d93845c..78bec39 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -448,9 +448,12 @@ time it will configure itself from all of the information that it has available at that time. It will examine the following information in order to configure itself: -* The file `$HOME/.zprintrc` or if that file does not exist, the file - `$HOME/.zprint.edn` for an options map in EDN format. -* If the file found above or the options map on the command line has +* The first file found among: + * `$HOME/.zprintrc` + * `$HOME/.zprint.edn` (for an options map in EDN format) + * `$XDG_CONFIG_HOME/zprint/zprintrc` + * `$XDG_CONFIG_HOME/zprint/zprint.edn` (for an options map in EDN format) +* If the file found above or the options map on the command line has `:search-config?` true, it will look in the current directory for a file `.zprintrc` and if it doesn't find one, it will look for `.zprint.edn`.l If it doesn't find either of them, diff --git a/src/zprint/config.cljc b/src/zprint/config.cljc index c917c12..9be477a 100644 --- a/src/zprint/config.cljc +++ b/src/zprint/config.cljc @@ -49,6 +49,8 @@ (def zprintrc ".zprintrc") (def zprintedn ".zprint.edn") +(def xdg-zprintrc "zprintrc") +(def xdg-zprintedn "zprint.edn") (declare merge-deep) @@ -2859,22 +2861,35 @@ op-options (select-op-options op-options) #_(println "op-options:" op-options) ; - ; $HOME/.zprintrc + ; $HOME/.zprintrc $HOME/.zprint.edn + ; + ; $XDG_CONFIG_HOME/zprint/zprintrc + ; $XDG_CONFIG_HOME/zprint/zprint.edn ; home #?(:clj (System/getenv "HOME") :cljs nil) file-separator #?(:clj (System/getProperty "file.separator") :cljs nil) - zprintrc-file (str home file-separator zprintrc) + xdg-config-home #?(:clj (or (System/getenv "XDG_CONFIG_HOME") + (when (and home file-separator) + (str home file-separator ".config"))) + :cljs nil) [opts-rcfile errors-rcfile rc-filename :as home-config] - (when (and home file-separator) - (get-config-from-path [zprintrc zprintedn] file-separator [home])) + (or (when (and home file-separator) + (get-config-from-path [zprintrc zprintedn] + file-separator + [home])) + (when (and xdg-config-home file-separator) + (get-config-from-path [xdg-zprintrc xdg-zprintedn] + file-separator + [xdg-config-home]))) _ (dbg-s (merge op-options opts-rcfile) #{:zprintrc} "~/.zprintrc:" home-config) [updated-map new-doc-map rc-errors] - (config-and-validate (str "Home directory file: " rc-filename) + (config-and-validate (str "$HOME/$XDG_CONFIG_HOME directory file: " + rc-filename) default-doc-map default-map opts-rcfile)