3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
11 #import <Carbon/Carbon.h>
12 #import <Cocoa/Cocoa.h>
14 #import <AvailabilityMacros.h>
16 #include "support/AppleSpeller.h"
18 typedef struct AppleSpellerRec {
19 NSSpellChecker * checker;
20 #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050)
30 static void freeSuggestionsAppleSpeller(AppleSpeller speller)
32 if (speller->suggestions) {
33 while (speller->numsug--) {
34 free(speller->suggestions[speller->numsug]);
36 free(speller->suggestions);
37 speller->suggestions = 0;
42 AppleSpeller newAppleSpeller(void)
44 AppleSpeller speller = calloc(1, sizeof(AppleSpellerRec));
45 speller->checker = [NSSpellChecker sharedSpellChecker];
46 speller->suggestions = 0;
47 speller->doctag = [NSSpellChecker uniqueSpellDocumentTag];
52 void freeAppleSpeller(AppleSpeller speller)
54 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
56 freeSuggestionsAppleSpeller(speller);
57 [speller->checker closeSpellDocumentWithTag:speller->doctag];
65 static NSString * toString(const char * word)
67 return [[NSString alloc] initWithBytes:word length:strlen(word) encoding:NSUTF8StringEncoding];
71 SpellCheckResult checkAppleSpeller(AppleSpeller speller, const char * word, const char * lang)
73 if (!speller->checker || !lang || !word)
74 return SPELL_CHECK_FAILED;
76 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
77 NSString * word_ = toString(word);
78 NSString * lang_ = toString(lang);
80 NSRange match = [speller->checker
81 checkSpellingOfString:word_
85 inSpellDocumentWithTag:speller->doctag
88 SpellCheckResult result = match.length == 0 ? SPELL_CHECK_OK : SPELL_CHECK_FAILED;
89 if (result == SPELL_CHECK_OK && [NSSpellChecker instancesRespondToSelector:@selector(hasLearnedWord:)]) {
90 if ([speller->checker hasLearnedWord:word_])
91 result = SPELL_CHECK_LEARNED;
102 void ignoreAppleSpeller(AppleSpeller speller, const char * word)
104 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
105 NSString * word_ = toString(word);
107 [speller->checker ignoreWord:word_ inSpellDocumentWithTag:(speller->doctag)];
114 size_t makeSuggestionAppleSpeller(AppleSpeller speller, const char * word, const char * lang)
116 if (!speller->checker || !word || !lang)
119 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
120 NSString * word_ = toString(word);
121 NSString * lang_ = toString(lang);
124 #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
125 // Mac OS X 10.6 only
126 NSInteger slen = [word_ length];
127 NSRange range = { 0, slen };
129 if ([NSSpellChecker instancesRespondToSelector:@selector(guessesForWordRange:)]) {
130 result = [speller->checker guessesForWordRange:range
133 inSpellDocumentWithTag:speller->doctag];
135 [speller->checker setLanguage:lang_];
136 result = [speller->checker guessesForWord:word_];
139 [speller->checker setLanguage:lang_];
140 result = [speller->checker guessesForWord:word_];
146 freeSuggestionsAppleSpeller(speller);
148 speller->numsug = [result count];
149 if (speller->numsug) {
150 speller->suggestions = calloc(speller->numsug + 1, sizeof(char *));
151 if (speller->suggestions) {
153 for (i = 0; i < speller->numsug; i++) {
154 NSString * str = [result objectAtIndex:i];
155 speller->suggestions[i] = strdup([str UTF8String]);
157 speller->suggestions[speller->numsug] = 0;
161 return speller->numsug;
165 const char * getSuggestionAppleSpeller(AppleSpeller speller, size_t pos)
167 const char * result = 0;
168 if (pos < speller->numsug && speller->suggestions) {
169 result = speller->suggestions[pos] ;
175 void learnAppleSpeller(AppleSpeller speller, const char * word)
177 #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050)
178 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
179 NSString * word_ = toString(word);
181 if ([NSSpellChecker instancesRespondToSelector:@selector(learnWord:)])
182 [speller->checker learnWord:word_];
191 void unlearnAppleSpeller(AppleSpeller speller, const char * word)
193 #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050)
194 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
195 NSString * word_ = toString(word);
197 if ([NSSpellChecker instancesRespondToSelector:@selector(unlearnWord:)])
198 [speller->checker unlearnWord:word_];
206 int hasLanguageAppleSpeller(AppleSpeller speller, const char * lang)
209 #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050)
210 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
211 NSString * lang_ = toString(lang);
212 if ([NSSpellChecker instancesRespondToSelector:@selector(availableLanguages:)]) {
213 NSArray * languages = [speller->checker availableLanguages];
215 for (NSString *element in languages) {
216 result = [element isEqualToString:lang_] || [lang_ hasPrefix:element];
225 return result ? 1 : 0;