]> git.lyx.org Git - lyx.git/blob - src/pspell.C
hopefully fix tex2lyx linking.
[lyx.git] / src / pspell.C
1 /**
2  * \file pspell.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Kevin Atkinson
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "debug.h"
15
16 #define USE_ORIGINAL_MANAGER_FUNCS 1
17 // new aspell pspell missing extern "C"
18 extern "C" {
19 #include <pspell/pspell.h>
20 }
21
22 #include "pspell.h"
23 #include "WordLangTuple.h"
24
25 #include <boost/assert.hpp>
26
27
28 namespace lyx {
29
30 using docstring;
31
32 using std::endl;
33 using std::string;
34
35
36 PSpell::PSpell(BufferParams const &, string const & lang)
37         : els(0), spell_error_object(0)
38 {
39         addManager(lang);
40         lyxerr[Debug::GUI] << "created pspell" << endl;
41 }
42
43
44 PSpell::~PSpell()
45 {
46         lyxerr[Debug::GUI] << "killed pspell" << endl;
47
48         if (spell_error_object) {
49                 delete_pspell_can_have_error(spell_error_object);
50                 spell_error_object = 0;
51         }
52
53         if (els)
54                 delete_pspell_string_emulation(els);
55
56         Managers::iterator it = managers_.begin();
57         Managers::iterator end = managers_.end();
58
59         for (; it != end; ++it) {
60                 pspell_manager_save_all_word_lists(it->second.manager);
61                 delete_pspell_manager(it->second.manager);
62                 delete_pspell_config(it->second.config);
63         }
64 }
65
66
67 void PSpell::addManager(string const & lang)
68 {
69         PspellConfig * config = new_pspell_config();
70         pspell_config_replace(config, "language-tag", lang.c_str());
71         PspellCanHaveError * err = new_pspell_manager(config);
72         if (spell_error_object)
73                 delete_pspell_can_have_error(spell_error_object);
74         spell_error_object = 0;
75
76         if (pspell_error_number(err) == 0) {
77                 Manager m;
78                 m.manager = to_pspell_manager(err);
79                 m.config = config;
80                 managers_[lang] = m;
81         } else {
82                 spell_error_object = err;
83         }
84 }
85
86
87 enum PSpell::Result PSpell::check(WordLangTuple const & word)
88 {
89         Result res = UNKNOWN_WORD;
90
91         Managers::iterator it = managers_.find(word.lang_code());
92         if (it == managers_.end()) {
93                 addManager(word.lang_code());
94                 it = managers_.find(word.lang_code());
95                 // FIXME
96                 if (it == managers_.end())
97                         return res;
98         }
99
100         PspellManager * m = it->second.manager;
101
102         int word_ok = pspell_manager_check(m, word.word().c_str());
103         BOOST_ASSERT(word_ok != -1);
104
105         if (word_ok) {
106                 res = OK;
107         } else {
108                 PspellWordList const * sugs =
109                         pspell_manager_suggest(m, word.word().c_str());
110                 BOOST_ASSERT(sugs != 0);
111                 els = pspell_word_list_elements(sugs);
112                 if (pspell_word_list_empty(sugs))
113                         res = UNKNOWN_WORD;
114                 else
115                         res = SUGGESTED_WORDS;
116         }
117         return res;
118 }
119
120
121 void PSpell::insert(WordLangTuple const & word)
122 {
123         Managers::iterator it = managers_.find(word.lang_code());
124         if (it != managers_.end())
125                 pspell_manager_add_to_personal(it->second.manager, word.word().c_str());
126 }
127
128
129 void PSpell::accept(WordLangTuple const & word)
130 {
131         Managers::iterator it = managers_.find(word.lang_code());
132         if (it != managers_.end())
133                 pspell_manager_add_to_session(it->second.manager, word.word().c_str());
134 }
135
136
137 string const PSpell::nextMiss()
138 {
139         char const * str = 0;
140
141         if (els)
142                 str = pspell_string_emulation_next(els);
143         if (str)
144                 return str;
145         return "";
146 }
147
148
149 docstring const PSpell::error()
150 {
151         char const * err = 0;
152
153         if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
154                 err = pspell_error_message(spell_error_object);
155         }
156
157         if (err)
158                 return err;
159         return "";
160 }
161
162
163 } // namespace lyx