GNU bug report logs - #39035
Show key bindings on M-x completion

Previous Next

Package: emacs;

Reported by: Stefan Kangas <stefan <at> marxist.se>

Date: Wed, 8 Jan 2020 10:18:01 UTC

Severity: wishlist

Tags: fixed

Fixed in version 28.1

Done: Juri Linkov <juri <at> linkov.net>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 39035 in the body.
You can then email your comments to 39035 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#39035; Package emacs. (Wed, 08 Jan 2020 10:18:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Stefan Kangas <stefan <at> marxist.se>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 08 Jan 2020 10:18:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefan <at> marxist.se>
To: bug-gnu-emacs <at> gnu.org
Subject: Show key bindings on M-x completion
Date: Wed, 8 Jan 2020 11:17:16 +0100
Severity: wishlist

In the completion interface for M-x, please add functionality to show
the key bindings in parenthesis after the command.  This functionality
is already there in the highly popular helm and swiper completion
frameworks, which could be useful for reference.

For example, when typing M-x kmacro TAB, one should see:

   kmacro-insert-counter (C-x C-k TAB)
   kmacro-set-counter (C-x C-k C-c)
   [...]

Ideally, the keybinding should also use a different color from the command.

This was discussed on emacs-devel, and Stefan Monnier suggested that
it shouldn't be too hard to do:
https://lists.gnu.org/archive/html/emacs-devel/2020-01/msg00115.html

Best regards,
Stefan Kangas




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#39035; Package emacs. (Tue, 28 Jan 2020 23:17:01 GMT) Full text and rfc822 format available.

Message #8 received at 39035 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Stefan Kangas <stefan <at> marxist.se>
Cc: 39035 <at> debbugs.gnu.org, Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: Re: bug#39035: Show key bindings on M-x completion
Date: Tue, 28 Jan 2020 23:39:13 +0200
[Message part 1 (text/plain, inline)]
> Severity: wishlist
>
> In the completion interface for M-x, please add functionality to show
> the key bindings in parenthesis after the command.  This functionality
> is already there in the highly popular helm and swiper completion
> frameworks, which could be useful for reference.
>
> For example, when typing M-x kmacro TAB, one should see:
>
>    kmacro-insert-counter (C-x C-k TAB)
>    kmacro-set-counter (C-x C-k C-c)
>    [...]
>
> Ideally, the keybinding should also use a different color from the command.
>
> This was discussed on emacs-devel, and Stefan Monnier suggested that
> it shouldn't be too hard to do:
> https://lists.gnu.org/archive/html/emacs-devel/2020-01/msg00115.html

Indeed, not hard at all:

[read-extended-command--annotation.patch (text/x-diff, inline)]
diff --git a/lisp/simple.el b/lisp/simple.el
index 00a706848b..b0159df203 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -1783,17 +1783,29 @@ read-extended-command
 	     ;; 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 . suggest-key-bindings))
+         (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 binding
+      (format " (%s)" (if (stringp binding)
+                          (concat "M-x " binding " RET")
+                        (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.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#39035; Package emacs. (Wed, 29 Jan 2020 03:04:02 GMT) Full text and rfc822 format available.

Message #11 received at 39035 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Juri Linkov <juri <at> linkov.net>
Cc: 39035 <at> debbugs.gnu.org, Stefan Kangas <stefan <at> marxist.se>
Subject: Re: bug#39035: Show key bindings on M-x completion
Date: Tue, 28 Jan 2020 22:02:55 -0500
> Indeed, not hard at all:

Looks about right, thanks.

> +       (if (and suggest-key-bindings (eq action 'metadata))
> +	   '(metadata
> +	     (annotation-function . read-extended-command--annotation)
> +	     (category . suggest-key-bindings))

The `category` should describe the things that are being completed,
i.e. `command` rather than `suggest-key-bindings`.

> +(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 binding
> +      (format " (%s)" (if (stringp binding)
> +                          (concat "M-x " binding " RET")
> +                        (key-description binding))))))

Since we're in the middle of a `M-x`, I think a `M-x foo RET` annotation
would not be useful at all: better not put anything when a command has
no corresponding key binding (one could argue that it could be useful to
put the `M-x <shortenedname> RET` like `M-x re-u RET` for
`rename-uniquely`, but it would be too costly to compute and still
wouldn't be terribly useful).


        Stefan





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#39035; Package emacs. (Wed, 29 Jan 2020 23:45:02 GMT) Full text and rfc822 format available.

Message #14 received at 39035 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: 39035 <at> debbugs.gnu.org, Stefan Kangas <stefan <at> marxist.se>
Subject: Re: bug#39035: Show key bindings on M-x completion
Date: Thu, 30 Jan 2020 01:42:55 +0200
tags 39035 fixed
close 39035 28.1
quit

>> +       (if (and suggest-key-bindings (eq action 'metadata))
>> +	   '(metadata
>> +	     (annotation-function . read-extended-command--annotation)
>> +	     (category . suggest-key-bindings))
>
> The `category` should describe the things that are being completed,
> i.e. `command` rather than `suggest-key-bindings`.

Fixed.

>> +(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 binding
>> +      (format " (%s)" (if (stringp binding)
>> +                          (concat "M-x " binding " RET")
>> +                        (key-description binding))))))
>
> Since we're in the middle of a `M-x`, I think a `M-x foo RET` annotation
> would not be useful at all: better not put anything when a command has
> no corresponding key binding (one could argue that it could be useful to
> put the `M-x <shortenedname> RET` like `M-x re-u RET` for
> `rename-uniquely`, but it would be too costly to compute and still
> wouldn't be terribly useful).

Thanks for suggestions, the fixed patch was pushed to master.




Added tag(s) fixed. Request was from Juri Linkov <juri <at> linkov.net> to control <at> debbugs.gnu.org. (Wed, 29 Jan 2020 23:45:03 GMT) Full text and rfc822 format available.

bug marked as fixed in version 28.1, send any further explanations to 39035 <at> debbugs.gnu.org and Stefan Kangas <stefan <at> marxist.se> Request was from Juri Linkov <juri <at> linkov.net> to control <at> debbugs.gnu.org. (Wed, 29 Jan 2020 23:45:03 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 27 Feb 2020 12:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 4 years and 59 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.