From a13719af45133ca2f6520a7fe8139664fffc0c9b Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Mon, 14 Apr 2025 22:58:59 -0400 Subject: [PATCH] refactor(lib): backport static-{if,when,unless} And deprecate our eval-{if,when}! macros. --- lisp/doom-compat.el | 42 ++++++++++++++++++++++++++++++++ lisp/doom-lib.el | 18 ++------------ lisp/lib/files.el | 2 +- modules/checkers/spell/config.el | 2 +- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/lisp/doom-compat.el b/lisp/doom-compat.el index 5fc74f2ee..a7abb0ce7 100644 --- a/lisp/doom-compat.el +++ b/lisp/doom-compat.el @@ -175,5 +175,47 @@ The functions in the hook are called with one parameter -- the enable-local-variables))) (funcall fn variables dir-name)))) +;; Introduced in 30.1 +(unless (fboundp 'static-if) + (defmacro static-if (condition then-form &rest else-forms) + "A conditional compilation macro. +Evaluate CONDITION at macro-expansion time. If it is non-nil, +expand the macro to THEN-FORM. Otherwise expand it to ELSE-FORMS +enclosed in a `progn' form. ELSE-FORMS may be empty." + (declare (indent 2) + (debug (sexp sexp &rest sexp))) + (if (eval condition lexical-binding) + then-form + (cons 'progn else-forms)))) + + +;;; From Emacs 31+ +(unless (fboundp 'static-when) + (defmacro static-when (condition &rest body) + "A conditional compilation macro. +Evaluate CONDITION at macro-expansion time. If it is non-nil, +expand the macro to evaluate all BODY forms sequentially and return +the value of the last one, or nil if there are none." + (declare (indent 1) (debug t)) + (if body + (if (eval condition lexical-binding) + (cons 'progn body) + nil) + (macroexp-warn-and-return (format-message "`static-when' with empty body") + (list 'progn nil nil) '(empty-body static-when) t))) + + (defmacro static-unless (condition &rest body) + "A conditional compilation macro. +Evaluate CONDITION at macro-expansion time. If it is nil, +expand the macro to evaluate all BODY forms sequentially and return +the value of the last one, or nil if there are none." + (declare (indent 1) (debug t)) + (if body + (if (eval condition lexical-binding) + nil + (cons 'progn body)) + (macroexp-warn-and-return (format-message "`static-unless' with empty body") + (list 'progn nil nil) '(empty-body static-unless) t)))) + (provide 'doom-compat) ;;; doom-compat.el ends here diff --git a/lisp/doom-lib.el b/lisp/doom-lib.el index 93522af63..40a48cc5c 100644 --- a/lisp/doom-lib.el +++ b/lisp/doom-lib.el @@ -486,22 +486,8 @@ echo-area, but not to *Messages*." (save-silently t)) (prog1 ,@forms (message "")))))) -(defmacro eval-if! (cond then &rest body) - "Expands to THEN if COND is non-nil, to BODY otherwise. -COND is checked at compile/expansion time, allowing BODY to be omitted entirely -when the elisp is byte-compiled. Use this for forms that contain expensive -macros that could safely be removed at compile time." - (declare (indent 2)) - (if (eval cond) - then - (macroexp-progn body))) - -(defmacro eval-when! (cond &rest body) - "Expands to BODY if CONDITION is non-nil at compile/expansion time. -See `eval-if!' for details on this macro's purpose." - (declare (indent 1)) - (when (eval cond) - (macroexp-progn body))) +(define-obsolete-function-alias 'eval-if! 'static-if "3.0.0") +(define-obsolete-function-alias 'eval-when! 'static-when "3.0.0") (defmacro versionp! (v1 comp v2 &rest comps) "Perform compound version checks. diff --git a/lisp/lib/files.el b/lisp/lib/files.el index 82c864a00..7f5389e85 100644 --- a/lisp/lib/files.el +++ b/lisp/lib/files.el @@ -596,7 +596,7 @@ which case it will save it without prompting." ;; Introduced in Emacs 29. ;;;###autoload -(eval-when! (not (fboundp 'find-sibling-file)) +(static-unless (fboundp 'find-sibling-file) (defvar find-sibling-rules nil) (defun find-sibling-file (file) diff --git a/modules/checkers/spell/config.el b/modules/checkers/spell/config.el index 9a168f371..796a657b6 100644 --- a/modules/checkers/spell/config.el +++ b/modules/checkers/spell/config.el @@ -70,7 +70,7 @@ ;; ;;; Implementations -(eval-if! (modulep! -flyspell) +(static-if (modulep! -flyspell) (use-package! spell-fu :when (executable-find "aspell")