aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Pluim2020-01-15 12:24:43 +0100
committerRobert Pluim2020-01-16 16:05:45 +0100
commit13995f31a219bfcb24da5887136583cbf2deff4c (patch)
treeb8abcb0057271c7ed2d6d90144c6925beaaea128
parent91cac24952806c6c4addc3c045854c2c314c2e31 (diff)
downloademacs-13995f31a219bfcb24da5887136583cbf2deff4c.tar.gz
emacs-13995f31a219bfcb24da5887136583cbf2deff4c.zip
Make emacs prefer an existing ~/.emacs.d to an existing XDG location
* doc/emacs/custom.texi (Find Init): Update description of how Emacs finds its init file directory and the interaction with $XDG_CONFIG_HOME (Early Init File): Correct XDG location of early-init.el * etc/NEWS: Update description to make it clear the ~/.emacs.d is preferred, even if the XDG location exists. * lisp/startup.el: Prefer ~/.emacs.d even if the XDG location exists. * lib-src/emacsclient.c (open_config): Prefer home directory the XDG location.
-rw-r--r--doc/emacs/custom.texi45
-rw-r--r--etc/NEWS37
-rw-r--r--lib-src/emacsclient.c17
-rw-r--r--lisp/startup.el42
4 files changed, 76 insertions, 65 deletions
diff --git a/doc/emacs/custom.texi b/doc/emacs/custom.texi
index 1958a86b92d..2e04a6b22d3 100644
--- a/doc/emacs/custom.texi
+++ b/doc/emacs/custom.texi
@@ -2655,26 +2655,37 @@ library. @xref{Hooks}.
2655 2655
2656 Emacs normally finds your init file in a location under your home 2656 Emacs normally finds your init file in a location under your home
2657directory. @xref{Init File}. By default this location is 2657directory. @xref{Init File}. By default this location is
2658@file{~/.config/emacs/init.el} where @file{~/} stands for your home directory. 2658@file{~/.emacs.d/init.el} where @file{~/} stands for your home directory.
2659This default can be overridden as described below. 2659This default can be overridden as described below.
2660 2660
2661 If @env{XDG_CONFIG_HOME} is set in your environment, its 2661Emacs looks for your init file
2662value replaces @file{~/.config} in the name of the default
2663init file.
2664
2665 If the default init file's parent directory does not exist but the
2666directory @file{~/.emacs.d} does exist, Emacs looks for your init file
2667using the filenames @file{~/.emacs.el}, @file{~/.emacs}, or 2662using the filenames @file{~/.emacs.el}, @file{~/.emacs}, or
2668@file{~/.emacs.d/init.el}; you can choose to use any one of these 2663@file{~/.emacs.d/init.el}; you can choose to use any one of these
2669names. (Note that only the locations directly in your home directory 2664names. (Note that only the locations directly in your home directory
2670have a leading dot in the location's basename.) Although this is 2665have a leading dot in the location's basename.)
2671backward-compatible with older Emacs versions, modern POSIX platforms 2666
2672prefer putting your initialization files under @file{~/.config} so 2667Emacs can also look in an XDG-compatible location for @file{init.el},
2673that troubleshooting a problem that might be due to a bad init file, 2668the default is the directory @file{~/.config/emacs}. This can be
2674or archiving a collection of init files, can be done by renaming that 2669overriden by setting @env{XDG_CONFIG_HOME} in your environment, its
2675directory. To help older Emacs versions find configuration files in 2670value replaces @file{~/.config} in the name of the default XDG init
2676their current default locations, you can execute the following 2671file. However @file{~/.emacs.d} and @file{~/.emacs} are always
2677Emacs Lisp code: 2672preferred if they exist, which means that you must delete or rename
2673them in order to use the XDG location.
2674
2675Note also that if neither the XDG location nor @file{~/.emacs.d}
2676exist, then Emacs will create @file{~/.emacs.d} (and therefore use it
2677during subsequent invocations).
2678
2679Emacs will set @var{user-emacs-directory} to the directory it decides
2680to use.
2681
2682Although this is backward-compatible with older Emacs versions, modern
2683POSIX platforms prefer putting your initialization files under
2684@file{~/.config} so that troubleshooting a problem that might be due
2685to a bad init file, or archiving a collection of init files, can be
2686done by renaming that directory. To help older Emacs versions find
2687configuration files in their current default locations, you can
2688execute the following Emacs Lisp code:
2678 2689
2679@example 2690@example
2680(make-symbolic-link ".config/emacs" "~/.emacs.d") 2691(make-symbolic-link ".config/emacs" "~/.emacs.d")
@@ -2694,7 +2705,7 @@ otherwise, it looks up the home directory corresponding to that user
2694name in the system's data base of users. 2705name in the system's data base of users.
2695 2706
2696 For brevity the rest of the Emacs documentation generally uses just 2707 For brevity the rest of the Emacs documentation generally uses just
2697the current default location @file{~/.config/emacs/init.el} for the 2708the current default location @file{~/.emacs.d/init.el} for the
2698init file. 2709init file.
2699@c LocalWords: backtab 2710@c LocalWords: backtab
2700 2711
@@ -2740,7 +2751,7 @@ Type @kbd{C-q}, followed by the key you want to bind, to insert @var{char}.
2740@xref{Init File}. However, it is sometimes desirable 2751@xref{Init File}. However, it is sometimes desirable
2741to have customizations that take effect during Emacs startup earlier than the 2752to have customizations that take effect during Emacs startup earlier than the
2742normal init file is processed. Such customizations can be put in the early 2753normal init file is processed. Such customizations can be put in the early
2743init file, @file{~/.config/emacs.d/early-init.el} or @file{~/.emacs.d/early-init.el}. This file is loaded before the 2754init file, @file{~/.config/emacs/early-init.el} or @file{~/.emacs.d/early-init.el}. This file is loaded before the
2744package system and GUI is initialized, so in it you can customize variables 2755package system and GUI is initialized, so in it you can customize variables
2745that affect frame appearance as well as the package initialization process, 2756that affect frame appearance as well as the package initialization process,
2746such as @code{package-enable-at-startup}, @code{package-load-list}, and 2757such as @code{package-enable-at-startup}, @code{package-load-list}, and
diff --git a/etc/NEWS b/etc/NEWS
index c1e00395c84..38fff3f7568 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -161,25 +161,24 @@ Emacs 28.
161* Startup Changes in Emacs 27.1 161* Startup Changes in Emacs 27.1
162 162
163+++ 163+++
164** Emacs now uses the XDG convention for init files. 164** Emacs can now use the XDG convention for init files.
165For example, it looks for init.el in "~/.config/emacs/init.el", and 165The 'XDG_CONFIG_HOME' environment variable (which defaults to
166similarly for other init files. 166"~/.config") specifies the XDG configuration parent directory. Emacs
167 167checks for "init.el" and other configuration files inside the "emacs"
168The 'XDG_CONFIG_HOME' environment variable (which defaults to "~/.config") 168subdirectory of 'XDG_CONFIG_HOME', i.e. "$XDG_CONFIG_HOME/emacs/init.el"
169specifies the parent directory of these and other configuration files, 169
170and will override their traditional locations (the home directory, 170However, Emacs will still initially look for init files in their
171"~/.emacs.d", etc.). 171traditional locations if "~/.emacs.d" or "~/.emacs" exist, even if
172 172"$XDG_CONFIG_HOME/emacs" also exists. This means that you must delete
173Emacs will still look for init files in their traditional locations if 173or rename any existing "~/.emacs.d" and "~/.emacs" to enable use of
174"$XDG_CONFIG_HOME/emacs" does not exist, but "~/.emacs.d" or 174the XDG directory.
175"~/.emacs" does exist, so invoking Emacs with XDG_CONFIG_HOME='/nowhere' 175
176might be useful if your new-location init files are scrambled, or if 176If "~/.emacs.d" does not exist, and Emacs has decided to use it
177you want to force Emacs to ignore files under 'XDG_CONFIG_HOME' for 177(i.e. "$XDG_CONFIG_HOME/emacs" does not exist), Emacs will create it.
178some other reason. 178Emacs will never create "$XDG_CONFIG_HOME/emacs".
179 179
180If neither "$XDG_CONFIG_HOME/emacs" nor "~/.emacs.d" exist, Emacs will 180Whichever directory Emacs decides to use, it will set
181now default to "$XDG_CONFIG_HOME/emacs", and will create that directory 181'user-emacs-directory' to point to it.
182and set 'user-emacs-directory' to point to it.
183 182
184+++ 183+++
185** Emacs can now be configured using an early init file. 184** Emacs can now be configured using an early init file.
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 645ff04c6d4..204064f1871 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -924,21 +924,22 @@ open_config (char const *home, char const *xdg, char const *config_file)
924 char *configname = xmalloc (max (xdgsubdirsize, homesubdirsizemax) 924 char *configname = xmalloc (max (xdgsubdirsize, homesubdirsizemax)
925 + strlen (config_file)); 925 + strlen (config_file));
926 FILE *config; 926 FILE *config;
927 if (xdg || home) 927
928 if (home)
928 { 929 {
929 strcpy ((xdg 930 strcpy (stpcpy (stpcpy (configname, home), "/.emacs.d/server/"),
930 ? stpcpy (stpcpy (configname, xdg), "/emacs/server/") 931 config_file);
931 : stpcpy (stpcpy (configname, home), "/.config/emacs/server/")),
932 config_file);
933 config = fopen (configname, "rb"); 932 config = fopen (configname, "rb");
934 } 933 }
935 else 934 else
936 config = NULL; 935 config = NULL;
937 936
938 if (! config && home) 937 if (! config && (xdg || home))
939 { 938 {
940 strcpy (stpcpy (stpcpy (configname, home), "/.emacs.d/server/"), 939 strcpy ((xdg
941 config_file); 940 ? stpcpy (stpcpy (configname, xdg), "/emacs/server/")
941 : stpcpy (stpcpy (configname, home), "/.config/emacs/server/")),
942 config_file);
942 config = fopen (configname, "rb"); 943 config = fopen (configname, "rb");
943 } 944 }
944 945
diff --git a/lisp/startup.el b/lisp/startup.el
index 2a85c004da1..1f545c66922 100644
--- a/lisp/startup.el
+++ b/lisp/startup.el
@@ -497,28 +497,28 @@ DIRS are relative."
497(defvar startup--xdg-config-home-emacs) 497(defvar startup--xdg-config-home-emacs)
498 498
499;; Return the name of the init file directory for Emacs, assuming 499;; Return the name of the init file directory for Emacs, assuming
500;; XDG-DIR is the XDG location and USER-NAME is the user name. 500;; XDG-DIR is the XDG location and USER-NAME is the user name. If
501;; If USER-NAME is nil or "", use the current user. 501;; USER-NAME is nil or "", use the current user. Prefer the XDG
502;; Prefer the XDG location unless it does does not exist and the 502;; location only if the .emacs.d location does not exist.
503;; .emacs.d location does exist.
504(defun startup--xdg-or-homedot (xdg-dir user-name) 503(defun startup--xdg-or-homedot (xdg-dir user-name)
505 (if (file-exists-p xdg-dir) 504 (let ((emacs-d-dir (concat "~" user-name
506 xdg-dir 505 (if (eq system-type 'ms-dos)
507 (let ((emacs-d-dir (concat "~" user-name 506 "/_emacs.d/"
508 (if (eq system-type 'ms-dos) 507 "/.emacs.d/"))))
509 "/_emacs.d/" 508 (cond
510 "/.emacs.d/")))) 509 ((or (file-exists-p emacs-d-dir)
511 (if (or (file-exists-p emacs-d-dir) 510 (if (eq system-type 'windows-nt)
512 (if (eq system-type 'windows-nt) 511 (if (file-directory-p (concat "~" user-name))
513 (if (file-directory-p (concat "~" user-name)) 512 (directory-files (concat "~" user-name) nil
514 (directory-files (concat "~" user-name) nil 513 "\\`[._]emacs\\(\\.elc?\\)?\\'"))
515 "\\`[._]emacs\\(\\.elc?\\)?\\'")) 514 (file-exists-p (concat "~" init-file-user
516 (file-exists-p (concat "~" init-file-user 515 (if (eq system-type 'ms-dos)
517 (if (eq system-type 'ms-dos) 516 "/_emacs"
518 "/_emacs" 517 "/.emacs")))))
519 "/.emacs"))))) 518 emacs-d-dir)
520 emacs-d-dir 519 ((file-exists-p xdg-dir)
521 xdg-dir)))) 520 xdg-dir)
521 (t emacs-d-dir))))
522 522
523(defun normal-top-level () 523(defun normal-top-level ()
524 "Emacs calls this function when it first starts up. 524 "Emacs calls this function when it first starts up.