2

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?

Tianxiang Xiong
  • 3,878
  • 18
  • 28

1 Answers1

5

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))))))
xuchunyang
  • 14,527
  • 1
  • 19
  • 39
  • You learn something new every day . Now, is there an inverse function that converts the string back to the number? – Tianxiang Xiong Feb 01 '18 at 07:39
  • 1
    @TianxiangXiong Maybe (aref (read-kbd-macro "C-x" t) 0) => 24. – xuchunyang Feb 01 '18 at 07: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 01 '18 at 07:57