From 01d1a814f9cbcb3556e1a195eaa63faa9c3c54c7 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sat, 9 Jun 2018 20:13:20 +0200 Subject: [PATCH] Refactor doom-initialize functions Removes doom-module-table; which was inflexible (though more stable). It prevented you from putting your doom! block in anywhere but ~/.doom.d/init.el. It is replaced (somewhat) by (doom-modules). --- bin/doom-doctor | 2 - core/autoload/debug.el | 4 +- core/autoload/modules.el | 30 ++++++------- core/core-packages.el | 96 ++++++++++++++++++---------------------- core/core.el | 8 ++-- 5 files changed, 61 insertions(+), 79 deletions(-) diff --git a/bin/doom-doctor b/bin/doom-doctor index 2f4128033..c3ad96a7a 100755 --- a/bin/doom-doctor +++ b/bin/doom-doctor @@ -280,8 +280,6 @@ (let ((inhibit-message t) noninteractive) (load (concat user-emacs-directory "core/core.el") nil t) - (doom-initialize t) - (doom-initialize-modules t) (success! "Attempt to load DOOM: success! Loaded v%s" doom-version)) ('error (warn! "Attempt to load DOOM: failed\n %s\n" diff --git a/core/autoload/debug.el b/core/autoload/debug.el index 2662128e5..cf1ba243d 100644 --- a/core/autoload/debug.el +++ b/core/autoload/debug.el @@ -10,10 +10,8 @@ (defun doom-info () "Returns diagnostic information about the current Emacs session in markdown, ready to be pasted in a bug report on github." - (doom-initialize) (require 'vc-git) - (let ((default-directory doom-emacs-dir) - (doom-modules (doom-module-table))) + (let ((default-directory doom-emacs-dir)) (format (concat "- OS: %s (%s)\n" "- Emacs: %s (%s)\n" diff --git a/core/autoload/modules.el b/core/autoload/modules.el index 53518580c..cca13f9af 100644 --- a/core/autoload/modules.el +++ b/core/autoload/modules.el @@ -30,8 +30,9 @@ init.el and config.el. Then runs `doom-reload-hook'." (red "There were issues!"))))) ((let ((load-prefer-newer t)) (doom//reload-autoloads force-p) - (doom-initialize t) - (ignore-errors (doom-initialize-modules t)) + (doom-initialize 'force) + (with-demoted-errors "PRIVATE CONFIG ERROR: %s" + (doom-initialize-modules 'force)) (print! (green "%d packages reloaded" (length package-alist))) (run-hooks 'doom-reload-hook) t)))) @@ -105,8 +106,7 @@ Emacs where to find lazy-loaded functions. This should be run whenever your `doom!' block, or a module autoload file, is modified." (interactive) - (let ((doom-modules (doom-module-table)) - (default-directory doom-emacs-dir) + (let ((default-directory doom-emacs-dir) (targets (file-expand-wildcards (expand-file-name "autoload/*.el" doom-core-dir)))) @@ -203,7 +203,7 @@ This should be run whenever your `doom!' block or update your packages." (file-exists-p doom-package-autoload-file) (not (file-newer-than-file-p package-user-dir doom-package-autoload-file)) (not (ignore-errors - (cl-loop for key being the hash-keys of (doom-module-table) + (cl-loop for key being the hash-keys of (doom-modules) for path = (doom-module-path (car key) (cdr key) "packages.el") if (file-newer-than-file-p path doom-package-autoload-file) return t)))) @@ -317,19 +317,17 @@ If RECOMPILE-P is non-nil, only recompile out-of-date files." (message "Aborting.") (cl-return-from 'byte-compile))) (unless recompile-p - (doom//clean-byte-compiled-files)) - (unless targets - (let ((inhibit-message t) - doom-emacs-changed-p - noninteractive) - ;; But first we must be sure that Doom and your private config have - ;; been fully loaded. Which usually aren't so in an noninteractive - ;; session. - (quiet! (doom//reload-autoloads)) - (doom-initialize t))) + (doom//clean-byte-compiled-files) + (doom//reload-autoloads)) + (let (doom-emacs-changed-p + noninteractive) + ;; But first we must be sure that Doom and your private config have + ;; been fully loaded. Which usually aren't so in an noninteractive + ;; session. + (doom-initialize) + (doom-initialize-modules 'force)) ;; If no targets were supplied, then we use your module list. (unless modules - (doom-initialize-modules t) (setq targets (append (list doom-core-dir) (doom-module-load-path)))) ;; Assemble el files we want to compile; taking into account that diff --git a/core/core-packages.el b/core/core-packages.el index 217eb6e52..090e45d95 100644 --- a/core/core-packages.el +++ b/core/core-packages.el @@ -282,9 +282,10 @@ to least)." ;; Loads `doom-package-autoload-file', which caches `load-path', ;; `auto-mode-alist', `Info-directory-list', `doom-disabled-packages' and ;; `package-activated-list'. A big reduction in startup time. - (unless (doom-initialize-autoloads doom-package-autoload-file force-p) - (unless (or force-p noninteractive) - (doom//reload-package-autoloads)))) + (unless (or force-p + (doom-initialize-autoloads doom-package-autoload-file) + noninteractive) + (doom//reload-package-autoloads))) ;; Initialize Doom core (unless noninteractive (add-hook! 'emacs-startup-hook @@ -307,9 +308,8 @@ non-nil." (load (expand-file-name "init" doom-private-dir) 'noerror 'nomessage)))) -(defun doom-initialize-autoloads (file &optional clear-p) - "Tries to load FILE (an autoloads file). Otherwise tries to regenerate it. If -CLEAR-P is non-nil, regenerate it anyway." +(defun doom-initialize-autoloads (file) + "Tries to load FILE (an autoloads file)." (unless clear-p (condition-case-unless-debug e (load (file-name-sans-extension file) 'noerror 'nomessage) @@ -339,7 +339,7 @@ them." ;; `package-alist' (when (or force-p (not (bound-and-true-p package-alist))) (setq load-path doom-site-load-path) - (doom-ensure-packages-initialized t)) + (doom-ensure-packages-initialized 'force)) ;; `quelpa-cache' (when (or force-p (not (bound-and-true-p quelpa-cache))) @@ -351,7 +351,7 @@ them." (error "Could not initialize quelpa")))) (when (or force-p (not doom-packages)) - (let ((doom-modules (doom-module-table))) + (let ((doom-modules (doom-modules))) (setq doom-packages nil) (cl-flet ((_load @@ -370,11 +370,12 @@ them." ;; We load the private packages file twice to ensure disabled ;; packages are seen ASAP, and a second time to ensure privately ;; overridden packages are properly overwritten. - (_load (expand-file-name "packages.el" doom-private-dir) t) - (cl-loop for key being the hash-keys of doom-modules - for path = (doom-module-path (car key) (cdr key) "packages.el") - do (let ((doom--current-module key)) (_load path t))) - (_load (expand-file-name "packages.el" doom-private-dir) t)))))))) + (let ((private-packages (expand-file-name "packages.el" doom-private-dir))) + (_load private-packages t) + (cl-loop for key being the hash-keys of doom-modules + for path = (doom-module-path (car key) (cdr key) "packages.el") + do (let ((doom--current-module key)) (_load path t))) + (_load private-packages t))))))))) ;; @@ -468,42 +469,16 @@ This doesn't require modules to be enabled. For enabled modules us (intern submodule)))))))) (defun doom-module-load-path () - "Returns a list of absolute file paths to activated modules." - (append (cl-loop for plist being the hash-values of doom-modules + "Return a list of absolute file paths to activated modules." + (append (cl-loop for plist being the hash-values of (doom-modules) collect (plist-get plist :path)) (list doom-private-dir))) -(defun doom-module-table (&optional modules) - "Converts MODULES (a malformed plist) into a hash table of modules, fit for -`doom-modules'. If MODULES is omitted, it will fetch your module mplist from the -`doom!' block in your private init.el file." - (let* ((doom-modules (make-hash-table :test #'equal - :size (if modules (length modules) 100) - :rehash-threshold 1.0))) - (when (null modules) - (let* ((init-file (expand-file-name "init.el" doom-private-dir)) - (short-init-file (abbreviate-file-name init-file))) - (if (not (file-exists-p init-file)) - (error "%s doesn't exist" short-init-file) - (with-temp-buffer - (delay-mode-hooks (emacs-lisp-mode)) - (insert-file-contents-literally init-file) - (when (re-search-forward "^\\s-*\\((doom! \\)" nil t) - (goto-char (match-beginning 1)) - (setq modules (cdr (sexp-at-point)))))) - (unless (or modules noninteractive) - (warn "Couldn't gather module list from %s" short-init-file)))) - (if (eq modules t) (setq modules nil)) - (let (category) - (dolist (m modules) - (cond ((keywordp m) (setq category m)) - ((not category) (error "No module category specified for %s" m)) - ((let ((module (if (listp m) (car m) m)) - (flags (if (listp m) (cdr m)))) - (if-let* ((path (doom-module-locate-path category module))) - (doom-module-set category module :flags flags :path path) - (message "Couldn't find the %s %s module" category module))))))) - doom-modules)) +(defun doom-modules (&optional refresh-p) + "Minimally initialize `doom-modules' (a hash table) and return it." + (let ((noninteractive t)) + (doom-initialize-modules refresh-p)) + doom-modules) ;; @@ -588,15 +563,28 @@ The overall load order of Doom is as follows: Module load order is determined by your `doom!' block. See `doom-modules-dirs' for a list of all recognized module trees. Order defines precedence (from most to least)." - (let ((doom-modules (doom-module-table (or modules t))) + (let ((doom-modules + (make-hash-table :test #'equal + :size (if modules (length modules) 100) + :rehash-threshold 1.0)) + category init-forms config-forms) - (maphash (lambda (key plist) - (let ((path (plist-get plist :path))) - (push `(let ((doom--current-module ',key)) (load! "init" ,path t)) - init-forms) - (push `(let ((doom--current-module ',key)) (load! "config" ,path t)) - config-forms))) - doom-modules) + (dolist (m modules) + (cond ((keywordp m) (setq category m)) + ((not category) (error "No module category specified for %s" m)) + ((let* ((module (if (listp m) (car m) m)) + (flags (if (listp m) (cdr m))) + (path (doom-module-locate-path category module))) + (if (not path) + (message "Couldn't find the %s %s module" category module) + (let ((key (cons category module))) + (doom-module-set category module :flags flags :path path) + (push `(let ((doom--current-module ',key)) + (load! "init" ,path t)) + init-forms) + (push `(let ((doom--current-module ',key)) + (load! "config" ,path t)) + config-forms))))))) `(let (file-name-handler-alist) (setq doom-modules ',doom-modules) ,@(nreverse init-forms) diff --git a/core/core.el b/core/core.el index 2ecb8edbe..01fc2812c 100644 --- a/core/core.el +++ b/core/core.el @@ -120,7 +120,6 @@ else (except for `window-setup-hook').") tramp-persistency-file-name (concat doom-cache-dir "tramp-persistency.el") url-cache-directory (concat doom-cache-dir "url/") url-configuration-directory (concat doom-etc-dir "url/")) -(load custom-file t t t) ;; @@ -201,11 +200,12 @@ this, you'll get stuttering and random freezes) and resets (require 'core-lib) (require 'core-packages) +(when noninteractive + (require 'core-dispatcher)) +(load custom-file t t t) (doom-initialize noninteractive) -(if noninteractive - (require 'core-dispatcher) - (doom-initialize-modules)) +(doom-initialize-modules) (provide 'core) ;;; core.el ends here