]> git.lyx.org Git - lyx.git/blob - src/AppleSpellChecker.cpp
Workaround for #6865: smarter FontList::setMisspelled implementation
[lyx.git] / src / AppleSpellChecker.cpp
1 /**
2  * \file AppleSpellChecker.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Stephan Witt
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "AppleSpellChecker.h"
14 #include "WordLangTuple.h"
15
16 #include "support/lassert.h"
17 #include "support/debug.h"
18 #include "support/docstring_list.h"
19 #include "support/AppleSpeller.h"
20
21 using namespace std;
22 using namespace lyx::support;
23
24 namespace lyx {
25
26 struct AppleSpellChecker::Private
27 {
28         Private();
29
30         ~Private();
31
32         SpellChecker::Result toResult(SpellCheckResult status);
33         string toString(SpellCheckResult status);
34
35         /// the speller
36         AppleSpeller speller;
37 };
38
39
40 AppleSpellChecker::Private::Private()
41 {
42         speller = newAppleSpeller();
43 }
44
45
46 AppleSpellChecker::Private::~Private()
47 {
48         freeAppleSpeller(speller);
49         speller = 0;
50 }
51
52
53 AppleSpellChecker::AppleSpellChecker(): d(new Private)
54 {
55 }
56
57
58 AppleSpellChecker::~AppleSpellChecker()
59 {
60         delete d;
61 }
62
63
64 SpellChecker::Result AppleSpellChecker::Private::toResult(SpellCheckResult status)
65 {
66         return status == SPELL_CHECK_FAILED ? UNKNOWN_WORD :
67                 status == SPELL_CHECK_LEARNED ? LEARNED_WORD : WORD_OK ;
68 }
69
70
71 string AppleSpellChecker::Private::toString(SpellCheckResult status)
72 {
73         return status == SPELL_CHECK_FAILED ? "FAILED" :
74                  status == SPELL_CHECK_LEARNED ? "LEARNED" : "OK";
75 }
76
77
78 SpellChecker::Result AppleSpellChecker::check(WordLangTuple const & word)
79 {
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);
84 }
85
86
87 // add to personal dictionary
88 void AppleSpellChecker::insert(WordLangTuple const & word)
89 {
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() << "\"") ;
93 }
94
95
96 // remove from personal dictionary
97 void AppleSpellChecker::remove(WordLangTuple const & word)
98 {
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() << "\"") ;
102 }
103
104
105 // ignore for session
106 void AppleSpellChecker::accept(WordLangTuple const & word)
107 {
108         string const word_str = to_utf8(word.word());
109         ignoreAppleSpeller(d->speller, word_str.c_str());
110 }
111
112
113 void AppleSpellChecker::suggest(WordLangTuple const & wl,
114         docstring_list & suggestions)
115 {
116         suggestions.clear();
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);
121                 if (!next) break;
122                 suggestions.push_back(from_utf8(next));
123         }
124 }
125
126
127 bool AppleSpellChecker::hasDictionary(Language const * lang) const
128 {
129         return hasLanguageAppleSpeller(d->speller,lang->code().c_str());
130 }
131
132
133 docstring const AppleSpellChecker::error()
134 {
135         return docstring();
136 }
137
138
139 } // namespace lyx