]> git.lyx.org Git - lyx.git/blob - src/pspell.C
last Friday's text*.C -> text_func shuffle
[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 #include "debug.h"
16
17 #define USE_ORIGINAL_MANAGER_FUNCS 1
18 // new aspell pspell missing extern "C"
19 extern "C" {
20 #include <pspell/pspell.h>
21 }
22
23 #include "pspell.h"
24 #include "WordLangTuple.h"
25
26 using namespace lyx::support;
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         lyxerr[Debug::GUI] << "created pspell" << endl;
35 }
36
37
38 PSpell::~PSpell()
39 {
40         lyxerr[Debug::GUI] << "killed pspell" << endl;
41
42         if (spell_error_object) {
43                 delete_pspell_can_have_error(spell_error_object);
44                 spell_error_object = 0;
45         }
46
47         if (els)
48                 delete_pspell_string_emulation(els);
49
50         Managers::iterator it = managers_.begin();
51         Managers::iterator end = managers_.end();
52
53         for (; it != end; ++it) {
54                 pspell_manager_save_all_word_lists(it->second.manager);
55                 delete_pspell_manager(it->second.manager);
56                 delete_pspell_config(it->second.config);
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         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                 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::insert(WordLangTuple const & word)
116 {
117         Managers::iterator it = managers_.find(word.lang_code());
118         if (it != managers_.end())
119                 pspell_manager_add_to_personal(it->second.manager, word.word().c_str());
120 }
121
122
123 void PSpell::accept(WordLangTuple const & word)
124 {
125         Managers::iterator it = managers_.find(word.lang_code());
126         if (it != managers_.end())
127                 pspell_manager_add_to_session(it->second.manager, word.word().c_str());
128 }
129
130
131 string const PSpell::nextMiss()
132 {
133         char const * str = 0;
134
135         if (els)
136                 str = pspell_string_emulation_next(els);
137         if (str)
138                 return str;
139         return "";
140 }
141
142
143 string const PSpell::error()
144 {
145         char const * err = 0;
146
147         if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
148                 err = pspell_error_message(spell_error_object);
149         }
150
151         if (err)
152                 return err;
153         return "";
154 }
155
156 #endif // USE_PSPELL