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