]> git.lyx.org Git - lyx.git/blob - src/Thesaurus.cpp
More useful message (tells the user that ESC cancels the long operation in progress).
[lyx.git] / src / Thesaurus.cpp
1 /**
2  * \file Thesaurus.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Thesaurus.h"
15
16 #include "LyXRC.h"
17
18 #include "SpellChecker.h"
19 #include "WordLangTuple.h"
20
21 #include "support/FileNameList.h"
22 #include "support/Package.h"
23 #include "support/debug.h"
24 #include "support/docstring_list.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28 #include "support/os.h"
29
30 #ifdef USE_EXTERNAL_MYTHES
31 #include MYTHES_H_LOCATION
32 #else
33 #include <cstdio>
34 #include "support/mythes/mythes.hxx"
35 #endif
36
37 #include "frontends/alert.h"
38
39 #include <algorithm>
40 #include <cstring>
41 #include <fstream>
42
43 using namespace std;
44 using namespace lyx::support;
45 using namespace lyx::support::os;
46
47 namespace lyx {
48
49 namespace {
50
51 typedef std::map<docstring, MyThes *> Thesauri;
52
53 } // namespace anon
54
55 struct Thesaurus::Private
56 {
57         ~Private()
58         {
59                 for (Thesauri::iterator it = thes_.begin();
60                      it != thes_.end(); ++it) {
61                         delete it->second;
62                 }
63         }
64         ///
65         bool thesaurusAvailable(docstring const & lang) const
66         {
67                 for (Thesauri::const_iterator it = thes_.begin();
68                         it != thes_.end(); ++it) {
69                                 if (it->first == lang)
70                                         if (it->second)
71                                                 return true;
72                 }
73                 return false;
74         }
75
76         ///
77         typedef std::pair<std::string, std::string> ThesFiles;
78         ///
79         ThesFiles getThesaurus(string const & path, docstring const & lang);
80         ThesFiles getThesaurus(docstring const & lang);
81         /// add a thesaurus to the list
82         bool addThesaurus(docstring const & lang);
83
84         /// the thesauri
85         Thesauri thes_;
86
87         /// the location below system/user directory
88         /// there the data+idx files lookup will happen
89         const string dataDirectory(void) { return "thes"; }
90
91 };
92
93
94 pair<string,string> Thesaurus::Private::getThesaurus(string const & path, docstring const & lang)
95 {
96         FileName base(path);
97         if (!base.isDirectory()) {
98                 return make_pair(string(), string());
99         }
100         FileNameList const idx_files = base.dirList("idx");
101         FileNameList const data_files = base.dirList("dat");
102         string idx;
103         string data;
104         string basename;
105
106         LYXERR(Debug::FILES, "thesaurus path: " << path);
107         for (FileNameList::const_iterator it = idx_files.begin(); it != idx_files.end(); ++it) {
108                 basename = it->onlyFileNameWithoutExt();
109                 if (contains(basename, to_ascii(lang))) {
110                         ifstream ifs(it->absFileName().c_str());
111                         if (ifs) {
112                                 // check for appropriate version of index file
113                                 string encoding; // first line is encoding
114                                 int items = 0;   // second line is no. of items
115                                 getline(ifs,encoding);
116                                 ifs >> items;
117                                 if (ifs.fail()) {
118                                         LYXERR(Debug::FILES, "ignore irregular thesaurus idx file: " << it->absFileName());
119                                         continue;
120                                 }
121                                 if (encoding.length() == 0 || encoding.find_first_of(',') != string::npos) {
122                                         LYXERR(Debug::FILES, "ignore version1 thesaurus idx file: " << it->absFileName());
123                                         continue;
124                                 }
125                         }
126                         idx = it->absFileName();
127                         LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
128                         break;
129                 }
130         }
131         if (idx.empty()) {
132                 return make_pair(string(), string());
133         }
134         for (support::FileNameList::const_iterator it = data_files.begin(); it != data_files.end(); ++it) {
135                 if (contains(it->onlyFileName(), basename)) {
136                         data = it->absFileName();
137                         LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
138                         break;
139                 }
140         }
141         return make_pair(idx, data);
142 }
143
144
145 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
146 {
147         string const thes_path = external_path(lyxrc.thesaurusdir_path);
148         pair<string,string> result ;
149
150         if (thesaurusAvailable(lang))
151                 return make_pair(string(), string());
152
153         if (!thes_path.empty()) {
154                 result = getThesaurus(thes_path, lang);
155         }
156         if (result.first.empty() || result.second.empty()) {
157                 string const sys_path = external_path(addName(lyx::support::package().system_support().absFileName(),dataDirectory())) ;
158                 result = getThesaurus(sys_path, lang);
159         }
160         if (result.first.empty() || result.second.empty()) {
161                 string const user_path = external_path(addName(lyx::support::package().user_support().absFileName(),dataDirectory())) ;
162                 result = getThesaurus(user_path, lang);
163         }
164         return result;
165 }
166
167
168 bool Thesaurus::Private::addThesaurus(docstring const & lang)
169 {
170         if (thesaurusAvailable(lang))
171                 return true;
172
173         ThesFiles files = getThesaurus(lang);
174         string const idx = files.first;
175         string const data = files.second;
176
177         if (idx.empty() || data.empty())
178                 return false;
179
180         char const * af = idx.c_str();
181         char const * df = data.c_str();
182         thes_[lang] = new MyThes(af, df);
183         return true;
184 }
185
186
187 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
188 {
189         return d->thesaurusAvailable(lang);
190 }
191
192
193 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
194 {
195         if (thesaurusAvailable(lang))
196                 return true;
197         pair<string, string> files = d->getThesaurus(lang);
198         return (!files.first.empty() && !files.second.empty());
199 }
200
201
202 Thesaurus::Meanings Thesaurus::lookup(WordLangTuple const & wl)
203 {
204         Meanings meanings;
205         MyThes * mythes = 0;
206
207         docstring const lang_code = from_ascii(wl.lang()->code());
208         docstring const t = wl.word();
209
210         if (!d->addThesaurus(lang_code))
211                 return meanings;
212
213         for (Thesauri::const_iterator it = d->thes_.begin();
214              it != d->thes_.end(); ++it) {
215                 if (it->first == lang_code) {
216                         mythes = it->second;
217                         break;
218                 }
219         }
220
221         if (!mythes)
222                 return meanings;
223
224         string const encoding = mythes->get_th_encoding();
225         
226         mentry * pmean;
227         string const text = to_iconv_encoding(support::lowercase(t), encoding);
228         int len = strlen(text.c_str());
229         int count = mythes->Lookup(text.c_str(), len, &pmean);
230         if (!count) {
231                 SpellChecker * speller = theSpellChecker();
232                 if (!speller)
233                         return meanings;
234                 docstring_list suggestions;
235                 speller->stem(wl, suggestions);
236                 for (size_t i = 0; i != suggestions.size(); ++i) {
237                         string const wordform = to_iconv_encoding(support::lowercase(suggestions[i]), encoding);
238                         len = strlen(wordform.c_str());
239                         count = mythes->Lookup(wordform.c_str(), len, &pmean);
240                         if (count)
241                                 break;
242                 }
243                 if (!count)
244                         return meanings;
245         }
246
247         // don't change value of pmean or count
248         // they are needed for the CleanUpAfterLookup routine
249         mentry * pm = pmean;
250         docstring meaning;
251         for (int i = 0; i < count; i++) {
252                 vector<docstring> ret;
253                 meaning = from_iconv_encoding(string(pm->defn), encoding);
254                 // remove silly item
255                 if (support::prefixIs(meaning, '-'))
256                         meaning = support::ltrim(meaning, "- ");
257                 for (int j = 0; j < pm->count; j++) {
258                         ret.push_back(from_iconv_encoding(string(pm->psyns[j]), encoding));
259                 }
260                 meanings[meaning] = ret;
261                 ++pm;
262         }
263         // now clean up all allocated memory
264         mythes->CleanUpAfterLookup(&pmean, count);
265
266         for (Meanings::iterator it = meanings.begin();
267              it != meanings.end(); ++it)
268                 sort(it->second.begin(), it->second.end());
269
270         return meanings;
271 }
272
273
274 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
275 {
276 }
277
278
279 Thesaurus::~Thesaurus()
280 {
281         delete d;
282 }
283
284 // Global instance
285 Thesaurus thesaurus;
286
287
288 } // namespace lyx