2018-03-21 15:21:18 -04:00
|
|
|
;;; early-init.el -*- lexical-binding: t; -*-
|
2022-06-18 15:04:12 +02:00
|
|
|
;;; Commentary:
|
|
|
|
;;
|
2022-07-27 12:06:07 +02:00
|
|
|
;; early-init.el was introduced in Emacs 27.1 and is loaded before init.el;
|
|
|
|
;; before Emacs initializes package.el and its UI; and before site files are
|
|
|
|
;; loaded. This is the best time to tweak Emacs (though any UI work will have to
|
|
|
|
;; be deferred).
|
2022-06-18 15:04:12 +02:00
|
|
|
;;
|
2022-07-27 12:06:07 +02:00
|
|
|
;; This file is responsible for bootstrapping an interactive session, and is
|
|
|
|
;; where all our dirtiest (and config-agnostic) startup hacks should live. It's
|
|
|
|
;; also home to Doom's bootloader, which lets you choose what Emacs config to
|
|
|
|
;; load with one of two switches:
|
|
|
|
;; - '--init-directory DIR' (backported from Emacs 29)
|
|
|
|
;; - Or Doom's profile system with '--profile NAME' (you declare configs in
|
|
|
|
;; $EMACSDIR/profiles.el or implicitly as directories in $EMACSDIR/profiles/).
|
|
|
|
;;
|
|
|
|
;; You should *never* load this file in non-interactive sessions (e.g. batch
|
|
|
|
;; scripts). Load `core-start' or use 'doom run' instead!
|
2022-06-18 15:04:12 +02:00
|
|
|
;;
|
|
|
|
;;; Code:
|
2018-03-28 00:41:57 -04:00
|
|
|
|
2022-07-27 12:06:07 +02:00
|
|
|
;; Garbage collection is a big contributor to startup times. This fends it off,
|
|
|
|
;; then is reset later by enabling `gcmh-mode'. Not resetting it will cause
|
|
|
|
;; stuttering/freezes.
|
2019-07-21 03:01:15 +02:00
|
|
|
(setq gc-cons-threshold most-positive-fixnum)
|
2018-09-19 00:25:17 +01:00
|
|
|
|
2022-07-27 12:06:07 +02:00
|
|
|
;; Prioritize old byte-compiled source files over newer sources. It saves us a
|
|
|
|
;; little IO time to skip all the mtime checks on each lookup.
|
|
|
|
(setq load-prefer-newer nil)
|
2018-06-16 11:38:19 +02:00
|
|
|
|
2022-02-17 21:12:40 +01:00
|
|
|
(unless (or (daemonp)
|
|
|
|
init-file-debug)
|
2021-04-20 22:34:59 -04:00
|
|
|
(let ((old-file-name-handler-alist file-name-handler-alist))
|
|
|
|
;; `file-name-handler-alist' is consulted on each `require', `load' and
|
|
|
|
;; various path/io functions. You get a minor speed up by unsetting this.
|
|
|
|
;; Some warning, however: this could cause problems on builds of Emacs where
|
|
|
|
;; its site lisp files aren't byte-compiled and we're forced to load the
|
|
|
|
;; *.el.gz files (e.g. on Alpine).
|
|
|
|
(setq-default file-name-handler-alist nil)
|
|
|
|
;; ...but restore `file-name-handler-alist' later, because it is needed for
|
|
|
|
;; handling encrypted or compressed files, among other things.
|
|
|
|
(defun doom-reset-file-handler-alist-h ()
|
|
|
|
(setq file-name-handler-alist
|
|
|
|
;; Merge instead of overwrite because there may have bene changes to
|
|
|
|
;; `file-name-handler-alist' since startup we want to preserve.
|
|
|
|
(delete-dups (append file-name-handler-alist
|
|
|
|
old-file-name-handler-alist))))
|
2021-10-09 19:31:21 +02:00
|
|
|
(add-hook 'emacs-startup-hook #'doom-reset-file-handler-alist-h 101))
|
|
|
|
|
2021-12-12 22:26:16 +01:00
|
|
|
;; Premature redisplays can substantially affect startup times and produce
|
|
|
|
;; ugly flashes of unstyled Emacs.
|
2021-10-18 00:38:36 +02:00
|
|
|
(setq-default inhibit-redisplay t
|
|
|
|
inhibit-message t)
|
|
|
|
(add-hook 'window-setup-hook
|
|
|
|
(lambda ()
|
|
|
|
(setq-default inhibit-redisplay nil
|
|
|
|
inhibit-message nil)
|
2021-11-25 01:17:42 +01:00
|
|
|
(redisplay)))
|
|
|
|
|
|
|
|
;; Site files tend to use `load-file', which emits "Loading X..." messages in
|
|
|
|
;; the echo area, which in turn triggers a redisplay. Redisplays can have a
|
|
|
|
;; substantial effect on startup times and in this case happens so early that
|
|
|
|
;; Emacs may flash white while starting up.
|
|
|
|
(define-advice load-file (:override (file) silence)
|
2022-07-27 12:06:07 +02:00
|
|
|
(load file nil :nomessage))
|
2021-11-25 01:17:42 +01:00
|
|
|
|
|
|
|
;; Undo our `load-file' advice above, to limit the scope of any edge cases it
|
|
|
|
;; may introduce down the road.
|
|
|
|
(define-advice startup--load-user-init-file (:before (&rest _) init-doom)
|
|
|
|
(advice-remove #'load-file #'load-file@silence)))
|
2021-10-09 19:31:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
2022-07-27 12:05:56 +02:00
|
|
|
;;; Detect `user-emacs-directory'
|
|
|
|
|
|
|
|
;; Prevent recursive profile processing, in case you're loading a Doom profile.
|
|
|
|
(unless (boundp 'doom-version)
|
|
|
|
;; Not using `command-switch-alist' to process --profile and --init-directory
|
|
|
|
;; was intentional. `command-switch-alist' is processed too late at startup to
|
|
|
|
;; change `user-emacs-directory' in time.
|
2021-10-09 19:31:21 +02:00
|
|
|
|
2022-07-27 12:05:56 +02:00
|
|
|
;; DEPRECATED Backported from 29. Remove this when 27/28 support is removed.
|
|
|
|
(let ((initdir (or (cadr (member "--init-directory" command-line-args))
|
|
|
|
(getenv-internal "EMACSDIR"))))
|
|
|
|
(when initdir
|
|
|
|
;; Discard the switch to prevent "invalid option" errors later.
|
|
|
|
(add-to-list 'command-switch-alist (cons "--init-directory" (lambda (_) (pop argv))))
|
|
|
|
(setq user-emacs-directory initdir)))
|
2019-09-06 15:40:38 -04:00
|
|
|
|
2022-07-27 12:05:56 +02:00
|
|
|
(let ((profile (or (cadr (member "--profile" command-line-args))
|
|
|
|
(getenv-internal "DOOMPROFILE"))))
|
|
|
|
(when profile
|
|
|
|
;; Discard the switch to prevent "invalid option" errors later.
|
|
|
|
(add-to-list 'command-switch-alist (cons "--profile" (lambda (_) (pop argv))))
|
|
|
|
;; Then process the requested profile, which should set
|
|
|
|
;; `user-emacs-directory'. If it doesn't, then you're essentially using
|
|
|
|
;; profiles.el as a glorified, runtime dir-locals.el -- which is fine.
|
|
|
|
(let ((profiles-dir (or (getenv-internal "DOOMPROFILESDIR")
|
|
|
|
(expand-file-name "profiles/" user-emacs-directory)))
|
|
|
|
(profiles-file (expand-file-name "profiles.el" user-emacs-directory)))
|
|
|
|
(if (file-exists-p profiles-file)
|
|
|
|
(with-temp-buffer
|
|
|
|
(let ((coding-system-for-read 'utf-8-auto))
|
|
|
|
(insert-file-contents profiles-file))
|
|
|
|
(condition-case err
|
|
|
|
(dolist (var (or (cdr (assq (intern profile) (read (current-buffer))))
|
|
|
|
(user-error "No %S profile found" profile)))
|
|
|
|
(if (eq var 'env)
|
|
|
|
(dolist (env var) (setenv (car env) (cdr env)))
|
|
|
|
(set (car var) (cdr var))))
|
|
|
|
(error (error "Failed to parse profiles.el: %s" (error-message-string err)))))
|
|
|
|
;; If the profile doesn't exist, then use anything in
|
|
|
|
;; $EMACSDIR/profiles/* as implicit profiles. I.e. If a foo profile
|
|
|
|
;; doesn't exist, `emacs --profile foo' is equivalent to `emacs
|
|
|
|
;; --init-directory $EMACSDIR/profile/foo', if the directory exists.
|
|
|
|
(let ((profile-dir (expand-file-name profile profiles-dir)))
|
|
|
|
(if (file-directory-p profile-dir)
|
|
|
|
(setq user-emacs-directory profile-dir)
|
|
|
|
(user-error "No profiles.el to look up %S in" profile))))))))
|
2022-06-18 15:04:12 +02:00
|
|
|
|
2022-07-27 11:15:30 +02:00
|
|
|
|
2022-07-27 12:05:56 +02:00
|
|
|
;;
|
|
|
|
;;; Bootstrap
|
2022-07-27 11:15:30 +02:00
|
|
|
|
2022-07-27 12:05:56 +02:00
|
|
|
;; Let er rip
|
|
|
|
(let (init-file)
|
2022-07-27 11:15:30 +02:00
|
|
|
;; 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.
|
2022-07-27 12:05:56 +02:00
|
|
|
(setq init-file (expand-file-name "core-start" doom-core-dir))
|
|
|
|
;; ...but if that fails, then this is likely not a Doom config.
|
2022-07-27 11:15:30 +02:00
|
|
|
(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).
|
2022-07-27 12:05:56 +02:00
|
|
|
;; - Later, 'doom sync' will dynamically generate its bootstrap file, which
|
|
|
|
;; will be important for Doom's profile system later. 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.
|
2022-07-27 11:15:30 +02:00
|
|
|
;; - 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 ()
|
2022-07-27 12:05:56 +02:00
|
|
|
(or init-file
|
2022-07-27 11:15:30 +02:00
|
|
|
(expand-file-name "init.el" user-emacs-directory)))
|
|
|
|
nil ; TODO Replace with safe mode initfile
|
|
|
|
(caddr args))))
|
2022-06-18 15:04:12 +02:00
|
|
|
|
|
|
|
;;; early-init.el ends here
|