unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: 45536@debbugs.gnu.org
Subject: bug#45536: [PATCH] Pretty-print keys without <> around modifiers
Date: Sat, 2 Jan 2021 18:20:52 +0100	[thread overview]
Message-ID: <6AE36074-6531-42B0-83D7-A03774E2BF5C@acm.org> (raw)
In-Reply-To: <877dowc1of.fsf@gnus.org>

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

1 jan. 2021 kl. 12.54 skrev Lars Ingebrigtsen <larsi@gnus.org>:

> Oh, right.  Well, if the manual displays these keys as M-<foo> etc, then
> I withdraw all my objections based on possible confusion -- this change
> would make things less confusing.  So I'm leaning towards applying the
> change and then see whether this breaks anything for anybody.

Good, here is the elaborated patch, complete with NEWS entry, a few doc fixes, and tests.


[-- Attachment #2: 0001-Pretty-print-keys-without-around-modifiers-bug-45536.patch --]
[-- Type: application/octet-stream, Size: 5993 bytes --]

From a50b205b43471938b547e4aec4a517bae7ca162d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Tue, 29 Dec 2020 16:55:06 +0100
Subject: [PATCH] Pretty-print keys without <> around modifiers (bug#45536)

Be consistent when pretty-printing keys: put modifiers outside <>,
thus the more logical C-M-<return> instead of <C-M-return>.

* src/keymap.c (Fsingle_key_description):
Skip modifier prefix before adding <>.
* doc/lispref/help.texi (Describing Characters): Update example.
* doc/lispref/debugging.texi (Backtraces):
* doc/lispref/minibuf.texi (Text from Minibuffer):
Use @kbd instead of @key.
* etc/NEWS: Announce the change.
* test/src/keymap-tests.el (keymap--key-description):
* test/lisp/subr-tests.el (subr--kbd): New tests.
---
 doc/lispref/debugging.texi |  2 +-
 doc/lispref/help.texi      |  2 +-
 doc/lispref/minibuf.texi   |  2 +-
 etc/NEWS                   |  7 +++++++
 src/keymap.c               | 20 +++++++++++++++-----
 test/lisp/subr-tests.el    |  8 ++++++++
 test/src/keymap-tests.el   | 12 ++++++++++++
 7 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/doc/lispref/debugging.texi b/doc/lispref/debugging.texi
index 1e779ac705..8e4b0ebfe9 100644
--- a/doc/lispref/debugging.texi
+++ b/doc/lispref/debugging.texi
@@ -424,7 +424,7 @@ Backtraces
 type @key{RET} while point is on any name of a function or variable
 which is not underlined, to see help information for that symbol in a
 help buffer, if any exists.  The @code{xref-find-definitions} command,
-bound to @key{M-.}, can also be used on any identifier in a backtrace
+bound to @kbd{M-.}, can also be used on any identifier in a backtrace
 (@pxref{Looking Up Identifiers,,,emacs, The GNU Emacs Manual}).
 
 In backtraces, the tails of long lists and the ends of long strings,
diff --git a/doc/lispref/help.texi b/doc/lispref/help.texi
index 2fd05b7391..298bec5230 100644
--- a/doc/lispref/help.texi
+++ b/doc/lispref/help.texi
@@ -545,7 +545,7 @@ Describing Characters
 @end group
 @group
 (single-key-description 'C-mouse-1)
-     @result{} "<C-mouse-1>"
+     @result{} "C-<mouse-1>"
 @end group
 @group
 (single-key-description 'C-mouse-1 t)
diff --git a/doc/lispref/minibuf.texi b/doc/lispref/minibuf.texi
index 81139b9e74..f0036f0ccf 100644
--- a/doc/lispref/minibuf.texi
+++ b/doc/lispref/minibuf.texi
@@ -348,7 +348,7 @@ Text from Minibuffer
 @item @key{RET}
 @code{exit-minibuffer}
 
-@item @key{M-<}
+@item @kbd{M-<}
 @code{minibuffer-beginning-of-buffer}
 
 @item @kbd{C-g}
diff --git a/etc/NEWS b/etc/NEWS
index b294ff1d23..671c6c2f56 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -212,6 +212,13 @@ This makes debugging Emacs Lisp scripts run in batch mode easier.  To
 get back the old behavior, set the new variable
 'backtrace-on-error-noninteractive' to a nil value.
 
++++
+** Modifiers now go outside angle brackets in pretty-printed key bindings.
+For example, <return> with Control and Meta modifiers is now shown as
+C-M-<return> instead of <C-M-return>.  Either variant can be used as
+input; functions such as 'kbd' and 'read-kbd-macro' accept both styles
+as equivalent (they have done so for a long time).
+
 \f
 * Editing Changes in Emacs 28.1
 
diff --git a/src/keymap.c b/src/keymap.c
index 1eeea81f62..a91f1456a6 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -2217,11 +2217,21 @@ DEFUN ("single-key-description", Fsingle_key_description,
     {
       if (NILP (no_angles))
 	{
-	  Lisp_Object result;
-	  char *buffer = SAFE_ALLOCA (sizeof "<>"
-				      + SBYTES (SYMBOL_NAME (key)));
-	  esprintf (buffer, "<%s>", SDATA (SYMBOL_NAME (key)));
-	  result = build_string (buffer);
+	  Lisp_Object namestr = SYMBOL_NAME (key);
+	  const char *sym = SSDATA (namestr);
+	  ptrdiff_t len = SBYTES (namestr);
+	  /* Find the extent of the modifier prefix, like "C-M-". */
+	  int i = 0;
+	  while (i < len - 3 && sym[i + 1] == '-' && strchr ("CMSsHA", sym[i]))
+	    i += 2;
+	  /* First I bytes of SYM are modifiers; put <> around the rest. */
+	  char *buffer = SAFE_ALLOCA (len + 3);
+	  memcpy (buffer, sym, i);
+	  buffer[i] = '<';
+	  memcpy (buffer + i + 1, sym + i, len - i);
+	  buffer [len + 1] = '>';
+	  buffer [len + 2] = '\0';
+	  Lisp_Object result = build_string (buffer);
 	  SAFE_FREE ();
 	  return result;
 	}
diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el
index 2f5b38d05d..8d19a26877 100644
--- a/test/lisp/subr-tests.el
+++ b/test/lisp/subr-tests.el
@@ -630,5 +630,13 @@ apropos-apropos-internal/predicate
   (should (>= (length (apropos-internal "^help" #'commandp)) 15))
   (should-not (apropos-internal "^next-line$" #'keymapp)))
 
+(ert-deftest subr--kbd ()
+  ;; Check that kbd handles both new and old style key descriptions
+  ;; (bug#45536).
+  (should (equal (kbd "s-<return>") [s-return]))
+  (should (equal (kbd "<s-return>") [s-return]))
+  (should (equal (kbd "C-M-<return>") [C-M-return]))
+  (should (equal (kbd "<C-M-return>") [C-M-return])))
+
 (provide 'subr-tests)
 ;;; subr-tests.el ends here
diff --git a/test/src/keymap-tests.el b/test/src/keymap-tests.el
index 74fb3c892d..d4f5fc3f19 100644
--- a/test/src/keymap-tests.el
+++ b/test/src/keymap-tests.el
@@ -248,6 +248,18 @@ help--describe-vector/bug-9293-same-command-does-not-shadow
 0 .. 3		foo
 ")))))
 
+(ert-deftest keymap--key-description ()
+  (should (equal (key-description [right] [?\C-x])
+                 "C-x <right>"))
+  (should (equal (key-description [M-H-right] [?\C-x])
+                 "C-x M-H-<right>"))
+  (should (equal (single-key-description 'home)
+                 "<home>"))
+  (should (equal (single-key-description 'home t)
+                 "home"))
+  (should (equal (single-key-description 'C-s-home)
+                 "C-s-<home>")))
+
 (provide 'keymap-tests)
 
 ;;; keymap-tests.el ends here
-- 
2.21.1 (Apple Git-122.3)


  reply	other threads:[~2021-01-02 17:20 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-29 16:21 bug#45536: [PATCH] Pretty-print keys without <> around modifiers Mattias Engdegård
2020-12-29 18:31 ` Drew Adams
2020-12-29 19:50   ` Mattias Engdegård
2020-12-29 20:14     ` Drew Adams
2020-12-29 20:01 ` Eli Zaretskii
2020-12-29 22:27   ` Mattias Engdegård
2020-12-30  3:54   ` Lars Ingebrigtsen
2020-12-30  9:52     ` Mattias Engdegård
2020-12-31  4:35       ` Lars Ingebrigtsen
2020-12-31  9:59         ` Mattias Engdegård
2020-12-31 12:29           ` Mattias Engdegård
2021-01-01 10:46             ` Lars Ingebrigtsen
2021-01-01 11:51               ` Mattias Engdegård
2021-01-01 11:54                 ` Lars Ingebrigtsen
2021-01-02 17:20                   ` Mattias Engdegård [this message]
2021-01-02 18:25                     ` Drew Adams
2021-01-05 10:52                       ` Mattias Engdegård
2021-01-05 10:47                     ` Mattias Engdegård
2021-05-17 15:11                       ` Lars Ingebrigtsen
2021-01-01 12:00               ` Eli Zaretskii
2021-01-01 12:10                 ` Mattias Engdegård
2021-01-01 12:28                   ` Eli Zaretskii
2021-01-01 12:43                     ` Mattias Engdegård
2021-01-01 12:54                       ` Eli Zaretskii
2021-01-01 13:29                         ` Mattias Engdegård
2021-01-01 13:34                           ` Eli Zaretskii
2021-01-01 19:41               ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6AE36074-6531-42B0-83D7-A03774E2BF5C@acm.org \
    --to=mattiase@acm.org \
    --cc=45536@debbugs.gnu.org \
    --cc=larsi@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).