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