all messages for Emacs-related lists mirrored at yhetil.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

* Re: [PATCH] Add new csetq macro
  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-03 15:16 ` Stefan Kangas
  2022-09-03 15:59 ` Philip Kaludercic
  2 siblings, 1 reply; 11+ messages in thread
From: Stefan Monnier @ 2022-09-03 15:11 UTC (permalink / raw)
  To: Lucien Cartier-Tilet; +Cc: emacs-devel

> It avoids repetition of `custom-set-variable' when setting the value
> of custom variables, while keeping the same syntax as `setq'.

`custom-set-variables` seems to solve this problem of
repetition already.

But more importantly, I think your `csetq` has been recently added
under the name `setopt`.


        Stefan




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

* Re: [PATCH] Add new csetq macro
  2022-09-03 14:07 [PATCH] Add new csetq macro Lucien Cartier-Tilet
  2022-09-03 15:11 ` Stefan Monnier
@ 2022-09-03 15:16 ` Stefan Kangas
  2022-09-04 11:46   ` Lucien Cartier-Tilet
  2022-09-03 15:59 ` Philip Kaludercic
  2 siblings, 1 reply; 11+ messages in thread
From: Stefan Kangas @ 2022-09-03 15:16 UTC (permalink / raw)
  To: Lucien Cartier-Tilet, emacs-devel

Lucien Cartier-Tilet <lucien@phundrak.com> writes:

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

Thanks.  How does it compare to the recently added `setopt' macro
though?  They look rather similar to me.  Am I missing something?



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

* Re: [PATCH] Add new csetq macro
  2022-09-03 14:07 [PATCH] Add new csetq macro Lucien Cartier-Tilet
  2022-09-03 15:11 ` Stefan Monnier
  2022-09-03 15:16 ` Stefan Kangas
@ 2022-09-03 15:59 ` Philip Kaludercic
  2022-09-04 11:48   ` Lucien Cartier-Tilet
  2 siblings, 1 reply; 11+ messages in thread
From: Philip Kaludercic @ 2022-09-03 15:59 UTC (permalink / raw)
  To: Lucien Cartier-Tilet; +Cc: emacs-devel

Lucien Cartier-Tilet <lucien@phundrak.com> writes:

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

Note that this would have the issue that changes would automatically be
added to the "user" theme, and hence be reproduced in the
`custom-set-variables' section.  If the user sets an option using this
macro, and the `custom-set-variables' is evaluated after this point, the
intended change would be silently overridden.

> +              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	[flat|nested] 11+ messages in thread

* Re: [PATCH] Add new csetq macro
  2022-09-03 15:11 ` Stefan Monnier
@ 2022-09-04 11:40   ` Lucien Cartier-Tilet
  2022-09-04 11:53     ` Lars Ingebrigtsen
  0 siblings, 1 reply; 11+ messages in thread
From: Lucien Cartier-Tilet @ 2022-09-04 11:40 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 891 bytes --]

> `custom-set-variables` seems to solve this problem of
> repetition already.

`custom-set-variables` explicitely does not evaluate the expressions
passed as arguments, while `csetq' does, like `setq' itself. Moreover, I
wanted to write this macro with the aim of being as close as possible
to `setq'’s syntax.

> But more importantly, I think your `csetq` has been recently added
> under the name `setopt`.

I did not know this macro existed, it seems to be indeed close to what
I intended. My only issue with it is you still have to quote the
custom variable you want to set, unlike `setq'.
,----
| (setopt 'custom-var1 value1
|         'custom-var2 value2)
| ;; vs
| (csetq custom-var1 value1
|        custom-var2 value2)
`----

--
Lucien “Phundrak” Cartier-Tilet
<https://phundrak.com> (Français)
<https://phundrak.com/en> (English)
Sent from GNU/Emacs

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 861 bytes --]

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

* Re: [PATCH] Add new csetq macro
  2022-09-03 15:16 ` Stefan Kangas
@ 2022-09-04 11:46   ` Lucien Cartier-Tilet
  0 siblings, 0 replies; 11+ messages in thread
From: Lucien Cartier-Tilet @ 2022-09-04 11:46 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: emacs-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 504 bytes --]

> Thanks.  How does it compare to the recently added `setopt' macro
> though?  They look rather similar to me.  Am I missing something?

As said in my answer to Stefan Monnier, I did not know this new macro
existed. Other than the syntax difference (which I explain in my other
reply), I am not aware of any significant behavioural difference
between the two.

--
Lucien “Phundrak” Cartier-Tilet
<https://phundrak.com> (Français)
<https://phundrak.com/en> (English)
Sent from GNU/Emacs

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 861 bytes --]

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

* Re: [PATCH] Add new csetq macro
  2022-09-03 15:59 ` Philip Kaludercic
@ 2022-09-04 11:48   ` Lucien Cartier-Tilet
  2022-09-04 17:07     ` Philip Kaludercic
  0 siblings, 1 reply; 11+ messages in thread
From: Lucien Cartier-Tilet @ 2022-09-04 11:48 UTC (permalink / raw)
  To: Philip Kaludercic; +Cc: emacs-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 770 bytes --]

> Note that this would have the issue that changes would automatically be
> added to the "user" theme, and hence be reproduced in the
> `custom-set-variables' section.  If the user sets an option using this
> macro, and the `custom-set-variables' is evaluated after this point, the
> intended change would be silently overridden.

I must admit I completely forgot about the `custom-set-variables'
section of the configuration since I disabled it entirely in my
configuration, due to this behaviour precisely. But wouldn’t that be
an issue if anything else like `setq' or `customize-variable' is used
instead of `csetq'?

--
Lucien “Phundrak” Cartier-Tilet
<https://phundrak.com> (Français)
<https://phundrak.com/en> (English)
Sent from GNU/Emacs

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 861 bytes --]

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

* Re: [PATCH] Add new csetq macro
  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
  0 siblings, 2 replies; 11+ messages in thread
From: Lars Ingebrigtsen @ 2022-09-04 11:53 UTC (permalink / raw)
  To: Lucien Cartier-Tilet; +Cc: Stefan Monnier, emacs-devel

Lucien Cartier-Tilet <lucien@phundrak.com> writes:

> I did not know this macro existed, it seems to be indeed close to what
> I intended. My only issue with it is you still have to quote the
> custom variable you want to set, unlike `setq'.
> ,----
> | (setopt 'custom-var1 value1
> |         'custom-var2 value2)

No, the setopt syntax is without quoting.




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

* Re: [PATCH] Add new csetq macro
  2022-09-04 11:53     ` Lars Ingebrigtsen
@ 2022-09-04 11:55       ` Lucien Cartier-Tilet
  2022-09-05 16:35       ` [External] : " Drew Adams
  1 sibling, 0 replies; 11+ messages in thread
From: Lucien Cartier-Tilet @ 2022-09-04 11:55 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Stefan Monnier, emacs-devel


[-- Attachment #1.1.1: Type: text/plain, Size: 627 bytes --]

Lars Ingebrigtsen <larsi@gnus.org> writes:

> Lucien Cartier-Tilet <lucien@phundrak.com> writes:
>
>> I did not know this macro existed, it seems to be indeed close to what
>> I intended. My only issue with it is you still have to quote the
>> custom variable you want to set, unlike `setq'.
>> ,----
>> | (setopt 'custom-var1 value1
>> |         'custom-var2 value2)
>
> No, the setopt syntax is without quoting.

I see, so these two macros do indeed serve the same purpose.

--
Lucien “Phundrak” Cartier-Tilet
<https://phundrak.com> (Français)
<https://phundrak.com/en> (English)
Sent from GNU/Emacs

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 861 bytes --]

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

* Re: [PATCH] Add new csetq macro
  2022-09-04 11:48   ` Lucien Cartier-Tilet
@ 2022-09-04 17:07     ` Philip Kaludercic
  0 siblings, 0 replies; 11+ messages in thread
From: Philip Kaludercic @ 2022-09-04 17:07 UTC (permalink / raw)
  To: Lucien Cartier-Tilet; +Cc: emacs-devel

Lucien Cartier-Tilet <lucien@phundrak.com> writes:

>> Note that this would have the issue that changes would automatically be
>> added to the "user" theme, and hence be reproduced in the
>> `custom-set-variables' section.  If the user sets an option using this
>> macro, and the `custom-set-variables' is evaluated after this point, the
>> intended change would be silently overridden.
>
> I must admit I completely forgot about the `custom-set-variables'
> section of the configuration since I disabled it entirely in my
> configuration, due to this behaviour precisely. But wouldn’t that be
> an issue if anything else like `setq' or `customize-variable' is used
> instead of `csetq'?

Yes, unless you make sure the customisation is not added to the user
theme, but either some other theme, as it is done with use-package or no
theme at all as is the case with setup.el.

> --
> Lucien “Phundrak” Cartier-Tilet
> <https://phundrak.com> (Français)
> <https://phundrak.com/en> (English)
> Sent from GNU/Emacs



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

* RE: [External] : Re: [PATCH] Add new csetq macro
  2022-09-04 11:53     ` Lars Ingebrigtsen
  2022-09-04 11:55       ` Lucien Cartier-Tilet
@ 2022-09-05 16:35       ` Drew Adams
  1 sibling, 0 replies; 11+ messages in thread
From: Drew Adams @ 2022-09-05 16:35 UTC (permalink / raw)
  To: Lars Ingebrigtsen, Lucien Cartier-Tilet
  Cc: Stefan Monnier, emacs-devel@gnu.org

> > My only issue with it is you still have to quote the
> > custom variable you want to set, unlike `setq'.
> > (setopt 'custom-var1 value1
> >         'custom-var2 value2)
> 
> No, the setopt syntax is without quoting.

Then it's misnamed, IMO.  The name should be
`setqopt' - or (better) `setq-option', or
similar.

Functions (and macros and special forms) that
set things and evaluate the first arg (the
thing to be set) use the prefix `set-', not
`set', in Emacs.  [Generally, at least, and
there might be no (other) exceptions; dunno.]

Also:

https://lists.gnu.org/archive/html/emacs-devel/2022-02/msg00779.html



^ permalink raw reply	[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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.