]> git.lyx.org Git - lyx.git/blob - src/pspell.C
Fix bug 2195: Slowness in rendering inside insets, especially on the Mac
[lyx.git] / src / pspell.C
1 /**
2  * \file pspell.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Kevin Atkinson
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "debug.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 #include <boost/assert.hpp>
26
27 using std::endl;
28 using std::string;
29
30
31 PSpell::PSpell(BufferParams const &, string const & lang)
32         : els(0), spell_error_object(0)
33 {
34         addManager(lang);
35         lyxerr[Debug::GUI] << "created pspell" << endl;
36 }
37
38
39 PSpell::~PSpell()
40 {
41         lyxerr[Debug::GUI] << "killed pspell" << endl;
42
43         if (spell_error_object) {
44                 delete_pspell_can_have_error(spell_error_object);
45                 spell_error_object = 0;
46         }
47
48         if (els)
49                 delete_pspell_string_emulation(els);
50
51         Managers::iterator it = managers_.begin();
52         Managers::iterator end = managers_.end();
53
54         for (; it != end; ++it) {
55                 pspell_manager_save_all_word_lists(it->second.manager);
56                 delete_pspell_manager(it->second.manager);
57                 delete_pspell_config(it->second.config);
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_WORD;
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         BOOST_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                 BOOST_ASSERT(sugs != 0);
106                 els = pspell_word_list_elements(sugs);
107                 if (pspell_word_list_empty(sugs))
108                         res = UNKNOWN_WORD;
109                 else
110                         res = SUGGESTED_WORDS;
111         }
112         return res;
113 }
114
115
116 void PSpell::insert(WordLangTuple const & word)
117 {
118         Managers::iterator it = managers_.find(word.lang_code());
119         if (it != managers_.end())
120                 pspell_manager_add_to_personal(it->second.manager, word.word().c_str());
121 }
122
123
124 void PSpell::accept(WordLangTuple const & word)
125 {
126         Managers::iterator it = managers_.find(word.lang_code());
127         if (it != managers_.end())
128                 pspell_manager_add_to_session(it->second.manager, word.word().c_str());
129 }
130
131
132 string const PSpell::nextMiss()
133 {
134         char const * str = 0;
135
136         if (els)
137                 str = pspell_string_emulation_next(els);
138         if (str)
139                 return str;
140         return "";
141 }
142
143
144 string const PSpell::error()
145 {
146         char const * err = 0;
147
148         if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
149                 err = pspell_error_message(spell_error_object);
150         }
151
152         if (err)
153                 return err;
154         return "";
155 }