Refactor startup process, hooks, doom-initialize & doom!

+ Brings back doom-pre-init-hook and doom-post-init-hook hooks.
+ Extracts autoload file loading logic into doom-initialize-autoloads
  function.
This commit is contained in:
Henrik Lissner 2018-05-19 23:58:14 +02:00
parent 17d5721102
commit 6f5e710d98
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 60 additions and 30 deletions

View file

@ -78,9 +78,6 @@ missing) and shouldn't be deleted.")
everyone in the universe and their dog, causing errors that make babies cry. No
one wants that.")
(defvar doom-reload-hook nil
"A list of hooks to run when `doom/reload-load-path' is called.")
(defvar doom-site-load-path load-path
"The starting load-path, before it is altered by `doom-initialize'.")
@ -91,6 +88,9 @@ one wants that.")
"Where to cache `load-path', `Info-directory-list', `doom-disabled-packages'
and `auto-mode-alist'.")
(defvar doom-reload-hook nil
"A list of hooks to run when `doom//reload-load-path' is called.")
(defvar doom--current-module nil)
(defvar doom--refreshed-p nil)
(defvar doom--stage 'init)
@ -181,8 +181,18 @@ If RETURN-P, return the message as a string instead of displaying it."
(or doom-init-time
(setq doom-init-time (float-time (time-subtract (current-time) before-init-time))))))
(add-hook 'emacs-startup-hook #'doom|display-benchmark)
(add-hook 'doom-reload-hook #'doom|display-benchmark)
(defun doom|post-init ()
"Run `doom-post-init-hook'. That's all."
(run-hooks 'doom-post-init-hook))
(defun doom|run-all-startup-hooks ()
"Run all startup Emacs hooks. Meant to follow running Emacs in a vanilla
session, with a different init.el, like so:
emacs -Q -l init.el -f doom|run-all-startup-hooks"
(run-hooks 'after-init-hook 'delayed-warnings-hook
'emacs-startup-hook 'term-setup-hook
'window-setup-hook))
;;
@ -232,34 +242,41 @@ FORCE-P is non-nil, do it anyway.
(if (package-installed-p package)
(message "✓ Installed %s" package)
(error "✕ Couldn't install %s" package)))
(message "Installing core packages...done"))))
;; autoloads file
(condition-case-unless-debug e
(unless
(let (byte-compile-warnings)
(load (substring doom-autoload-file 0 -3) 'noerror 'nomessage))
(error "No autoloads file! Run make autoloads"))
(error
(funcall (if noninteractive #'warn #'error)
"Autoload file error: %s -> %s" (car e) (error-message-string e))))
(add-to-list 'load-path doom-core-dir))
;; initialize Doom core
(require 'core-os)
(message "Installing core packages...done")))
(unless noninteractive
(add-hook 'doom-pre-init-hook #'doom|refresh-cache)))
;; autoloads file
(doom-initialize-autoloads))
;; initialize Doom core
(unless noninteractive
(unless doom-init-p
;; Cache important packages.el state
(doom|refresh-cache))
(require 'core-ui)
(require 'core-editor)
(require 'core-projects)
(require 'core-keybinds))
;; bootstrap Doom
(unless doom-init-p
(unless noninteractive
(add-hook! 'doom-reload-hook
#'(doom|refresh-cache doom|display-benchmark))
(add-hook! 'emacs-startup-hook
#'(doom|post-init doom|display-benchmark)))
(run-hooks 'doom-pre-init-hook)
(when doom-private-dir
(load (concat doom-private-dir "init") t t)))
(setq doom-init-p t))
(defun doom-initialize-autoloads ()
"Ensures that `doom-autoload-file' exists and is loaded. Otherwise run
`doom//reload-autoloads' to generate it. Used from Doom's Makefile."
(unless (file-exists-p doom-autoload-file)
(quiet! (doom//reload-autoloads))))
"Tries to load `doom-autoload-file', otherwise throws an error (unless in a
noninteractive session)."
(unless
(condition-case-unless-debug e
(load (substring doom-autoload-file 0 -3) 'noerror 'nomessage)
(error
(funcall (if noninteractive #'warn #'error)
"Autoload error: %s -> %s"
(car e) (error-message-string e))))
(unless noninteractive
(error "No autoloads file! Run make autoloads"))))
(defun doom-initialize-packages (&optional force-p)
"Ensures that `doom-packages', `packages-alist' and `quelpa-cache' are
@ -448,11 +465,13 @@ MODULES is an malformed plist of modules to load."
`(let (file-name-handler-alist)
(setq doom-modules ',doom-modules)
,@(nreverse init-forms)
(run-hooks 'doom-init-hook)
(unless noninteractive
(let ((doom--stage 'config))
,@(nreverse config-forms)
(when doom-private-dir
(load ,(concat doom-private-dir "config") t t)))))))
(load ,(concat doom-private-dir "config")
t (not doom-debug-mode))))))))
(defmacro def-package! (name &rest plist)
"A thin wrapper around `use-package'."

View file

@ -107,8 +107,18 @@ XDG directory conventions if ~/.config/doom exists.")
;; Custom init hooks; clearer than `after-init-hook', `emacs-startup-hook', and
;; `window-setup-hook'.
(defvar doom-pre-init-hook nil
"Hooks run after Doom is first initialized; after Doom's core files are
loaded, but before your private init.el file or anything else is loaded.")
(defvar doom-init-hook nil
"A list of hooks run when DOOM is initialized.")
"Hooks run after all init.el files are loaded, including your private and all
module init.el files, but before their config.el files are loaded.")
(defvar doom-post-init-hook nil
"A list of hooks run when Doom is fully initialized. Fires at the end of
`emacs-startup-hook', as late as possible. Guaranteed to run after everything
else (except for `window-setup-hook').")
;;
@ -156,7 +166,7 @@ with functions that require it (like modeline segments)."
;;
;; Bootstrap
;; Optimize startup
;;
(defvar doom--file-name-handler-alist file-name-handler-alist)
@ -173,11 +183,12 @@ with functions that require it (like modeline segments)."
"Resets garbage collection settings to reasonable defaults (if you don't do
this, you'll get stuttering and random freezes) and resets
`file-name-handler-alist'."
(unless noninteractive
(run-hooks 'doom-init-hook))
(setq file-name-handler-alist doom--file-name-handler-alist
gc-cons-threshold 16777216
gc-cons-percentage 0.15))
gc-cons-threshold 8388608
gc-cons-percentage 0.1))
(add-hook 'emacs-startup-hook #'doom|finalize)
(add-hook 'doom-reload-hook #'doom|finalize)
;;