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