From 5db3324a7e11280437e69e27fe364dfd80b66ca2 Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Thu, 30 Jan 2020 01:42:11 +0200 Subject: [PATCH] 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. --- etc/NEWS | 3 +++ lisp/simple.el | 33 +++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 5395f2ccfb2..04ae3a4ac86 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -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' diff --git a/lisp/simple.el b/lisp/simple.el index 2ec3da680f8..09447900ded 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -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) -- 2.39.2