]> git.lyx.org Git - lyx.git/blob - src/pspell.C
Convert the spell checking machinery to docstring.
[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 std::endl;
31 using std::string;
32
33
34 PSpell::PSpell(BufferParams const &, string const & lang)
35         : els(0), spell_error_object(0)
36 {
37         addManager(lang);
38         lyxerr[Debug::GUI] << "created pspell" << endl;
39 }
40
41
42 PSpell::~PSpell()
43 {
44         lyxerr[Debug::GUI] << "killed pspell" << endl;
45
46         if (spell_error_object) {
47                 delete_pspell_can_have_error(spell_error_object);
48                 spell_error_object = 0;
49         }
50
51         if (els)
52                 delete_pspell_string_emulation(els);
53
54         Managers::iterator it = managers_.begin();
55         Managers::iterator end = managers_.end();
56
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);
61         }
62 }
63
64
65 void PSpell::addManager(string const & lang)
66 {
67         PspellConfig * config = new_pspell_config();
68         pspell_config_replace(config, "language-tag", lang.c_str());
69         PspellCanHaveError * err = new_pspell_manager(config);
70         if (spell_error_object)
71                 delete_pspell_can_have_error(spell_error_object);
72         spell_error_object = 0;
73
74         if (pspell_error_number(err) == 0) {
75                 Manager m;
76                 m.manager = to_pspell_manager(err);
77                 m.config = config;
78                 managers_[lang] = m;
79         } else {
80                 spell_error_object = err;
81         }
82 }
83
84
85 enum PSpell::Result PSpell::check(WordLangTuple const & word)
86 {
87         Result res = UNKNOWN_WORD;
88
89         Managers::iterator it = managers_.find(word.lang_code());
90         if (it == managers_.end()) {
91                 addManager(word.lang_code());
92                 it = managers_.find(word.lang_code());
93                 // FIXME
94                 if (it == managers_.end())
95                         return res;
96         }
97
98         PspellManager * m = it->second.manager;
99
100         // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
101         int word_ok = pspell_manager_check(m, to_utf8(word.word()).c_str());
102         BOOST_ASSERT(word_ok != -1);
103
104         if (word_ok) {
105                 res = OK;
106         } else {
107                 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
108                 PspellWordList const * sugs =
109                         pspell_manager_suggest(m, to_utf8(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                 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
126                 pspell_manager_add_to_personal(it->second.manager, to_utf8(word.word()).c_str());
127 }
128
129
130 void PSpell::accept(WordLangTuple const & word)
131 {
132         Managers::iterator it = managers_.find(word.lang_code());
133         if (it != managers_.end())
134                 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
135                 pspell_manager_add_to_session(it->second.manager, to_utf8(word.word()).c_str());
136 }
137
138
139 docstring const PSpell::nextMiss()
140 {
141         char const * str = 0;
142
143         if (els)
144                 str = pspell_string_emulation_next(els);
145         if (str)
146                 // FIXME UNICODE: str is not in UTF8, but probably the locale encoding
147                 return from_utf8(str);
148         return docstring();
149 }
150
151
152 docstring const PSpell::error()
153 {
154         char const * err = 0;
155
156         if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
157                 err = pspell_error_message(spell_error_object);
158         }
159
160         if (err)
161                 // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
162                 return from_utf8(err);
163         return docstring();
164 }
165
166
167 } // namespace lyx