fix: add :depth field to modules

This introduces a depth field for modules so that they may dictate their
load order explicitly, it also treats depths <= -100 or >= 100 as
special depths, which will be loaded early, before their respective
doom-{before,after}-module-{init,config}-hook. This permits psuedo
modules like :core and :user modules to be treated as normal modules
without too many special cases.

This also fixes a module load order issue on Emacs 29 (#6813), caused by
emacs-mirror/emacs@4311bd0bd7, which changed the return value order of
hash-table-{keys,values} causing modules to be loaded in reverse order;
resulting in the loss of evil keybinds, among other things.

Other notable changes:
- Changes the data structure for module data caches from a list to a
  vector. Uses less memory and permits faster lookups. Also adds two
  depth fields to the front of it.
- Changes the signature of doom-module-list and doom-package-list.
- Renames doom--read-packages -> doom-packages--read for consistency
  with naming convention.
- Add doom-module-depth function.
- Adds a temporary doom-core-dir/init.el file, which is responsible for
  loading doom-*.el.

Fix: #6813
Ref: emacs-mirror/emacs@4311bd0bd7
This commit is contained in:
Henrik Lissner 2022-09-23 18:55:20 +02:00
parent 772f9f26d9
commit 5a5195b84d
No known key found for this signature in database
GPG key ID: B60957CA074D39A3
9 changed files with 250 additions and 178 deletions

View file

@ -386,44 +386,53 @@ Defaults to the profile at `doom-profile-default'."
branch ,(if (zerop (car branch)) (cdr branch))))))))
(defun doom-profile--generate-load-modules ()
(let ((module-list (cddr (doom-module-list))))
;; FIX: Same as above (see `doom-profile--generate-init-vars').
`((unless doom-init-time
(set 'doom-disabled-packages ',doom-disabled-packages)
(set 'doom-modules ',doom-modules)
;; Cache module state and flags in symbol plists for quick lookup by
;; `modulep!' later.
,@(cl-loop for (category . modules) in (seq-group-by #'car (doom-module-list))
collect `(setplist ',category
(quote ,(cl-loop for (_ . module) in modules
nconc `(,module ,(get category module))))))
(doom-run-hooks 'doom-before-modules-init-hook)
;; TODO: Until these files are byte-compiler-ready, I must use `load'
;; instead of `require', as to not invite the byte-compiler to load them
;; while this init file is compiled.
(doom-load ,(doom-path doom-core-dir "doom-keybinds"))
(doom-load ,(doom-path doom-core-dir "doom-ui"))
(doom-load ,(doom-path doom-core-dir "doom-projects"))
(doom-load ,(doom-path doom-core-dir "doom-editor"))
,@(cl-loop for (cat . mod) in module-list
for dir = (doom-module-locate-path cat mod)
if (locate-file-internal doom-module-init-file (list dir) load-suffixes)
collect `(let ((doom--current-module '(,cat . ,mod))
(doom--current-flags ',(doom-module-get cat mod :flags)))
(doom-load ,it)))
(doom-run-hooks 'doom-after-modules-init-hook)
(doom-run-hooks 'doom-before-modules-config-hook)
,@(cl-loop for (cat . mod) in module-list
for dir = (doom-module-locate-path cat mod)
if (locate-file-internal doom-module-config-file (list dir) load-suffixes)
collect `(let ((doom--current-module '(,cat . ,mod))
(doom--current-flags ',(doom-module-get cat mod :flags)))
(doom-load ,it)))
(doom-run-hooks 'doom-after-modules-config-hook)
(let ((old-custom-file custom-file))
(doom-load ,(doom-path doom-user-dir doom-module-config-file) 'noerror)
(when (eq custom-file old-custom-file)
(doom-load custom-file 'noerror)))))))
(let* ((init-modules-list (doom-module-list nil t))
(config-modules-list (doom-module-list))
(pre-init-modules
(seq-filter (fn! (<= (doom-module-depth (car %) (cdr %) t) -100))
(remove '(:user) init-modules-list)))
(init-modules
(seq-filter (fn! (<= 0 (doom-module-depth (car %) (cdr %) t) 100))
init-modules-list))
(config-modules
(seq-filter (fn! (<= 0 (doom-module-depth (car %) (cdr %)) 100))
config-modules-list))
(post-config-modules
(seq-filter (fn! (>= (doom-module-depth (car %) (cdr %)) 100))
config-modules-list))
(init-file (concat doom-module-init-file ".el"))
(config-file (concat doom-module-config-file ".el")))
(letf! ((defun module-loader (group name file &optional noerror)
`(let ((doom--current-module '(,group . ,name))
(doom--current-flags ',(doom-module-get group name :flags)))
(doom-load ,(abbreviate-file-name file))))
(defun module-list-loader (modules file &optional noerror)
(cl-loop for (cat . mod) in modules
if (doom-module-locate-path cat mod file)
collect (module-loader cat mod it noerror))))
;; FIX: Same as above (see `doom-profile--generate-init-vars').
`((unless doom-init-time
(set 'doom-modules ',doom-modules)
(set 'doom-disabled-packages ',doom-disabled-packages)
;; Cache module state and flags in symbol plists for quick lookup by
;; `modulep!' later.
,@(cl-loop
for (category . modules) in (seq-group-by #'car config-modules-list)
collect
`(setplist ',category
(quote ,(cl-loop for (_ . module) in modules
nconc `(,module ,(get category module))))))
(let ((old-custom-file custom-file))
,@(module-list-loader pre-init-modules init-file)
(doom-run-hooks 'doom-before-modules-init-hook)
,@(module-list-loader init-modules init-file)
(doom-run-hooks 'doom-after-modules-init-hook)
(doom-run-hooks 'doom-before-modules-config-hook)
,@(module-list-loader config-modules config-file)
(doom-run-hooks 'doom-after-modules-config-hook)
,@(module-list-loader post-config-modules config-file t)
(when (eq custom-file old-custom-file)
(doom-load custom-file 'noerror))))))))
(defun doom-profile--generate-doom-autoloads ()
(doom-autoloads--scan