]> git.lyx.org Git - lyx.git/blob - src/AppleSpellChecker.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / AppleSpellChecker.cpp
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
43 AppleSpellChecker::Private::Private()
44 {
45         speller = newAppleSpeller();
46 }
47
48
49 AppleSpellChecker::Private::~Private()
50 {
51         freeAppleSpeller(speller);
52         speller = 0;
53 }
54
55
56 AppleSpellChecker::AppleSpellChecker()
57         : d(new Private)
58 {}
59
60
61 AppleSpellChecker::~AppleSpellChecker()
62 {
63         delete d;
64 }
65
66
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
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
81 SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word,
82         std::vector<WordLangTuple> const & docdict)
83 {
84         if (!hasDictionary(word.lang()))
85                 return NO_DICTIONARY;
86
87         string const word_str = to_utf8(word.word());
88         string const lang = d->languageMap[word.lang()->lang()];
89
90         vector<WordLangTuple>::const_iterator it = docdict.begin();
91         for (; it != docdict.end(); ++it) {
92                 if (it->lang()->code() != word.lang()->code())
93                         continue;
94                 if (it->word() == word.word())
95                         return DOCUMENT_LEARNED_WORD;
96         }
97
98         SpellCheckResult result =
99                 AppleSpeller_check(d->speller,
100                         word_str.c_str(), lang.c_str());
101         LYXERR(Debug::GUI, "spellCheck: \"" <<
102                    word.word() << "\" = " << d->toString(result) <<
103                    ", lang = " << lang) ;
104         return d->toResult(result);
105 }
106
107
108 void AppleSpellChecker::advanceChangeNumber()
109 {
110         nextChangeNumber();
111 }
112
113
114 // add to personal dictionary
115 void AppleSpellChecker::insert(WordLangTuple const & word)
116 {
117         string const word_str = to_utf8(word.word());
118         AppleSpeller_learn(d->speller, word_str.c_str());
119         LYXERR(Debug::GUI, "learn word: \"" << word.word() << "\"");
120         advanceChangeNumber();
121 }
122
123
124 // remove from personal dictionary
125 void AppleSpellChecker::remove(WordLangTuple const & word)
126 {
127         string const word_str = to_utf8(word.word());
128         AppleSpeller_unlearn(d->speller, word_str.c_str());
129         LYXERR(Debug::GUI, "unlearn word: \"" << word.word() << "\"");
130         advanceChangeNumber();
131 }
132
133
134 // ignore for session
135 void AppleSpellChecker::accept(WordLangTuple const & word)
136 {
137         string const word_str = to_utf8(word.word());
138         AppleSpeller_ignore(d->speller, word_str.c_str());
139         LYXERR(Debug::GUI, "ignore word: \"" << word.word() << "\"");
140         advanceChangeNumber();
141 }
142
143
144 void AppleSpellChecker::suggest(WordLangTuple const & wl,
145         docstring_list & suggestions)
146 {
147         suggestions.clear();
148         string const word_str = to_utf8(wl.word());
149         size_t num = AppleSpeller_makeSuggestion(d->speller,
150                                         word_str.c_str(), wl.lang()->code().c_str());
151         for (size_t i = 0; i < num; i++) {
152                 char const * next = AppleSpeller_getSuggestion(d->speller, i);
153                 if (!next) break;
154                 suggestions.push_back(from_utf8(next));
155         }
156 }
157
158
159 bool AppleSpellChecker::hasDictionary(Language const * lang) const
160 {
161         string const langmap = d->languageMap[lang->lang()];
162         bool result = !langmap.empty();
163
164         if (result)
165                 return result;
166
167         result = AppleSpeller_hasLanguage(d->speller, lang->code().c_str());
168         if (result) {
169                 d->languageMap[lang->lang()] = lang->code();
170         } else {
171                 result = AppleSpeller_hasLanguage(d->speller, lang->lang().c_str());
172                 if (result)
173                         d->languageMap[lang->lang()] = lang->lang();
174         }
175         LYXERR(Debug::GUI, "has dictionary: " << lang->lang() << " = " << result);
176         return result;
177 }
178
179
180 int AppleSpellChecker::numDictionaries() const
181 {
182         int result = 0;
183         map<string, string>::const_iterator it = d->languageMap.begin();
184         map<string, string>::const_iterator et = d->languageMap.end();
185
186         for (; it != et; ++it) {
187                 string const langmap = it->second;
188                 result += langmap.empty() ? 0 : 1;
189         }
190         return result;
191 }
192
193
194 int AppleSpellChecker::numMisspelledWords() const
195 {
196         return AppleSpeller_numMisspelledWords(d->speller);
197 }
198
199
200 void AppleSpellChecker::misspelledWord(int index, int & start, int & length) const
201 {
202         AppleSpeller_misspelledWord(d->speller, index, &start, &length);
203 }
204
205
206 docstring const AppleSpellChecker::error()
207 {
208         return docstring();
209 }
210
211
212 } // namespace lyx