all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: Alan Third <alan@idiocy.org>
Cc: 45502@debbugs.gnu.org, "Daniel Martín" <mardani29@yahoo.es>
Subject: bug#45502: [PATCH] Prettier key bindings in NS menu entries
Date: Wed, 30 Dec 2020 13:19:48 +0100	[thread overview]
Message-ID: <0F1B9914-2668-4775-822A-49A36D4A9F3F@acm.org> (raw)
In-Reply-To: <X+vAbgjq4x4/Wunf@breton.holly.idiocy.org>

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

30 dec. 2020 kl. 00.49 skrev Alan Third <alan@idiocy.org>:

> I think it's maybe visually a little neater to use autorelease, but
> either way works and I would probably do it the other way in different
> circumstances. Feel free to change it if you want.

Thanks, I'm sticking to autorelease for uniformity here; slightly delayed deallocation is no worse than GC after all.

>> Presumably 'atitle' should be sent autorelease (or release) as well?
> 
> Yes, I missed that one. And actually, I think the alloc'd NSMenuItem
> on line 484 will need released too. It should probably be autoreleased
> because it's returned to the calling function, and the caller can then
> decide whether to retain it or not (it doesn't in this case).

Yes, fixed.

(By the way, 'chording' isn't quite the same as a multi-key sequence; chords are rather simultaneous presses, like C-M-x, no? To continue a musical metaphor, perhaps an Emacs key sequence is an arpeggio?)

Here is a slightly less ugly variant of the symbol substitution patch. Maybe we should apply it and see if there are any complaints, or if we turn against it ourselves later on?


[-- Attachment #2: 0001-Use-standard-key-symbols-in-NS-menu-entries.patch --]
[-- Type: application/octet-stream, Size: 3757 bytes --]

From 7ccf892b25d04d94bdcb76dfb6a000028e7b3c2a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Wed, 30 Dec 2020 13:06:47 +0100
Subject: [PATCH] Use standard key symbols in NS menu entries

* src/nsmenu.m (skipspc): Remove.
(key_symbols, prettify_key): New.
([EmacsMenu fillWithWidgetValue:]): Call prettify_key.
---
 src/nsmenu.m | 70 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 60 insertions(+), 10 deletions(-)

diff --git a/src/nsmenu.m b/src/nsmenu.m
index 34912705e2..c124bf500d 100644
--- a/src/nsmenu.m
+++ b/src/nsmenu.m
@@ -458,14 +458,6 @@ - (BOOL)performKeyEquivalent: (NSEvent *)theEvent
 }
 
 
-static const char *
-skipspc (const char *s)
-{
-  while (*s == ' ')
-    s++;
-  return s;
-}
-
 - (NSMenuItem *)addItemWithWidgetValue: (void *)wvptr
                             attributes: (NSDictionary *)attributes
 {
@@ -485,7 +477,7 @@ - (NSMenuItem *)addItemWithWidgetValue: (void *)wvptr
       item = [[[NSMenuItem alloc] init] autorelease];
       if (wv->key)
         {
-          NSString *key = [NSString stringWithUTF8String: skipspc (wv->key)];
+          NSString *key = [NSString stringWithUTF8String: wv->key];
 #ifdef NS_IMPL_COCOA
           /* Cocoa only permits a single key (with modifiers) as
              keyEquivalent, so we put them in the title string
@@ -537,6 +529,63 @@ -(void)removeAllItems
 }
 
 
+typedef struct {
+  const char *from, *to;
+} subst_t;
+
+/* Standard keyboard symbols used in menus. */
+static const subst_t key_symbols[] = {
+  {"<backspace>",  "⌫"},
+  {"DEL",          "⌫"},
+  {"<deletechar>", "⌦"},
+  {"<return>",     "↩"},
+  {"RET",          "↩"},
+  {"<left>",       "←"},
+  {"<right>",      "→"},
+  {"<up>",         "↑"},
+  {"<down>",       "↓"},
+  {"<prior>",      "⇞"},
+  {"<next>",       "⇟"},
+  {"<home>",       "↖"},
+  {"<end>",        "↘"},
+  {"<tab>",        "⇥"},
+  {"TAB",          "⇥"},
+  {"<backtab>",    "⇤"},
+};
+
+/* Transform the key sequence KEY into something prettier by
+   substituting keyboard symbols. */
+static char *
+prettify_key (const char *key)
+{
+  while (*key == ' ') key++;
+
+  int len = strlen (key);
+  char *buf = xmalloc (len + 1);
+  memcpy (buf, key, len + 1);
+  for (int i = 0; i < ARRAYELTS (key_symbols); i++)
+    {
+      ptrdiff_t fromlen = strlen (key_symbols[i].from);
+      char *p = buf;
+      while (p < buf + len)
+        {
+          char *match = memmem (buf, len, key_symbols[i].from, fromlen);
+          if (!match)
+            break;
+          ptrdiff_t tolen = strlen (key_symbols[i].to);
+          eassert (tolen <= fromlen);
+          memcpy (match, key_symbols[i].to, tolen);
+          memmove (match + tolen, match + fromlen,
+                   len - (match + fromlen - buf) + 1);
+          len -= fromlen - tolen;
+          p = match + tolen;
+        }
+    }
+  Lisp_Object result = build_string (buf);
+  xfree (buf);
+  return SSDATA (result);
+}
+
 - (void)fillWithWidgetValue: (void *)wvptr
 {
   widget_value *first_wv = (widget_value *)wvptr;
@@ -560,7 +609,8 @@ - (void)fillWithWidgetValue: (void *)wvptr
         maxNameWidth = MAX(maxNameWidth, nameSize.width);
         if (wv->key)
           {
-            NSString *key = [NSString stringWithUTF8String: skipspc (wv->key)];
+            wv->key = prettify_key (wv->key);
+            NSString *key = [NSString stringWithUTF8String: wv->key];
             NSSize keySize = [key sizeWithAttributes: font_attribs];
             maxKeyWidth = MAX(maxKeyWidth, keySize.width);
           }
-- 
2.21.1 (Apple Git-122.3)


  reply	other threads:[~2020-12-30 12:19 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-28 14:23 bug#45502: [PATCH] Prettier key bindings in NS menu entries Mattias Engdegård
2020-12-28 18:36 ` Alan Third
2020-12-29 12:02   ` Mattias Engdegård
2020-12-29 13:53     ` Alan Third
2020-12-29 14:41       ` Mattias Engdegård
2020-12-29 15:50         ` Alan Third
2020-12-29 17:34           ` Mattias Engdegård
2020-12-29 21:24             ` Alan Third
2020-12-29 22:53               ` Mattias Engdegård
2020-12-29 23:49                 ` Alan Third
2020-12-30 12:19                   ` Mattias Engdegård [this message]
2020-12-30 12:46                     ` Alan Third
2020-12-30 13:09                       ` Alan Third
2020-12-30 15:53                         ` Mattias Engdegård
2020-12-30 13:12                       ` Mattias Engdegård
2020-12-28 22:46 ` Unknown

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

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

  git send-email \
    --in-reply-to=0F1B9914-2668-4775-822A-49A36D4A9F3F@acm.org \
    --to=mattiase@acm.org \
    --cc=45502@debbugs.gnu.org \
    --cc=alan@idiocy.org \
    --cc=mardani29@yahoo.es \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.