]> git.lyx.org Git - lyx.git/blob - src/PSpell.cpp
'using namespace std' instead of 'using std::xxx'
[lyx.git] / src / PSpell.cpp
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.
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 "support/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 using namespace std;
28
29 namespace lyx {
30
31
32 PSpell::PSpell(BufferParams const &, string const & lang)
33         : els(0), spell_error_object(0)
34 {
35         addManager(lang);
36         LYXERR(Debug::GUI, "created pspell");
37 }
38
39
40 PSpell::~PSpell()
41 {
42         LYXERR(Debug::GUI, "killed pspell");
43
44         if (spell_error_object) {
45                 delete_pspell_can_have_error(spell_error_object);
46                 spell_error_object = 0;
47         }
48
49         if (els)
50                 delete_pspell_string_emulation(els);
51
52         Managers::iterator it = managers_.begin();
53         Managers::iterator end = managers_.end();
54
55         for (; it != end; ++it) {
56                 pspell_manager_save_all_word_lists(it->second.manager);
57                 delete_pspell_manager(it->second.manager);
58                 delete_pspell_config(it->second.config);
59         }
60 }
61
62
63 void PSpell::addManager(string const & lang)
64 {
65         PspellConfig * config = new_pspell_config();
66         pspell_config_replace(config, "language-tag", lang.c_str());
67         pspell_config_replace(config, "encoding", "utf-8");
68         PspellCanHaveError * err = new_pspell_manager(config);
69         if (spell_error_object)
70                 delete_pspell_can_have_error(spell_error_object);
71         spell_error_object = 0;
72
73         if (pspell_error_number(err) == 0) {
74                 Manager m;
75                 m.manager = to_pspell_manager(err);
76                 m.config = config;
77                 managers_[lang] = m;
78         } else {
79                 spell_error_object = err;
80         }
81 }
82
83
84 enum PSpell::Result PSpell::check(WordLangTuple const & word)
85 {
86         Result res = UNKNOWN_WORD;
87
88         Managers::iterator it = managers_.find(word.lang_code());
89         if (it == managers_.end()) {
90                 addManager(word.lang_code());
91                 it = managers_.find(word.lang_code());
92                 // FIXME
93                 if (it == managers_.end())
94                         return res;
95         }
96
97         PspellManager * m = it->second.manager;
98
99         int word_ok = pspell_manager_check(m, to_utf8(word.word()).c_str());
100         BOOST_ASSERT(word_ok != -1);
101
102         if (word_ok) {
103                 res = OK;
104         } else {
105                 PspellWordList const * sugs =
106                         pspell_manager_suggest(m, to_utf8(word.word()).c_str());
107                 BOOST_ASSERT(sugs != 0);
108                 els = pspell_word_list_elements(sugs);
109                 if (pspell_word_list_empty(sugs))
110                         res = UNKNOWN_WORD;
111                 else
112                         res = SUGGESTED_WORDS;
113         }
114         return res;
115 }
116
117
118 void PSpell::insert(WordLangTuple const & word)
119 {
120         Managers::iterator it = managers_.find(word.lang_code());
121         if (it != managers_.end())
122                 pspell_manager_add_to_personal(it->second.manager, to_utf8(word.word()).c_str());
123 }
124
125
126 void PSpell::accept(WordLangTuple const & word)
127 {
128         Managers::iterator it = managers_.find(word.lang_code());
129         if (it != managers_.end())
130                 pspell_manager_add_to_session(it->second.manager, to_utf8(word.word()).c_str());
131 }
132
133
134 docstring const PSpell::nextMiss()
135 {
136         char const * str = 0;
137
138         if (els)
139                 str = pspell_string_emulation_next(els);
140         if (str)
141                 return from_utf8(str);
142         return docstring();
143 }
144
145
146 docstring const PSpell::error()
147 {
148         char const * err = 0;
149
150         if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
151                 err = pspell_error_message(spell_error_object);
152         }
153
154         if (err)
155                 return from_utf8(err);
156         return docstring();
157 }
158
159
160 } // namespace lyx