diff options
| author | Philipp Stephani | 2017-12-25 22:00:00 +0100 |
|---|---|---|
| committer | Philipp Stephani | 2018-01-07 19:42:57 +0100 |
| commit | 703ac3ea1c1ce381f385469a0e88bc29d3fe83c2 (patch) | |
| tree | 1705907cf4ad37a8a3c40f094cbd0a751a4571c5 /src | |
| parent | 95d0be0ae596b9f9f8b100576ca5d53a681e329c (diff) | |
| download | emacs-703ac3ea1c1ce381f385469a0e88bc29d3fe83c2.tar.gz emacs-703ac3ea1c1ce381f385469a0e88bc29d3fe83c2.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.
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 d8bc5250264..da625403441 100644 --- a/src/coding.c +++ b/src/coding.c | |||
| @@ -1515,13 +1515,6 @@ encode_coding_utf_8 (struct coding_system *coding) | |||
| 1515 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". | 1515 | /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions". |
| 1516 | Return true if a text is encoded in one of UTF-16 based coding systems. */ | 1516 | Return true if a text is encoded in one of UTF-16 based coding systems. */ |
| 1517 | 1517 | ||
| 1518 | #define UTF_16_HIGH_SURROGATE_P(val) \ | ||
| 1519 | (((val) & 0xFC00) == 0xD800) | ||
| 1520 | |||
| 1521 | #define UTF_16_LOW_SURROGATE_P(val) \ | ||
| 1522 | (((val) & 0xFC00) == 0xDC00) | ||
| 1523 | |||
| 1524 | |||
| 1525 | static bool | 1518 | static bool |
| 1526 | detect_coding_utf_16 (struct coding_system *coding, | 1519 | detect_coding_utf_16 (struct coding_system *coding, |
| 1527 | struct coding_detection_info *detect_info) | 1520 | struct coding_detection_info *detect_info) |
diff --git a/src/coding.h b/src/coding.h index 54100ccd312..d90b799d76e 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 dd5c7d91ea4..3f59e33c7b1 100644 --- a/src/nsterm.m +++ b/src/nsterm.m | |||
| @@ -6283,14 +6283,13 @@ not_in_argv (NSString *arg) | |||
| 6283 | by doCommandBySelector: deleteBackward: */ | 6283 | by doCommandBySelector: deleteBackward: */ |
| 6284 | - (void)insertText: (id)aString | 6284 | - (void)insertText: (id)aString |
| 6285 | { | 6285 | { |
| 6286 | int code; | 6286 | NSString *s = aString; |
| 6287 | int len = [(NSString *)aString length]; | 6287 | NSUInteger len = [s length]; |
| 6288 | int i; | ||
| 6289 | 6288 | ||
| 6290 | NSTRACE ("[EmacsView insertText:]"); | 6289 | NSTRACE ("[EmacsView insertText:]"); |
| 6291 | 6290 | ||
| 6292 | if (NS_KEYLOG) | 6291 | if (NS_KEYLOG) |
| 6293 | NSLog (@"insertText '%@'\tlen = %d", aString, len); | 6292 | NSLog (@"insertText '%@'\tlen = %lu", aString, (unsigned long) len); |
| 6294 | processingCompose = NO; | 6293 | processingCompose = NO; |
| 6295 | 6294 | ||
| 6296 | if (!emacs_event) | 6295 | if (!emacs_event) |
| @@ -6300,10 +6299,24 @@ not_in_argv (NSString *arg) | |||
| 6300 | if (workingText != nil) | 6299 | if (workingText != nil) |
| 6301 | [self deleteWorkingText]; | 6300 | [self deleteWorkingText]; |
| 6302 | 6301 | ||
| 6302 | /* It might be preferable to use getCharacters:range: below, | ||
| 6303 | cf. https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CocoaPerformance/Articles/StringDrawing.html#//apple_ref/doc/uid/TP40001445-112378. | ||
| 6304 | However, we probably can't use SAFE_NALLOCA here because it might | ||
| 6305 | exit nonlocally. */ | ||
| 6306 | |||
| 6303 | /* now insert the string as keystrokes */ | 6307 | /* now insert the string as keystrokes */ |
| 6304 | for (i =0; i<len; i++) | 6308 | for (NSUInteger i = 0; i < len; i++) |
| 6305 | { | 6309 | { |
| 6306 | code = [aString characterAtIndex: i]; | 6310 | NSUInteger code = [s characterAtIndex:i]; |
| 6311 | if (UTF_16_HIGH_SURROGATE_P (code) && i < len - 1) | ||
| 6312 | { | ||
| 6313 | unichar low = [s characterAtIndex:i + 1]; | ||
| 6314 | if (UTF_16_LOW_SURROGATE_P (low)) | ||
| 6315 | { | ||
| 6316 | code = surrogates_to_codepoint (low, code); | ||
| 6317 | ++i; | ||
| 6318 | } | ||
| 6319 | } | ||
| 6307 | /* TODO: still need this? */ | 6320 | /* TODO: still need this? */ |
| 6308 | if (code == 0x2DC) | 6321 | if (code == 0x2DC) |
| 6309 | code = '~'; /* 0x7E */ | 6322 | code = '~'; /* 0x7E */ |