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);
40 AppleSpellChecker::Private::Private()
42 speller = newAppleSpeller();
46 AppleSpellChecker::Private::~Private()
48 freeAppleSpeller(speller);
53 AppleSpellChecker::AppleSpellChecker(): d(new Private)
58 AppleSpellChecker::~AppleSpellChecker()
64 SpellChecker::Result AppleSpellChecker::Private::toResult(SpellCheckResult status)
66 return status == SPELL_CHECK_FAILED ? UNKNOWN_WORD :
67 status == SPELL_CHECK_LEARNED ? LEARNED_WORD : WORD_OK ;
71 string AppleSpellChecker::Private::toString(SpellCheckResult status)
73 return status == SPELL_CHECK_FAILED ? "FAILED" :
74 status == SPELL_CHECK_LEARNED ? "LEARNED" : "OK";
78 SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word)
80 string const word_str = to_utf8(word.word());
81 SpellCheckResult result =
82 AppleSpeller_check(d->speller,
83 word_str.c_str(), word.lang()->code().c_str());
84 LYXERR(Debug::GUI, "spellCheck: \"" <<
85 word.word() << "\" = " << d->toString(result) <<
86 ", lang = " << word.lang()->code()) ;
87 return d->toResult(result);
91 void AppleSpellChecker::advanceChangeNumber()
97 // add to personal dictionary
98 void AppleSpellChecker::insert(WordLangTuple const & word)
100 string const word_str = to_utf8(word.word());
101 AppleSpeller_learn(d->speller, word_str.c_str());
102 LYXERR(Debug::GUI, "learn word: \"" << word.word() << "\"") ;
103 advanceChangeNumber();
107 // remove from personal dictionary
108 void AppleSpellChecker::remove(WordLangTuple const & word)
110 string const word_str = to_utf8(word.word());
111 AppleSpeller_unlearn(d->speller, word_str.c_str());
112 LYXERR(Debug::GUI, "unlearn word: \"" << word.word() << "\"") ;
113 advanceChangeNumber();
117 // ignore for session
118 void AppleSpellChecker::accept(WordLangTuple const & word)
120 string const word_str = to_utf8(word.word());
121 AppleSpeller_ignore(d->speller, word_str.c_str());
122 advanceChangeNumber();
126 void AppleSpellChecker::suggest(WordLangTuple const & wl,
127 docstring_list & suggestions)
130 string const word_str = to_utf8(wl.word());
131 size_t num = AppleSpeller_makeSuggestion(d->speller, word_str.c_str(), wl.lang()->code().c_str());
132 for (size_t i = 0; i < num; i++) {
133 char const * next = AppleSpeller_getSuggestion(d->speller, i);
135 suggestions.push_back(from_utf8(next));
140 bool AppleSpellChecker::hasDictionary(Language const * lang) const
142 return AppleSpeller_hasLanguage(d->speller,lang->code().c_str());
146 int AppleSpellChecker::numMisspelledWords() const
148 return AppleSpeller_numMisspelledWords(d->speller);
152 void AppleSpellChecker::misspelledWord(int index, int & start, int & length) const
154 AppleSpeller_misspelledWord(d->speller, index, &start, &length);
158 docstring const AppleSpellChecker::error()