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