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