Subject: Re: bug#21551: Fix Mac OS X key bindings bug

From: Alan Third <[email-masked]>
Date: Tue, 13 Feb 2018 18:56:20 +0000

On Sun, Feb 11, 2018 at 05:57:32PM +0000, Philipp Stephani wrote:
> Anders Lindgren <[email-masked]> schrieb am Sa., 2. Jan. 2016 um
> 09:08 Uhr:
> 
> > I found a case where the code in question is needed, which none of the
> > suggested patches handle correctly.
> >
> > Steps to repeat:
> >
> >     (setq ns-alternate-modifier nil)
> >
> >     Press left CMD-ALT-9
> >
> >     An unmodified Emacs replies "s-]" is not bound. (This assumes a
> > Swedish keyboard layout, other layouts would yield a different character,
> > but the principle is the same).
> >
> >     With either of the two patches, Emacs respond with "s-9" is not bound,
> > which isn't correct.
> >
> 
> In the current master this is now interpreted as M-s-9, which is at least
> somewhat reasonable.

For me it’s s-9, not M-s-9.

> > Unfortunately, I don't know how to distinguish between the cases where we
> > need to strip away modifiers (C-s-a) and when we shouldn't, so I'm leaving
> > this open for now.
> >
> 
> It seems like in the macOS input model, we can't: either we strip away all
> modifiers, or none.

It appears you can using some Carbon stuff. Half‐assed proof of
concept patch attached.

(It’s broken the arrow keys, and I don’t know why...)
-- 
Alan Third
diff --git a/configure.ac b/configure.ac
index f2a8332d71..b5db45801d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -5260,7 +5260,7 @@ AC_DEFUN
    if test "$HAVE_NS" = "yes"; then
      libs_nsgui="-framework AppKit"
      if test "$NS_IMPL_COCOA" = "yes"; then
-        libs_nsgui="$libs_nsgui -framework IOKit"
+        libs_nsgui="$libs_nsgui -framework IOKit -framework Carbon"
      fi
    else
      libs_nsgui=
diff --git a/src/nsterm.m b/src/nsterm.m
index 29aef0e9b6..2038c02e60 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -69,6 +69,8 @@ Updated by Christian Limpach ([email-masked])
 #include "macfont.h"
 #endif
 
+#include <Carbon/Carbon.h>
+
 static EmacsMenu *dockMenu;
 #ifdef NS_IMPL_COCOA
 static EmacsMenu *mainMenu;
@@ -2681,6 +2683,45 @@ so some key presses (TAB) are swallowed by the system. */
 }

+static UniChar
+ns_get_shifted_character (NSEvent *event)
+{
+  static UInt32 deadKeyState;
+
+  CFDataRef layout_ref = (CFDataRef) TISGetInputSourceProperty
+    (TISCopyCurrentKeyboardLayoutInputSource (), kTISPropertyUnicodeKeyLayoutData);
+  UCKeyboardLayout* layout = CFDataGetBytePtr (layout_ref);
+
+  UniChar buf[255];
+  UniCharCount maxStringLength;
+  UniCharCount actualStringLength;
+  OSStatus result;
+
+  unsigned int flags = [event modifierFlags];
+  UInt32 modifiers = (flags & NSEventModifierFlagShift) ? shiftKey : 0;
+  modifiers |= ((flags & NSRightAlternateKeyMask) == NSRightAlternateKeyMask
+                && EQ (ns_right_alternate_modifier, Qnone)) ? rightOptionKey : 0;
+  modifiers |= ((flags & NSLeftAlternateKeyMask) == NSLeftAlternateKeyMask
+                && EQ (ns_alternate_modifier, Qnone)) ? optionKey : 0;
+
+  modifiers |= ((flags & NSRightCommandKeyMask) == NSRightCommandKeyMask
+                && EQ (ns_right_command_modifier, Qnone)) ? cmdKey : 0;
+  modifiers |= ((flags & NSLeftCommandKeyMask) == NSLeftCommandKeyMask
+                && EQ (ns_command_modifier, Qnone)) ? cmdKey : 0;
+
+  result = UCKeyTranslate (layout,
+                           [event keyCode], kUCKeyActionDown,
+                           (modifiers >> 8) & 0xFF,
+                           LMGetKbdType (), kUCKeyTranslateNoDeadKeysBit,
+                           &deadKeyState, 255, &actualStringLength,
+                           buf);
+
+  if (result != 0)
+    NSLog(@"Disaster!");
+
+  return buf[0];
+}
+
 
 /* ==========================================================================
 
@@ -6147,7 +6188,8 @@ most recently updated (I guess), which is not the correct one. */
       /* FIXME: What should happen for key sequences with more than
          one character?  */
       code = ([[theEvent charactersIgnoringModifiers] length] == 0) ?
-        0 : [[theEvent charactersIgnoringModifiers] characterAtIndex: 0];
+        0 : //[[theEvent charactersIgnoringModifiers] characterAtIndex: 0];
+        ns_get_shifted_character (theEvent);
 
       /* (Carbon way: [theEvent keyCode]) */