Refactor & optimize doom! + module API

This commit is contained in:
Henrik Lissner 2018-09-25 23:52:20 -04:00
parent aa930256b8
commit 9afc61e57e
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -12,9 +12,9 @@
"A list of module root directories. Order determines priority.")
(defconst doom-obsolete-modules
'(((:tools rotate-text) (:editor rotate-text))
((:emacs electric-indent) (:emacs electric))
((:feature version-control) (:emacs vc) (:ui vc-gutter)))
'((:tools (rotate-text (:editor rotate-text)))
(:emacs (electric-indent (:emacs electric)))
(:feature (version-control (:emacs vc) (:ui vc-gutter))))
"An alist of deprecated modules, mapping deprecated modules to an optional new
location (which will create an alias). Each CAR and CDR is a (CATEGORY .
MODULES). E.g.
@ -80,16 +80,19 @@ non-nil."
(plist-get plist property)
plist)))
(defun doom-module-put (category module property value &rest rest)
(defun doom-module-put (category module &rest plist)
"Set a PROPERTY for CATEGORY MODULE to VALUE. PLIST should be additional pairs
of PROPERTY and VALUEs."
(when-let* ((plist (doom-module-get category module)))
(plist-put plist property value)
(when rest
(when (cl-oddp (length rest))
(signal 'wrong-number-of-arguments (list (length rest))))
(while rest
(plist-put rest (pop rest) (pop rest))))
of PROPERTY and VALUEs.
\(fn CATEGORY MODULE PROPERTY VALUE &rest [PROPERTY VALUE [...]])"
(if-let* ((old-plist (doom-module-get category module)))
(progn
(when plist
(when (cl-oddp (length plist))
(signal 'wrong-number-of-arguments (list (length plist))))
(while plist
(plist-put old-plist (pop plist) (pop plist))))
(puthash (cons category module) old-plist doom-modules))
(puthash (cons category module) plist doom-modules)))
(defun doom-module-set (category module &rest plist)
@ -103,13 +106,6 @@ following properties:
Example:
(doom-module-set :lang 'haskell :flags '(+intero))"
(when plist
(let ((old-plist (doom-module-get category module)))
(unless (plist-member plist :flags)
(plist-put plist :flags (plist-get old-plist :flags)))
(unless (plist-member plist :path)
(plist-put plist :path (or (plist-get old-plist :path)
(doom-module-locate-path category module))))))
(puthash (cons category module)
plist
doom-modules))
@ -135,7 +131,7 @@ returns nil, otherwise an absolute path.
This doesn't require modules to be enabled. For enabled modules us
`doom-module-path'."
(when (keywordp category)
(setq category (substring (symbol-name category) 1)))
(setq category (doom-keyword-name category)))
(when (and module (symbolp module))
(setq module (symbol-name module)))
(cl-loop with file-name-handler-alist = nil
@ -147,7 +143,8 @@ This doesn't require modules to be enabled. For enabled modules us
(defun doom-module-from-path (&optional path)
"Returns a cons cell (CATEGORY . MODULE) derived from PATH (a file path)."
(or doom--current-module
(let ((path (or path (FILE!))))
(let* (file-name-handler-alist
(path (or path (FILE!))))
(save-match-data
(setq path (file-truename path))
(when (string-match "/modules/\\([^/]+\\)/\\([^/]+\\)\\(?:/.*\\)?$" path)
@ -174,14 +171,14 @@ non-nil, return paths of possible modules, activated or otherwise."
"Minimally initialize `doom-modules' (a hash table) and return it."
(or (unless refresh-p doom-modules)
(let ((noninteractive t)
(doom-modules
(make-hash-table :test 'equal
:size 20
:rehash-threshold 1.0))
doom-modules
doom-init-modules-p)
(message "Initializing modules")
(load! "init" doom-private-dir t)
doom-modules)))
(or doom-modules
(make-hash-table :test 'equal
:size 20
:rehash-threshold 1.0)))))
;;
@ -288,7 +285,7 @@ to least)."
(unless doom-modules
(setq doom-modules
(make-hash-table :test 'equal
:size (if modules (length modules) 100)
:size (if modules (length modules) 150)
:rehash-threshold 1.0)))
(let (category m)
(while modules
@ -298,7 +295,8 @@ to least)."
((catch 'doom-modules
(let* ((module (if (listp m) (car m) m))
(flags (if (listp m) (cdr m))))
(when-let* ((new (assoc (list category module) doom-obsolete-modules)))
(when-let* ((obsolete (assq category doom-obsolete-modules))
(new (assq module obsolete)))
(let ((newkeys (cdr new)))
(if (null newkeys)
(message "Warning: the %s module is deprecated" key)
@ -310,8 +308,8 @@ to least)."
(throw 'doom-modules t))))
(if-let* ((path (doom-module-locate-path category module)))
(doom-module-set category module :flags flags :path path)
(message "Warning: couldn't find the %s %s module" category module)))))))
`(setq doom-modules ',doom-modules)))
(message "Warning: couldn't find the %s %s module" category module))))))))
`(setq doom-modules ',doom-modules))
(defvar doom-disabled-packages)
(defmacro def-package! (name &rest plist)
@ -349,7 +347,16 @@ to have them return non-nil (or exploit that to overwrite Doom's config)."
(defmacro require! (category module &rest plist)
"Loads the module specified by CATEGORY (a keyword) and MODULE (a symbol)."
`(let ((module-path (doom-module-locate-path ,category ',module)))
(doom-module-set ,category ',module ,@plist)
(doom-module-set
,category ',module
,@(when plist
(let ((old-plist (doom-module-get category module)))
(unless (plist-member plist :flags)
(plist-put plist :flags (plist-get old-plist :flags)))
(unless (plist-member plist :path)
(plist-put plist :path (or (plist-get old-plist :path)
(doom-module-locate-path category module)))))
plist))
(if (directory-name-p module-path)
(condition-case-unless-debug ex
(let ((doom--current-module ',(cons category module)))