Skip to content

Commit 70dee6f

Browse files
committed
Show keybindings in smex results.
Getting keybindings is relatively slow (a few seconds), so we only update keybindings when Emacs is idle. We repurpose `smex-auto-update` for this, as it's currently unused.
1 parent 8c40191 commit 70dee6f

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

smex.el

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,45 @@ Set this to nil to disable fuzzy matching."
128128
(setq commands (mapcar #'symbol-name commands))
129129
(smex-read-and-run commands)))
130130

131+
(defun smex-extract-command-name (pretty-name)
132+
"Given a string \"foo (C-c f)\", return \"foo\"."
133+
(car (split-string pretty-name " ")))
134+
135+
(defvar smex-command-keybindings
136+
(make-hash-table)
137+
"A keybinding (a string) for each command symbol.")
138+
139+
(defun smex-update-command-keybindings ()
140+
"Ensure `smex-command-keybindings' contains the latest keybindings.
141+
This can take ~5 seconds, so we only run when Emacs
142+
is idle."
143+
(interactive)
144+
(clrhash smex-command-keybindings)
145+
(mapc (lambda (symbol-with-index)
146+
(let* ((symbol (car symbol-with-index))
147+
(keybinding (smex-find-keybinding symbol)))
148+
(if keybinding
149+
(puthash symbol keybinding smex-command-keybindings))))
150+
smex-cache))
151+
152+
(defun smex-find-keybinding (command)
153+
"Find the first keybinding for COMMAND, if one exists.
154+
Uses the currently active keymap."
155+
(let* ((keybindings (where-is-internal command))
156+
(first-binding (car keybindings)))
157+
(when first-binding
158+
(key-description first-binding))))
159+
131160
(defun smex-completing-read (choices initial-input)
132161
(let ((ido-completion-map ido-completion-map)
133162
(ido-setup-hook (cons 'smex-prepare-ido-bindings ido-setup-hook))
134163
(ido-enable-prefix nil)
135164
(ido-enable-flex-matching smex-flex-matching)
136165
(ido-max-prospects 10)
137166
(minibuffer-completion-table choices))
138-
(ido-completing-read (smex-prompt-with-prefix-arg) choices nil nil
139-
initial-input 'extended-command-history (car choices))))
167+
(smex-extract-command-name
168+
(ido-completing-read (smex-prompt-with-prefix-arg) choices nil nil
169+
initial-input 'extended-command-history (car choices)))))
140170

141171
(defun smex-prompt-with-prefix-arg ()
142172
(if (not current-prefix-arg)
@@ -183,7 +213,15 @@ Set this to nil to disable fuzzy matching."
183213
(setq smex-ido-cache (smex-convert-for-ido smex-cache)))
184214

185215
(defun smex-convert-for-ido (command-items)
186-
(mapcar (lambda (command-item) (symbol-name (car command-item))) command-items))
216+
(mapcar (lambda (command-item)
217+
(let* ((command (car command-item))
218+
(command-name (symbol-name command))
219+
(keybinding
220+
(gethash command smex-command-keybindings)))
221+
(if keybinding
222+
(format "%s (%s)" command-name keybinding)
223+
command-name)))
224+
command-items))
187225

188226
(defun smex-restore-history ()
189227
"Rearranges `smex-cache' according to `smex-history'"
@@ -223,11 +261,12 @@ Set this to nil to disable fuzzy matching."
223261
(unless (= i smex-command-count)
224262
(setq smex-command-count i))))
225263

226-
(defun smex-auto-update (&optional idle-time)
227-
"Update Smex when Emacs has been idle for IDLE-TIME."
228-
(unless idle-time (setq idle-time 60))
229-
(run-with-idle-timer idle-time t
230-
'(lambda () (if (smex-detect-new-commands) (smex-update)))))
264+
(defun smex-background-update ()
265+
"Update Smex when Emacs has been idle for 20 seconds."
266+
(run-with-idle-timer 20 t
267+
'(lambda () (when (smex-detect-new-commands)
268+
(smex-update-command-keybindings)
269+
(smex-update)))))
231270

232271
;;;###autoload
233272
(defun smex-initialize ()
@@ -237,6 +276,7 @@ Set this to nil to disable fuzzy matching."
237276
(smex-detect-new-commands)
238277
(smex-rebuild-cache)
239278
(add-hook 'kill-emacs-hook 'smex-save-to-file)
279+
(smex-background-update)
240280
(setq smex-initialized-p t))
241281

242282
(defun smex-initialize-ido ()

0 commit comments

Comments
 (0)