unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Lucien Cartier-Tilet <lucien@phundrak.com>
To: emacs-devel@gnu.org
Cc: Lucien Cartier-Tilet <lucien@phundrak.com>
Subject: [PATCH] Add new csetq macro
Date: Sat,  3 Sep 2022 16:07:25 +0200	[thread overview]
Message-ID: <20220903140723.809684-1-lucien@phundrak.com> (raw)

[-- 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




             reply	other threads:[~2022-09-03 14:07 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-03 14:07 Lucien Cartier-Tilet [this message]
2022-09-03 15:11 ` [PATCH] Add new csetq macro 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220903140723.809684-1-lucien@phundrak.com \
    --to=lucien@phundrak.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).