]> git.lyx.org Git - lyx.git/blob - src/ASpell.cpp
A little cleanup of the layout files.
[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 "support/debug.h"
15
16 #include <aspell.h>
17
18 #include "ASpell_local.h"
19 #include "LyXRC.h"
20 #include "WordLangTuple.h"
21
22 #include "support/lassert.h"
23
24 using namespace std;
25
26 namespace lyx {
27
28 ASpell::ASpell(): els(0), spell_error_object(0)
29 {
30 }
31
32
33 ASpell::~ASpell()
34 {
35         if (spell_error_object) {
36                 delete_aspell_can_have_error(spell_error_object);
37                 spell_error_object = 0;
38         }
39
40         if (els)
41                 delete_aspell_string_enumeration(els);
42
43         Spellers::iterator it = spellers_.begin();
44         Spellers::iterator end = spellers_.end();
45
46         for (; it != end; ++it) {
47                 aspell_speller_save_all_word_lists(it->second.speller);
48                 delete_aspell_speller(it->second.speller);
49                 delete_aspell_config(it->second.config);
50         }
51 }
52
53
54 void ASpell::addSpeller(string const & lang)
55 {
56         AspellConfig * config = new_aspell_config();
57         // FIXME The aspell documentation says to use "lang"
58         aspell_config_replace(config, "language-tag", lang.c_str());
59         // Set the encoding to utf-8.
60         // aspell does also understand "ucs-4", so we would not need a
61         // conversion in theory, but if this is used it expects all
62         // char const * arguments to be a cast from  uint const *, and it
63         // seems that this uint is not compatible with our char_type on some
64         // platforms (cygwin, OS X). Therefore we use utf-8, that does
65         // always work.
66         aspell_config_replace(config, "encoding", "utf-8");
67         if (lyxrc.spellchecker_accept_compound)
68                 // Consider run-together words as legal compounds
69                 aspell_config_replace(config, "run-together", "true");
70         else
71                 // Report run-together words as errors
72                 aspell_config_replace(config, "run-together", "false");
73         AspellCanHaveError * err = new_aspell_speller(config);
74         if (spell_error_object)
75                 delete_aspell_can_have_error(spell_error_object);
76         spell_error_object = 0;
77
78         if (aspell_error_number(err) == 0) {
79                 Speller m;
80                 m.speller = to_aspell_speller(err);
81                 m.config = config;
82                 spellers_[lang] = m;
83         } else {
84                 spell_error_object = err;
85         }
86 }
87
88
89 ASpell::Result ASpell::check(WordLangTuple const & word)
90 {
91         Result res = UNKNOWN_WORD;
92
93         Spellers::iterator it = spellers_.find(word.lang_code());
94         if (it == spellers_.end()) {
95                 addSpeller(word.lang_code());
96                 it = spellers_.find(word.lang_code());
97                 // FIXME
98                 if (it == spellers_.end())
99                         return res;
100         }
101
102         AspellSpeller * m = it->second.speller;
103
104         int const word_ok = aspell_speller_check(m, to_utf8(word.word()).c_str(), -1);
105         LASSERT(word_ok != -1, /**/);
106
107         if (word_ok)
108                 return OK;
109
110         AspellWordList const * sugs =
111                 aspell_speller_suggest(m, to_utf8(word.word()).c_str(), -1);
112         LASSERT(sugs != 0, /**/);
113         els = aspell_word_list_elements(sugs);
114         if (aspell_word_list_empty(sugs))
115                 res = UNKNOWN_WORD;
116         else
117                 res = SUGGESTED_WORDS;
118
119         return res;
120 }
121
122
123 void ASpell::insert(WordLangTuple const & word)
124 {
125         Spellers::iterator it = spellers_.find(word.lang_code());
126         if (it != spellers_.end())
127                 aspell_speller_add_to_personal(it->second.speller, to_utf8(word.word()).c_str(), -1);
128 }
129
130
131 void ASpell::accept(WordLangTuple const & word)
132 {
133         Spellers::iterator it = spellers_.find(word.lang_code());
134         if (it != spellers_.end())
135                 aspell_speller_add_to_session(it->second.speller, to_utf8(word.word()).c_str(), -1);
136 }
137
138
139 docstring const ASpell::nextMiss()
140 {
141         char const * str = 0;
142
143         if (els)
144                 str = aspell_string_enumeration_next(els);
145
146         return (str ? from_utf8(str) : docstring());
147 }
148
149
150 docstring const ASpell::error()
151 {
152         char const * err = 0;
153
154         if (spell_error_object && aspell_error_number(spell_error_object) != 0)
155                 err = aspell_error_message(spell_error_object);
156
157         // FIXME UNICODE: err is not in UTF8, but probably the locale encoding
158         return (err ? from_utf8(err) : docstring());
159 }
160
161
162 } // namespace lyx