diff options
| author | Paul Eggert | 2019-08-31 14:47:04 -0700 |
|---|---|---|
| committer | Paul Eggert | 2019-08-31 14:48:56 -0700 |
| commit | 2befb4f0a1494f699f56215d5f28ba055663d881 (patch) | |
| tree | 468b034bfbbb1e2edb7c015aedf5fa7043ee30e0 | |
| parent | 7791005544836f93542e8277ad5897f8f5920f05 (diff) | |
| download | emacs-2befb4f0a1494f699f56215d5f28ba055663d881.tar.gz emacs-2befb4f0a1494f699f56215d5f28ba055663d881.zip | |
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.
| -rw-r--r-- | lisp/startup.el | 51 | ||||
| -rw-r--r-- | lisp/subr.el | 14 |
2 files changed, 41 insertions, 24 deletions
diff --git a/lisp/startup.el b/lisp/startup.el index c1e429b8db7..a16db242da0 100644 --- a/lisp/startup.el +++ b/lisp/startup.el | |||
| @@ -490,6 +490,27 @@ DIRS are relative." | |||
| 490 | (when tail | 490 | (when tail |
| 491 | (setcdr tail (append (mapcar 'expand-file-name dirs) (cdr tail)))))) | 491 | (setcdr tail (append (mapcar 'expand-file-name dirs) (cdr tail)))))) |
| 492 | 492 | ||
| 493 | ;; The default location for XDG-convention Emacs init files. | ||
| 494 | (defconst startup--xdg-config-default "~/.config/emacs/") | ||
| 495 | ;; The location for XDG-convention Emacs init files. | ||
| 496 | (defvar startup--xdg-config-home-emacs) | ||
| 497 | |||
| 498 | ;; Return the name of the init file directory for Emacs, assuming | ||
| 499 | ;; XDG-DIR is the XDG location and USER-NAME is the user name. | ||
| 500 | ;; If USER-NAME is nil or "", use the current user. | ||
| 501 | ;; Prefer the XDG location unless it does does not exist and the | ||
| 502 | ;; .emacs.d location does exist. | ||
| 503 | (defun startup--xdg-or-homedot (xdg-dir user-name) | ||
| 504 | (if (file-exists-p xdg-dir) | ||
| 505 | xdg-dir | ||
| 506 | (let ((emacs-d-dir (concat "~" user-name | ||
| 507 | (if (eq system-type 'ms-dos) | ||
| 508 | "/_emacs.d/" | ||
| 509 | "/.emacs.d/")))) | ||
| 510 | (if (file-exists-p emacs-d-dir) | ||
| 511 | emacs-d-dir | ||
| 512 | xdg-dir)))) | ||
| 513 | |||
| 493 | (defun normal-top-level () | 514 | (defun normal-top-level () |
| 494 | "Emacs calls this function when it first starts up. | 515 | "Emacs calls this function when it first starts up. |
| 495 | It sets `command-line-processed', processes the command-line, | 516 | It sets `command-line-processed', processes the command-line, |
| @@ -499,6 +520,14 @@ It is the default value of the variable `top-level'." | |||
| 499 | (message internal--top-level-message) | 520 | (message internal--top-level-message) |
| 500 | (setq command-line-processed t) | 521 | (setq command-line-processed t) |
| 501 | 522 | ||
| 523 | (setq startup--xdg-config-home-emacs | ||
| 524 | (let ((xdg-config-home (getenv-internal "XDG_CONFIG_HOME"))) | ||
| 525 | (if xdg-config-home | ||
| 526 | (concat xdg-config-home "/emacs/") | ||
| 527 | startup--xdg-config-default))) | ||
| 528 | (setq user-emacs-directory | ||
| 529 | (startup--xdg-or-homedot startup--xdg-config-home-emacs nil)) | ||
| 530 | |||
| 502 | ;; Look in each dir in load-path for a subdirs.el file. If we | 531 | ;; Look in each dir in load-path for a subdirs.el file. If we |
| 503 | ;; find one, load it, which will add the appropriate subdirs of | 532 | ;; find one, load it, which will add the appropriate subdirs of |
| 504 | ;; that dir into load-path. This needs to be done before setting | 533 | ;; that dir into load-path. This needs to be done before setting |
| @@ -1167,19 +1196,17 @@ please check its value") | |||
| 1167 | :error)))) | 1196 | :error)))) |
| 1168 | 1197 | ||
| 1169 | ;; Calculate the name of the Emacs init directory. | 1198 | ;; Calculate the name of the Emacs init directory. |
| 1170 | ;; This is typically equivalent to ~/.config/emacs if the user is | 1199 | ;; This is typically ~INIT-FILE-USER/.config/emacs unless the user |
| 1171 | ;; following the XDG convention, and is ~INIT-FILE-USER/.emacs.d | 1200 | ;; is following the ~INIT-FILE-USER/.emacs.d convention. |
| 1172 | ;; on other systems. | 1201 | (setq xdg-dir startup--xdg-config-home-emacs) |
| 1173 | (setq xdg-dir (concat (or (getenv "XDG_CONFIG_HOME") | ||
| 1174 | (concat "~" init-file-user "/.config")) | ||
| 1175 | "/emacs/")) | ||
| 1176 | (setq startup-init-directory | 1202 | (setq startup-init-directory |
| 1177 | (if (file-exists-p xdg-dir) | 1203 | (if (or (zerop (length init-file-user)) |
| 1178 | xdg-dir | 1204 | (and (eq xdg-dir user-emacs-directory) |
| 1179 | (let ((emacs-d-dir (concat "~" init-file-user "/.emacs.d/"))) | 1205 | (not (eq xdg-dir startup--xdg-config-default)))) |
| 1180 | (if (file-exists-p emacs-d-dir) | 1206 | user-emacs-directory |
| 1181 | emacs-d-dir | 1207 | ;; The name is not obvious, so access more directories to calculate it. |
| 1182 | xdg-dir)))) | 1208 | (setq xdg-dir (concat "~" init-file-user "/.config/emacs/")) |
| 1209 | (startup--xdg-or-homedot xdg-dir init-file-user))) | ||
| 1183 | 1210 | ||
| 1184 | ;; Load the early init file, if found. | 1211 | ;; Load the early init file, if found. |
| 1185 | (startup--load-user-init-file | 1212 | (startup--load-user-init-file |
diff --git a/lisp/subr.el b/lisp/subr.el index 566a3fc758e..cf6fb108e9c 100644 --- a/lisp/subr.el +++ b/lisp/subr.el | |||
| @@ -2938,18 +2938,8 @@ This hook is normally set up with a function to put the buffer in Help | |||
| 2938 | mode.") | 2938 | mode.") |
| 2939 | 2939 | ||
| 2940 | (defconst user-emacs-directory | 2940 | (defconst user-emacs-directory |
| 2941 | (let ((config-dir (concat (or (getenv-internal "XDG_CONFIG_HOME") | 2941 | ;; The value does not matter since Emacs sets this at startup. |
| 2942 | "~/.config") | 2942 | nil |
| 2943 | "/emacs/"))) | ||
| 2944 | (if (file-exists-p config-dir) | ||
| 2945 | config-dir | ||
| 2946 | (let ((emacs-d-dir (if (eq system-type 'ms-dos) | ||
| 2947 | ;; MS-DOS cannot have initial dot. | ||
| 2948 | "~/_emacs.d/" | ||
| 2949 | "~/.emacs.d/"))) | ||
| 2950 | (if (file-exists-p emacs-d-dir) | ||
| 2951 | emacs-d-dir | ||
| 2952 | config-dir)))) | ||
| 2953 | "Directory beneath which additional per-user Emacs-specific files are placed. | 2943 | "Directory beneath which additional per-user Emacs-specific files are placed. |
| 2954 | Various programs in Emacs store information in this directory. | 2944 | Various programs in Emacs store information in this directory. |
| 2955 | Note that this should end with a directory separator. | 2945 | Note that this should end with a directory separator. |