Subject: Re: bug#52523: 29.0.50; defcustom :type 'key-sequence not following key-valid-p format

From: Stefan Kangas <[email-masked]>
Date: Mon, 27 Dec 2021 19:08:42 -0800

tags 52523 + patch
thanks

Lars Ingebrigtsen <[email-masked]> writes:

> The problem is that we don't use the name "kbd" anywhere in the new
> keymap functions, so it'd be confusing to use them in defcustom.

Having looked over the documentation to these functions, I actually
think "key" is the least confusing name here.

How does the attached patch look?
From c433b9c0212c1b6d25dd06ba1b6359d7dedcdedd Mon Sep 17 00:00:00 2001
From: Stefan Kangas <[email-masked]>
Date: Tue, 28 Dec 2021 02:59:51 +0100
Subject: [PATCH] New :type key for defcustom

As compared to the old type key-sequence that deals with raw key
sequences, this :type conforms to the format used by the new keymap-*
functions.
* lisp/wid-edit.el (key): New widget type.  (Bug#52523)
(widget-key-prompt-value-history): New variable.
(widget-key-validate): New function.
(key-sequence): Doc fix.
* doc/lispref/customize.texi (Simple Types): Document above new type.
---
 doc/lispref/customize.texi |  6 +++++-
 etc/NEWS                   |  5 +++++
 lisp/wid-edit.el           | 23 ++++++++++++++++++++++-
 3 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/doc/lispref/customize.texi b/doc/lispref/customize.texi
index 00287a7212..da5e761198 100644
--- a/doc/lispref/customize.texi
+++ b/doc/lispref/customize.texi
@@ -654,10 +654,14 @@ Simple Types
 specify the text to describe each value in a way that fits the specific
 meaning of the alternative.
 
+@item key
+The value is a valid key according to @kbd{key-valid-p}, and suitable
+for use with, for example @code{keymap-set}.
+
 @item key-sequence
 The value is a key sequence.  The customization buffer shows the key
 sequence using the same syntax as the @kbd{kbd} function.  @xref{Key
-Sequences}.
+Sequences}.  This is a legacy type; use @code{key} instead.
 
 @item coding-system
 The value must be a coding-system name, and you can do completion with
diff --git a/etc/NEWS b/etc/NEWS
index 01e1a8e940..ad9e61480b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -974,6 +974,11 @@ syntax.
 This is like 'kbd', but only returns vectors instead of a mix of
 vectors and strings.
 
++++
+*** New ':type' for 'defcustom' for keys.
+The new 'key' type can be used for options that should be a valid key
+according to 'key-valid-p'.  The type 'key-sequence' is now obsolete.
+
 +++
 ** New substitution in docstrings and 'substitute-command-keys'.
 Use \\`KEYSEQ' to insert a literal key sequence "KEYSEQ" (for example
diff --git a/lisp/wid-edit.el b/lisp/wid-edit.el
index a53add7d08..754aa83d0a 100644
--- a/lisp/wid-edit.el
+++ b/lisp/wid-edit.el
@@ -3459,7 +3459,7 @@ widget-key-sequence-map
     map))
 
 (define-widget 'key-sequence 'restricted-sexp
-  "A key sequence."
+  "A key sequence.  Now obsolete; use the `key' type instead."
   :prompt-value 'widget-field-prompt-value
   :prompt-internal 'widget-symbol-prompt-internal
 ; :prompt-match 'fboundp   ;; What was this good for?  KFS
@@ -3524,6 +3524,27 @@ widget-key-sequence-value-to-external
 	(read-kbd-macro value))
     value))
 
+
+(defvar widget-key-prompt-value-history nil
+  "History of input to `widget-key-prompt-value'.")
+
+(define-widget 'key 'editable-field
+  "A key sequence."
+  :prompt-value 'widget-field-prompt-value
+  :match 'key-valid-p
+  :format "%{%t%}: %v"
+  :validate 'widget-key-validate
+  :keymap widget-key-sequence-map
+  :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
+  :tag "Key")
+
+(defun widget-key-validate (widget)
+  (unless (and (stringp (widget-value widget))
+               (key-valid-p (widget-value widget)))
+    (widget-put widget :error (format "Invalid key: %S"
+                                      (widget-value widget)))
+    widget))
+
 
 (define-widget 'sexp 'editable-field
   "An arbitrary Lisp expression."
-- 
2.30.2