]> git.lyx.org Git - lyx.git/blob - src/aspell.C
The speed patch: redraw only rows that have changed
[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
26 ASpell::ASpell(BufferParams const &, string const & lang)
27         : els(0), spell_error_object(0)
28 {
29         addSpeller(lang);
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         aspell_config_replace(config, "language-tag", lang.c_str());
58         AspellCanHaveError * err = new_aspell_speller(config);
59         if (spell_error_object)
60                 delete_aspell_can_have_error(spell_error_object);
61         spell_error_object = 0;
62
63         if (aspell_error_number(err) == 0) {
64                 Speller m;
65                 m.speller = to_aspell_speller(err);
66                 m.config = config;
67                 spellers_[lang] = m;
68         } else {
69                 spell_error_object = err;
70         }
71 }
72
73
74 ASpell::Result ASpell::check(WordLangTuple const & word)
75 {
76         Result res = UNKNOWN_WORD;
77
78         Spellers::iterator it = spellers_.find(word.lang_code());
79         if (it == spellers_.end()) {
80                 addSpeller(word.lang_code());
81                 it = spellers_.find(word.lang_code());
82                 // FIXME
83                 if (it == spellers_.end())
84                         return res;
85         }
86
87         AspellSpeller * m = it->second.speller;
88
89         int const word_ok = aspell_speller_check(m, word.word().c_str(), -1);
90         BOOST_ASSERT(word_ok != -1);
91
92         if (word_ok) {
93                 res = OK;
94         } else {
95                 AspellWordList const * sugs =
96                         aspell_speller_suggest(m, word.word().c_str(), -1);
97                 BOOST_ASSERT(sugs != 0);
98                 els = aspell_word_list_elements(sugs);
99                 if (aspell_word_list_empty(sugs))
100                         res = UNKNOWN_WORD;
101                 else
102                         res = SUGGESTED_WORDS;
103         }
104         return res;
105 }
106
107
108 void ASpell::insert(WordLangTuple const & word)
109 {
110         Spellers::iterator it = spellers_.find(word.lang_code());
111         if (it != spellers_.end())
112                 aspell_speller_add_to_personal(it->second.speller, word.word().c_str(), -1);
113 }
114
115
116 void ASpell::accept(WordLangTuple const & word)
117 {
118         Spellers::iterator it = spellers_.find(word.lang_code());
119         if (it != spellers_.end())
120                 aspell_speller_add_to_session(it->second.speller, word.word().c_str(), -1);
121 }
122
123
124 string const ASpell::nextMiss()
125 {
126         char const * str = 0;
127
128         if (els)
129                 str = aspell_string_enumeration_next(els);
130
131         return (str ? str : "");
132 }
133
134
135 string const ASpell::error()
136 {
137         char const * err = 0;
138
139         if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
140                 err = aspell_error_message(spell_error_object);
141         }
142
143         return (err ? err : "");
144 }