]> git.lyx.org Git - lyx.git/blob - src/HunspellChecker.cpp
Properly restore the file encoding after a LaTeX environment with local scope.
[lyx.git] / src / HunspellChecker.cpp
1 /**
2  * \file HunspellChecker.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Abdelrazak Younes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "HunspellChecker.h"
14
15 #include "LyXRC.h"
16 #include "WordLangTuple.h"
17
18 #include "support/lassert.h"
19 #include "support/debug.h"
20 #include "support/docstring_list.h"
21
22 #include <hunspell/hunspell.hxx>
23
24 #include <map>
25 #include <string>
26
27 // FIXME (Abdel): I still got linking problems but if anybody wants
28 // to try, defines this to 1.
29 #define TRY_HUNSPELL 0
30
31 using namespace std;
32
33 namespace lyx {
34
35 namespace {
36
37 typedef map<std::string, Hunspell *> Spellers;
38
39 } // anon namespace
40
41 struct HunspellChecker::Private
42 {
43         Private() {}
44
45         ~Private();
46
47         Hunspell * addSpeller(string const & lang);
48         Hunspell * speller(string const & lang);
49
50         /// the spellers
51         Spellers spellers_;
52 };
53
54
55 HunspellChecker::Private::~Private()
56 {
57 #if TRY_HUNSPELL
58         Spellers::iterator it = spellers_.begin();
59         Spellers::iterator end = spellers_.end();
60
61         for (; it != end; ++it) {
62                 delete it->second;
63         }
64 #endif
65 }
66
67
68 Hunspell * HunspellChecker::Private::addSpeller(string const & lang)
69 {
70         // FIXME: not implemented!
71
72         // FIXME: We should we indicate somehow that this language is not
73         // supported.
74         return 0;
75 }
76
77
78 Hunspell * HunspellChecker::Private::speller(string const & lang)
79 {
80         Spellers::iterator it = spellers_.find(lang);
81         if (it != spellers_.end())
82                 return it->second;
83         
84         return addSpeller(lang);
85 }
86
87
88 HunspellChecker::HunspellChecker(): d(new Private)
89 {
90 }
91
92
93 HunspellChecker::~HunspellChecker()
94 {
95         delete d;
96 }
97
98
99 SpellChecker::Result HunspellChecker::check(WordLangTuple const & wl)
100 {
101         string const word_to_check = to_utf8(wl.word());
102 #if TRY_HUNSPELL
103         Hunspell * h = d->speller(wl.lang_code());
104         int info;
105         if (h->spell(word_to_check.c_str(), &info))
106                 return OK;
107         // FIXME: What to do with that?
108         switch (info) {
109         case SPELL_COMPOUND:
110         case SPELL_FORBIDDEN:
111         default:
112                 return UNKNOWN_WORD;
113         }
114         return UNKNOWN_WORD;
115 #endif
116         return OK;
117 }
118
119
120 void HunspellChecker::insert(WordLangTuple const & wl)
121 {
122         string const word_to_check = to_utf8(wl.word());
123 #if TRY_HUNSPELL
124         Hunspell * h = d->speller(wl.lang_code());
125         h->add(word_to_check.c_str());
126 #endif
127 }
128
129
130 void HunspellChecker::accept(WordLangTuple const & word)
131 {
132         // FIXME: not implemented!
133 }
134
135
136 void HunspellChecker::suggest(WordLangTuple const & wl,
137         docstring_list & suggestions)
138 {
139         suggestions.clear();
140         string const word_to_check = to_utf8(wl.word());
141 #if TRY_HUNSPELL
142         Hunspell * h = d->speller(wl.lang_code());
143         char *** suggestion_list = 0;
144         int const suggestion_number = h->suggest(suggestion_list, word_to_check.c_str());
145         if (suggestion_number == 0)
146                 return;
147         h->free_list(suggestion_list, suggestion_number);
148 #endif
149 }
150
151
152 docstring const HunspellChecker::error()
153 {
154         return docstring();
155 }
156
157
158 } // namespace lyx