From: Lars Ingebrigtsen Date: Tue, 19 Oct 2021 03:07:51 +0000 (+0200) Subject: Make `lookup-key' understand the new key sequence syntax X-Git-Tag: emacs-29.0.90~3671^2~500 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=f3aa648093a70c8ed15e764863a16fdf7126cdc4;p=emacs.git 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. --- diff --git a/src/keymap.c b/src/keymap.c index 60e736efc71..ca1dbe368ea 100644 --- a/src/keymap.c +++ b/src/keymap.c @@ -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) { diff --git a/test/src/keymap-tests.el b/test/src/keymap-tests.el index 68b42c346c9..13f47b45f81 100644 --- a/test/src/keymap-tests.el +++ b/test/src/keymap-tests.el @@ -317,6 +317,13 @@ g .. h foo (should (equal (single-key-description 'C-s-home) "C-s-"))) +(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