Skip to content

Instantly share code, notes, and snippets.

@halbtuerke
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halbtuerke/af1cd2e771ca58e83cc0 to your computer and use it in GitHub Desktop.
Save halbtuerke/af1cd2e771ca58e83cc0 to your computer and use it in GitHub Desktop.
Find key sequences of Emacs lisp function
;; PACKAGE: DASH
(use-package dash
:defer t
:ensure t)
(defun my-where-is (definition count &optional length)
"Return COUNT key sequences that invoke the command DEFINITTION as a string.
If COUNT is 0 then all key sequences will be returned.
The optional LENGTH argument limits the returned string to LENGTH number of chars.
For convenience there are also the functions `my-where-is-first' and `my-where-is-all'.
This function relies on Magnar Sveen's dash.el library."
(interactive
(let ((fn (function-called-at-point))
(enable-recursive-minibuffers t)
val)
(setq val (completing-read
(if fn
(format "Where is command (default %s): " fn)
"Where is command: ")
obarray 'commandp t nil nil
(and fn (symbol-name fn))))
(list (unless (equal val "") (intern val))
current-prefix-arg)))
(unless definition (error "No command"))
(let ((func (indirect-function definition))
(defs nil)
string)
;; In DEFS, find all symbols that are aliases for DEFINITION.
(mapatoms (lambda (symbol)
(and (fboundp symbol)
(not (eq symbol definition))
(eq func (condition-case ()
(indirect-function symbol)
(error symbol)))
(push symbol defs))))
;; Look at all the symbols--first DEFINITION,
;; then its aliases.
(dolist (symbol (cons definition defs))
(let* ((remapped (command-remapping symbol))
(keys (where-is-internal
symbol overriding-local-map nil nil remapped))
(keys (if (> count 0)
(mapconcat 'key-description (-take count keys) ", ")
(mapconcat 'key-description keys ", "))))
(setq string
(if (> (length keys) 0)
(if remapped
(format "%s is remapped to %s which is on %s"
symbol remapped keys)
(format "%s" keys))
;; If this is the command the user asked about,
;; and it is not on any key, say so.
;; For other symbols, its aliases, say nothing
;; about them unless they are on keys.
(if (eq symbol definition)
(format "NA"))))))
(if length
(substring string 0 (min (length string) (+ 1 length)))
string)))
(defun my-where-is-first (definition &optional length)
"Return the first key sequence for DEFINITION as a string.
The optional LENGTH argument limits the returned string to LENGTH number of chars.
This is a convenience function for `my-where-is'."
(interactive)
(my-where-is definition 1 length))
(defun my-where-is-all (definition &optional length)
"Return all key sequence for DEFINITION as a string.
The optional LENGTH argument limits the returned string to LENGTH number of chars.
This is a convenience function for `my-where-is'."
(interactive)
(my-where-is definition 0 length))
;; ######################################################################
;;
;; EXAMPLES
;;
;; ######################################################################
(my-where-is 'helm-projectile 1 10)
(my-where-is-first 'helm-projectile 5)
(my-where-is-all 'helm-projectile)
(defhydra hydra-projectile (:color teal)
"
PROJECTILE
Find File Search/Tags Buffers Cache
------------------------------------------------------------------------------------------
_s-f_: file _a_: ag %(my-where-is-first 'projectile-ag) ^^_i_: Ibuffer _c_: cache clear
^_ff_: file dwim _g_: update gtags ^^_b_: switch to buffer %(my-where-is-first 'projectile-switch-to-buffer) _x_: remove known project
^_fd_: file curr dir _o_: multi-occur _s-k_: Kill all buffers _X_: cleanup non-existing
^^_r_: recent file ^^_z_: cache current
^^_d_: dir
"
("a" projectile-ag nil)
("b" projectile-switch-to-buffer nil)
("c" projectile-invalidate-cache nil)
("d" projectile-find-dir nil)
("s-f" projectile-find-file nil)
("ff" projectile-find-file-dwim nil)
("fd" projectile-find-file-in-directory nil)
("g" ggtags-update-tags nil)
("s-g" ggtags-update-tags nil)
("i" projectile-ibuffer nil)
("K" projectile-kill-buffers nil)
("s-k" projectile-kill-buffers nil)
("m" projectile-multi-occur nil)
("o" projectile-multi-occur nil)
("s-p" projectile-switch-project "switch project")
("p" projectile-switch-project nil)
("s" projectile-switch-project nil)
("r" projectile-recentf nil)
("x" projectile-remove-known-project nil)
("X" projectile-cleanup-known-projects nil)
("z" projectile-cache-current-file nil)
("`" hydra-projectile-other-window/body "other window")
("q" nil "cancel" :color blue))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment