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