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/lassert.h"
17 #include "support/debug.h"
18 #include "support/docstring_list.h"
19 #include "support/AppleSpeller.h"
22 using namespace lyx::support;
26 struct AppleSpellChecker::Private
32 SpellChecker::Result toResult(SpellCheckResult status);
33 string toString(SpellCheckResult status);
34 int numDictionaries() const;
40 map<string, string> languageMap;
45 AppleSpellChecker::Private::Private()
47 speller = newAppleSpeller();
51 AppleSpellChecker::Private::~Private()
53 freeAppleSpeller(speller);
58 AppleSpellChecker::AppleSpellChecker(): d(new Private)
63 AppleSpellChecker::~AppleSpellChecker()
69 SpellChecker::Result AppleSpellChecker::Private::toResult(SpellCheckResult status)
71 return status == SPELL_CHECK_FAILED ? UNKNOWN_WORD :
72 status == SPELL_CHECK_LEARNED ? LEARNED_WORD : WORD_OK ;
76 string AppleSpellChecker::Private::toString(SpellCheckResult status)
78 return status == SPELL_CHECK_FAILED ? "FAILED" :
79 status == SPELL_CHECK_LEARNED ? "LEARNED" : "OK";
83 SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word)
85 if (!hasDictionary(word.lang()))
88 string const word_str = to_utf8(word.word());
89 string const lang = d->languageMap[word.lang()->lang()];
90 SpellCheckResult result =
91 AppleSpeller_check(d->speller,
92 word_str.c_str(), lang.c_str());
93 LYXERR(Debug::GUI, "spellCheck: \"" <<
94 word.word() << "\" = " << d->toString(result) <<
95 ", lang = " << lang) ;
96 return d->toResult(result);
100 void AppleSpellChecker::advanceChangeNumber()
106 // add to personal dictionary
107 void AppleSpellChecker::insert(WordLangTuple const & word)
109 string const word_str = to_utf8(word.word());
110 AppleSpeller_learn(d->speller, word_str.c_str());
111 LYXERR(Debug::GUI, "learn word: \"" << word.word() << "\"") ;
112 advanceChangeNumber();
116 // remove from personal dictionary
117 void AppleSpellChecker::remove(WordLangTuple const & word)
119 string const word_str = to_utf8(word.word());
120 AppleSpeller_unlearn(d->speller, word_str.c_str());
121 LYXERR(Debug::GUI, "unlearn word: \"" << word.word() << "\"") ;
122 advanceChangeNumber();
126 // ignore for session
127 void AppleSpellChecker::accept(WordLangTuple const & word)
129 string const word_str = to_utf8(word.word());
130 AppleSpeller_ignore(d->speller, word_str.c_str());
131 LYXERR(Debug::GUI, "ignore word: \"" << word.word() << "\"") ;
132 advanceChangeNumber();
136 void AppleSpellChecker::suggest(WordLangTuple const & wl,
137 docstring_list & suggestions)
140 string const word_str = to_utf8(wl.word());
141 size_t num = AppleSpeller_makeSuggestion(d->speller,
142 word_str.c_str(), wl.lang()->code().c_str());
143 for (size_t i = 0; i < num; i++) {
144 char const * next = AppleSpeller_getSuggestion(d->speller, i);
146 suggestions.push_back(from_utf8(next));
151 bool AppleSpellChecker::hasDictionary(Language const * lang) const
153 string const langmap = d->languageMap[lang->lang()];
154 bool result = !langmap.empty();
159 result = AppleSpeller_hasLanguage(d->speller,lang->code().c_str());
161 d->languageMap[lang->lang()] = lang->code();
163 result = AppleSpeller_hasLanguage(d->speller,lang->lang().c_str());
165 d->languageMap[lang->lang()] = lang->lang();
167 LYXERR(Debug::GUI, "has dictionary: " << lang->lang() << " = " << result) ;
172 int AppleSpellChecker::numDictionaries() const
175 map<string, string>::const_iterator it = d->languageMap.begin();
176 map<string, string>::const_iterator et = d->languageMap.end();
178 for (; it != et; ++it) {
179 string const langmap = it->second;
180 result += langmap.empty() ? 0 : 1;
186 int AppleSpellChecker::numMisspelledWords() const
188 return AppleSpeller_numMisspelledWords(d->speller);
192 void AppleSpellChecker::misspelledWord(int index, int & start, int & length) const
194 AppleSpeller_misspelledWord(d->speller, index, &start, &length);
198 docstring const AppleSpellChecker::error()