Skip to content

Commit

Permalink
Add support for browsing active keymaps
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiltyDolphin committed May 8, 2020
1 parent f7a15ae commit 351525c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
- =CHANGELOG.org=
- =.travis.yml=
- new dependency: =dash (2.17.0)=
- you can now specify a prefix argument to
=emaps-describe-keymap= and =emaps-describe-keymap-bindings=
to select from only active keymaps

** Changed

Expand Down
3 changes: 2 additions & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Run ~M-x emaps-describe-keymap-bindings~ to display the
bindings for a given keymap (see below),
~emaps-describe-keymap~ can be used to view keymaps as
variables (but with characters displayed instead of integers,
where possible).
where possible). Executing these commands with a prefix
argument allows you to choose from only active keymaps.

#+CAPTION: Display after running ~emaps-describe-keymap-bindings~ with ~magit-status-mode-map~.
#+NAME: fig:emaps-describe-keymap-bindings-magit-status-mode-map
Expand Down
28 changes: 20 additions & 8 deletions emaps.el
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
"Return non-NIL if X is a symbol with a value."
(and (symbolp x) (boundp x)))

(defun emaps--active-keymap-p (x)
"Return non-NIL if X is a keymap that is currently active."
(and (keymapp x) (memq x (current-active-maps))))

(defun emaps--completing-read-variable (prompt &optional pred def)
"Prompt the user with PROMPT for a variable that satisfied PRED (if supplied).
Expand All @@ -78,7 +82,9 @@ If DEF is supplied and satisfies PRED, use that as the default value, otherwise
(lambda (it)
(and (emaps--bound-symbol-p it)
(if pred (funcall pred (symbol-value it)) t))))
(v (or (and (funcall check def) def) (variable-at-point)))
(v (or (and (funcall check def) def)
(let ((var (variable-at-point)))
(and (funcall check var) var))))
vars
val)
(mapatoms (lambda (atom) (when (funcall check atom) (push atom vars))))
Expand All @@ -90,7 +96,7 @@ If DEF is supplied and satisfies PRED, use that as the default value, otherwise
vars
check
t nil nil
(if (symbolp v) (symbol-name v))))
(if (funcall check v) (symbol-name v))))
(list (if (equal val "") v (intern val)))))

(defun emaps--keymap-symbol-p (x)
Expand All @@ -102,10 +108,12 @@ If DEF is supplied and satisfies PRED, use that as the default value, otherwise
(let ((vap (variable-at-point)))
(and (emaps--keymap-symbol-p vap) vap)))

(defun emaps--read-keymap ()
"Read the name of a keymap from the minibuffer and return it as a symbol."
(defun emaps--read-keymap (&optional active-only)
"Read the name of a keymap from the minibuffer and return it as a symbol.
If ACTIVE-ONLY is non-NIL, allow selection only from active keymaps."
(emaps--completing-read-variable
"Enter keymap" 'keymapp
"Enter keymap" (if active-only #'emaps--active-keymap-p 'keymapp)
;; if there is a keymap at point, use this as the default as the
;; user probably means to query this, otherwise default to the
;; keymap variable for the current major mode.
Expand All @@ -115,8 +123,10 @@ If DEF is supplied and satisfies PRED, use that as the default value, otherwise
(defun emaps-describe-keymap (keymap)
"Display the full documentation of KEYMAP (a symbol).
When interactively called with a prefix argument, prompt only for active keymaps.
Unlike `describe-variable', this will display characters as strings rather than integers."
(interactive (emaps--read-keymap))
(interactive (emaps--read-keymap current-prefix-arg))
(describe-variable keymap)
(emaps--with-modify-help-buffer
(save-excursion
Expand All @@ -134,8 +144,10 @@ Unlike `describe-variable', this will display characters as strings rather than

;;;###autoload
(defun emaps-describe-keymap-bindings (keymap)
"Like `describe-bindings', but only describe bindings in KEYMAP."
(interactive (emaps--read-keymap))
"Like `describe-bindings', but only describe bindings in KEYMAP.
When interactively called with a prefix argument, prompt only for active keymaps."
(interactive (progn (emaps--read-keymap current-prefix-arg)))
(let* ((keymap-name (if (emaps--keymap-symbol-p keymap) (symbol-name keymap) "?"))
(keymap (if (emaps--keymap-symbol-p keymap) (symbol-value keymap) keymap))
(temp-map '(keymap))
Expand Down

0 comments on commit 351525c

Please sign in to comment.