Show key bindings on M-x completion (bug#39035)

* lisp/simple.el (read-extended-command--annotation): New function.
(read-extended-command): Use annotation-function to show key-bindings.
This commit is contained in:
Juri Linkov 2020-01-30 01:42:11 +02:00
parent bb3b0990d8
commit 5db3324a7e
2 changed files with 26 additions and 10 deletions

View file

@ -109,6 +109,9 @@ like cell phones, tablets or cameras.
* Incompatible Lisp Changes in Emacs 28.1
** When 'suggest-key-bindings' is non-nil, the completion list of M-x
shows equivalent key-bindings for all commands that have them.
** 'equal' no longer examines some contents of window configurations.
Instead, it considers window configurations to be equal only if they
are 'eq'. To compare contents, use 'compare-window-configurations'

View file

@ -1788,23 +1788,36 @@ to get different commands to edit and resubmit."
;; and it serves as a shorthand for "Extended command: ".
"M-x ")
(lambda (string pred action)
(let ((pred
(if (memq action '(nil t))
;; Exclude obsolete commands from completions.
(lambda (sym)
(and (funcall pred sym)
(or (equal string (symbol-name sym))
(not (get sym 'byte-obsolete-info)))))
pred)))
(complete-with-action action obarray string pred)))
(if (and suggest-key-bindings (eq action 'metadata))
'(metadata
(annotation-function . read-extended-command--annotation)
(category . command))
(let ((pred
(if (memq action '(nil t))
;; Exclude obsolete commands from completions.
(lambda (sym)
(and (funcall pred sym)
(or (equal string (symbol-name sym))
(not (get sym 'byte-obsolete-info)))))
pred)))
(complete-with-action action obarray string pred))))
#'commandp t nil 'extended-command-history)))
(defun read-extended-command--annotation (command-name)
(let* ((function (and (stringp command-name) (intern-soft command-name)))
(binding (where-is-internal function overriding-local-map t)))
(when (and binding (not (stringp binding)))
(format " (%s)" (key-description binding)))))
(defcustom suggest-key-bindings t
"Non-nil means show the equivalent key-binding when M-x command has one.
The value can be a length of time to show the message for.
If the value is non-nil and not a number, we wait 2 seconds.
Also see `extended-command-suggest-shorter'."
Also see `extended-command-suggest-shorter'.
Equivalent key-bindings are also shown in the completion list of
M-x for all commands that have them."
:group 'keyboard
:type '(choice (const :tag "off" nil)
(integer :tag "time" 2)