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)
83 if (!hasDictionary(word.lang()))
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);
98 void AppleSpellChecker::advanceChangeNumber()
104 // add to personal dictionary
105 void AppleSpellChecker::insert(WordLangTuple const & word)
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();
114 // remove from personal dictionary
115 void AppleSpellChecker::remove(WordLangTuple const & word)
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();
124 // ignore for session
125 void AppleSpellChecker::accept(WordLangTuple const & word)
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();
134 void AppleSpellChecker::suggest(WordLangTuple const & wl,
135 docstring_list & suggestions)
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);
144 suggestions.push_back(from_utf8(next));
149 bool AppleSpellChecker::hasDictionary(Language const * lang) const
151 string const langmap = d->languageMap[lang->lang()];
152 bool result = !langmap.empty();
157 result = AppleSpeller_hasLanguage(d->speller, lang->code().c_str());
159 d->languageMap[lang->lang()] = lang->code();
161 result = AppleSpeller_hasLanguage(d->speller, lang->lang().c_str());
163 d->languageMap[lang->lang()] = lang->lang();
165 LYXERR(Debug::GUI, "has dictionary: " << lang->lang() << " = " << result);
170 int AppleSpellChecker::numDictionaries() const
173 map<string, string>::const_iterator it = d->languageMap.begin();
174 map<string, string>::const_iterator et = d->languageMap.end();
176 for (; it != et; ++it) {
177 string const langmap = it->second;
178 result += langmap.empty() ? 0 : 1;
184 int AppleSpellChecker::numMisspelledWords() const
186 return AppleSpeller_numMisspelledWords(d->speller);
190 void AppleSpellChecker::misspelledWord(int index, int & start, int & length) const
192 AppleSpeller_misspelledWord(d->speller, index, &start, &length);
196 docstring const AppleSpellChecker::error()