]> git.lyx.org Git - lyx.git/blob - src/aspell.C
fix aspell encoding (confirmed on linux, cygwin and native windows,
[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         // FIXME The aspell documentation says to use "lang"
59         aspell_config_replace(config, "language-tag", lang.c_str());
60         aspell_config_replace(config, "encoding", "utf-8");
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 ASpell::Result ASpell::check(WordLangTuple const & word)
78 {
79         Result res = UNKNOWN_WORD;
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 const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
93         BOOST_ASSERT(word_ok != -1);
94
95         if (word_ok) {
96                 res = OK;
97         } else {
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                 aspell_speller_add_to_personal(it->second.speller, to_utf8(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, to_utf8(word.word()).c_str(), -1);
124 }
125
126
127 docstring const ASpell::nextMiss()
128 {
129         char const * str = 0;
130
131         if (els)
132                 str = aspell_string_enumeration_next(els);
133
134         return (str ? from_utf8(str) : docstring());
135 }
136
137
138 docstring const ASpell::error()
139 {
140         char const * err = 0;
141         
142         if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
143                 err = aspell_error_message(spell_error_object);
144         }
145
146         // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
147         return (err ? from_utf8(err) : docstring());
148 }
149
150
151 } // namespace lyx