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