0

When viewing a keymap, prefix keys and keys in their maps are displayed as numerical values. E.g.

(keymap
 (M-delete . sp-unwrap-sexp)
 (27 keymap
     (23 . sp-copy-sexp)
     (6 . *-forward-sexp)))

Keys at the top-level map, as we can see, are not numbers. Here, there is M-delete, which is clearly easier to understand than 27.

Is it possible to turn these numbers back to keys sequences? How?

  • You can view a keymap in a human-readable way, showing key sequences as you expect, using C-h M-k (describe-keymap) from help-fns+.el. (See Help+.) – Drew Feb 1 '18 at 15:01
1

You can use single-key-description to convert a number into a key in string form, for example,

?\C-x
     => 24

(single-key-description 24)
     => "C-x"

I have the following in my init file:

(defun chunyang-display-number-as-char (&optional undo)
  "Display number as character, for example, display 24 as C-x.

Why? Becuase I find the output of 'C-h v help-map' is hard to
read."
  (interactive "P")
  (if undo
      (remove-overlays nil nil 'chunyang-show-number-as-char t)
    (save-excursion
      (goto-char (point-min))
      (let (ov)
        (while (re-search-forward "[0-9]+" nil :no-error)
          (setq ov (make-overlay (match-beginning 0) (match-end 0)))
          (overlay-put ov 'display (single-key-description
                                    (string-to-number (match-string 0))))
          (overlay-put ov 'chunyang-show-number-as-char t))))))
  • You learn something new every day 😃. Now, is there an inverse function that converts the string back to the number? – Tianxiang Xiong Feb 1 '18 at 7:39
  • 1
    @TianxiangXiong Maybe (aref (read-kbd-macro "C-x" t) 0) => 24. – xuchunyang Feb 1 '18 at 7:43
  • Ugh...really wish Emacs had a more straightforward API for this stuff. You'd think something as fundamental as keymaps would have every utility function anyone would want available 😛. Thanks! – Tianxiang Xiong Feb 1 '18 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.