]> git.lyx.org Git - lyx.git/blob - src/ASpell.cpp
start using FileName::exists()
[lyx.git] / src / ASpell.cpp
1 /**
2  * \file ASpell.cpp
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         // Set the encoding to utf-8.
61         // aspell does also understand "ucs-4", so we would not need a
62         // conversion in theory, but if this is used it expects all
63         // char const * arguments to be a cast from  uint const *, and it
64         // seems that this uint is not compatible with our char_type on some
65         // platforms (cygwin, OS X). Therefore we use utf-8, that does
66         // always work.
67         aspell_config_replace(config, "encoding", "utf-8");
68         AspellCanHaveError * err = new_aspell_speller(config);
69         if (spell_error_object)
70                 delete_aspell_can_have_error(spell_error_object);
71         spell_error_object = 0;
72
73         if (aspell_error_number(err) == 0) {
74                 Speller m;
75                 m.speller = to_aspell_speller(err);
76                 m.config = config;
77                 spellers_[lang] = m;
78         } else {
79                 spell_error_object = err;
80         }
81 }
82
83
84 ASpell::Result ASpell::check(WordLangTuple const & word)
85 {
86         Result res = UNKNOWN_WORD;
87
88         Spellers::iterator it = spellers_.find(word.lang_code());
89         if (it == spellers_.end()) {
90                 addSpeller(word.lang_code());
91                 it = spellers_.find(word.lang_code());
92                 // FIXME
93                 if (it == spellers_.end())
94                         return res;
95         }
96
97         AspellSpeller * m = it->second.speller;
98
99         int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
100         BOOST_ASSERT(word_ok != -1);
101
102         if (word_ok) {
103                 res = OK;
104         } else {
105                 AspellWordList const * sugs =
106                         aspell_speller_suggest(m, to_utf8(word.word()).c_str(), -1);
107                 BOOST_ASSERT(sugs != 0);
108                 els = aspell_word_list_elements(sugs);
109                 if (aspell_word_list_empty(sugs))
110                         res = UNKNOWN_WORD;
111                 else
112                         res = SUGGESTED_WORDS;
113         }
114         return res;
115 }
116
117
118 void ASpell::insert(WordLangTuple const & word)
119 {
120         Spellers::iterator it = spellers_.find(word.lang_code());
121         if (it != spellers_.end())
122                 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
123 }
124
125
126 void ASpell::accept(WordLangTuple const & word)
127 {
128         Spellers::iterator it = spellers_.find(word.lang_code());
129         if (it != spellers_.end())
130                 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
131 }
132
133
134 docstring const ASpell::nextMiss()
135 {
136         char const * str = 0;
137
138         if (els)
139                 str = aspell_string_enumeration_next(els);
140
141         return (str ? from_utf8(str) : docstring());
142 }
143
144
145 docstring const ASpell::error()
146 {
147         char const * err = 0;
148
149         if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
150                 err = aspell_error_message(spell_error_object);
151         }
152
153         // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
154         return (err ? from_utf8(err) : docstring());
155 }
156
157
158 } // namespace lyx