[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master f3aa648: Make `lookup-key' understand the new key sequence syntax
From: |
Lars Ingebrigtsen |
Subject: |
master f3aa648: Make `lookup-key' understand the new key sequence syntax |
Date: |
Mon, 18 Oct 2021 23:08:02 -0400 (EDT) |
branch: master
commit f3aa648093a70c8ed15e764863a16fdf7126cdc4
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>
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.
---
src/keymap.c | 40 +++++++++++++++++++++++++---------------
test/src/keymap-tests.el | 7 +++++++
2 files changed, 32 insertions(+), 15 deletions(-)
diff --git a/src/keymap.c b/src/keymap.c
index 60e736e..ca1dbe3 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 68b42c3..13f47b4 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-<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
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- master f3aa648: Make `lookup-key' understand the new key sequence syntax,
Lars Ingebrigtsen <=