aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mendler <mail@daniel-mendler.de>2023-01-03 18:29:49 +0100
committerDaniel Mendler <mail@daniel-mendler.de>2023-01-03 18:29:49 +0100
commit438d683afd6f92c77acbb91763520ee80d68ac12 (patch)
tree33505476e45cf46536482e5d6a9463765db406ce
parent4aa07eac2241d155831aaf16d7aebd958ae2a041 (diff)
Add compat--with-feature helper
-rw-r--r--compat-macs.el28
-rw-r--r--compat-tests.el7
2 files changed, 14 insertions, 21 deletions
diff --git a/compat-macs.el b/compat-macs.el
index 9a87032..c45fd76 100644
--- a/compat-macs.el
+++ b/compat-macs.el
@@ -29,6 +29,14 @@
(setq compat--current-version version)
nil)
+(defun compat--with-feature (feature body)
+ "Protect BODY with `eval-after-load' if FEATURE is non-nil."
+ (declare (indent 1))
+ (if feature
+ ;; See https://nullprogram.com/blog/2018/02/22/:
+ `(eval-after-load ,feature `(funcall ',(lambda () ,body)))
+ body))
+
(defvar compat--generate-function #'compat--generate-default
"Function used to generate compatibility code.
The function must take six arguments: NAME, DEF-FN, INSTALL-FN,
@@ -100,27 +108,17 @@ DEF-FN, INSTALL-FN, CHECK-FN and ATTR."
(when (and (version<= version emacs-version)
(fboundp actual-name)
check)
- `(,@check
- ,(if feature
- ;; See https://nullprogram.com/blog/2018/02/22/:
- `(eval-after-load ,feature `(funcall ',(lambda () ,body)))
- body))))))
+ `(,@check ,(compat--with-feature feature body))))))
((plist-get attr :realname)
`(progn
,(funcall def-fn realname version)
,(and check
`(,@check
- ,(let ((body (funcall install-fn realname version)))
- (if feature
- ;; See https://nullprogram.com/blog/2018/02/22/:
- `(eval-after-load ,feature `(funcall ',(lambda () ,body)))
- body))))))
+ ,(compat--with-feature feature
+ (funcall install-fn realname version))))))
(check
- (let ((body `(,@check ,(funcall def-fn name version))))
- (if feature
- ;; See https://nullprogram.com/blog/2018/02/22/:
- `(eval-after-load ,feature `(funcall ',(lambda () ,body)))
- body))))))
+ (compat--with-feature feature
+ `(,@check ,(funcall def-fn name version)))))))
(defun compat--define-function (type name arglist docstring rest)
"Generate compatibility code for a function NAME.
diff --git a/compat-tests.el b/compat-tests.el
index ad2611d..9555d2b 100644
--- a/compat-tests.el
+++ b/compat-tests.el
@@ -70,12 +70,7 @@ DEF-FN, INSTALL-FN, CHECK-FN and ATTR."
(put ',realname 'compat-min-version ,min-version)
(put ',realname 'compat-max-version ,max-version)
,(funcall def-fn realname version)
- ,(and check
- `(,@check
- ,(if feature
- ;; See https://nullprogram.com/blog/2018/02/22/:
- `(eval-after-load ,feature `(funcall ',(lambda () ,body)))
- body))))))
+ ,(and check `(,@check ,(compat--with-feature feature body))))))
(setq compat--generate-function #'compat--generate-testable)