]> git.lyx.org Git - lyx.git/blob - src/pspell.C
fix keys to be unique
[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 using std::endl;
30
31 PSpell::PSpell(BufferParams const &, string const & lang)
32         : els(0), spell_error_object(0)
33 {
34         addManager(lang); 
35 }
36
37
38 PSpell::~PSpell()
39 {
40         cleanUp();
41         close();
42         if (els)
43                 delete_pspell_string_emulation(els);
44         Managers::iterator it = managers_.begin();
45         Managers::iterator end = managers_.end();
46  
47         for (; it != end; ++it) { 
48                 delete_pspell_manager(it->second.manager);
49                 delete_pspell_config(it->second.config);
50         }
51 }
52
53
54 void PSpell::cleanUp()
55 {
56         if (spell_error_object) {
57                 delete_pspell_can_have_error(spell_error_object);
58                 spell_error_object = 0;
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         lyx::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                 lyx::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::close()
118 {
119         Managers::iterator it = managers_.begin();
120         Managers::iterator end = managers_.end();
121  
122         for (; it != end; ++it) { 
123                 pspell_manager_save_all_word_lists(it->second.manager);
124         }
125 }
126
127
128 void PSpell::insert(WordLangTuple const & word)
129 {
130         Managers::iterator it = managers_.find(word.lang_code());
131         if (it != managers_.end())
132                 pspell_manager_add_to_personal(it->second.manager, word.word().c_str());
133 }
134
135
136 void PSpell::accept(WordLangTuple const & word)
137 {
138         Managers::iterator it = managers_.find(word.lang_code());
139         if (it != managers_.end()) 
140                 pspell_manager_add_to_session(it->second.manager, word.word().c_str());
141 }
142
143
144 string const PSpell::nextMiss()
145 {
146         char const * str = 0;
147  
148         if (els)
149                 str = pspell_string_emulation_next(els);
150         if (str)
151                 return str;
152         return "";
153 }
154
155
156 string const PSpell::error()
157 {
158         char const * err = 0;
159  
160         if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
161                 err = pspell_error_message(spell_error_object);
162         }
163
164         if (err)
165                 return err;
166         return "";
167 }
168
169 #endif // USE_PSPELL