branch: externals/modus-themes
commit 9c65704b61e9ed4c94c8860f48f57ac3494759f1
Author: Protesilaos Stavrou <[email protected]>
Commit: Protesilaos Stavrou <[email protected]>

    Avoid recursion in modus-themes-with-colors through enable-theme-functions
    
    The modus-themes-with-colors should either work on an already loaded
    theme or do nothing. Otherwise with the enable-theme-functions we end
    up with excessive lisp nesting.
    
    Thanks to Alexandr Semenov for reporting the bug in issue 170:
    <https://github.com/protesilaos/modus-themes/issues/170>.
---
 modus-themes.el | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/modus-themes.el b/modus-themes.el
index 951116472c..bb25cf1efe 100644
--- a/modus-themes.el
+++ b/modus-themes.el
@@ -3779,7 +3779,7 @@ Also see `modus-themes-get-themes'.")
   ;; `custom-known-themes' because loading the theme has the desired
   ;; side effect of adding the relevant `theme-properties' to it.
   (unless (and (memq theme modus-themes--activated-themes)
-              (custom-theme-p theme))
+               (custom-theme-p theme))
     (load-theme theme t t)
     (add-to-list 'modus-themes--activated-themes theme)))
 
@@ -3789,18 +3789,21 @@ Also see `modus-themes-get-themes'.")
               (theme-family (plist-get properties :family)))
     (eq theme-family family)))
 
-(defun modus-themes-get-all-known-themes (&optional theme-family)
+(defun modus-themes-get-all-known-themes (&optional theme-family no-enable)
   "Return all known Modus themes or derivatives, enabling them if needed.
 With optional THEME-FAMILY, operate only on the themes whose :family
 property is that.  Else consider the Modus themes as well as all their
 derivatives.
 
+With optional NO-ENABLE, do not try to enable the themes.
+
 Also see `modus-themes-sort'."
   (let ((themes (pcase theme-family
                   ('modus-themes modus-themes-items)
                   ((pred (not null)) modus-themes-registered-items)
                   (_ (seq-union modus-themes-items 
modus-themes-registered-items)))))
-    (mapc #'modus-themes--activate themes)
+    (unless no-enable
+      (mapc #'modus-themes--activate themes))
     (if theme-family
         (seq-filter
          (lambda (theme)
@@ -3849,10 +3852,11 @@ With optional SHOW-ERROR, throw an error instead of 
returning nil."
      (t
       (error "Themes `%S' is not a symbol or a list of symbols" themes)))))
 
-(defun modus-themes-get-current-theme ()
-  "Return current enabled Modus theme."
+(defun modus-themes-get-current-theme (&optional no-enable)
+  "Return current enabled Modus theme.
+With optional NO-ENABLE, do not try to enable any themes."
   (let ((current (car custom-enabled-themes)))
-    (when (memq current (modus-themes-get-all-known-themes))
+    (when (memq current (modus-themes-get-all-known-themes no-enable))
       current)))
 
 (defun modus-themes--get-theme-palette-subr (theme with-overrides 
with-user-palette)
@@ -7319,7 +7323,7 @@ whose value is another symbol, which ultimately resolves 
to a string or
 (defun modus-themes-with-colors-subr (body-function)
   "Do the work of `modus-themes-with-colors' for BODY-FUNCTION."
   (condition-case data
-      (when-let* ((theme (modus-themes-get-current-theme)))
+      (when-let* ((theme (modus-themes-get-current-theme :no-enable)))
         (eval
          `(let* (,@(modus-themes--with-colors-resolve-palette-sort
                     (modus-themes--get-theme-palette-subr theme 
:with-overrides :with-user-palette)))

Reply via email to