}
-static const char *
-skipspc (const char *s)
-{
- while (*s == ' ')
- s++;
- return s;
-}
-
- (NSMenuItem *)addItemWithWidgetValue: (void *)wvptr
attributes: (NSDictionary *)attributes
{
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
}
+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;
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);
}