Make `lookup-key' understand the new key sequence syntax

* src/keymap.c (possibly_translate_key_sequence): Factored out
into own function.
(Fdefine_key):
(Flookup_key): Use it.
This commit is contained in:
Lars Ingebrigtsen 2021-10-19 05:07:51 +02:00
parent 5c996471ba
commit f3aa648093
2 changed files with 32 additions and 15 deletions

View file

@ -1024,6 +1024,28 @@ is not copied. */)
/* Simple Keymap mutators and accessors. */
static Lisp_Object
possibly_translate_key_sequence (Lisp_Object key, ptrdiff_t *length)
{
if (VECTORP (key) && ASIZE (key) == 1 && STRINGP (AREF (key, 0)))
{
/* KEY is on the ["C-c"] format, so translate to internal
format. */
if (NILP (Ffboundp (Qkbd_valid_p)))
xsignal2 (Qerror,
build_string ("`kbd-valid-p' is not defined, so this syntax can't be used: %s"),
key);
if (NILP (call1 (Qkbd_valid_p, AREF (key, 0))))
xsignal2 (Qerror, build_string ("Invalid `kbd' syntax: %S"), key);
key = call1 (Qkbd, AREF (key, 0));
*length = CHECK_VECTOR_OR_STRING (key);
if (*length == 0)
xsignal2 (Qerror, build_string ("Invalid `kbd' syntax: %S"), key);
}
return key;
}
/* GC is possible in this function if it autoloads a keymap. */
DEFUN ("define-key", Fdefine_key, Sdefine_key, 3, 3, 0,
@ -1084,21 +1106,7 @@ binding KEY to DEF is added at the front of KEYMAP. */)
def = tmp;
}
if (VECTORP (key) && ASIZE (key) == 1 && STRINGP (AREF (key, 0)))
{
/* KEY is on the ["C-c"] format, so translate to internal
format. */
if (NILP (Ffboundp (Qkbd_valid_p)))
xsignal2 (Qerror,
build_string ("`kbd-valid-p' is not defined, so this syntax can't be used: %s"),
key);
if (NILP (call1 (Qkbd_valid_p, AREF (key, 0))))
xsignal2 (Qerror, build_string ("Invalid `kbd' syntax: %S"), key);
key = call1 (Qkbd, AREF (key, 0));
length = CHECK_VECTOR_OR_STRING (key);
if (length == 0)
xsignal2 (Qerror, build_string ("Invalid `kbd' syntax: %S"), key);
}
key = possibly_translate_key_sequence (key, &length);
ptrdiff_t idx = 0;
while (1)
@ -1229,6 +1237,8 @@ recognize the default bindings, just as `read-key-sequence' does. */)
if (length == 0)
return keymap;
key = possibly_translate_key_sequence (key, &length);
ptrdiff_t idx = 0;
while (1)
{

View file

@ -317,6 +317,13 @@ g .. h foo
(should (equal (single-key-description 'C-s-home)
"C-s-<home>")))
(ert-deftest keymap-test-lookups ()
(should (eq (lookup-key (current-global-map) "\C-x\C-f") 'find-file))
(should (eq (lookup-key (current-global-map) [(control x) (control f)])
'find-file))
(should (eq (lookup-key (current-global-map) ["C-x C-f"]) 'find-file))
(should (eq (lookup-key (current-global-map) [?\C-x ?\C-f]) 'find-file)))
(provide 'keymap-tests)
;;; keymap-tests.el ends here