]> git.lyx.org Git - lyx.git/blob - src/pspell.C
9c850d62c7393489ffa3828f9350817c78401fa9
[lyx.git] / src / pspell.C
1 /**
2  * \file pspell.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 __GNUG__
13 #pragma implementation
14 #endif
15
16 #ifdef USE_PSPELL
17
18 #include "support/LAssert.h"
19
20 #define USE_ORIGINAL_MANAGER_FUNCS 1
21 // new aspell pspell missing extern "C" 
22 extern "C" { 
23 #include <pspell/pspell.h>
24 }
25
26 #include "pspell.h"
27
28 using std::endl;
29
30 PSpell::PSpell(BufferParams const &, string const & lang)
31         : sc(0), els(0), spell_error_object(0), alive_(false)
32 {
33         PspellConfig * config = new_pspell_config();
34         pspell_config_replace(config, "lang", lang.c_str());
35         spell_error_object = new_pspell_manager(config);
36         if (pspell_error_number(spell_error_object) == 0) {
37                 sc = to_pspell_manager(spell_error_object);
38                 spell_error_object = 0;
39                 alive_ = true;
40         } else {
41                 lyxerr << pspell_error_message(spell_error_object) << endl;
42         }
43 }
44
45
46 PSpell::~PSpell()
47 {
48         cleanUp();
49         close();
50         if (els)
51                 delete_pspell_string_emulation(els);
52 }
53
54
55 void PSpell::cleanUp()
56 {
57         if (spell_error_object) {
58                 delete_pspell_can_have_error(spell_error_object);
59                 spell_error_object = 0;
60         }
61 }
62
63
64 enum PSpell::Result PSpell::check(string const & word)
65 {
66         Result res = UNKNOWN;
67  
68         if (!sc)
69                 return res;
70
71         int word_ok = pspell_manager_check(sc, word.c_str());
72         lyx::Assert(word_ok != -1);
73
74         if (word_ok) {
75                 res = OK;
76         } else {
77                 PspellWordList const * sugs =
78                         pspell_manager_suggest(sc, word.c_str());
79                 lyx::Assert(sugs != 0);
80                 els = pspell_word_list_elements(sugs);
81                 if (pspell_word_list_empty(sugs))
82                         res = UNKNOWN;
83                 else
84                         res = MISSED;
85         }
86         return res;
87 }
88
89
90 void PSpell::close()
91 {
92         if (sc)
93                 pspell_manager_save_all_word_lists(sc);
94 }
95
96
97 void PSpell::insert(string const & word)
98 {
99         if (sc)
100                 pspell_manager_add_to_personal(sc, word.c_str());
101 }
102
103
104 void PSpell::accept(string const & word)
105 {
106         if (sc)
107                 pspell_manager_add_to_session(sc, word.c_str());
108 }
109
110
111 string const PSpell::nextMiss()
112 {
113         char const * str = 0;
114  
115         if (els)
116                 str = pspell_string_emulation_next(els);
117         if (str)
118                 return str;
119         return "";
120 }
121
122
123 string const PSpell::error()
124 {
125         char const * err = 0;
126  
127         if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
128                 err = pspell_error_message(spell_error_object);
129         }
130
131         if (err)
132                 return err;
133         return "";
134 }
135
136 #endif // USE_PSPELL