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