diff options
| author | Philipp Stephani | 2017-12-25 22:00:00 +0100 |
|---|---|---|
| committer | Alan Third | 2018-06-17 11:50:58 +0100 |
| commit | 0deab3fbd8a51fc83ab7c8031f4e296a4003b055 (patch) | |
| tree | c6df46e597238d00b5cee8157e4f76be36d9999d /src | |
| parent | ebe065fddf76fde64a9c07b419b67fe47fb6c1cb (diff) | |
| download | emacs-0deab3fbd8a51fc83ab7c8031f4e296a4003b055.tar.gz emacs-0deab3fbd8a51fc83ab7c8031f4e296a4003b055.zip | |
Allow inserting non-BMP characters
* src/coding.h (UTF_16_HIGH_SURROGATE_P, UTF_16_LOW_SURROGATE_P): Move
from coding.c and document.
(surrogates_to_codepoint): New function.
* src/nsterm.m (insertText:): Properly handle surrogate pairs.
(cherry picked from commit 703ac3ea1c1ce381f385469a0e88bc29d3fe83c2)
Diffstat (limited to 'src')
| -rw-r--r-- | src/coding.c | 7 | ||||
| -rw-r--r-- | src/coding.h | 24 | ||||
| -rw-r--r-- | src/nsterm.m | 25 |
3 files changed, 43 insertions, 13 deletions
diff --git a/src/coding.c b/src/coding.c index b1eb2edb497..867f84de609 100644 --- a/src/coding.c +++ b/src/coding.c | |||
| @@ -1518,13 +1518,6 @@ encode_coding_utf_8 (struct coding_system *coding) | |||
| 1518 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | 1518 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
| 1519 | Return true if a text is encoded in one of UTF-16 based coding systems. */ | 1519 | Return true if a text is encoded in one of UTF-16 based coding systems. */ |
| 1520 | 1520 | ||
| 1521 | #define UTF_16_HIGH_SURROGATE_P(val) \ | ||
| 1522 | (((val) & 0xFC00) == 0xD800) | ||
| 1523 | |||
| 1524 | #define UTF_16_LOW_SURROGATE_P(val) \ | ||
| 1525 | (((val) & 0xFC00) == 0xDC00) | ||
| 1526 | |||
| 1527 | |||
| 1528 | static bool | 1521 | static bool |
| 1529 | detect_coding_utf_16 (struct coding_system *coding, | 1522 | detect_coding_utf_16 (struct coding_system *coding, |
| 1530 | struct coding_detection_info *detect_info) | 1523 | struct coding_detection_info *detect_info) |
diff --git a/src/coding.h b/src/coding.h index 2a87fc32e9d..502c4723149 100644 --- a/src/coding.h +++ b/src/coding.h | |||
| @@ -662,6 +662,30 @@ struct coding_system | |||
| 662 | /* Note that this encodes utf-8, not utf-8-emacs, so it's not a no-op. */ | 662 | /* Note that this encodes utf-8, not utf-8-emacs, so it's not a no-op. */ |
| 663 | #define ENCODE_UTF_8(str) code_convert_string_norecord (str, Qutf_8, true) | 663 | #define ENCODE_UTF_8(str) code_convert_string_norecord (str, Qutf_8, true) |
| 664 | 664 | ||
| 665 | /* Return true if VAL is a high surrogate. VAL must be a 16-bit code | ||
| 666 | unit. */ | ||
| 667 | |||
| 668 | #define UTF_16_HIGH_SURROGATE_P(val) \ | ||
| 669 | (((val) & 0xFC00) == 0xD800) | ||
| 670 | |||
| 671 | /* Return true if VAL is a low surrogate. VAL must be a 16-bit code | ||
| 672 | unit. */ | ||
| 673 | |||
| 674 | #define UTF_16_LOW_SURROGATE_P(val) \ | ||
| 675 | (((val) & 0xFC00) == 0xDC00) | ||
| 676 | |||
| 677 | /* Return the Unicode code point for the given UTF-16 surrogates. */ | ||
| 678 | |||
| 679 | INLINE int | ||
| 680 | surrogates_to_codepoint (int low, int high) | ||
| 681 | { | ||
| 682 | eassert (0 <= low && low <= 0xFFFF); | ||
| 683 | eassert (0 <= high && high <= 0xFFFF); | ||
| 684 | eassert (UTF_16_LOW_SURROGATE_P (low)); | ||
| 685 | eassert (UTF_16_HIGH_SURROGATE_P (high)); | ||
| 686 | return 0x10000 + (low - 0xDC00) + ((high - 0xD800) * 0x400); | ||
| 687 | } | ||
| 688 | |||
| 665 | /* Extern declarations. */ | 689 | /* Extern declarations. */ |
| 666 | extern Lisp_Object code_conversion_save (bool, bool); | 690 | extern Lisp_Object code_conversion_save (bool, bool); |
| 667 | extern bool encode_coding_utf_8 (struct coding_system *); | 691 | extern bool encode_coding_utf_8 (struct coding_system *); |
diff --git a/src/nsterm.m b/src/nsterm.m index 1afd637b619..799bbd5bc06 100644 --- a/src/nsterm.m +++ b/src/nsterm.m | |||
| @@ -6317,14 +6317,13 @@ not_in_argv (NSString *arg) | |||
| 6317 | by doCommandBySelector: deleteBackward: */ | 6317 | by doCommandBySelector: deleteBackward: */ |
| 6318 | - (void)insertText: (id)aString | 6318 | - (void)insertText: (id)aString |
| 6319 | { | 6319 | { |
| 6320 | int code; | 6320 | NSString *s = aString; |
| 6321 | int len = [(NSString *)aString length]; | 6321 | NSUInteger len = [s length]; |
| 6322 | int i; | ||
| 6323 | 6322 | ||
| 6324 | NSTRACE ("[EmacsView insertText:]"); | 6323 | NSTRACE ("[EmacsView insertText:]"); |
| 6325 | 6324 | ||
| 6326 | if (NS_KEYLOG) | 6325 | if (NS_KEYLOG) |
| 6327 | NSLog (@"insertText '%@'\tlen = %d", aString, len); | 6326 | NSLog (@"insertText '%@'\tlen = %lu", aString, (unsigned long) len); |
| 6328 | processingCompose = NO; | 6327 | processingCompose = NO; |
| 6329 | 6328 | ||
| 6330 | if (!emacs_event) | 6329 | if (!emacs_event) |
| @@ -6334,10 +6333,24 @@ not_in_argv (NSString *arg) | |||
| 6334 | if (workingText != nil) | 6333 | if (workingText != nil) |
| 6335 | [self deleteWorkingText]; | 6334 | [self deleteWorkingText]; |
| 6336 | 6335 | ||
| 6336 | /* It might be preferable to use getCharacters:range: below, | ||
| 6337 | cf. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CocoaPerformance/Articles/StringDrawing.html#//apple_ref/doc/uid/TP40001445-112378. | ||
| 6338 | However, we probably can't use SAFE_NALLOCA here because it might | ||
| 6339 | exit nonlocally. */ | ||
| 6340 | |||
| 6337 | /* now insert the string as keystrokes */ | 6341 | /* now insert the string as keystrokes */ |
| 6338 | for (i =0; i<len; i++) | 6342 | for (NSUInteger i = 0; i < len; i++) |
| 6339 | { | 6343 | { |
| 6340 | code = [aString characterAtIndex: i]; | 6344 | NSUInteger code = [s characterAtIndex:i]; |
| 6345 | if (UTF_16_HIGH_SURROGATE_P (code) && i < len - 1) | ||
| 6346 | { | ||
| 6347 | unichar low = [s characterAtIndex:i + 1]; | ||
| 6348 | if (UTF_16_LOW_SURROGATE_P (low)) | ||
| 6349 | { | ||
| 6350 | code = surrogates_to_codepoint (low, code); | ||
| 6351 | ++i; | ||
| 6352 | } | ||
| 6353 | } | ||
| 6341 | /* TODO: still need this? */ | 6354 | /* TODO: still need this? */ |
| 6342 | if (code == 0x2DC) | 6355 | if (code == 0x2DC) |
| 6343 | code = '~'; /* 0x7E */ | 6356 | code = '~'; /* 0x7E */ |