1 /**
2 * \file AppleSpellChecker.cpp
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 #include <config.h>
12
13 #include "AppleSpellChecker.h"
14 #include "WordLangTuple.h"
15
16 #include "support/debug.h"
17 #include "support/docstring_list.h"
18 #include "support/AppleSpeller.h"
19
20 using namespace std;
21 using namespace lyx::support;
22
23 namespace lyx {
24
25 struct AppleSpellChecker::Private
26 {
27 Private();
28
29 ~Private();
30
31 SpellChecker::Result toResult(SpellCheckResult status);
32 string toString(SpellCheckResult status);
33 int numDictionaries() const;
34
35 /// the speller
36 AppleSpeller speller;
37
38 /// language map
39 map<string, string> languageMap;
40 };
41
42
Private()43 AppleSpellChecker::Private::Private()
44 {
45 speller = newAppleSpeller();
46 }
47
48
~Private()49 AppleSpellChecker::Private::~Private()
50 {
51 freeAppleSpeller(speller);
52 speller = 0;
53 }
54
55
AppleSpellChecker()56 AppleSpellChecker::AppleSpellChecker()
57 : d(new Private)
58 {}
59
60
~AppleSpellChecker()61 AppleSpellChecker::~AppleSpellChecker()
62 {
63 delete d;
64 }
65
66
toResult(SpellCheckResult status)67 SpellChecker::Result AppleSpellChecker::Private::toResult(SpellCheckResult status)
68 {
69 return status == SPELL_CHECK_FAILED ? UNKNOWN_WORD :
70 status == SPELL_CHECK_LEARNED ? LEARNED_WORD : WORD_OK ;
71 }
72
73
toString(SpellCheckResult status)74 string AppleSpellChecker::Private::toString(SpellCheckResult status)
75 {
76 return status == SPELL_CHECK_FAILED ? "FAILED" :
77 status == SPELL_CHECK_LEARNED ? "LEARNED" : "OK";
78 }
79
80
check(WordLangTuple const & word)81 SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word)
82 {
83 if (!hasDictionary(word.lang()))
84 return NO_DICTIONARY;
85
86 string const word_str = to_utf8(word.word());
87 string const lang = d->languageMap[word.lang()->lang()];
88 SpellCheckResult result =
89 AppleSpeller_check(d->speller,
90 word_str.c_str(), lang.c_str());
91 LYXERR(Debug::GUI, "spellCheck: \"" <<
92 word.word() << "\" = " << d->toString(result) <<
93 ", lang = " << lang) ;
94 return d->toResult(result);
95 }
96
97
advanceChangeNumber()98 void AppleSpellChecker::advanceChangeNumber()
99 {
100 nextChangeNumber();
101 }
102
103
104 // add to personal dictionary
insert(WordLangTuple const & word)105 void AppleSpellChecker::insert(WordLangTuple const & word)
106 {
107 string const word_str = to_utf8(word.word());
108 AppleSpeller_learn(d->speller, word_str.c_str());
109 LYXERR(Debug::GUI, "learn word: \"" << word.word() << "\"");
110 advanceChangeNumber();
111 }
112
113
114 // remove from personal dictionary
remove(WordLangTuple const & word)115 void AppleSpellChecker::remove(WordLangTuple const & word)
116 {
117 string const word_str = to_utf8(word.word());
118 AppleSpeller_unlearn(d->speller, word_str.c_str());
119 LYXERR(Debug::GUI, "unlearn word: \"" << word.word() << "\"");
120 advanceChangeNumber();
121 }
122
123
124 // ignore for session
accept(WordLangTuple const & word)125 void AppleSpellChecker::accept(WordLangTuple const & word)
126 {
127 string const word_str = to_utf8(word.word());
128 AppleSpeller_ignore(d->speller, word_str.c_str());
129 LYXERR(Debug::GUI, "ignore word: \"" << word.word() << "\"");
130 advanceChangeNumber();
131 }
132
133
suggest(WordLangTuple const & wl,docstring_list & suggestions)134 void AppleSpellChecker::suggest(WordLangTuple const & wl,
135 docstring_list & suggestions)
136 {
137 suggestions.clear();
138 string const word_str = to_utf8(wl.word());
139 size_t num = AppleSpeller_makeSuggestion(d->speller,
140 word_str.c_str(), wl.lang()->code().c_str());
141 for (size_t i = 0; i < num; i++) {
142 char const * next = AppleSpeller_getSuggestion(d->speller, i);
143 if (!next) break;
144 suggestions.push_back(from_utf8(next));
145 }
146 }
147
148
hasDictionary(Language const * lang) const149 bool AppleSpellChecker::hasDictionary(Language const * lang) const
150 {
151 string const langmap = d->languageMap[lang->lang()];
152 bool result = !langmap.empty();
153
154 if (result)
155 return result;
156
157 result = AppleSpeller_hasLanguage(d->speller, lang->code().c_str());
158 if (result) {
159 d->languageMap[lang->lang()] = lang->code();
160 } else {
161 result = AppleSpeller_hasLanguage(d->speller, lang->lang().c_str());
162 if (result)
163 d->languageMap[lang->lang()] = lang->lang();
164 }
165 LYXERR(Debug::GUI, "has dictionary: " << lang->lang() << " = " << result);
166 return result;
167 }
168
169
numDictionaries() const170 int AppleSpellChecker::numDictionaries() const
171 {
172 int result = 0;
173 map<string, string>::const_iterator it = d->languageMap.begin();
174 map<string, string>::const_iterator et = d->languageMap.end();
175
176 for (; it != et; ++it) {
177 string const langmap = it->second;
178 result += langmap.empty() ? 0 : 1;
179 }
180 return result;
181 }
182
183
numMisspelledWords() const184 int AppleSpellChecker::numMisspelledWords() const
185 {
186 return AppleSpeller_numMisspelledWords(d->speller);
187 }
188
189
misspelledWord(int index,int & start,int & length) const190 void AppleSpellChecker::misspelledWord(int index, int & start, int & length) const
191 {
192 AppleSpeller_misspelledWord(d->speller, index, &start, &length);
193 }
194
195
error()196 docstring const AppleSpellChecker::error()
197 {
198 return docstring();
199 }
200
201
202 } // namespace lyx
203