]> git.lyx.org Git - lyx.git/blob - src/AppleSpellChecker.cpp
Increase tex2lyx output format to 296.
[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 =
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);
88 }
89
90
91 void AppleSpellChecker::advanceChangeNumber()
92 {
93         nextChangeNumber();
94 }
95
96
97 // add to personal dictionary
98 void AppleSpellChecker::insert(WordLangTuple const & word)
99 {
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();
104 }
105
106
107 // remove from personal dictionary
108 void AppleSpellChecker::remove(WordLangTuple const & word)
109 {
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();
114 }
115
116
117 // ignore for session
118 void AppleSpellChecker::accept(WordLangTuple const & word)
119 {
120         string const word_str = to_utf8(word.word());
121         AppleSpeller_ignore(d->speller, word_str.c_str());
122         LYXERR(Debug::GUI, "ignore word: \"" << word.word() << "\"") ;
123         advanceChangeNumber();
124 }
125
126
127 void AppleSpellChecker::suggest(WordLangTuple const & wl,
128         docstring_list & suggestions)
129 {
130         suggestions.clear();
131         string const word_str = to_utf8(wl.word());
132         size_t num = AppleSpeller_makeSuggestion(d->speller, word_str.c_str(), wl.lang()->code().c_str());
133         for (size_t i = 0; i < num; i++) {
134                 char const * next = AppleSpeller_getSuggestion(d->speller, i);
135                 if (!next) break;
136                 suggestions.push_back(from_utf8(next));
137         }
138 }
139
140
141 bool AppleSpellChecker::hasDictionary(Language const * lang) const
142 {
143         return AppleSpeller_hasLanguage(d->speller,lang->code().c_str());
144 }
145
146
147 int AppleSpellChecker::numMisspelledWords() const
148 {
149         return AppleSpeller_numMisspelledWords(d->speller);
150 }
151
152
153 void AppleSpellChecker::misspelledWord(int index, int & start, int & length) const
154 {
155         AppleSpeller_misspelledWord(d->speller, index, &start, &length);
156 }
157
158
159 docstring const AppleSpellChecker::error()
160 {
161         return docstring();
162 }
163
164
165 } // namespace lyx