emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Add new csetq macro


From: Lucien Cartier-Tilet
Subject: [PATCH] Add new csetq macro
Date: Sat, 3 Sep 2022 16:07:25 +0200

The csetq macro is a quality of life macro for Emacs users and wraps
`custom-set-variable' in a `setq'-like macro, hence its name.

It avoids repetition of `custom-set-variable' when setting the value
of custom variables, while keeping the same syntax as `setq'.  I
believe this could also help and encourage people to switch more
easily to the correct way of setting the value of custom variables
instead of using raw `setq's, since `setq' doesn't call the setter of
a custom variable –if it exists– while `custom-set-variable' does.

It also has the same behaviour from the user's perspective: all the
variables are set sequentially and in order, and the last value is
returned to the user.

---
 lisp/subr.el | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/lisp/subr.el b/lisp/subr.el
index e4d3245537..19327e49f6 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -190,7 +190,33 @@ setq-local
       (setq pairs (cdr (cdr pairs))))
     (macroexp-progn (nreverse expr))))
 
+(defmacro csetq (&rest forms)
+  "Bind each custom variable FORM to the value of its VAL.
+
+FORMS is a list of pairs of values [FORM VAL].
+`customize-set-variable' is called sequentially on each pairs
+contained in FORMS.  `csetq' thus has a similar behaviour as
+`setq': each VAL expression are evaluated sequentially, i.e. the
+first VAL is evaluated before the second, and so on.  This means
+the value of the first FORM can be used to set the second FORM.
+
+The return value of `csetq' is the value of the last VAL.
+
+\(fn [FORM VAL]...)"
+  (declare (debug (&rest sexp form))
+           (indent 1))
+  ;; Check if we have an even number of arguments
+  (when (= (mod (length forms) 2) 1)
+    (signal 'wrong-number-of-arguments (list 'csetq (1+ (length forms)))))
+  ;; Transform FORMS into a list of pairs (FORM . VALUE)
+  (let (sexps)
+    (while forms
+      (let ((form  (pop forms))
+            (value (pop forms)))
+        (push `(customize-set-variable ',form ,value)
+              sexps)))
+    `(progn ,@(nreverse sexps))))
+
 (defmacro defvar-local (var val &optional docstring)
   "Define VAR as a buffer-local variable with default value VAL.
 Like `defvar' but additionally marks the variable as being automatically
--
2.37.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]