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