all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Philipp Stephani <p.stephani2@gmail.com>
To: emacs-devel@gnu.org
Cc: Philipp Stephani <phst@google.com>
Subject: [PATCH] Allow inserting non-BMP characters
Date: Mon, 25 Dec 2017 22:01:15 +0100	[thread overview]
Message-ID: <20171225210115.13789-1-phst@google.com> (raw)
In-Reply-To: <CAArVCkRx8p_vaFKJ_kXRuoZCKVBSYr=94RJANGpU0NXvkEZv6A@mail.gmail.com>

* src/character.h (char_low_surrogate_p, char_high_surrogate_p)
(surrogates_to_codepoint): New functions.

* src/nsterm.m (insertText:): Properly handle surrogate pairs.
---
 src/character.h | 26 ++++++++++++++++++++++++++
 src/nsterm.m    | 27 ++++++++++++++++++++-------
 2 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/src/character.h b/src/character.h
index c716885d46..8fa79c6bd9 100644
--- a/src/character.h
+++ b/src/character.h
@@ -614,6 +614,32 @@ char_surrogate_p (int c)
   return 0xD800 <= c && c <= 0xDFFF;
 }
 
+/* Return true if C is a low surrogate.  */
+
+INLINE bool
+char_low_surrogate_p (int c)
+{
+  return 0xDC00 <= c && c <= 0xDFFF;
+}
+
+/* Return true if C is a high surrogate.  */
+
+INLINE bool
+char_high_surrogate_p (int c)
+{
+  return 0xD800 <= c && c <= 0xDBFF;
+}
+
+/* Return the Unicode code point for the given UTF-16 surrogates.  */
+
+INLINE int
+surrogates_to_codepoint (int low, int high)
+{
+  eassert (char_low_surrogate_p (low));
+  eassert (char_high_surrogate_p (high));
+  return 0x10000 + (low - 0xDC00) + ((high - 0xD800) * 0x400);
+}
+
 /* Data type for Unicode general category.
 
    The order of members must be in sync with the 8th element of the
diff --git a/src/nsterm.m b/src/nsterm.m
index 07ac8f978f..da8d4dce6b 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -6283,14 +6283,13 @@ flag set (this is probably a bug in the OS).
          by doCommandBySelector: deleteBackward: */
 - (void)insertText: (id)aString
 {
-  int code;
-  int len = [(NSString *)aString length];
-  int i;
+  NSString *s = aString;
+  NSUInteger len = [s length];
 
   NSTRACE ("[EmacsView insertText:]");
 
   if (NS_KEYLOG)
-    NSLog (@"insertText '%@'\tlen = %d", aString, len);
+    NSLog (@"insertText '%@'\tlen = %lu", aString, (unsigned long) len);
   processingCompose = NO;
 
   if (!emacs_event)
@@ -6301,9 +6300,22 @@ - (void)insertText: (id)aString
     [self deleteWorkingText];
 
   /* now insert the string as keystrokes */
-  for (i =0; i<len; i++)
-    {
-      code = [aString characterAtIndex: i];
+  USE_SAFE_ALLOCA;
+  unichar *utf16_buffer;
+  SAFE_NALLOCA (utf16_buffer, 1, len);
+  [s getCharacters:utf16_buffer range:NSMakeRange (0, len)];
+  for (NSUInteger i = 0; i < len; i++)
+    {
+      NSUInteger code = utf16_buffer[i];
+      if (char_high_surrogate_p (code) && i < len - 1)
+        {
+          unichar low = utf16_buffer[i + 1];
+          if (char_low_surrogate_p (low))
+            {
+              code = surrogates_to_codepoint (low, code);
+              ++i;
+            }
+        }
       /* TODO: still need this? */
       if (code == 0x2DC)
         code = '~'; /* 0x7E */
@@ -6314,6 +6326,7 @@ - (void)insertText: (id)aString
       emacs_event->code = code;
       EV_TRAILER ((id)nil);
     }
+  SAFE_FREE ();
 }
 
 
-- 
2.15.1




  reply	other threads:[~2017-12-25 21:01 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-24 16:00 bug#29837: UTF-16 char display problems and the macOS "character palette" Alan Third
2017-12-24 16:56 ` Eli Zaretskii
2017-12-24 18:23   ` Alan Third
2017-12-24 18:57     ` Eli Zaretskii
2017-12-24 19:28       ` Alan Third
2017-12-24 19:34         ` Eli Zaretskii
2017-12-25 20:13           ` Philipp Stephani
2017-12-25 21:01             ` Philipp Stephani [this message]
2017-12-26  1:26               ` [PATCH] Allow inserting non-BMP characters Alan Third
2017-12-26  4:46               ` Eli Zaretskii
2017-12-26 10:35                 ` Philipp Stephani
2017-12-26 16:11                   ` Eli Zaretskii
2017-12-26 18:50                     ` Philipp Stephani
2017-12-26 20:22                       ` Eli Zaretskii
2017-12-26 21:36                         ` Alan Third
2017-12-27  3:41                           ` Eli Zaretskii
2017-12-28 11:38                             ` Alan Third
2017-12-28 12:31                               ` Philipp Stephani
2017-12-28 16:29                                 ` Eli Zaretskii
2017-12-29 20:14                                   ` Philipp Stephani
2017-12-29 20:27                                     ` Eli Zaretskii
2018-01-07 15:51                                       ` Philipp Stephani
2018-01-07 17:40                                         ` Eli Zaretskii
2018-01-07 18:44                                           ` Philipp Stephani
2017-12-25 21:07             ` bug#29837: UTF-16 char display problems and the macOS "character palette" Philipp Stephani
2017-12-26  1:34             ` Alan Third

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=20171225210115.13789-1-phst@google.com \
    --to=p.stephani2@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=phst@google.com \
    /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.