Subject: Re: bug#63838: 28.2; 'wid-edit.el' problems with 'integer' (and 'sexp') widgets.

From: Mauro Aranda <[email-masked]>
Date: Sat, 9 Sep 2023 12:13:53 -0300

tags 63838 patch
quit

I attach a patch to handle an empty restricted sexp widget, together
with a test.

From a43899c409703af4966e619a92bda123e80122e0 Mon Sep 17 00:00:00 2001
From: Mauro Aranda <[email-masked]>
Date: Sat, 9 Sep 2023 21:59:30 +0800
Subject: [PATCH] Avoid errors when a restricted-sexp widget is empty

* lisp/wid-edit.el (restricted-sexp): Don't try to read
an empty string when converting the current value to the
external format.  (Bug#63838)

* test/lisp/wid-edit-tests.el (widget-test-restricted-sexp-empty-val):
New test.
---
 lisp/wid-edit.el            |  4 +++-
 test/lisp/wid-edit-tests.el | 11 +++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/lisp/wid-edit.el b/lisp/wid-edit.el
index d18d721f7ed..74412414113 100644
--- a/lisp/wid-edit.el
+++ b/lisp/wid-edit.el
@@ -3681,7 +3681,9 @@ 'restricted-sexp
                           :warning)
                          ;; Make sure we will `read' a string.
                          (setq value (prin1-to-string value)))
-                       (read value)))
+                       (if (string-empty-p value)
+                           value
+                       (read value))))
 
 (defun widget-restricted-sexp-match (widget value)
   (let ((alternatives (widget-get widget :match-alternatives))
diff --git a/test/lisp/wid-edit-tests.el b/test/lisp/wid-edit-tests.el
index ebfe729bc9a..66bff4ad2e3 100644
--- a/test/lisp/wid-edit-tests.el
+++ b/test/lisp/wid-edit-tests.el
@@ -380,4 +380,15 @@ widget-test-alist-default-value-4
                                     :value (("1" . 1) ("2" . 2))))))
       (should (equal '(("1" . 1) ("2" . 2)) (widget-default-get w))))))
 
+(ert-deftest widget-test-restricted-sexp-empty-val ()
+  "Test that we handle an empty restricted-sexp widget just fine."
+  (with-temp-buffer
+    (let ((w (widget-create '(restricted-sexp
+                              :value 3
+                              :match-alternatives (integerp)))))
+      (widget-setup)
+      (widget-backward 1)
+      (delete-char 1)
+      (should (string= (widget-value w) "")))))
+
 ;;; wid-edit-tests.el ends here
-- 
2.34.1