summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Bernoulli <jonas@bernoul.li>2025-10-19 16:14:49 +0200
committerJonas Bernoulli <jonas@bernoul.li>2025-10-19 16:14:49 +0200
commit0ca86c5f498b5daf7a206d1478c9d0833a7e24ec (patch)
tree1b37fd181acd14a9f5e8bdf987bb281517c88762
parentaa6f4c7e52b2a04b57428214802555f1952904e5 (diff)
cond-let--when$: New macro
-rw-r--r--cond-let-tests.el26
-rw-r--r--cond-let.el15
2 files changed, 39 insertions, 2 deletions
diff --git a/cond-let-tests.el b/cond-let-tests.el
index dca81dc..e4302b9 100644
--- a/cond-let-tests.el
+++ b/cond-let-tests.el
@@ -437,9 +437,30 @@
(let ((anon1 1))
(when anon1 2))))
+(ert-deftest cond-let-test--105-expand--when$ ()
+ (cond-let-test--macroexpansion nil 3
+
+ (when$ (+ 0 1)
+ (+ $ 2))
+
+ (let (($ (+ 0 1)))
+ (when $
+ (+ $ 2))))
+
+ (cond-let-test--macroexpansion nil 4
+
+ (when$ (+ 0 1)
+ (cl-incf $)
+ (+ $ 2))
+
+ (let (($ (+ 0 1)))
+ (when $
+ (cl-incf $)
+ (+ $ 2)))))
+
;;; While
-(ert-deftest cond-let-test--111-expand--while-let* ()
+(ert-deftest cond-let-test--112-expand--while-let* ()
(let ((n 5))
(cond-let-test--macroexpansion nil nil
(while-let* ((a (setq n (1- n)))
@@ -454,7 +475,7 @@
(print a)
(throw ':while-let*2 nil))))))))
-(ert-deftest cond-let-test--112-expand--while-let ()
+(ert-deftest cond-let-test--113-expand--while-let ()
(let ((n 5))
(cond-let-test--macroexpansion nil nil
(while-let ((a (setq n (1- n)))
@@ -479,6 +500,7 @@
;; ("and>" . "cond-let--and>")
;; ("and-let" . "cond-let--and-let")
;; ("if-let" . "cond-let--if-let")
+;; ("when$" . "cond-let--when$")
;; ("when-let" . "cond-let--when-let")
;; ("while-let" . "cond-let--while-let"))
;; End:
diff --git a/cond-let.el b/cond-let.el
index b2a64f6..28038d3 100644
--- a/cond-let.el
+++ b/cond-let.el
@@ -441,6 +441,21 @@ and return nil.
(when ,lastvar
,bodyform ,@body))))))
+(defmacro cond-let--when$ (varform bodyform &rest body)
+ "Bind variable `$' to value of VARFORM and conditionally evaluate BODY.
+
+If VARFORM yields a non-nil value, bind the symbol `$' to that value,
+evaluate BODY with that binding in effect, and return the value of the
+last form. If VARFORM yields nil, do not evaluate BODY, and return nil.
+BODY must be one or more expressions. If VARLIST is empty, do nothing
+and return nil.
+
+\(fn VARLIST BODY...)"
+ (declare (debug (form form)))
+ `(let (($ ,varform))
+ (when $
+ ,bodyform ,@body)))
+
;;; While
(defmacro cond-let--while-let* (varlist &rest body)