unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Mauro Aranda <maurooaranda@gmail.com>
To: Drew Adams <drew.adams@oracle.com>
Cc: 25152@debbugs.gnu.org
Subject: bug#25152: 25.1; Customize: errors for `restricted-sexp' in `repeat'
Date: Fri, 23 Oct 2020 09:59:45 -0300	[thread overview]
Message-ID: <CABczVwergJaDq=hw6cN=3aqsY7aC7EEuoKL85=tGoCzh5uZ3mw@mail.gmail.com> (raw)
In-Reply-To: <CABczVwd7DQ8pfV4bzt6hRZw2XG3iOwGFqynktcndV3PcJe72Ag@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 1406 bytes --]

Mauro Aranda <maurooaranda@gmail.com> writes:

> Drew Adams <drew.adams@oracle.com> writes:
>
>>> To sum it up, the prompt is an unfortunate mistake, and maybe we could
>>> protect against that, but I think the real problem comes from the
>>> defcustom, which fails to provide a valid default value.
>>
>> Thanks for this explanation.  Makes sense.
>>
>> Can we somehow help users by raising an error when they
>> do this?  Seems like a simple mistake to make.
>
> I think it makes sense, but I'm not sure where would be the right place
> to do it.

Coming back to this, perhaps a good place to warn about a bad default
value is where we find it for the first time.  So I attach a patch that
makes the restricted-sexp widget warn (but not error out) if the
internal value is not a string.

So, for the defcustom in the recipe:
(defcustom bar
  `(ignore)
  "..."
  :type
  '(repeat (restricted-sexp :match-alternatives (functionp)))
  :group 'emacs)

When the user clicks the INS button, the following warning pops up:
Warning (widget-bad-default-value):
A widget of type restricted-sexp has a bad default value.
value: nil
match function: widget-restricted-sexp-match
match-alternatives: (functionp)

which I hope conveys good enough information to fix the mistake.

I made it just a warning, because this mistake doesn't always result in
a messed up buffer.  But it can be changed to an error, of course.

[-- Attachment #1.2: Type: text/html, Size: 1795 bytes --]

[-- Attachment #2: 0001-Warn-about-a-bad-default-value-in-restricted-sexp-wi.patch --]
[-- Type: text/x-patch, Size: 2327 bytes --]

From dcf1a7ac3f4fe2e7e6da80d0b219479c8b454858 Mon Sep 17 00:00:00 2001
From: Mauro Aranda <maurooaranda@gmail.com>
Date: Fri, 23 Oct 2020 09:17:04 -0300
Subject: [PATCH] Warn about a bad default value in restricted-sexp widget

* lisp/wid-edit.el (restricted-sexp widget): New :value-to-external
function.  If value is not in the internal format, then we might be
dealing with a bad default value for the widget, so display a warning
about that.  (Bug#25152)
---
 lisp/wid-edit.el | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/lisp/wid-edit.el b/lisp/wid-edit.el
index 009c6b4faf..4e2cf7416d 100644
--- a/lisp/wid-edit.el
+++ b/lisp/wid-edit.el
@@ -3585,7 +3585,30 @@ 'restricted-sexp
   :value-to-internal (lambda (widget value)
 		       (if (widget-apply widget :match value)
                            (widget-sexp-value-to-internal widget value)
-			 value)))
+                         value))
+  :value-to-external (lambda (widget value)
+                       ;; We expect VALUE to be a string, so we can convert it
+                       ;; into the external format just by `read'ing it.
+                       ;; But for a restricted-sexp widget with a bad default
+                       ;; value, we might end up calling read with a nil
+                       ;; argument, resulting in an undesired prompt to the
+                       ;; user.  A bad default value is not always a big
+                       ;; problem, but might end up in a messed up buffer,
+                       ;; so display a warning here.  (Bug#25152)
+                       (unless (stringp value)
+                         (display-warning
+                          'widget-bad-default-value
+                          (format-message
+                           "\nA widget of type %S has a bad default value.
+value: %S
+match function: %S
+match-alternatives: %S"
+                           (widget-type widget)
+                           value
+                           (widget-get widget :match)
+                           (widget-get widget :match-alternatives))
+                          :warning))
+                       (read value)))
 
 (defun widget-restricted-sexp-match (widget value)
   (let ((alternatives (widget-get widget :match-alternatives))
-- 
2.28.0


  reply	other threads:[~2020-10-23 12:59 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-09 18:10 bug#25152: 25.1; Customize: errors for `restricted-sexp' in `repeat' Drew Adams
2016-12-10  4:24 ` npostavs
2016-12-10  4:39   ` Drew Adams
2020-09-05 11:57 ` Mauro Aranda
2020-09-05 14:46   ` Drew Adams
2020-09-05 16:53     ` Mauro Aranda
2020-10-23 12:59       ` Mauro Aranda [this message]
2020-10-23 16:47         ` Drew Adams
2020-10-24 12:33           ` Mauro Aranda
2020-10-24 20:16             ` Drew Adams
2020-10-24 19:41         ` Lars Ingebrigtsen
2020-10-24 20:23           ` Drew Adams

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='CABczVwergJaDq=hw6cN=3aqsY7aC7EEuoKL85=tGoCzh5uZ3mw@mail.gmail.com' \
    --to=maurooaranda@gmail.com \
    --cc=25152@debbugs.gnu.org \
    --cc=drew.adams@oracle.com \
    /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).