]> git.lyx.org Git - lyx.git/blob - src/pspell.C
cba210de6c04efe1e48a1a699486bc38ba19165f
[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, "language-tag", 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         }
41 }
42
43
44 PSpell::~PSpell()
45 {
46         cleanUp();
47         close();
48         if (els)
49                 delete_pspell_string_emulation(els);
50 }
51
52
53 void PSpell::cleanUp()
54 {
55         if (spell_error_object) {
56                 delete_pspell_can_have_error(spell_error_object);
57                 spell_error_object = 0;
58         }
59 }
60
61
62 enum PSpell::Result PSpell::check(string const & word)
63 {
64         Result res = UNKNOWN;
65  
66         if (!sc)
67                 return res;
68
69         int word_ok = pspell_manager_check(sc, word.c_str());
70         lyx::Assert(word_ok != -1);
71
72         if (word_ok) {
73                 res = OK;
74         } else {
75                 PspellWordList const * sugs =
76                         pspell_manager_suggest(sc, word.c_str());
77                 lyx::Assert(sugs != 0);
78                 els = pspell_word_list_elements(sugs);
79                 if (pspell_word_list_empty(sugs))
80                         res = UNKNOWN;
81                 else
82                         res = MISSED;
83         }
84         return res;
85 }
86
87
88 void PSpell::close()
89 {
90         if (sc)
91                 pspell_manager_save_all_word_lists(sc);
92 }
93
94
95 void PSpell::insert(string const & word)
96 {
97         if (sc)
98                 pspell_manager_add_to_personal(sc, word.c_str());
99 }
100
101
102 void PSpell::accept(string const & word)
103 {
104         if (sc)
105                 pspell_manager_add_to_session(sc, word.c_str());
106 }
107
108
109 string const PSpell::nextMiss()
110 {
111         char const * str = 0;
112  
113         if (els)
114                 str = pspell_string_emulation_next(els);
115         if (str)
116                 return str;
117         return "";
118 }
119
120
121 string const PSpell::error()
122 {
123         char const * err = 0;
124  
125         if (spell_error_object && pspell_error_number(spell_error_object) != 0) {
126                 err = pspell_error_message(spell_error_object);
127         }
128
129         if (err)
130                 return err;
131         return "";
132 }
133
134 #endif // USE_PSPELL