X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2FAppleSpeller.m;h=3e9a2f84529f286ab2174669fdd92e63567948e5;hb=351363c599db664d612fbe1b426403f91758de05;hp=37a361b453311bf9a0c12be71352a3077aca92f9;hpb=e4f2484cb5899da6d98e0be6db465ca011989e04;p=lyx.git diff --git a/src/support/AppleSpeller.m b/src/support/AppleSpeller.m index 37a361b453..3e9a2f8452 100644 --- a/src/support/AppleSpeller.m +++ b/src/support/AppleSpeller.m @@ -8,7 +8,6 @@ * Full author contact details are available in file CREDITS. */ -#import #import #import @@ -22,29 +21,18 @@ typedef struct AppleSpellerRec { #else int doctag; #endif - char ** suggestions; - size_t numsug; + NSArray * suggestions; + NSArray * misspelled; } AppleSpellerRec ; -static void freeSuggestionsAppleSpeller(AppleSpeller speller) -{ - if (speller->suggestions) { - while (speller->numsug--) { - free(speller->suggestions[speller->numsug]); - } - free(speller->suggestions); - speller->suggestions = 0; - } -} - - AppleSpeller newAppleSpeller(void) { AppleSpeller speller = calloc(1, sizeof(AppleSpellerRec)); speller->checker = [NSSpellChecker sharedSpellChecker]; - speller->suggestions = 0; speller->doctag = [NSSpellChecker uniqueSpellDocumentTag]; + speller->suggestions = nil; + speller->misspelled = nil; return speller; } @@ -53,9 +41,11 @@ void freeAppleSpeller(AppleSpeller speller) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; - freeSuggestionsAppleSpeller(speller); [speller->checker closeSpellDocumentWithTag:speller->doctag]; + [speller->suggestions release]; + [speller->misspelled release]; + [pool release]; free(speller); @@ -68,7 +58,32 @@ static NSString * toString(const char * word) } -SpellCheckResult checkAppleSpeller(AppleSpeller speller, const char * word, const char * lang) +static NSString * toLanguage(AppleSpeller speller, const char * lang) +{ + NSString * result = nil; +#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050) + NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; + NSString * lang_ = toString(lang); + if ([NSSpellChecker instancesRespondToSelector:@selector(availableLanguages)]) { + NSArray * languages = [speller->checker availableLanguages]; + + for (NSString *element in languages) { + if (0 == [element caseInsensitiveCompare:lang_]) { + result = element; + break; + } else if ([lang_ hasPrefix:element]) { + result = element; + } + } + } + [lang_ release]; + [pool release]; +#endif + return result; +} + + +SpellCheckResult AppleSpeller_check(AppleSpeller speller, const char * word, const char * lang) { if (!speller->checker || !lang || !word) return SPELL_CHECK_FAILED; @@ -76,30 +91,52 @@ SpellCheckResult checkAppleSpeller(AppleSpeller speller, const char * word, cons NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString * word_ = toString(word); NSString * lang_ = toString(lang); + SpellCheckResult result = SPELL_CHECK_FAILED; + int start = 0; + int length = [word_ length]; + + [speller->misspelled release]; + speller->misspelled = nil; - NSRange match = [speller->checker - checkSpellingOfString:word_ - startingAt:0 - language:lang_ - wrap:NO - inSpellDocumentWithTag:speller->doctag - wordCount:NULL]; - - SpellCheckResult result = match.length == 0 ? SPELL_CHECK_OK : SPELL_CHECK_FAILED; - if (result == SPELL_CHECK_OK && [NSSpellChecker instancesRespondToSelector:@selector(hasLearnedWord:)]) { - if ([speller->checker hasLearnedWord:word_]) - result = SPELL_CHECK_LEARNED; + while (result == SPELL_CHECK_FAILED && start < length) { + NSRange match = [speller->checker + checkSpellingOfString:word_ + startingAt:start + language:lang_ + wrap:(BOOL)NO + inSpellDocumentWithTag:speller->doctag + wordCount:NULL]; + + result = match.length == 0 ? SPELL_CHECK_OK : SPELL_CHECK_FAILED; + if (result == SPELL_CHECK_OK) { + if ([NSSpellChecker instancesRespondToSelector:@selector(hasLearnedWord:)]) { + if ([speller->checker hasLearnedWord:word_]) + result = SPELL_CHECK_LEARNED; + } + } else { +#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050) + NSUInteger capacity = [speller->misspelled count] + 1; +#else + int capacity = [speller->misspelled count] + 1; +#endif + NSMutableArray * misspelled = [NSMutableArray arrayWithCapacity:capacity]; + [misspelled addObjectsFromArray:speller->misspelled]; + [misspelled addObject:[NSValue valueWithRange:match]]; + [speller->misspelled release]; + speller->misspelled = [[NSArray arrayWithArray:misspelled] retain]; + start = match.location + match.length + 1; + } } [word_ release]; [lang_ release]; [pool release]; - return result; + return [speller->misspelled count] ? SPELL_CHECK_FAILED : result; } -void ignoreAppleSpeller(AppleSpeller speller, const char * word) +void AppleSpeller_ignore(AppleSpeller speller, const char * word) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString * word_ = toString(word); @@ -111,7 +148,7 @@ void ignoreAppleSpeller(AppleSpeller speller, const char * word) } -size_t makeSuggestionAppleSpeller(AppleSpeller speller, const char * word, const char * lang) +size_t AppleSpeller_makeSuggestion(AppleSpeller speller, const char * word, const char * lang) { if (!speller->checker || !word || !lang) return 0; @@ -143,36 +180,25 @@ size_t makeSuggestionAppleSpeller(AppleSpeller speller, const char * word, const [word_ release]; [lang_ release]; - freeSuggestionsAppleSpeller(speller); + [speller->suggestions release]; + speller->suggestions = [[NSArray arrayWithArray:result] retain]; - speller->numsug = [result count]; - if (speller->numsug) { - speller->suggestions = calloc(speller->numsug + 1, sizeof(char *)); - if (speller->suggestions) { - size_t i; - for (i = 0; i < speller->numsug; i++) { - NSString * str = [result objectAtIndex:i]; - speller->suggestions[i] = strdup([str UTF8String]); - } - speller->suggestions[speller->numsug] = 0; - } - } [pool release]; - return speller->numsug; + return [speller->suggestions count]; } -const char * getSuggestionAppleSpeller(AppleSpeller speller, size_t pos) +const char * AppleSpeller_getSuggestion(AppleSpeller speller, size_t pos) { const char * result = 0; - if (pos < speller->numsug && speller->suggestions) { - result = speller->suggestions[pos] ; + if (pos < [speller->suggestions count]) { + result = [[speller->suggestions objectAtIndex:pos] UTF8String] ; } return result; } -void learnAppleSpeller(AppleSpeller speller, const char * word) +void AppleSpeller_learn(AppleSpeller speller, const char * word) { #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050) NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; @@ -187,8 +213,7 @@ void learnAppleSpeller(AppleSpeller speller, const char * word) } - -void unlearnAppleSpeller(AppleSpeller speller, const char * word) +void AppleSpeller_unlearn(AppleSpeller speller, const char * word) { #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050) NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; @@ -203,24 +228,29 @@ void unlearnAppleSpeller(AppleSpeller speller, const char * word) } -int hasLanguageAppleSpeller(AppleSpeller speller, const char * lang) +int AppleSpeller_numMisspelledWords(AppleSpeller speller) { - BOOL result = NO; -#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050) - NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; - NSString * lang_ = toString(lang); - if ([NSSpellChecker instancesRespondToSelector:@selector(availableLanguages:)]) { - NSArray * languages = [speller->checker availableLanguages]; + return [speller->misspelled count]; +} - for (NSString *element in languages) { - result = [element isEqualToString:lang_] || [lang_ hasPrefix:element]; - if (result) break; - } - } - [lang_ release]; - [pool release]; +void AppleSpeller_misspelledWord(AppleSpeller speller, int index, int * start, int * length) +{ +#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050) + NSRange range = [[speller->misspelled objectAtIndex:(NSUInteger)index] rangeValue]; +#else + NSRange range = [[speller->misspelled objectAtIndex:index] rangeValue]; #endif + *start = range.location; + *length = range.length; +} + - return result ? 1 : 0; +int AppleSpeller_hasLanguage(AppleSpeller speller, const char * lang) +{ +#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050) + return toLanguage(speller, lang) != nil; +#else + return true; +#endif }