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;
44 AppleSpellChecker::Private::Private()
46 speller = newAppleSpeller();
50 AppleSpellChecker::Private::~Private()
52 freeAppleSpeller(speller);
57 AppleSpellChecker::AppleSpellChecker()
62 AppleSpellChecker::~AppleSpellChecker()
68 SpellChecker::Result AppleSpellChecker::Private::toResult(SpellCheckResult status)
70 return status == SPELL_CHECK_FAILED ? UNKNOWN_WORD :
71 status == SPELL_CHECK_LEARNED ? LEARNED_WORD : WORD_OK ;
75 string AppleSpellChecker::Private::toString(SpellCheckResult status)
77 return status == SPELL_CHECK_FAILED ? "FAILED" :
78 status == SPELL_CHECK_LEARNED ? "LEARNED" : "OK";
82 SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word)
84 if (!hasDictionary(word.lang()))
87 string const word_str = to_utf8(word.word());
88 string const lang = d->languageMap[word.lang()->lang()];
89 SpellCheckResult result =
90 AppleSpeller_check(d->speller,
91 word_str.c_str(), lang.c_str());
92 LYXERR(Debug::GUI, "spellCheck: \"" <<
93 word.word() << "\" = " << d->toString(result) <<
94 ", lang = " << lang) ;
95 return d->toResult(result);
99 void AppleSpellChecker::advanceChangeNumber()
105 // add to personal dictionary
106 void AppleSpellChecker::insert(WordLangTuple const & word)
108 string const word_str = to_utf8(word.word());
109 AppleSpeller_learn(d->speller, word_str.c_str());
110 LYXERR(Debug::GUI, "learn word: \"" << word.word() << "\"") ;
111 advanceChangeNumber();
115 // remove from personal dictionary
116 void AppleSpellChecker::remove(WordLangTuple const & word)
118 string const word_str = to_utf8(word.word());
119 AppleSpeller_unlearn(d->speller, word_str.c_str());
120 LYXERR(Debug::GUI, "unlearn word: \"" << word.word() << "\"") ;
121 advanceChangeNumber();
125 // ignore for session
126 void AppleSpellChecker::accept(WordLangTuple const & word)
128 string const word_str = to_utf8(word.word());
129 AppleSpeller_ignore(d->speller, word_str.c_str());
130 LYXERR(Debug::GUI, "ignore word: \"" << word.word() << "\"") ;
131 advanceChangeNumber();
135 void AppleSpellChecker::suggest(WordLangTuple const & wl,
136 docstring_list & suggestions)
139 string const word_str = to_utf8(wl.word());
140 size_t num = AppleSpeller_makeSuggestion(d->speller,
141 word_str.c_str(), wl.lang()->code().c_str());
142 for (size_t i = 0; i < num; i++) {
143 char const * next = AppleSpeller_getSuggestion(d->speller, i);
145 suggestions.push_back(from_utf8(next));
150 bool AppleSpellChecker::hasDictionary(Language const * lang) const
152 string const langmap = d->languageMap[lang->lang()];
153 bool result = !langmap.empty();
158 result = AppleSpeller_hasLanguage(d->speller,lang->code().c_str());
160 d->languageMap[lang->lang()] = lang->code();
162 result = AppleSpeller_hasLanguage(d->speller,lang->lang().c_str());
164 d->languageMap[lang->lang()] = lang->lang();
166 LYXERR(Debug::GUI, "has dictionary: " << lang->lang() << " = " << result) ;
171 int AppleSpellChecker::numDictionaries() const
174 map<string, string>::const_iterator it = d->languageMap.begin();
175 map<string, string>::const_iterator et = d->languageMap.end();
177 for (; it != et; ++it) {
178 string const langmap = it->second;
179 result += langmap.empty() ? 0 : 1;
185 int AppleSpellChecker::numMisspelledWords() const
187 return AppleSpeller_numMisspelledWords(d->speller);
191 void AppleSpellChecker::misspelledWord(int index, int & start, int & length) const
193 AppleSpeller_misspelledWord(d->speller, index, &start, &length);
197 docstring const AppleSpellChecker::error()