読者です 読者をやめる 読者になる 読者になる

Get key binding from command

emacs

Sometimes I want to get key bindings of command for implementing like following command. Please see mini-buffer.

f:id:syohex:20160813150435p:plain

(where-is-internal func &optional map ...)

where-is-internal returns list of keys of command(func) from map. For example

(where-is-internal 'next-line global-map)
;; => ([14] [down])

[14] is Control-n and [down] is down arrow key. Type of return value is list of key vector. It should be converted to human readable style.

(key-description key-vector)

key-description returns a pretty description of key-sequence. For example

(key-description [14])
;; => "C-n"

Write function

So we can write the function which returns human readable key sequence from the command

(cl-defun function-to-keybinds (func &optional (map global-map))
  (key-description (car-safe (where-is-internal func map))))

(function-to-keybinds #'next-line)
;; => "C-n"
(function-to-keybinds #'python-shell-send-string python-mode-map)
;; => "C-c C-s"