From 781783fe85e887968774734afd7df5d6f4a4924f Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Mon, 22 Jul 2019 19:04:03 +0200 Subject: [PATCH] Refactor doom-module-from-path Now returns (:core) or (:private) for files in doom-core-dir and doom-private-dir, respectively, and will otherwise return the module for the current file if called with no arguments. Also fixes the case where it would disregard arguments and return whatever the current value of doom--current-module. --- core/core-modules.el | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/core/core-modules.el b/core/core-modules.el index b849d31b5..f13f204e5 100644 --- a/core/core-modules.el +++ b/core/core-modules.el @@ -178,16 +178,27 @@ This doesn't require modules to be enabled. For enabled modules us (defun doom-module-from-path (&optional path enabled-only) "Returns a cons cell (CATEGORY . MODULE) derived from PATH (a file path). If ENABLED-ONLY, return nil if the containing module isn't enabled." - (or doom--current-module - (let* ((file-name-handler-alist nil) - (path (file-truename (or path (file!))))) - (save-match-data - (when (string-match "/modules/\\([^/]+\\)/\\([^/]+\\)\\(?:/.*\\)?$" path) - (when-let* ((category (doom-keyword-intern (match-string 1 path))) - (module (intern (match-string 2 path)))) - (and (or (null enabled-only) - (doom-module-p category module)) - (cons category module)))))))) + (if (null path) + (if doom--current-module + (if enabled-only + (and (doom-module-p (car doom--current-module) + (cdr doom--current-module)) + doom--current-module) + doom--current-module) + (doom-module-from-path (file!))) + (let* ((file-name-handler-alist nil) + (path (file-truename (or path (file!))))) + (save-match-data + (cond ((string-match "/modules/\\([^/]+\\)/\\([^/]+\\)\\(?:/.*\\)?$" path) + (when-let* ((category (doom-keyword-intern (match-string 1 path))) + (module (intern (match-string 2 path)))) + (and (or (null enabled-only) + (doom-module-p category module)) + (cons category module)))) + ((file-in-directory-p path doom-core-dir) + (cons :core (intern (file-name-base path)))) + ((file-in-directory-p path doom-private-dir) + (cons :private (intern (file-name-base path))))))))) (defun doom-module-load-path (&optional module-dirs) "Return a list of file paths to activated modules. @@ -498,12 +509,11 @@ CATEGORY and MODULE can be omitted When this macro is used from inside a module (and (cond (flag (memq flag (doom-module-get category module :flags))) (module (doom-module-p category module)) (doom--current-flags (memq category doom--current-flags)) - ((let ((module-pair - (or doom--current-module - (doom-module-from-path (file!))))) - (unless module-pair - (error "featurep! call couldn't auto-detect what module its in (from %s)" (file!))) - (memq category (doom-module-get (car module-pair) (cdr module-pair) :flags))))) + ((let ((module (doom-module-from-path))) + (unless module + (error "featurep! couldn't figure out what module it was called from (in %s)" + (file!))) + (memq category (doom-module-get (car module) (cdr module) :flags))))) t)) (defmacro after! (package &rest body)