]> git.lyx.org Git - lyx.git/blob - src/aspell.C
Alfredo's second patch
[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 std::endl;
23
24 ASpell::ASpell(BufferParams const &, string const & lang)
25         : els(0), spell_error_object(0)
26 {
27         addSpeller(lang);
28 }
29
30
31 ASpell::~ASpell()
32 {
33         if (spell_error_object) {
34                 delete_aspell_can_have_error(spell_error_object);
35                 spell_error_object = 0;
36         }
37
38         if (els)
39                 delete_aspell_string_enumeration(els);
40
41         Spellers::iterator it = spellers_.begin();
42         Spellers::iterator end = spellers_.end();
43
44         for (; it != end; ++it) {
45                 aspell_speller_save_all_word_lists(it->second.speller);
46                 delete_aspell_speller(it->second.speller);
47                 delete_aspell_config(it->second.config);
48         }
49 }
50
51
52 void ASpell::addSpeller(string const & lang)
53 {
54         AspellConfig * config = new_aspell_config();
55         aspell_config_replace(config, "language-tag", lang.c_str());
56         AspellCanHaveError * err = new_aspell_speller(config);
57         if (spell_error_object)
58                 delete_aspell_can_have_error(spell_error_object);
59         spell_error_object = 0;
60
61         if (aspell_error_number(err) == 0) {
62                 Speller m;
63                 m.speller = to_aspell_speller(err);
64                 m.config = config;
65                 spellers_[lang] = m;
66         } else {
67                 spell_error_object = err;
68         }
69 }
70
71
72 enum ASpell::Result ASpell::check(WordLangTuple const & word)
73 {
74         Result res = UNKNOWN;
75
76         Spellers::iterator it = spellers_.find(word.lang_code());
77         if (it == spellers_.end()) {
78                 addSpeller(word.lang_code());
79                 it = spellers_.find(word.lang_code());
80                 // FIXME
81                 if (it == spellers_.end())
82                         return res;
83         }
84
85         AspellSpeller * m = it->second.speller;
86
87         int word_ok = aspell_speller_check(m, word.word().c_str(), -1);
88         lyx::Assert(word_ok != -1);
89
90         if (word_ok) {
91                 res = OK;
92         } else {
93                 AspellWordList const * sugs =
94                         aspell_speller_suggest(m, word.word().c_str(), -1);
95                 lyx::Assert(sugs != 0);
96                 els = aspell_word_list_elements(sugs);
97                 if (aspell_word_list_empty(sugs))
98                         res = UNKNOWN;
99                 else
100                         res = MISSED;
101         }
102         return res;
103 }
104
105
106 void ASpell::insert(WordLangTuple const & word)
107 {
108         Spellers::iterator it = spellers_.find(word.lang_code());
109         if (it != spellers_.end())
110                 aspell_speller_add_to_personal(it->second.speller, word.word().c_str(), -1);
111 }
112
113
114 void ASpell::accept(WordLangTuple const & word)
115 {
116         Spellers::iterator it = spellers_.find(word.lang_code());
117         if (it != spellers_.end())
118                 aspell_speller_add_to_session(it->second.speller, word.word().c_str(), -1);
119 }
120
121
122 string const ASpell::nextMiss()
123 {
124         char const * str = 0;
125
126         if (els)
127                 str = aspell_string_enumeration_next(els);
128         if (str)
129                 return str;
130         return "";
131 }
132
133
134 string const ASpell::error()
135 {
136         char const * err = 0;
137
138         if (spell_error_object && aspell_error_number(spell_error_object) != 0) {
139                 err = aspell_error_message(spell_error_object);
140         }
141
142         if (err)
143                 return err;
144         return "";
145 }
146
147 #endif // USE_ASPELL