]> git.lyx.org Git - lyx.git/blob - src/aspell.C
we rely on Windows and maybe Linux on a Qt bug
[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         int const word_ok = aspell_speller_check(m, word.word().c_str(), -1);
91         BOOST_ASSERT(word_ok != -1);
92
93         if (word_ok) {
94                 res = OK;
95         } else {
96                 AspellWordList const * sugs =
97                         aspell_speller_suggest(m, word.word().c_str(), -1);
98                 BOOST_ASSERT(sugs != 0);
99                 els = aspell_word_list_elements(sugs);
100                 if (aspell_word_list_empty(sugs))
101                         res = UNKNOWN_WORD;
102                 else
103                         res = SUGGESTED_WORDS;
104         }
105         return res;
106 }
107
108
109 void ASpell::insert(WordLangTuple const & word)
110 {
111         Spellers::iterator it = spellers_.find(word.lang_code());
112         if (it != spellers_.end())
113                 aspell_speller_add_to_personal(it->second.speller, word.word().c_str(), -1);
114 }
115
116
117 void ASpell::accept(WordLangTuple const & word)
118 {
119         Spellers::iterator it = spellers_.find(word.lang_code());
120         if (it != spellers_.end())
121                 aspell_speller_add_to_session(it->second.speller, word.word().c_str(), -1);
122 }
123
124
125 string const ASpell::nextMiss()
126 {
127         char const * str = 0;
128
129         if (els)
130                 str = aspell_string_enumeration_next(els);
131
132         return (str ? str : "");
133 }
134
135
136 docstring const ASpell::error()
137 {
138         char const * err = 0;
139         
140         if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
141                 err = aspell_error_message(spell_error_object);
142         }
143
144         return (err ? from_utf8(err) : docstring());
145 }
146
147
148 } // namespace lyx