unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#39035: Show key bindings on M-x completion
@ 2020-01-08 10:17 Stefan Kangas
  2020-01-28 21:39 ` Juri Linkov
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Kangas @ 2020-01-08 10:17 UTC (permalink / raw)
  To: 39035

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





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#39035: Show key bindings on M-x completion
  2020-01-08 10:17 bug#39035: Show key bindings on M-x completion Stefan Kangas
@ 2020-01-28 21:39 ` Juri Linkov
  2020-01-29  3:02   ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Juri Linkov @ 2020-01-28 21:39 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: 39035, Stefan Monnier

[-- Attachment #1: Type: text/plain, Size: 725 bytes --]

> 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:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: read-extended-command--annotation.patch --]
[-- Type: text/x-diff, Size: 1949 bytes --]

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.

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* bug#39035: Show key bindings on M-x completion
  2020-01-28 21:39 ` Juri Linkov
@ 2020-01-29  3:02   ` Stefan Monnier
  2020-01-29 23:42     ` Juri Linkov
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2020-01-29  3:02 UTC (permalink / raw)
  To: Juri Linkov; +Cc: 39035, Stefan Kangas

> 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






^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#39035: Show key bindings on M-x completion
  2020-01-29  3:02   ` Stefan Monnier
@ 2020-01-29 23:42     ` Juri Linkov
  0 siblings, 0 replies; 4+ messages in thread
From: Juri Linkov @ 2020-01-29 23:42 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 39035, Stefan Kangas

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.





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-01-29 23:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-01-08 10:17 bug#39035: Show key bindings on M-x completion Stefan Kangas
2020-01-28 21:39 ` Juri Linkov
2020-01-29  3:02   ` Stefan Monnier
2020-01-29 23:42     ` Juri Linkov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).