]> git.lyx.org Git - features.git/blob - src/support/AppleSpeller.m
silence some warnings by removing unused parameters
[features.git] / src / support / AppleSpeller.m
1 /**
2  * \file AppleSpeller.m
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Stephan Witt
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #import <Carbon/Carbon.h>
12 #import <Cocoa/Cocoa.h>
13
14 #import <AvailabilityMacros.h>
15
16 #include "support/AppleSpeller.h"
17
18 typedef struct AppleSpellerRec {
19         NSSpellChecker * checker;
20 #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050)
21         NSInteger doctag;
22 #else
23         int doctag;
24 #endif
25         char ** suggestions;
26         size_t numsug;
27 } AppleSpellerRec ;
28
29
30 static void freeSuggestionsAppleSpeller(AppleSpeller speller)
31 {
32         if (speller->suggestions) {
33                 while (speller->numsug--) {
34                         free(speller->suggestions[speller->numsug]);
35                 }
36                 free(speller->suggestions);
37                 speller->suggestions = 0;
38         }
39 }
40
41
42 AppleSpeller newAppleSpeller(void)
43 {
44         AppleSpeller speller = calloc(1, sizeof(AppleSpellerRec));
45         speller->checker = [NSSpellChecker sharedSpellChecker];
46         speller->suggestions = 0;
47         speller->doctag = [NSSpellChecker uniqueSpellDocumentTag];
48         return speller;
49 }
50
51
52 void freeAppleSpeller(AppleSpeller speller)
53 {
54         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
55
56         freeSuggestionsAppleSpeller(speller);
57         [speller->checker closeSpellDocumentWithTag:speller->doctag];
58
59         [pool release];
60
61         free(speller);
62 }
63
64
65 static NSString * toString(const char * word)
66 {
67         return [[NSString alloc] initWithBytes:word length:strlen(word) encoding:NSUTF8StringEncoding];
68 }
69
70
71 int checkAppleSpeller(AppleSpeller speller, const char * word, const char * lang)
72 {
73         if (!speller->checker || !lang || !word)
74                 return 0;
75
76         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
77         NSString * word_ = toString(word);
78         NSString * lang_ = toString(lang);
79
80         NSRange result = [speller->checker
81                 checkSpellingOfString:word_
82                 startingAt:0
83                 language:lang_
84                 wrap:NO
85                 inSpellDocumentWithTag:speller->doctag
86                 wordCount:NULL];
87
88         [word_ release];
89         [lang_ release];
90         [pool release];
91
92         return (result.length ? 0 : 1);
93 }
94
95
96 void ignoreAppleSpeller(AppleSpeller speller, const char * word)
97 {
98         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
99         NSString * word_ = toString(word);
100
101         [speller->checker ignoreWord:word_ inSpellDocumentWithTag:(speller->doctag)];
102
103         [word_ release];
104         [pool release];
105 }
106
107
108 size_t makeSuggestionAppleSpeller(AppleSpeller speller, const char * word, const char * lang)
109 {
110         if (!speller->checker || !word || !lang)
111                 return 0;
112
113         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
114         NSString * word_ = toString(word);
115         NSString * lang_ = toString(lang);
116         NSArray * result ;
117
118 #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1060)
119         // Mac OS X 10.6 only
120         NSInteger slen = [word_ length];
121         NSRange range = { 0, slen };
122         
123         if ([NSSpellChecker instancesRespondToSelector:@selector(guessesForWordRange:)]) {
124                 result = [speller->checker guessesForWordRange:range
125                         inString:word_
126                         language:lang_
127                         inSpellDocumentWithTag:speller->doctag];
128         } else {
129                 [speller->checker setLanguage:lang_];
130                 result = [speller->checker guessesForWord:word_];
131         }
132 #else
133         [speller->checker setLanguage:lang_];
134         result = [speller->checker guessesForWord:word_];
135 #endif
136
137         [word_ release];
138         [lang_ release];
139
140         freeSuggestionsAppleSpeller(speller);
141
142         speller->numsug = [result count];
143         if (speller->numsug) {
144                 speller->suggestions = calloc(speller->numsug + 1, sizeof(char *));
145                 if (speller->suggestions) {
146                         size_t i;
147                         for (i = 0; i < speller->numsug; i++) {
148                                 NSString * str = [result objectAtIndex:i];
149                                 speller->suggestions[i] = strdup([str UTF8String]);
150                         }
151                         speller->suggestions[speller->numsug] = 0;
152                 }
153         }
154         [pool release];
155         return speller->numsug;
156 }
157
158
159 const char * getSuggestionAppleSpeller(AppleSpeller speller, size_t pos)
160 {
161         const char * result = 0;
162         if (pos < speller->numsug && speller->suggestions) {
163                 result = speller->suggestions[pos] ;
164         }
165         return result;
166 }
167
168
169 void learnAppleSpeller(AppleSpeller speller, const char * word)
170 {
171 #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050)
172         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
173         NSString * word_ = toString(word);
174
175         if ([NSSpellChecker instancesRespondToSelector:@selector(learnWord)])
176                 [speller->checker learnWord:word_];
177         
178         [word_ release];
179         [pool release];
180 #endif
181 }
182
183
184 int hasLanguageAppleSpeller(AppleSpeller speller, const char * lang)
185 {
186         BOOL result = NO;
187 #if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1050)
188         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
189         NSString * lang_ = toString(lang);
190         if ([NSSpellChecker instancesRespondToSelector:@selector(availableLanguages)]) {
191                 NSArray * languages = [speller->checker availableLanguages];
192
193                 for (NSString *element in languages) {
194                         result = [element isEqualToString:lang_] || [lang_ hasPrefix:element];
195                         if (result) break;
196                 }
197         }
198
199         [lang_ release];
200         [pool release];
201 #endif
202
203         return result ? 1 : 0;
204 }