]> git.lyx.org Git - lyx.git/blob - src/PSpell.cpp
english_language was not used at all.
[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 "support/lassert.h"
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32
33 PSpell::PSpell(BufferParams const &, string const & lang)
34         : els(0), spell_error_object(0)
35 {
36         addManager(lang);
37         LYXERR(Debug::GUI, "created pspell");
38 }
39
40
41 PSpell::~PSpell()
42 {
43         LYXERR(Debug::GUI, "killed pspell");
44
45         if (spell_error_object) {
46                 delete_pspell_can_have_error(spell_error_object);
47                 spell_error_object = 0;
48         }
49
50         if (els)
51                 delete_pspell_string_emulation(els);
52
53         Managers::iterator it = managers_.begin();
54         Managers::iterator end = managers_.end();
55
56         for (; it != end; ++it) {
57                 pspell_manager_save_all_word_lists(it->second.manager);
58                 delete_pspell_manager(it->second.manager);
59                 delete_pspell_config(it->second.config);
60         }
61 }
62
63
64 void PSpell::addManager(string const & lang)
65 {
66         PspellConfig * config = new_pspell_config();
67         pspell_config_replace(config, "language-tag", lang.c_str());
68         pspell_config_replace(config, "encoding", "utf-8");
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         int word_ok = pspell_manager_check(m, to_utf8(word.word()).c_str());
101         LASSERT(word_ok != -1, /**/);
102
103         if (word_ok) {
104                 res = OK;
105         } else {
106                 PspellWordList const * sugs =
107                         pspell_manager_suggest(m, to_utf8(word.word()).c_str());
108                 LASSERT(sugs != 0, /**/);
109                 els = pspell_word_list_elements(sugs);
110                 if (pspell_word_list_empty(sugs))
111                         res = UNKNOWN_WORD;
112                 else
113                         res = SUGGESTED_WORDS;
114         }
115         return res;
116 }
117
118
119 void PSpell::insert(WordLangTuple const & word)
120 {
121         Managers::iterator it = managers_.find(word.lang_code());
122         if (it != managers_.end())
123                 pspell_manager_add_to_personal(it->second.manager, to_utf8(word.word()).c_str());
124 }
125
126
127 void PSpell::accept(WordLangTuple const & word)
128 {
129         Managers::iterator it = managers_.find(word.lang_code());
130         if (it != managers_.end())
131                 pspell_manager_add_to_session(it->second.manager, to_utf8(word.word()).c_str());
132 }
133
134
135 docstring const PSpell::nextMiss()
136 {
137         char const * str = 0;
138
139         if (els)
140                 str = pspell_string_emulation_next(els);
141         if (str)
142                 return from_utf8(str);
143         return docstring();
144 }
145
146
147 docstring const PSpell::error()
148 {
149         char const * err = 0;
150
151         if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
152                 err = pspell_error_message(spell_error_object);
153         }
154
155         if (err)
156                 return from_utf8(err);
157         return docstring();
158 }
159
160
161 } // namespace lyx