unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: emacs-devel@gnu.org
Subject: Re: custom-set-variables considered harmful
Date: Mon, 13 Nov 2017 11:26:29 -0500	[thread overview]
Message-ID: <jwv7euup2ql.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <jwvbmkinf9p.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Sat, 04 Nov 2017 13:10:11 -0400")

> When writing customizations, instead of writing
>
>     (custom-set-variables
>      ;; Big ugly warning which doesn't help enough.
>      '(VAR1 VAL1)
>      '(VAR2 VAL2 nil '(REQUEST) COMMENT)
>      '(VAR3 VAL3)
>      ...)
>
> we write
>
>     (autogenerated-custom-settings
>       ;; Big warning, still, but less important.
>       (setq VAR1 VAL1)
>       (require 'REQUEST)
>       (customize-set-variable VAR2 VAL2 COMMENT)
>       (customize-set-variable VAR3 VAL3)
>       ...)

Here's a sample patch to do that.  Comments?


        Stefan


diff --git a/lisp/cus-edit.el b/lisp/cus-edit.el
index edf3545cad..0f9b38974d 100644
--- a/lisp/cus-edit.el
+++ b/lisp/cus-edit.el
@@ -4374,6 +4382,16 @@ custom-file
 if only the first line of the docstring is shown."))
   :group 'customize)
 
+(defcustom custom-mimic-plain-elisp t
+  "If non-nil, save user settings with the new format.
+This new format tries to mimick the code that would be written by hand
+in plain Elisp.  But it relies on `custom-autogenerated-user-settings' which
+is a new macro in Emacs-27, so settings saved with this will either
+require a recent enough Emacs, or some ad-hoc hack such
+as (defalias 'custom-autogenerated-user-settings #'ignore)."
+  :type 'boolean
+  :group 'customize)
+
 (defun custom-file (&optional no-error)
   "Return the file name for saving customizations."
   (if (or (null user-init-file)
@@ -4505,6 +4523,7 @@ custom-save-variables
   "Save all customized variables in `custom-file'."
   (save-excursion
     (custom-save-delete 'custom-set-variables)
+    (custom-save-delete 'custom-autogenerated-user-settings)
     (let ((standard-output (current-buffer))
 	  (saved-list (make-list 1 0))
 	  sort-fold-case)
@@ -4519,8 +4538,12 @@ custom-save-variables
       (setq saved-list (sort (cdr saved-list) 'string<))
       (unless (bolp)
 	(princ "\n"))
-      (princ "(custom-set-variables
- ;; custom-set-variables was added by Custom.
+      (princ (if custom-mimic-plain-elisp
+                 "(custom-autogenerated-user-settings
+ ;; This custom-autogenerated-user-settings was added by Custom."
+               "(custom-set-variables
+ ;; This custom-set-variables was added by Custom."))
+      (princ "
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.\n")
@@ -4555,28 +4578,43 @@ custom-save-variables
 	    ;; with the customize facility.
 	    (unless (bolp)
 	      (princ "\n"))
-	    (princ " '(")
-	    (prin1 symbol)
-	    (princ " ")
-	    (let ((val (prin1-to-string (car value))))
-	      (if (< (length val) 60)
-		  (insert val)
-		(newline-and-indent)
-		(let ((beginning-of-val (point)))
-		  (insert val)
-		  (save-excursion
-		    (goto-char beginning-of-val)
-		    (indent-pp-sexp 1)))))
-	    (when (or now requests comment)
+            (if custom-mimic-plain-elisp
+                ;; Do the inverse conversion of
+                ;; custom-autogenerated-user-settings.
+                (let* ((e (cond
+                           ((get symbol 'custom-set)
+                            `(customize-set-variable ',symbol ,(car value)))
+                           ((local-variable-if-set-p
+                             symbol (get-buffer-create "*scratch*"))
+                            `(setq-default ,symbol ,(car value)))
+                           (t `(setq ,symbol ,(car value))))))
+                  (dolist (e (nconc (mapcar (lambda (r) `(require ',r)) requests)
+                                    (if comment (list comment e) (list e))))
+                    (princ " ")
+                    (pp e)
+                    (unless (bolp) (princ "\n"))))
+	      (princ " '(")
+	      (prin1 symbol)
 	      (princ " ")
-	      (prin1 now)
-	      (when (or requests comment)
-		(princ " ")
-		(prin1 requests)
-		(when comment
+	      (let ((val (prin1-to-string (car value))))
+	        (if (< (length val) 60)
+		    (insert val)
+		  (newline-and-indent)
+		  (let ((beginning-of-val (point)))
+		    (insert val)
+		    (save-excursion
+		      (goto-char beginning-of-val)
+		      (indent-pp-sexp 1)))))
+	      (when (or now requests comment)
+	        (princ " ")
+	        (prin1 now)
+	        (when (or requests comment)
 		  (princ " ")
-		  (prin1 comment))))
-	    (princ ")"))))
+		  (prin1 requests)
+		  (when comment
+		    (princ " ")
+		    (prin1 comment))))
+	      (princ ")")))))
       (if (bolp)
 	  (princ " "))
       (princ ")")
@@ -4586,7 +4624,7 @@ custom-save-variables
 (defun custom-save-faces ()
   "Save all customized faces in `custom-file'."
   (save-excursion
-    (custom-save-delete 'custom-reset-faces)
+    (custom-save-delete 'custom-reset-faces) ;FIXME: never written!?
     (custom-save-delete 'custom-set-faces)
     (let ((standard-output (current-buffer))
 	  (saved-list (make-list 1 0))
@@ -4738,9 +4776,9 @@ customize-menu-create
 
 (defvar tool-bar-map)
 
-;;; `custom-tool-bar-map' used to be set up here.  This will fail to
-;;; DTRT when `display-graphic-p' returns nil during compilation.  Hence
-;;; we set this up lazily in `Custom-mode'.
+;; `custom-tool-bar-map' used to be set up here.  This will fail to
+;; DTRT when `display-graphic-p' returns nil during compilation.  Hence
+;; we set this up lazily in `Custom-mode'.
 (defvar custom-tool-bar-map nil
   "Keymap for toolbar in Custom mode.")
 
diff --git a/lisp/custom.el b/lisp/custom.el
index 352fc6bd53..f541b51420 100644
--- a/lisp/custom.el
+++ b/lisp/custom.el
@@ -1,4 +1,4 @@
-;;; custom.el --- tools for declaring and initializing options
+;;; custom.el --- tools for declaring and initializing options  -*- lexical-binding:t -*-
 ;;
 ;; Copyright (C) 1996-1997, 1999, 2001-2017 Free Software Foundation,
 ;; Inc.
@@ -926,6 +933,35 @@ custom-fix-face-spec
 	  (nreverse result))
       spec)))
 \f
+(defmacro custom-autogenerated-user-settings (&rest body)
+  "Install user customizations of variable values specified in ARGS.
+Expect the format output by `custom-save-variables'."
+  (let* ((settings '())
+         (requests '())
+         (comment nil)
+         (mk (lambda (x e)
+               (push
+                `'(,x ,e
+                      . ,(when (or comment requests)
+                           `(nil ,(prog1 (nreverse requests)
+                                    (setq requests '()))
+                                 . ,(when comment
+                                      (prog1 (list comment)
+                                        (setq comment nil))))))
+                settings))))
+    (dolist (e body)
+      (pcase e
+        (`(,(or 'setq 'setq-default) ,x ,e) (funcall mk x e))
+        (`(require ',p) (push p requests))
+        (`(customize-set-variable ',x ,e) (funcall mk x e))
+        (`(,x ,(and v (or 1 -1))) (funcall mk x (> v 0)))
+        ((pred stringp)
+         (and comment (message "Dropping extra comment: %S" comment))
+         (setq comment e))
+        (_
+         (message "Unrecognized expression in custom-autogenerated-user-settings: %S" e))))
+    `(custom-set-variables ,@(nreverse settings))))
+
 (defun custom-set-variables (&rest args)
   "Install user customizations of variable values specified in ARGS.
 These settings are registered as theme `user'.
@@ -940,7 +976,7 @@ custom-set-variables
 REQUEST is a list of features we must require in order to
 handle SYMBOL properly.
 COMMENT is a comment string about SYMBOL."
-  (apply 'custom-theme-set-variables 'user args))
+  (apply #'custom-theme-set-variables 'user args))
 
 (defun custom-theme-set-variables (theme &rest args)
   "Initialize variables for theme THEME according to settings in ARGS.
@@ -1505,7 +1541,7 @@ custom-reset-variables
     (VARIABLE IGNORED)
 
 This means reset VARIABLE.  (The argument IGNORED is ignored)."
-    (apply 'custom-theme-reset-variables 'user args))
+    (apply #'custom-theme-reset-variables 'user args))
 
 ;;; The End.
 



  parent reply	other threads:[~2017-11-13 16:26 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-04 17:10 custom-set-variables considered harmful Stefan Monnier
2017-11-06  9:02 ` Philippe Vaucher
2017-11-06 12:19   ` Clément Pit-Claudel
2017-11-06 16:36   ` raman
2017-11-07 19:53     ` Stefan Monnier
2017-11-08 10:06 ` Vivek Dasmohapatra
2017-11-08 13:38   ` Stefan Monnier
2017-11-08 14:23     ` Vivek Dasmohapatra
2017-11-08 15:17       ` Stefan Monnier
2017-11-08 15:34         ` Vivek Dasmohapatra
2017-11-13 16:26 ` Stefan Monnier [this message]
2017-11-24  7:04   ` Elias Mårtenson
2017-11-24  7:16     ` Stefan Monnier
2017-11-24 17:37       ` Clément Pit-Claudel
2017-11-24 17:56         ` Stefan Monnier
2017-11-26 12:15       ` Elias Mårtenson
2017-11-26 16:30         ` Drew Adams
2017-11-28 14:12         ` Philippe Vaucher
2017-11-28 16:50           ` John Wiegley
2017-12-01  9:44             ` Philippe Vaucher
2017-11-29  3:36           ` Elias Mårtenson
2017-11-29 15:00             ` Stefan Monnier
2017-11-29 15:03               ` Drew Adams
2017-12-01 20:42                 ` Robert Weiner
2017-12-02  0:10                   ` Richard Stallman
2017-12-02  1:58                     ` Drew Adams
2017-12-02  5:56                 ` Teemu Likonen
2017-11-29 16:35               ` Elias Mårtenson
2017-11-29 19:57                 ` Scott Randby
2017-11-29 22:08                   ` Stefan Monnier
2017-11-30  0:40                     ` Scott Randby
2017-12-01  9:51             ` Philippe Vaucher
2017-12-04 13:48   ` Stefan Monnier
2017-12-04 15:36     ` Eli Zaretskii
2017-12-04 16:05     ` Robert Weiner
2017-12-04 23:50     ` Richard Stallman
2017-12-05  1:45       ` Stefan Monnier
2017-11-23 20:12 ` John Wiegley

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=jwv7euup2ql.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --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).