Subject: bug#45035: 28.0.50; Command completion annotations show minibuffer keybindings

From: Juri Linkov <[email-masked]>
Date: Wed, 16 Dec 2020 11:15:40 +0200

>> Thanks, I think your idea is correct.  But I'm not sure if
>> minibuffer-completion-help is the right place for wrapping in
>> with-minibuffer-selected-window.  This means that all calls of
>> annotation-function will be in the original buffer, whereas
>> only read-extended-command--annotation needs such wrapping.
>
> Oh, that's a very good point. Probably it would be best to leave
> minibuffer-completion-help as is, and change only
> read-extended-command--annotation.
>
>> Or do you care that such wrapping will be called for every command
>> in read-extended-command--annotation?  Then instead of annotation-function
>> you could try to use a new function affixation-function that is called
>> only once, so you could add wrapping to it.
>
> My Emacs doesn't have affixation-function yet, so I don't really know
> anything about it.

I'm sorry that your version of Emacs is not updated too often.
So here I created a patch based on current master, and after
pushing it, sometimes it will arrive to your updated version.

diff --git a/lisp/simple.el b/lisp/simple.el
index c309df11ad..f669996c2f 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1958,22 +1958,27 @@ read-extended-command
      (lambda (string pred action)
        (if (and suggest-key-bindings (eq action 'metadata))
 	   '(metadata
-	     (annotation-function . read-extended-command--annotation)
+	     (affixation-function . read-extended-command--affixation)
 	     (category . command))
          (complete-with-action action obarray string pred)))
      #'commandp t nil 'extended-command-history)))
 
-(defun read-extended-command--annotation (command-name)
-  (let* ((fun (and (stringp command-name) (intern-soft command-name)))
-         (binding (where-is-internal fun overriding-local-map t))
-         (obsolete (get fun 'byte-obsolete-info))
-         (alias (symbol-function fun)))
-    (cond ((symbolp alias)
-           (format " (%s)" alias))
-          (obsolete
-           (format " (%s)" (car obsolete)))
-          ((and binding (not (stringp binding)))
-           (format " (%s)" (key-description binding))))))
+(defun read-extended-command--affixation (command-names)
+  (with-selected-window (or (minibuffer-selected-window) (selected-window))
+    (mapcar
+     (lambda (command-name)
+       (let* ((fun (and (stringp command-name) (intern-soft command-name)))
+              (binding (where-is-internal fun overriding-local-map t))
+              (obsolete (get fun 'byte-obsolete-info))
+              (alias (symbol-function fun))
+              (suffix (cond ((symbolp alias)
+                             (format " (%s)" alias))
+                            (obsolete
+                             (format " (%s)" (car obsolete)))
+                            ((and binding (not (stringp binding)))
+                             (format " (%s)" (key-description binding))))))
+         (if suffix (list command-name suffix) command-name)))
+     command-names)))
 
 (defcustom suggest-key-bindings t
   "Non-nil means show the equivalent key-binding when M-x command has one.