]> git.lyx.org Git - lyx.git/blob - src/aspell.C
Fix dEPM crash:
[lyx.git] / src / aspell.C
1 /**
2  * \file aspell.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 #include <aspell.h>
17
18 #include "aspell_local.h"
19 #include "WordLangTuple.h"
20
21 #include <boost/assert.hpp>
22
23 using std::string;
24
25 namespace lyx {
26
27 ASpell::ASpell(BufferParams const &, string const & lang)
28         : els(0), spell_error_object(0)
29 {
30         addSpeller(lang);
31 }
32
33
34 ASpell::~ASpell()
35 {
36         if (spell_error_object) {
37                 delete_aspell_can_have_error(spell_error_object);
38                 spell_error_object = 0;
39         }
40
41         if (els)
42                 delete_aspell_string_enumeration(els);
43
44         Spellers::iterator it = spellers_.begin();
45         Spellers::iterator end = spellers_.end();
46
47         for (; it != end; ++it) {
48                 aspell_speller_save_all_word_lists(it->second.speller);
49                 delete_aspell_speller(it->second.speller);
50                 delete_aspell_config(it->second.config);
51         }
52 }
53
54
55 void ASpell::addSpeller(string const & lang)
56 {
57         AspellConfig * config = new_aspell_config();
58         aspell_config_replace(config, "language-tag", lang.c_str());
59         AspellCanHaveError * err = new_aspell_speller(config);
60         if (spell_error_object)
61                 delete_aspell_can_have_error(spell_error_object);
62         spell_error_object = 0;
63
64         if (aspell_error_number(err) == 0) {
65                 Speller m;
66                 m.speller = to_aspell_speller(err);
67                 m.config = config;
68                 spellers_[lang] = m;
69         } else {
70                 spell_error_object = err;
71         }
72 }
73
74
75 ASpell::Result ASpell::check(WordLangTuple const & word)
76 {
77         Result res = UNKNOWN_WORD;
78
79         Spellers::iterator it = spellers_.find(word.lang_code());
80         if (it == spellers_.end()) {
81                 addSpeller(word.lang_code());
82                 it = spellers_.find(word.lang_code());
83                 // FIXME
84                 if (it == spellers_.end())
85                         return res;
86         }
87
88         AspellSpeller * m = it->second.speller;
89
90         // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
91         int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
92         BOOST_ASSERT(word_ok != -1);
93
94         if (word_ok) {
95                 res = OK;
96         } else {
97                 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
98                 AspellWordList const * sugs =
99                         aspell_speller_suggest(m, to_utf8(word.word()).c_str(), -1);
100                 BOOST_ASSERT(sugs != 0);
101                 els = aspell_word_list_elements(sugs);
102                 if (aspell_word_list_empty(sugs))
103                         res = UNKNOWN_WORD;
104                 else
105                         res = SUGGESTED_WORDS;
106         }
107         return res;
108 }
109
110
111 void ASpell::insert(WordLangTuple const & word)
112 {
113         Spellers::iterator it = spellers_.find(word.lang_code());
114         if (it != spellers_.end())
115                 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
116                 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
117 }
118
119
120 void ASpell::accept(WordLangTuple const & word)
121 {
122         Spellers::iterator it = spellers_.find(word.lang_code());
123         if (it != spellers_.end())
124                 // FIXME UNICODE: we don't need to convert to UTF8, but probably to the locale encoding
125                 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
126 }
127
128
129 docstring const ASpell::nextMiss()
130 {
131         char const * str = 0;
132
133         if (els)
134                 str = aspell_string_enumeration_next(els);
135
136         // FIXME UNICODE: str is not in UTF8, but probably the locale encoding
137         return (str ? from_utf8(str) : docstring());
138 }
139
140
141 docstring const ASpell::error()
142 {
143         char const * err = 0;
144         
145         if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
146                 err = aspell_error_message(spell_error_object);
147         }
148
149         // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
150         return (err ? from_utf8(err) : docstring());
151 }
152
153
154 } // namespace lyx