unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 45536@debbugs.gnu.org
Subject: bug#45536: [PATCH] Pretty-print keys without <> around modifiers
Date: Tue, 29 Dec 2020 23:27:51 +0100	[thread overview]
Message-ID: <5F3D56BD-12C6-4600-B912-EBE649976DDD@acm.org> (raw)
In-Reply-To: <83sg7oidp6.fsf@gnu.org>

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

29 dec. 2020 kl. 21.01 skrev Eli Zaretskii <eliz@gnu.org>:

> IMO, making this kind of changes is just asking for trouble: the gains
> are null and void (I don't see why we should care about consistency
> here), while the potential for breaking something out there is very
> real.

The gains are not null and void (or the change would not have been proposed).
There is no evidence for it being likely to break anything, but we can make the reform optional and turned off by default. That way, code for which it is desirable and safe could bind a dynamic variable during the pretty-printing.

Updated patch attached.


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

From 7aa5bef4a0949846ec565e42b9799e6c910aa492 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>.
This behaviour is controlled by modifiers-inside-angle-brackets,
by default t (for compatibility).

* src/keymap.c (Fsingle_key_description): Optionally skip modifier
prefix before adding <>.
(syms_of_keymap): Add modifiers-inside-angle-brackets.
---
 src/keymap.c | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/keymap.c b/src/keymap.c
index ca2d33dba4..c167772311 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -2013,6 +2013,9 @@ DEFUN ("key-description", Fkey_description, Skey_description, 1, 2, 0,
 Optional arg PREFIX is the sequence of keys leading up to KEYS.
 For example, [?\C-x ?l] is converted into the string \"C-x l\".
 
+Modifiers are put inside angle brackets iff `modifiers-inside-angle-brackets'
+is non-nil.
+
 For an approximate inverse of this, see `kbd'.  */)
   (Lisp_Object keys, Lisp_Object prefix)
 {
@@ -2229,6 +2232,8 @@ DEFUN ("single-key-description", Fsingle_key_description,
 Control characters turn into C-whatever, etc.
 Optional argument NO-ANGLES non-nil means don't put angle brackets
 around function keys and event symbols.
+Modifiers are put inside angle brackets iff `modifiers-inside-angle-brackets'
+is non-nil.
 
 See `text-char-description' for describing character codes.  */)
   (Lisp_Object key, Lisp_Object no_angles)
@@ -2260,11 +2265,23 @@ 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);
+	  /* Optionally find the extent of the modifier prefix, like "C-M-",
+	     to avoid putting modifiers around them. */
+	  int i = 0;
+	  if (!modifiers_inside_angle_brackets)
+	    while (i < len - 3 && sym[i + 1] == '-'
+		   && strchr ("CMSsHA", sym[i]))
+	      i += 2;
+	  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;
 	}
@@ -3331,6 +3348,13 @@ syms_of_keymap (void)
   Vwhere_is_preferred_modifier = Qnil;
   where_is_preferred_modifier = 0;
 
+  DEFVAR_BOOL ("modifiers-inside-angle-brackets",
+	       modifiers_inside_angle_brackets,
+	       doc: /* Whether modifiers go inside <> in key descriptions.
+When non-nil, <return> with a Control modifier is written <C-return>;
+when nil, it is written C-<return>. */);
+  modifiers_inside_angle_brackets = true;
+
   DEFSYM (Qmenu_bar, "menu-bar");
   DEFSYM (Qmode_line, "mode-line");
 
-- 
2.21.1 (Apple Git-122.3)


  reply	other threads:[~2020-12-29 22:27 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 [this message]
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
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=5F3D56BD-12C6-4600-B912-EBE649976DDD@acm.org \
    --to=mattiase@acm.org \
    --cc=45536@debbugs.gnu.org \
    --cc=eliz@gnu.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).