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 = checkAppleSpeller(d->speller, word_str.c_str(), word.lang()->code().c_str());
82 LYXERR(Debug::GUI, "spellCheck: \"" << word.word() << "\" = " << d->toString(result)) ;
83 return d->toResult(result);
87 // add to personal dictionary
88 void AppleSpellChecker::insert(WordLangTuple const & word)
90 string const word_str = to_utf8(word.word());
91 learnAppleSpeller(d->speller, word_str.c_str());
92 LYXERR(Debug::GUI, "learn word: \"" << word.word() << "\"") ;
96 // remove from personal dictionary
97 void AppleSpellChecker::remove(WordLangTuple const & word)
99 string const word_str = to_utf8(word.word());
100 unlearnAppleSpeller(d->speller, word_str.c_str());
101 LYXERR(Debug::GUI, "unlearn word: \"" << word.word() << "\"") ;
105 // ignore for session
106 void AppleSpellChecker::accept(WordLangTuple const & word)
108 string const word_str = to_utf8(word.word());
109 ignoreAppleSpeller(d->speller, word_str.c_str());
113 void AppleSpellChecker::suggest(WordLangTuple const & wl,
114 docstring_list & suggestions)
117 string const word_str = to_utf8(wl.word());
118 size_t num = makeSuggestionAppleSpeller(d->speller, word_str.c_str(), wl.lang()->code().c_str());
119 for (size_t i = 0; i < num; i++) {
120 char const * next = getSuggestionAppleSpeller(d->speller, i);
122 suggestions.push_back(from_utf8(next));
127 bool AppleSpellChecker::hasDictionary(Language const * lang) const
129 return hasLanguageAppleSpeller(d->speller,lang->code().c_str());
133 docstring const AppleSpellChecker::error()