Optimize core-lib & pure module functions

And confer module membership check to run-time, rather than compile
time.
This commit is contained in:
Henrik Lissner 2018-06-24 19:54:50 +02:00
parent d874f628bb
commit e91af20003
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 25 additions and 16 deletions

View file

@ -51,6 +51,7 @@ Returns
(file-exists-p \"/an/absolute/path\")))) (file-exists-p \"/an/absolute/path\"))))
This is used by `associate!', `file-exists-p!' and `project-file-exists-p!'." This is used by `associate!', `file-exists-p!' and `project-file-exists-p!'."
(declare (pure t) (side-effect-free t))
(cond ((stringp spec) (cond ((stringp spec)
`(file-exists-p `(file-exists-p
,(if (file-name-absolute-p spec) ,(if (file-name-absolute-p spec)
@ -72,6 +73,7 @@ This is used by `associate!', `file-exists-p!' and `project-file-exists-p!'."
(t spec))) (t spec)))
(defun doom--resolve-hook-forms (hooks) (defun doom--resolve-hook-forms (hooks)
(declare (pure t) (side-effect-free t))
(cl-loop with quoted-p = (eq (car-safe hooks) 'quote) (cl-loop with quoted-p = (eq (car-safe hooks) 'quote)
for hook in (doom-enlist (doom-unquote hooks)) for hook in (doom-enlist (doom-unquote hooks))
if (eq (car-safe hook) 'quote) if (eq (car-safe hook) 'quote)
@ -97,24 +99,26 @@ This is used by `associate!', `file-exists-p!' and `project-file-exists-p!'."
(defun doom-unquote (exp) (defun doom-unquote (exp)
"Return EXP unquoted." "Return EXP unquoted."
(declare (pure t) (side-effect-free t))
(while (memq (car-safe exp) '(quote function)) (while (memq (car-safe exp) '(quote function))
(setq exp (cadr exp))) (setq exp (cadr exp)))
exp) exp)
(defun doom-enlist (exp) (defun doom-enlist (exp)
"Return EXP wrapped in a list, or as-is if already a list." "Return EXP wrapped in a list, or as-is if already a list."
(declare (pure t) (side-effect-free t))
(if (listp exp) exp (list exp))) (if (listp exp) exp (list exp)))
(defun doom-keyword-intern (str) (defun doom-keyword-intern (str)
"Converts STR (a string) into a keyword (`keywordp')." "Converts STR (a string) into a keyword (`keywordp')."
(or (stringp str) (declare (pure t) (side-effect-free t))
(signal 'wrong-type-argument (list 'stringp str))) (cl-check-type str string)
(intern (concat ":" str))) (intern (concat ":" str)))
(defun doom-keyword-name (keyword) (defun doom-keyword-name (keyword)
"Returns the string name of KEYWORD (`keywordp') minus the leading colon." "Returns the string name of KEYWORD (`keywordp') minus the leading colon."
(or (keywordp keyword) (declare (pure t) (side-effect-free t))
(signal 'wrong-type-argument (list 'keywordp keyword))) (cl-check-type :test keyword)
(substring (symbol-name keyword) 1)) (substring (symbol-name keyword) 1))
(cl-defun doom-files-in (cl-defun doom-files-in
@ -199,17 +203,17 @@ MATCH is a string regexp. Only entries that match it will be included."
;; Macros ;; Macros
;; ;;
(defmacro FILE! () (defun FILE! ()
"Return the emacs lisp file this macro is called from." "Return the emacs lisp file this macro is called from."
`(cond ((bound-and-true-p byte-compile-current-file)) (cond ((bound-and-true-p byte-compile-current-file))
((stringp (car-safe current-load-list)) (car current-load-list)) (load-file-name)
(load-file-name) (buffer-file-name)
(buffer-file-name))) ((stringp (car-safe current-load-list)) (car current-load-list))))
(defmacro DIR! () (defun DIR! ()
"Returns the directory of the emacs lisp file this macro is called from." "Returns the directory of the emacs lisp file this macro is called from."
`(let ((file (FILE!))) (let ((file (FILE!)))
(and file (file-name-directory file)))) (and file (file-name-directory file))))
(defmacro λ! (&rest body) (defmacro λ! (&rest body)
"A shortcut for inline interactive lambdas." "A shortcut for inline interactive lambdas."

View file

@ -51,12 +51,14 @@ non-nil."
(defun doom-module-p (category module) (defun doom-module-p (category module)
"Returns t if CATEGORY MODULE is enabled (ie. present in `doom-modules')." "Returns t if CATEGORY MODULE is enabled (ie. present in `doom-modules')."
(declare (pure t) (side-effect-free t))
(and (hash-table-p doom-modules) (and (hash-table-p doom-modules)
(gethash (cons category module) doom-modules) (gethash (cons category module) doom-modules)
t)) t))
(defun doom-module-get (category module &optional property) (defun doom-module-get (category module &optional property)
"Returns the plist for CATEGORY MODULE. Gets PROPERTY, specifically, if set." "Returns the plist for CATEGORY MODULE. Gets PROPERTY, specifically, if set."
(declare (pure t) (side-effect-free t))
(when-let* ((plist (gethash (cons category module) doom-modules))) (when-let* ((plist (gethash (cons category module) doom-modules)))
(if property (if property
(plist-get plist property) (plist-get plist property)
@ -102,7 +104,8 @@ MODULE (symbol).
If the category isn't enabled this will always return nil. For finding disabled If the category isn't enabled this will always return nil. For finding disabled
modules use `doom-module-locate-path'." modules use `doom-module-locate-path'."
(let ((path (doom-module-get category module :path))) (let ((path (doom-module-get category module :path))
file-name-handler-alist)
(if file (expand-file-name file path) (if file (expand-file-name file path)
path))) path)))
@ -119,7 +122,8 @@ This doesn't require modules to be enabled. For enabled modules us
(setq category (substring (symbol-name category) 1))) (setq category (substring (symbol-name category) 1)))
(when (and module (symbolp module)) (when (and module (symbolp module))
(setq module (symbol-name module))) (setq module (symbol-name module)))
(cl-loop for default-directory in doom-modules-dirs (cl-loop with file-name-handler-alist = nil
for default-directory in doom-modules-dirs
for path = (concat category "/" module "/" file) for path = (concat category "/" module "/" file)
if (file-exists-p path) if (file-exists-p path)
return (expand-file-name path))) return (expand-file-name path)))
@ -139,6 +143,7 @@ This doesn't require modules to be enabled. For enabled modules us
(defun doom-module-load-path (&optional all-p) (defun doom-module-load-path (&optional all-p)
"Return a list of absolute file paths to activated modules. If ALL-P is "Return a list of absolute file paths to activated modules. If ALL-P is
non-nil, return paths of possible modules, activated or otherwise." non-nil, return paths of possible modules, activated or otherwise."
(declare (pure t) (side-effect-free t))
(append (if all-p (append (if all-p
(doom-files-in doom-modules-dirs (doom-files-in doom-modules-dirs
:type 'dirs :type 'dirs
@ -370,8 +375,8 @@ omitted. eg. (featurep! +flag1)"
module (car module-pair) module (car module-pair)
submodule (cdr module-pair)))) submodule (cdr module-pair))))
(if flag (if flag
(and (memq flag (doom-module-get module submodule :flags)) t) `(and (memq ',flag (doom-module-get ,module ',submodule :flags)) t)
(doom-module-p module submodule))) `(doom-module-p ,module ',submodule)))
;; ;;