2 * \file EnchantChecker.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Caolán McNamara
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include <enchant++.h>
16 #include "EnchantChecker.h"
18 #include "WordLangTuple.h"
20 #include "support/lassert.h"
21 #include "support/debug.h"
22 #include "support/docstring_list.h"
34 enchant::Dict * speller;
37 typedef map<string, Speller> Spellers;
41 struct EnchantChecker::Private
48 /// add a speller of the given language
49 enchant::Dict * addSpeller(string const & lang);
52 enchant::Dict * speller(string const & lang);
59 EnchantChecker::Private::~Private()
61 Spellers::iterator it = spellers_.begin();
62 Spellers::iterator end = spellers_.end();
64 for (; it != end; ++it)
65 delete it->second.speller;
69 enchant::Dict * EnchantChecker::Private::addSpeller(string const & lang)
71 enchant::Broker * instance = enchant::Broker::instance();
73 if (!instance->dict_exists(lang))
74 // FIXME error handling?
77 enchant::Dict * dict = instance->request_dict(lang);
85 // FIXME error handling?
90 enchant::Dict * EnchantChecker::Private::speller(string const & lang)
92 Spellers::iterator it = spellers_.find(lang);
93 if (it != spellers_.end())
94 return it->second.speller;
96 return addSpeller(lang);
100 EnchantChecker::EnchantChecker()
105 EnchantChecker::~EnchantChecker()
111 SpellChecker::Result EnchantChecker::check(WordLangTuple const & word)
113 enchant::Dict * m = d->speller(word.lang()->code());
116 return NO_DICTIONARY;
118 if (word.word().empty())
121 string utf8word = to_utf8(word.word());
123 if (m->check(utf8word))
130 void EnchantChecker::advanceChangeNumber()
136 void EnchantChecker::insert(WordLangTuple const & word)
138 Spellers::iterator it = d->spellers_.find(word.lang()->code());
139 if (it != d->spellers_.end()) {
140 it->second.speller->add(to_utf8(word.word()));
141 advanceChangeNumber();
146 void EnchantChecker::remove(WordLangTuple const & word)
148 Spellers::iterator it = d->spellers_.find(word.lang()->code());
149 if (it != d->spellers_.end()) {
150 it->second.speller->remove(to_utf8(word.word()));
151 advanceChangeNumber();
156 void EnchantChecker::accept(WordLangTuple const & word)
158 Spellers::iterator it = d->spellers_.find(word.lang()->code());
159 if (it != d->spellers_.end()) {
160 it->second.speller->add_to_session(to_utf8(word.word()));
161 advanceChangeNumber();
166 void EnchantChecker::suggest(WordLangTuple const & wl,
167 docstring_list & suggestions)
170 enchant::Dict * m = d->speller(wl.lang()->code());
175 string utf8word = to_utf8(wl.word());
177 vector<string> suggs = m->suggest(utf8word);
178 vector<string>::const_iterator it = suggs.begin();
180 for (; it != suggs.end(); ++it)
181 suggestions.push_back(from_utf8(*it));
185 bool EnchantChecker::hasDictionary(Language const * lang) const
189 enchant::Broker * instance = enchant::Broker::instance();
190 return (instance->dict_exists(lang->code()));
194 int EnchantChecker::numDictionaries() const
196 return d->spellers_.size();
200 docstring const EnchantChecker::error()