2 * \file AppleSpellChecker.cpp
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.
13 #include "AppleSpellChecker.h"
14 #include "WordLangTuple.h"
16 #include "support/debug.h"
17 #include "support/docstring_list.h"
18 #include "support/AppleSpeller.h"
21 using namespace lyx::support;
25 struct AppleSpellChecker::Private
31 SpellChecker::Result toResult(SpellCheckResult status);
32 string toString(SpellCheckResult status);
33 int numDictionaries() const;
39 map<string, string> languageMap;
43 AppleSpellChecker::Private::Private()
45 speller = newAppleSpeller();
49 AppleSpellChecker::Private::~Private()
51 freeAppleSpeller(speller);
56 AppleSpellChecker::AppleSpellChecker()
61 AppleSpellChecker::~AppleSpellChecker()
67 SpellChecker::Result AppleSpellChecker::Private::toResult(SpellCheckResult status)
69 return status == SPELL_CHECK_FAILED ? UNKNOWN_WORD :
70 status == SPELL_CHECK_LEARNED ? LEARNED_WORD : WORD_OK ;
74 string AppleSpellChecker::Private::toString(SpellCheckResult status)
76 return status == SPELL_CHECK_FAILED ? "FAILED" :
77 status == SPELL_CHECK_LEARNED ? "LEARNED" : "OK";
81 SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word,
82 std::vector<WordLangTuple> const & docdict)
84 if (!hasDictionary(word.lang()))
87 string const word_str = to_utf8(word.word());
88 string const lang = d->languageMap[word.lang()->lang()];
90 vector<WordLangTuple>::const_iterator it = docdict.begin();
91 for (; it != docdict.end(); ++it) {
92 if (it->lang()->code() != word.lang()->code())
94 if (it->word() == word.word())
95 return DOCUMENT_LEARNED_WORD;
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);
108 void AppleSpellChecker::advanceChangeNumber()
114 // add to personal dictionary
115 void AppleSpellChecker::insert(WordLangTuple const & word)
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();
124 // remove from personal dictionary
125 void AppleSpellChecker::remove(WordLangTuple const & word)
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();
134 // ignore for session
135 void AppleSpellChecker::accept(WordLangTuple const & word)
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();
144 void AppleSpellChecker::suggest(WordLangTuple const & wl,
145 docstring_list & suggestions)
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);
154 suggestions.push_back(from_utf8(next));
159 bool AppleSpellChecker::hasDictionary(Language const * lang) const
161 string const langmap = d->languageMap[lang->lang()];
162 bool result = !langmap.empty();
167 result = AppleSpeller_hasLanguage(d->speller, lang->code().c_str());
169 d->languageMap[lang->lang()] = lang->code();
171 result = AppleSpeller_hasLanguage(d->speller, lang->lang().c_str());
173 d->languageMap[lang->lang()] = lang->lang();
175 LYXERR(Debug::GUI, "has dictionary: " << lang->lang() << " = " << result);
180 int AppleSpellChecker::numDictionaries() const
183 map<string, string>::const_iterator it = d->languageMap.begin();
184 map<string, string>::const_iterator et = d->languageMap.end();
186 for (; it != et; ++it) {
187 string const langmap = it->second;
188 result += langmap.empty() ? 0 : 1;
194 int AppleSpellChecker::numMisspelledWords() const
196 return AppleSpeller_numMisspelledWords(d->speller);
200 void AppleSpellChecker::misspelledWord(int index, int & start, int & length) const
202 AppleSpeller_misspelledWord(d->speller, index, &start, &length);
206 docstring const AppleSpellChecker::error()