From 5108ffc44db5c66780b2fa953c2ae2b57a915c26 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Wed, 27 Jul 2022 11:15:30 +0200 Subject: [PATCH] feat: backport --init-directory for 27/28 users --init-directory was added in Emacs 29. This backports it for 27/28 users, so users can trivially load an Emacs config living in another directory (will be necessary for future versions of Doom's sandbox). However, for this to work, Doom needs to live in ~/.emacs.d or ~/.config/emacs. Ref: emacs-mirror/emacs@8eaf04de83fd --- early-init.el | 70 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/early-init.el b/early-init.el index 19ee204b9..e00879817 100644 --- a/early-init.el +++ b/early-init.el @@ -69,33 +69,51 @@ ;; ;;; Bootstrap -;; Ensure Doom is running out of this file's directory -(setq user-emacs-directory (file-name-directory load-file-name)) +(let (;; DEPRECATED Backported from 29. Remove when 27/28 support is removed. + (initdir (cadr (member "--init-directory" command-line-args))) -;; Load the heart of Doom Emacs -(load (concat user-emacs-directory "core/core") nil 'nomessage) + initfile) -;; We hijack Emacs' initfile resolver to inject our own entry point. Why do -;; this? Because: -;; -;; - It spares Emacs the effort of looking for/loading useless initfiles, like -;; ~/.emacs and ~/_emacs. And skips ~/.emacs.d/init.el, which won't exist if -;; you're using Doom (fyi: doom hackers or chemacs users could then use -;; $EMACSDIR as their $DOOMDIR, if they wanted). -;; - Later, 'doom sync' will dynamically generate its bootstrap file, which is -;; important for Doom's soon-to-be profile system (which can replace Chemacs). -;; Until then, we'll use core/core-start.el. -;; - A "fallback" initfile can be trivially specified, in case the bootstrapper -;; is missing (if the user hasn't run 'doom sync' or is a first-timer). This -;; is an opportunity to display a "safe mode" environment that's less -;; intimidating and more helpful than the broken state errors would've left -;; Emacs in, otherwise. -;; - A generated config allows for a file IO optimized startup. -(define-advice startup--load-user-init-file (:filter-args (args) init-doom) - "Initialize Doom Emacs in an interactive session." - (list (lambda () - (expand-file-name "core-start" doom-core-dir)) - nil ; TODO Replace with safe mode initfile - (caddr args))) + ;; But discard the switches later to prevent "invalid option" errors. + (when initdir + (add-to-list 'command-switch-alist (cons "--init-directory" (lambda (_) (pop argv))))) + + ;; Detect emacs directory + (setq user-emacs-directory + (cond (initdir (expand-file-name initdir)) + ((getenv-internal "EMACSDIR")) + (user-emacs-directory))) + + ;; Load the heart of Doom Emacs + (if (load (expand-file-name "core/core" user-emacs-directory) t t) + ;; ...and prepare it for an interactive session. + (setq initfile (expand-file-name "core-start" doom-core-dir)) + ;; ...but if that fails, then assume this isn't a Doom config. + (setq early-init-file (expand-file-name "early-init" user-emacs-directory)) + (load early-init-file t t)) + + ;; We hijack Emacs' initfile resolver to inject our own entry point. Why do + ;; this? Because: + ;; + ;; - It spares Emacs the effort of looking for/loading useless initfiles, like + ;; ~/.emacs and ~/_emacs. And skips ~/.emacs.d/init.el, which won't exist if + ;; you're using Doom (fyi: doom hackers or chemacs users could then use + ;; $EMACSDIR as their $DOOMDIR, if they wanted). + ;; - Later, 'doom sync' will dynamically generate its bootstrap file, which is + ;; important for Doom's soon-to-be profile system (which can replace Chemacs). + ;; Until then, we'll use core/core-start.el. + ;; - A "fallback" initfile can be trivially specified, in case the bootstrapper + ;; is missing (if the user hasn't run 'doom sync' or is a first-timer). This + ;; is an opportunity to display a "safe mode" environment that's less + ;; intimidating and more helpful than the broken state errors would've left + ;; Emacs in, otherwise. + ;; - A generated config allows for a file IO optimized startup. + (define-advice startup--load-user-init-file (:filter-args (args) init-doom) + "Initialize Doom Emacs in an interactive session." + (list (lambda () + (or initfile + (expand-file-name "init.el" user-emacs-directory))) + nil ; TODO Replace with safe mode initfile + (caddr args)))) ;;; early-init.el ends here