]> git.lyx.org Git - lyx.git/blob - src/aspell.C
More 'standard conformant blurb' nonsense.
[lyx.git] / src / aspell.C
1 /**
2  * \file aspell.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Kevin Atkinson
7  * \author John Levon <levon@movementarian.org>
8  */
9
10 #include <config.h>
11
12 #ifdef USE_ASPELL
13
14 #include "support/LAssert.h"
15 #include "debug.h"
16
17 #include <aspell.h>
18
19 #include "aspell_local.h"
20 #include "WordLangTuple.h"
21
22 using namespace lyx::support;
23
24 using std::endl;
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 enum ASpell::Result ASpell::check(WordLangTuple const & word)
75 {
76         Result res = UNKNOWN;
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 word_ok = aspell_speller_check(m, word.word().c_str(), -1);
90         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                 Assert(sugs != 0);
98                 els = aspell_word_list_elements(sugs);
99                 if (aspell_word_list_empty(sugs))
100                         res = UNKNOWN;
101                 else
102                         res = MISSED;
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         if (str)
131                 return str;
132         return "";
133 }
134
135
136 string 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         if (err)
145                 return err;
146         return "";
147 }
148
149 #endif // USE_ASPELL