From 6f5e710d981248d9ef85c91deafd1e745592dba6 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sat, 19 May 2018 23:58:14 +0200 Subject: [PATCH] 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. --- core/core-packages.el | 67 +++++++++++++++++++++++++++---------------- core/core.el | 23 +++++++++++---- 2 files changed, 60 insertions(+), 30 deletions(-) diff --git a/core/core-packages.el b/core/core-packages.el index 4508747f9..a06569712 100644 --- a/core/core-packages.el +++ b/core/core-packages.el @@ -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")))) + (message "Installing core packages...done"))) + (unless noninteractive + (add-hook 'doom-pre-init-hook #'doom|refresh-cache))) ;; 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)) + (doom-initialize-autoloads)) ;; initialize Doom core - (require 'core-os) (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'." diff --git a/core/core.el b/core/core.el index 2cea52f61..e9f68279f 100644 --- a/core/core.el +++ b/core/core.el @@ -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) ;;