From 8eebec8591578111810f5e256b5052d8212b0f00 Mon Sep 17 00:00:00 2001 From: Timo Wilken Date: Wed, 11 Oct 2023 22:28:34 +0200 Subject: Add option to create requested Emacs XDG directory --- tw/home/files/emacs-init.el | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'tw/home/files/emacs-init.el') diff --git a/tw/home/files/emacs-init.el b/tw/home/files/emacs-init.el index 8fd624fc..2c11692b 100644 --- a/tw/home/files/emacs-init.el +++ b/tw/home/files/emacs-init.el @@ -13,18 +13,22 @@ ;; (when (file-readable-p custom-file) ;; (load custom-file)) -(defun tw/xdg-emacs-subdir (type name) - "Get the name of a subdirectory called NAME under $XDG__HOME/emacs." - (expand-file-name - (concat "emacs/" (string-trim-right name "/")) - (pcase type - ('cache (or (getenv "XDG_CACHE_HOME") "~/.cache/")) - ('config (or (getenv "XDG_CONFIG_HOME") "~/.config/")) - ('data (or (getenv "XDG_DATA_HOME") "~/.local/share/")) - ;; The following two are Guix/GuixSD extensions. - ('log (or (getenv "XDG_LOG_HOME") "~/.local/var/log/")) - ('state (or (getenv "XDG_STATE_HOME") "~/.local/var/lib/")) - (_ (error "Unknown XDG directory type: %S" type))))) +(defun tw/xdg-emacs-subdir (type name &optional create) + "Get the name of a file or directory called NAME under $XDG__HOME/emacs. +If CREATE is true and the resulting directory does not exist, create it." + (let ((dir (expand-file-name + (concat "emacs/" (string-trim-right name "/")) + (pcase type + ('cache (or (getenv "XDG_CACHE_HOME") "~/.cache/")) + ('config (or (getenv "XDG_CONFIG_HOME") "~/.config/")) + ('data (or (getenv "XDG_DATA_HOME") "~/.local/share/")) + ;; The following two are Guix/GuixSD extensions. + ('log (or (getenv "XDG_LOG_HOME") "~/.local/var/log/")) + ('state (or (getenv "XDG_STATE_HOME") "~/.local/var/lib/")) + (_ (error "Unknown XDG directory type: %S" type)))))) + (when (and create (not (file-accessible-directory-p dir))) + (make-directory dir t)) + dir)) ;; Global/built-in Custom settings ;; Apply these as early as possible so that e.g. the native-comp files go to the right place. @@ -64,7 +68,7 @@ (backup-by-copying t "Avoid breaking hardlinks when making backup files.") (auto-save-file-name-transforms ;; `file-name-as-directory' is important, since Emacs takes the directory part when UNIQUIFY is t. - ((".*" ,(file-name-as-directory (tw/xdg-emacs-subdir 'data "auto-save")) t)) + ((".*" ,(file-name-as-directory (tw/xdg-emacs-subdir 'data "auto-save" t)) t)) "Put auto-save #files# in a sensible directory.") (recentf-max-saved-items 1000 "Save lots of recently-opened files.") (recentf-save-file ,(tw/xdg-emacs-subdir 'data "recentf.el") "Save recently-opened files here.") @@ -72,8 +76,6 @@ (savehist-file ,(tw/xdg-emacs-subdir 'data "savehist.el") "Save minibuffer history here.") (savehist-mode t "Save minibuffer history on quit."))) -(make-directory (cadr (assoc-string ".*" auto-save-file-name-transforms)) t) - (defalias 'yes-or-no-p #'y-or-n-p "Always use `y-or-n-p' when asking for confirmation.") @@ -164,7 +166,7 @@ (use-package dash-docs :custom (dash-docs-docsets-path - (file-name-as-directory (tw/xdg-emacs-subdir 'data "dash-docsets")) + (file-name-as-directory (tw/xdg-emacs-subdir 'data "dash-docsets" t)) "Store docsets in the XDG data directory.") (dash-docs-browser-func 'eww "Open documentation pages using `eww' instead of an external browser.") (dash-docs-enable-debugging nil "Disable popping up useless warnings.")) -- cgit v1.2.3