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);
39 map<string, string> languageMap;
44 AppleSpellChecker::Private::Private()
46 speller = newAppleSpeller();
50 AppleSpellChecker::Private::~Private()
52 freeAppleSpeller(speller);
57 AppleSpellChecker::AppleSpellChecker(): d(new Private)
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()->code()];
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, 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->code()];
152 bool result = !langmap.empty();
157 result = AppleSpeller_hasLanguage(d->speller,lang->code().c_str());
159 d->languageMap[lang->code()] = lang->code();
161 result = AppleSpeller_hasLanguage(d->speller,lang->lang().c_str());
163 d->languageMap[lang->code()] = lang->lang();
169 int AppleSpellChecker::numMisspelledWords() const
171 return AppleSpeller_numMisspelledWords(d->speller);
175 void AppleSpellChecker::misspelledWord(int index, int & start, int & length) const
177 AppleSpeller_misspelledWord(d->speller, index, &start, &length);
181 docstring const AppleSpellChecker::error()