scramble email addresses
[lyx.git] / src / PSpell.cpp
blob3d5f043dcf523961cd989ce0f437b97b2d7522cc
1 /**
2 * \file PSpell.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Kevin Atkinson
7 * \author John Levon
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "debug.h"
16 #define USE_ORIGINAL_MANAGER_FUNCS 1
17 // new aspell pspell missing extern "C"
18 extern "C" {
19 #include <pspell/PSpell.h>
22 #include "PSpell.h"
23 #include "WordLangTuple.h"
25 #include <boost/assert.hpp>
28 namespace lyx {
30 using std::endl;
31 using std::string;
34 PSpell::PSpell(BufferParams const &, string const & lang)
35 : els(0), spell_error_object(0)
37 addManager(lang);
38 LYXERR(Debug::GUI) << "created pspell" << endl;
42 PSpell::~PSpell()
44 LYXERR(Debug::GUI) << "killed pspell" << endl;
46 if (spell_error_object) {
47 delete_pspell_can_have_error(spell_error_object);
48 spell_error_object = 0;
51 if (els)
52 delete_pspell_string_emulation(els);
54 Managers::iterator it = managers_.begin();
55 Managers::iterator end = managers_.end();
57 for (; it != end; ++it) {
58 pspell_manager_save_all_word_lists(it->second.manager);
59 delete_pspell_manager(it->second.manager);
60 delete_pspell_config(it->second.config);
65 void PSpell::addManager(string const & lang)
67 PspellConfig * config = new_pspell_config();
68 pspell_config_replace(config, "language-tag", lang.c_str());
69 pspell_config_replace(config, "encoding", "utf-8");
70 PspellCanHaveError * err = new_pspell_manager(config);
71 if (spell_error_object)
72 delete_pspell_can_have_error(spell_error_object);
73 spell_error_object = 0;
75 if (pspell_error_number(err) == 0) {
76 Manager m;
77 m.manager = to_pspell_manager(err);
78 m.config = config;
79 managers_[lang] = m;
80 } else {
81 spell_error_object = err;
86 enum PSpell::Result PSpell::check(WordLangTuple const & word)
88 Result res = UNKNOWN_WORD;
90 Managers::iterator it = managers_.find(word.lang_code());
91 if (it == managers_.end()) {
92 addManager(word.lang_code());
93 it = managers_.find(word.lang_code());
94 // FIXME
95 if (it == managers_.end())
96 return res;
99 PspellManager * m = it->second.manager;
101 int word_ok = pspell_manager_check(m, to_utf8(word.word()).c_str());
102 BOOST_ASSERT(word_ok != -1);
104 if (word_ok) {
105 res = OK;
106 } else {
107 PspellWordList const * sugs =
108 pspell_manager_suggest(m, to_utf8(word.word()).c_str());
109 BOOST_ASSERT(sugs != 0);
110 els = pspell_word_list_elements(sugs);
111 if (pspell_word_list_empty(sugs))
112 res = UNKNOWN_WORD;
113 else
114 res = SUGGESTED_WORDS;
116 return res;
120 void PSpell::insert(WordLangTuple const & word)
122 Managers::iterator it = managers_.find(word.lang_code());
123 if (it != managers_.end())
124 pspell_manager_add_to_personal(it->second.manager, to_utf8(word.word()).c_str());
128 void PSpell::accept(WordLangTuple const & word)
130 Managers::iterator it = managers_.find(word.lang_code());
131 if (it != managers_.end())
132 pspell_manager_add_to_session(it->second.manager, to_utf8(word.word()).c_str());
136 docstring const PSpell::nextMiss()
138 char const * str = 0;
140 if (els)
141 str = pspell_string_emulation_next(els);
142 if (str)
143 return from_utf8(str);
144 return docstring();
148 docstring const PSpell::error()
150 char const * err = 0;
152 if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
153 err = pspell_error_message(spell_error_object);
156 if (err)
157 return from_utf8(err);
158 return docstring();
162 } // namespace lyx