emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 570a11052b: keymap.el: Ease up support for non-`kbd` formats.


From: Stefan Monnier
Subject: master 570a11052b: keymap.el: Ease up support for non-`kbd` formats.
Date: Sun, 2 Oct 2022 14:09:13 -0400 (EDT)

branch: master
commit 570a11052be6178954956a1c59c8ebcbdb77d38e
Author: Stefan Monnier <monnier@iro.umontreal.ca>
Commit: Stefan Monnier <monnier@iro.umontreal.ca>

    keymap.el: Ease up support for non-`kbd` formats.
    
    While we want to standardize on the `kbd` syntax for user-facing code,
    the internal vector representation of key sequences is not going away,
    so let's not impose silly `key-description + key-parse` roundtrips.
    Also, provide some support for packages stuck with user configs defined
    to hold old-style key formats.
    
    * lisp/keymap.el (keymap-set): Allow vectors as `key`.
    (key-parse-old-format): New function, which stands out better than
    `key-description` when searching for uses of the old syntax.
    
    * list/outline.el (outline-minor-mode): Use it.
---
 lisp/keymap.el  | 19 ++++++++++++++++---
 lisp/outline.el |  3 ++-
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/lisp/keymap.el b/lisp/keymap.el
index 107565590c..c46e72f0a8 100644
--- a/lisp/keymap.el
+++ b/lisp/keymap.el
@@ -39,7 +39,8 @@
 
 (defun keymap-set (keymap key definition)
   "Set KEY to DEFINITION in KEYMAP.
-KEY is a string that satisfies `key-valid-p'.
+KEY is a string that satisfies `key-valid-p' (or a vector using the
+internal representation of key sequences).
 
 DEFINITION is anything that can be a key's definition:
  nil (means key is undefined in this keymap),
@@ -57,13 +58,13 @@ DEFINITION is anything that can be a key's definition:
  or an extended menu item definition.
  (See info node `(elisp)Extended Menu Items'.)"
   (declare (compiler-macro (lambda (form) (keymap--compile-check key) form)))
-  (keymap--check key)
+  (unless (vectorp key) (keymap--check key))
   ;; If we're binding this key to another key, then parse that other
   ;; key, too.
   (when (stringp definition)
     (keymap--check definition)
     (setq definition (key-parse definition)))
-  (define-key keymap (key-parse key) definition))
+  (define-key keymap (if (vectorp key) key (key-parse key)) definition))
 
 (defun keymap-global-set (key command)
   "Give KEY a global binding as COMMAND.
@@ -283,6 +284,18 @@ See `kbd' for a descripion of KEYS."
               (setq res (vconcat res key))))))
       res)))
 
+(defun key-parse-old-format (keys)
+  "Convert old-style string representation of KEYS to a vector.
+Simpler alternative to the Rube-Goldbergesque composition of
+`key-description' and `key-parse'."
+  (cond
+   ((vectorp keys) keys)
+   ((multibyte-string-p keys) (vconcat keys))
+   ((stringp keys)
+    (vconcat (mapcar (lambda (c) (if (> c 127) (logior (- c 128) ?\M-\0) c))
+                     keys)))
+   (t (error "Invalid old-style key sequence: %S" keys))))
+
 (defun key-valid-p (keys)
   "Say whether KEYS is a valid key.
 A key is a string consisting of one or more key strokes.
diff --git a/lisp/outline.el b/lisp/outline.el
index 93a9247f61..388a04ed25 100644
--- a/lisp/outline.el
+++ b/lisp/outline.el
@@ -496,7 +496,8 @@ See the command `outline-mode' for more information on this 
mode."
             "<right-margin> <mouse-1>" 'outline-cycle
             "<left-margin> S-<mouse-1>" 'outline-cycle-buffer
             "<right-margin> S-<mouse-1>" 'outline-cycle-buffer
-            (key-description outline-minor-mode-prefix) 
outline-mode-prefix-map)
+            (key-parse-old-format outline-minor-mode-prefix)
+            outline-mode-prefix-map)
   (if outline-minor-mode
       (progn
         (cond



reply via email to

[Prev in Thread] Current Thread [Next in Thread]