Subject: bug#583: Use XDG basedir spec for configuration files?

From: Paul Eggert <[email-masked]>
Date: Sat, 31 Aug 2019 14:51:55 -0700

Glenn Morris wrote:
> I guess user-emacs-directory is being set at build time, not run time.

Thanks for reporting that. I installed the attached to fix it. I plan to follow 
up soon on the other issues raised here.
>From 2befb4f0a1494f699f56215d5f28ba055663d881 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sat, 31 Aug 2019 14:47:04 -0700
Subject: [PATCH] Calculate user-emacs-directory on startup

Problem reported by Glenn Morris (Bug#583#56).
* lisp/startup.el (startup--xdg-config-default): New constant.
(startup--xdg-config-home-emacs): New var.
(startup--xdg-or-homedot): New function.
(normal-top-level): Use it to set user-emacs-directory early on.
(command-line): Also use it to determine the startup init directory.
* lisp/subr.el (user-emacs-directory): Just initialize to nil.
---
 lisp/startup.el | 51 +++++++++++++++++++++++++++++++++++++------------
 lisp/subr.el    | 14 ++------------
 2 files changed, 41 insertions(+), 24 deletions(-)

diff --git a/lisp/startup.el b/lisp/startup.el
index c1e429b8db..a16db242da 100644
--- a/lisp/startup.el
+++ b/lisp/startup.el
@@ -490,6 +490,27 @@ normal-top-level-add-to-load-path
     (when tail
       (setcdr tail (append (mapcar 'expand-file-name dirs) (cdr tail))))))
 
+;; The default location for XDG-convention Emacs init files.
+(defconst startup--xdg-config-default "~/.config/emacs/")
+;; The location for XDG-convention Emacs init files.
+(defvar startup--xdg-config-home-emacs)
+
+;; Return the name of the init file directory for Emacs, assuming
+;; XDG-DIR is the XDG location and USER-NAME is the user name.
+;; If USER-NAME is nil or "", use the current user.
+;; Prefer the XDG location unless it does does not exist and the
+;; .emacs.d location does exist.
+(defun startup--xdg-or-homedot (xdg-dir user-name)
+  (if (file-exists-p xdg-dir)
+      xdg-dir
+    (let ((emacs-d-dir (concat "~" user-name
+			       (if (eq system-type 'ms-dos)
+				   "/_emacs.d/"
+				 "/.emacs.d/"))))
+      (if (file-exists-p emacs-d-dir)
+	  emacs-d-dir
+	xdg-dir))))
+
 (defun normal-top-level ()
   "Emacs calls this function when it first starts up.
 It sets `command-line-processed', processes the command-line,
@@ -499,6 +520,14 @@ normal-top-level
       (message internal--top-level-message)
     (setq command-line-processed t)
 
+    (setq startup--xdg-config-home-emacs
+	  (let ((xdg-config-home (getenv-internal "XDG_CONFIG_HOME")))
+	    (if xdg-config-home
+		(concat xdg-config-home "/emacs/")
+	      startup--xdg-config-default)))
+    (setq user-emacs-directory
+	  (startup--xdg-or-homedot startup--xdg-config-home-emacs nil))
+
     ;; Look in each dir in load-path for a subdirs.el file.  If we
     ;; find one, load it, which will add the appropriate subdirs of
     ;; that dir into load-path.  This needs to be done before setting
@@ -1167,19 +1196,17 @@ command-line
                          :error))))
 
   ;; Calculate the name of the Emacs init directory.
-  ;; This is typically equivalent to ~/.config/emacs if the user is
-  ;; following the XDG convention, and is ~INIT-FILE-USER/.emacs.d
-  ;; on other systems.
-  (setq xdg-dir (concat (or (getenv "XDG_CONFIG_HOME")
-			    (concat "~" init-file-user "/.config"))
-			"/emacs/"))
+  ;; This is typically ~INIT-FILE-USER/.config/emacs unless the user
+  ;; is following the ~INIT-FILE-USER/.emacs.d convention.
+  (setq xdg-dir startup--xdg-config-home-emacs)
   (setq startup-init-directory
-	(if (file-exists-p xdg-dir)
-	    xdg-dir
-	  (let ((emacs-d-dir (concat "~" init-file-user "/.emacs.d/")))
-	    (if (file-exists-p emacs-d-dir)
-		emacs-d-dir
-	      xdg-dir))))
+	(if (or (zerop (length init-file-user))
+		(and (eq xdg-dir user-emacs-directory)
+		     (not (eq xdg-dir startup--xdg-config-default))))
+	    user-emacs-directory
+	  ;; The name is not obvious, so access more directories to calculate it.
+	  (setq xdg-dir (concat "~" init-file-user "/.config/emacs/"))
+	  (startup--xdg-or-homedot xdg-dir init-file-user)))
 
   ;; Load the early init file, if found.
   (startup--load-user-init-file
diff --git a/lisp/subr.el b/lisp/subr.el
index 566a3fc758..cf6fb108e9 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -2938,18 +2938,8 @@ temp-buffer-setup-hook
 mode.")
 
 (defconst user-emacs-directory
-  (let ((config-dir (concat (or (getenv-internal "XDG_CONFIG_HOME")
-				"~/.config")
-			    "/emacs/")))
-    (if (file-exists-p config-dir)
-	config-dir
-      (let ((emacs-d-dir (if (eq system-type 'ms-dos)
-			     ;; MS-DOS cannot have initial dot.
-			     "~/_emacs.d/"
-			   "~/.emacs.d/")))
-	(if (file-exists-p emacs-d-dir)
-	    emacs-d-dir
-	  config-dir))))
+  ;; The value does not matter since Emacs sets this at startup.
+  nil
   "Directory beneath which additional per-user Emacs-specific files are placed.
 Various programs in Emacs store information in this directory.
 Note that this should end with a directory separator.
-- 
2.17.1