]> git.lyx.org Git - lyx.git/blob - src/pspell.C
Point fix, earlier forgotten
[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 #ifdef USE_PSPELL
15
16 #include "support/LAssert.h"
17 #include "debug.h"
18
19 #define USE_ORIGINAL_MANAGER_FUNCS 1
20 // new aspell pspell missing extern "C"
21 extern "C" {
22 #include <pspell/pspell.h>
23 }
24
25 #include "pspell.h"
26 #include "WordLangTuple.h"
27
28 using namespace lyx::support;
29
30 using std::endl;
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" << endl;
37 }
38
39
40 PSpell::~PSpell()
41 {
42         lyxerr[Debug::GUI] << "killed pspell" << endl;
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         PspellCanHaveError * err = new_pspell_manager(config);
68         if (spell_error_object)
69                 delete_pspell_can_have_error(spell_error_object);
70         spell_error_object = 0;
71
72         if (pspell_error_number(err) == 0) {
73                 Manager m;
74                 m.manager = to_pspell_manager(err);
75                 m.config = config;
76                 managers_[lang] = m;
77         } else {
78                 spell_error_object = err;
79         }
80 }
81
82
83 enum PSpell::Result PSpell::check(WordLangTuple const & word)
84 {
85         Result res = UNKNOWN;
86
87         Managers::iterator it = managers_.find(word.lang_code());
88         if (it == managers_.end()) {
89                 addManager(word.lang_code());
90                 it = managers_.find(word.lang_code());
91                 // FIXME
92                 if (it == managers_.end())
93                         return res;
94         }
95
96         PspellManager * m = it->second.manager;
97
98         int word_ok = pspell_manager_check(m, word.word().c_str());
99         Assert(word_ok != -1);
100
101         if (word_ok) {
102                 res = OK;
103         } else {
104                 PspellWordList const * sugs =
105                         pspell_manager_suggest(m, word.word().c_str());
106                 Assert(sugs != 0);
107                 els = pspell_word_list_elements(sugs);
108                 if (pspell_word_list_empty(sugs))
109                         res = UNKNOWN;
110                 else
111                         res = MISSED;
112         }
113         return res;
114 }
115
116
117 void PSpell::insert(WordLangTuple const & word)
118 {
119         Managers::iterator it = managers_.find(word.lang_code());
120         if (it != managers_.end())
121                 pspell_manager_add_to_personal(it->second.manager, word.word().c_str());
122 }
123
124
125 void PSpell::accept(WordLangTuple const & word)
126 {
127         Managers::iterator it = managers_.find(word.lang_code());
128         if (it != managers_.end())
129                 pspell_manager_add_to_session(it->second.manager, word.word().c_str());
130 }
131
132
133 string const PSpell::nextMiss()
134 {
135         char const * str = 0;
136
137         if (els)
138                 str = pspell_string_emulation_next(els);
139         if (str)
140                 return str;
141         return "";
142 }
143
144
145 string const PSpell::error()
146 {
147         char const * err = 0;
148
149         if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
150                 err = pspell_error_message(spell_error_object);
151         }
152
153         if (err)
154                 return err;
155         return "";
156 }
157
158 #endif // USE_PSPELL