Index: trunk/Cocoa/Pester/Source/NSString-NJRExtensions.m
===================================================================
--- trunk/Cocoa/Pester/Source/NSString-NJRExtensions.m	(revision 102)
+++ trunk/Cocoa/Pester/Source/NSString-NJRExtensions.m	(revision 129)
@@ -8,6 +8,13 @@
 
 #import "NSString-NJRExtensions.h"
+#import "NSFont-NJRExtensions.h"
+#include <Carbon/Carbon.h>
 
 @implementation NSString (NJRExtensions)
+
++ (NSString *)stringWithCharacter:(unichar)character;
+{
+    return [self stringWithCharacters: &character length: 1];
+}
 
 - (NSAttributedString *)attributedStringWithFont:(NSFont *)font;
@@ -29,4 +36,85 @@
 {
     return [self attributedStringWithFont: [NSFont boldSystemFontOfSize: [NSFont smallSystemFontSize]]];
+}
+
+static unichar combiningHelpChar[] = {0x003F, 0x20DD};
+
+static NSFont *menuItemCmdKeyFont = nil;
+static NSFont *menuItemFont = nil;
+
+static void initialize() {
+    if (menuItemCmdKeyFont != nil) return;
+
+    menuItemCmdKeyFont = [[NSFont themeFont: kThemeMenuItemCmdKeyFont] retain];
+    menuItemFont = [[NSFont themeFont: kThemeMenuItemFont] retain];
+}
+
+- (NSString *)keyEquivalentString;
+{
+    if ([self length] != 0) {
+        const char *str = [self UTF8String];
+        unichar keyChar;
+        if (str[1] != '\0') {
+            keyChar = [self characterAtIndex: 0];
+            switch (keyChar) {
+                case NSUpArrowFunctionKey: keyChar = 0x21E1; break;
+                case NSDownArrowFunctionKey: keyChar = 0x21E3; break;
+                case NSLeftArrowFunctionKey: keyChar = 0x21E0; break;
+                case NSRightArrowFunctionKey: keyChar = 0x21E2; break;
+                case NSInsertFunctionKey:
+                    return [NSString stringWithCharacters: combiningHelpChar length: 2];
+                case NSDeleteFunctionKey: keyChar = 0x2326; break;
+                case NSHomeFunctionKey: keyChar = 0x2196; break;
+                case NSEndFunctionKey: keyChar = 0x2198; break;
+                case NSPageUpFunctionKey: keyChar = 0x21DE; break;
+                case NSPageDownFunctionKey: keyChar = 0x21DF; break;
+                case NSClearLineFunctionKey: keyChar = 0x2327; break;
+                default:
+                if (keyChar >= NSF1FunctionKey && keyChar <= NSF35FunctionKey) {
+                    return [NSString stringWithFormat: @"F%u", keyChar - NSF1FunctionKey + 1];
+                }
+                return [NSString stringWithFormat: @"[unknown %lX]", keyChar];
+            }
+        } else if (str[0] >= 'A' && str[0] <= 'Z') {
+            return self;
+        } else if (str[0] >= 'a' && str[0] <= 'z') return [self uppercaseString];
+        else switch (str[0]) {
+            case '\t': keyChar = 0x21e5; break;
+            case '\r': keyChar = 0x21a9; break;
+            case '\e': keyChar = 0x238b; break;
+            case ' ': keyChar = 0x2423; break;
+            case 0x7f: keyChar = 0x232b; break; // delete
+            case 0x03: keyChar = 0x2324; break; // enter
+            case 0x19: keyChar = 0x21e4; break; // backtab
+            case 0: return @"";
+            // case '': keyChar = 0x; break;
+            default: return self; // return [NSString stringWithFormat: @"[huh? %x]", (int)str[0]]; // 
+        }
+        return [NSString stringWithCharacter: keyChar];
+    }
+    return self;
+}
+
+- (NSAttributedString *)keyEquivalentAttributedStringWithModifierMask:(unsigned int)modifierMask;
+{
+    initialize();
+    NSString *keyEquivalentStringNoMask = [self keyEquivalentString];
+    NSAttributedString *keyEquivalentAttributedString =
+        [[NSString stringWithFormat: @"%@%@%@%@%@",
+            (modifierMask & NSControlKeyMask) ? [NSString stringWithCharacter: kControlUnicode] : @"",
+            (modifierMask & NSAlternateKeyMask) ? [NSString stringWithCharacter: kOptionUnicode] : @"",
+            (modifierMask & NSShiftKeyMask) ? [NSString stringWithCharacter: kShiftUnicode] : @"",
+            (modifierMask & NSCommandKeyMask) ? [NSString stringWithCharacter: kCommandUnicode] : @"",
+                keyEquivalentStringNoMask]
+        attributedStringWithFont: menuItemCmdKeyFont];
+    unsigned noMaskLength = [keyEquivalentStringNoMask length];
+    if (noMaskLength > 3 || // Fxx
+        (noMaskLength == 1 && [keyEquivalentStringNoMask characterAtIndex: 0] <= 0x7F)) {
+        NSMutableAttributedString *astr = [keyEquivalentAttributedString mutableCopy];
+        [astr setAttributes: [NSDictionary dictionaryWithObject: menuItemFont forKey: NSFontAttributeName] range: NSMakeRange([astr length] - noMaskLength, noMaskLength)];
+        keyEquivalentAttributedString = [[astr copy] autorelease];
+        [astr release];
+    }
+    return keyEquivalentAttributedString;
 }
 
