unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Add new csetq macro
@ 2022-09-03 14:07 Lucien Cartier-Tilet
  2022-09-03 15:11 ` Stefan Monnier
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Lucien Cartier-Tilet @ 2022-09-03 14:07 UTC (permalink / raw)
  To: emacs-devel; +Cc: Lucien Cartier-Tilet

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2269 bytes --]

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




^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-09-05 16:35 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-03 14:07 [PATCH] Add new csetq macro Lucien Cartier-Tilet
2022-09-03 15:11 ` Stefan Monnier
2022-09-04 11:40   ` Lucien Cartier-Tilet
2022-09-04 11:53     ` Lars Ingebrigtsen
2022-09-04 11:55       ` Lucien Cartier-Tilet
2022-09-05 16:35       ` [External] : " Drew Adams
2022-09-03 15:16 ` Stefan Kangas
2022-09-04 11:46   ` Lucien Cartier-Tilet
2022-09-03 15:59 ` Philip Kaludercic
2022-09-04 11:48   ` Lucien Cartier-Tilet
2022-09-04 17:07     ` Philip Kaludercic

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).