From: Stefan Kangas Date: Sun, 2 Jan 2022 15:46:02 +0000 (+0100) Subject: New :type key for defcustom X-Git-Tag: emacs-29.0.90~3297 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=984391a9dc384627533758f6fced219b5381c91f;p=emacs.git 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. --- diff --git a/doc/lispref/customize.texi b/doc/lispref/customize.texi index 9508ca86200..54059d7b6ed 100644 --- a/doc/lispref/customize.texi +++ b/doc/lispref/customize.texi @@ -654,10 +654,14 @@ you can specify that the value must be @code{nil} or @code{t}, but also 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 29e329edc90..9c892b285d1 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -985,6 +985,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 831b2ccfca8..f00a524c0c4 100644 --- a/lisp/wid-edit.el +++ b/lisp/wid-edit.el @@ -3460,7 +3460,7 @@ It reads a directory name from an editable text field." map)) (define-widget 'key-sequence 'restricted-sexp - "A key sequence." + "A key sequence. This is 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 @@ -3525,6 +3525,27 @@ It reads a directory name from an editable text field." (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."