Subject: bug#63290: 30.0.50; Customize UI shows extra fields for (choice (const ...) (alist ...))

From: Mauro Aranda <[email-masked]>
Date: Thu, 10 Aug 2023 19:58:24 -0300

Thomas Fitzsimmons <[email-masked]> writes:

 > Hi Mauro,
 >
 > Mauro Aranda <[email-masked]> writes:
 >
 >> I ended up adding a custom :default-get
 >> function for the list widget, to make it respect a nil value as the
 >> :value.  This should be backward compatible with other widgets, and
 >> should fix these "ghost" elements insertions. I also added a test
 >> for cus-edit-tests.
 >
 > Can you try this patch with:
 >
 > M-x package-install RET excorporate RET
 >
 > Then:
 >
 > M-x customize-variable RET excorporate-configuration RET
 >
 > then select "Value Menu" and 3, which is "EWS URL OAuth 2.0 settings
 > (no autodiscovery)".  With your wis-edit.el patch applied I still get
 > empty values for:
 >
 >    INS DEL Argument name:             Argument value:
 >
 > and:
 >
 >    INS DEL OAuth 2.0 setting name:             OAuth 2.0 setting
 >   value:
 >
 > and when I apply the setting the value contains:   (... (... (#1#
 > . #1#))    (#1# . #1#))
 >
 > Maybe this is a more complicated case than the test case I provided
 > (which does now work for me with your patch)?

I think this ammended patch fixes it.  Since we want
widget-list-default-get to respect a nil :value property, the alist
widget needs to be modified so that its default value is nil.

From 5ae17fcc45ea405f5cc2dc516efa1e9c05782021 Mon Sep 17 00:00:00 2001
From: Mauro Aranda <[email-masked]>
Date: Wed, 9 Aug 2023 09:17:43 -0300
Subject: [PATCH] Respect the :value property in a list widget (Bug#63290)

* lisp/wid-edit.el (widget-list-default-get): New function.
(alist): Define nil as a default value.
* test/lisp/cus-edit-tests.el (cus-edit-test-bug-63290-option)
(cus-edit-test-bug-63290-option2): New test options.
(cus-edit-test-bug63290): New test.
---
 lisp/wid-edit.el            | 12 +++++++++++
 test/lisp/cus-edit-tests.el | 43 +++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/lisp/wid-edit.el b/lisp/wid-edit.el
index 47531113ba8..d538dc42d53 100644
--- a/lisp/wid-edit.el
+++ b/lisp/wid-edit.el
@@ -3812,8 +3812,19 @@ widget-character-notify
 (define-widget 'list 'group
   "A Lisp list."
   :tag "List"
+  :default-get #'widget-list-default-get
   :format "%{%t%}:\n%v")
 
+(defun widget-list-default-get (widget)
+  "Return the default external value for a list WIDGET.
+
+The default value is the one stored in the :value property, even if it is nil,
+or a list with the default value of each component of the list WIDGET."
+  (widget-apply widget :value-to-external
+                (if (widget-member widget :value)
+                    (widget-get widget :value)
+                  (widget-group-default-get widget))))
+
 (define-widget 'vector 'group
   "A Lisp vector."
   :tag "Vector"
@@ -3952,6 +3963,7 @@ 'alist
   :key-type '(sexp :tag "Key")
   :value-type '(sexp :tag "Value")
   :convert-widget 'widget-alist-convert-widget
+  :value nil
   :tag "Alist")
 
 (defvar widget-alist-value-type)	;Dynamic variable
diff --git a/test/lisp/cus-edit-tests.el b/test/lisp/cus-edit-tests.el
index eca35d7c96a..3a788f19745 100644
--- a/test/lisp/cus-edit-tests.el
+++ b/test/lisp/cus-edit-tests.el
@@ -92,5 +92,48 @@ test-setopt
             (buffer-substring-no-properties (point-min) (point-max)))))
     (should (string-search "Value `:foo' does not match type number"
                            warn-txt))))
+
+(defcustom cus-edit-test-bug63290-option nil
+  "Choice option for testing Bug#63290."
+  :type '(choice (alist
+                  :key-type (string :tag "key")
+                  :value-type (string :tag "value"))
+                 (const :tag "auto" auto)))
+
+(defcustom cus-edit-test-bug63290-option2 'some
+  "Choice option for testing Bug#63290."
+  :type '(choice
+          (const :tag "some" some)
+          (alist
+           :key-type (string :tag "key")
+           :value-type (string :tag "value"))))
+
+(ert-deftest cus-edit-test-bug63290 ()
+  "Test that changing a choice value back to an alist respects its nil value."
+  (customize-variable 'cus-edit-test-bug63290-option)
+  (search-forward "Value")
+  ;; Simulate changing the value.
+  (let* ((choice (widget-at))
+         (args (widget-get choice :args))
+         (list-opt (car (widget-get choice :children)))
+         (const-opt (nth 1 args)))
+    (widget-put choice :explicit-choice const-opt)
+    (widget-value-set choice (widget-default-get const-opt))
+    (widget-put choice :explicit-choice list-opt)
+    (widget-value-set choice (widget-default-get list-opt)))
+  ;; No empty key/value pairs should show up.
+  (should-not (search-forward "key" nil t))
+  (customize-variable 'cus-edit-test-bug63290-option2)
+  (search-forward "Value")
+  ;; Simulate changing the value.
+  (let* ((choice (widget-at))
+         (args (widget-get choice :args))
+         (const-opt (car (widget-get choice :children)))
+         (list-opt (nth 1 args)))
+    (widget-put choice :explicit-choice list-opt)
+    (widget-value-set choice (widget-default-get list-opt)))
+  ;; No empty key/value pairs should show up.
+  (should-not (search-forward "key" nil t)))
+
 (provide 'cus-edit-tests)
 ;;; cus-edit-tests.el ends here
-- 
2.34.1