]> git.lyx.org Git - lyx.git/blob - src/ModuleList.cpp
Update RELEASE-NOTES
[lyx.git] / src / ModuleList.cpp
1 // -*- C++ -*-
2 /**
3  * \file ModuleList.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Richard Heck
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ModuleList.h"
15
16 #include "LaTeXFeatures.h"
17 #include "Lexer.h"
18
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/gettext.h"
22 #include "support/filetools.h"
23 #include "support/lstrings.h"
24
25 #include <algorithm>
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32
33 //global variable: module list
34 ModuleList theModuleList;
35
36
37 LyXModule::LyXModule(string const & n, string const & i,
38                            string const & d, vector<string> const & p,
39                            vector<string> const & r, vector<string> const & e,
40                            string const & c):
41         name_(n), id_(i), description_(d), package_list_(p),
42         required_modules_(r), excluded_modules_(e), category_(c),
43         checked_(false)
44 {
45         filename_ = id_ + ".module";
46 }
47
48
49 vector<string> LyXModule::prerequisites() const
50 {
51 #ifdef TEX2LYX
52         return vector<string>();
53 #else
54         if (!checked_)
55                 isAvailable();
56         return prerequisites_;
57 #endif
58 }
59
60
61 bool LyXModule::isAvailable() const
62 {
63 #ifdef TEX2LYX
64         return true;
65 #else
66         if (package_list_.empty())
67                 return true;
68         if (checked_)
69                 return available_;
70         checked_ = true;
71         available_ = true;
72         //check whether all of the required packages are available
73         vector<string>::const_iterator it  = package_list_.begin();
74         vector<string>::const_iterator end = package_list_.end();
75         for (; it != end; ++it) {
76                 if (!LaTeXFeatures::isAvailable(*it)) {
77                         available_ = false;
78                         prerequisites_.push_back(*it);
79                 }
80         }
81         return available_;
82 #endif
83 }
84
85
86 bool LyXModule::isCompatible(string const & modname) const
87 {
88         // do we exclude it?
89         if (find(excluded_modules_.begin(), excluded_modules_.end(), modname) !=
90                         excluded_modules_.end())
91                 return false;
92
93         LyXModule const * const lm = theModuleList[modname];
94         if (!lm)
95                 return true;
96
97         // does it exclude us?
98         vector<string> const excmods = lm->getExcludedModules();
99         if (find(excmods.begin(), excmods.end(), id_) != excmods.end())
100                 return false;
101
102         return true;
103 }
104
105
106 bool LyXModule::areCompatible(string const & mod1, string const & mod2)
107 {
108         LyXModule const * const lm1 = theModuleList[mod1];
109         if (lm1)
110                 return lm1->isCompatible(mod2);
111         LyXModule const * const lm2 = theModuleList[mod2];
112         if (lm2)
113                 return lm2->isCompatible(mod1);
114         // Can't check it either way.
115         return true;
116 }
117
118
119 // used when sorting the module list.
120 class ModuleSorter {
121 public:
122         int operator()(LyXModule const & lm1, LyXModule const & lm2) const
123         {
124                 return _(lm1.getName()) < _(lm2.getName());
125         }
126 };
127
128
129 // Much of this is borrowed from LayoutFileList::read()
130 bool ModuleList::read()
131 {
132         FileName const real_file = libFileSearch("", "lyxmodules.lst");
133         LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
134
135         if (real_file.empty()) {
136                 LYXERR0("unable to find modules file `lyxmodules.lst'.\n"
137                         << "No modules will be available.");
138                 return false;
139         }
140
141         Lexer lex;
142         if (!lex.setFile(real_file)) {
143                 LYXERR0("lyxlex was not able to set file: "
144                         << real_file << ".\nNo modules will be available.");
145                 return false;
146         }
147
148         if (!lex.isOK()) {
149                 LYXERR0("unable to open modules file  `"
150                         << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
151                         << "'\nNo modules will be available.");
152                 return false;
153         }
154
155         bool finished = false;
156         // Parse modules files
157         LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
158         while (lex.isOK() && !finished) {
159                 LYXERR(Debug::TCLASS, "\tline by line");
160                 switch (lex.lex()) {
161                 case Lexer::LEX_FEOF:
162                         finished = true;
163                         break;
164                 default:
165                         string const modname = lex.getString();
166                         LYXERR(Debug::TCLASS, "Module name: " << modname);
167                         if (!lex.next())
168                                 break;
169                         string const fname = lex.getString();
170                         LYXERR(Debug::TCLASS, "Filename: " << fname);
171                         if (!lex.next(true))
172                                 break;
173                         string const desc = lex.getString();
174                         LYXERR(Debug::TCLASS, "Description: " << desc);
175                         //FIXME Add packages
176                         if (!lex.next())
177                                 break;
178                         string str = lex.getString();
179                         LYXERR(Debug::TCLASS, "Packages: " << str);
180                         vector<string> pkgs;
181                         while (!str.empty()) {
182                                 string p;
183                                 str = split(str, p, ',');
184                                 pkgs.push_back(p);
185                         }
186                         if (!lex.next())
187                                 break;
188                         str = lex.getString();
189                         LYXERR(Debug::TCLASS, "Required: " << str);
190                         vector<string> req;
191                         while (!str.empty()) {
192                                 string p;
193                                 str = split(str, p, '|');
194                                 req.push_back(p);
195                         }
196                         if (!lex.next())
197                                 break;
198                         str = lex.getString();
199                         LYXERR(Debug::TCLASS, "Excluded: " << str);
200                         vector<string> exc;
201                         while (!str.empty()) {
202                                 string p;
203                                 str = split(str, p, '|');
204                                 exc.push_back(p);
205                         }
206                         if (!lex.next())
207                                 break;
208                         string const catgy = lex.getString();
209                         LYXERR(Debug::TCLASS, "Category: " << catgy);
210                         // This code is run when we have
211                         // modName, fname, desc, pkgs, req, exc, and catgy
212                         addLayoutModule(modname, fname, desc, pkgs, req, exc, catgy);
213                 } // end switch
214         } //end while
215
216         LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
217
218         if (!theModuleList.empty())
219                 sort(theModuleList.begin(), theModuleList.end(), ModuleSorter());
220         return true;
221 }
222
223
224 void ModuleList::addLayoutModule(string const & modname,
225         string const & filename, string const & description,
226         vector<string> const & pkgs, vector<string> const & req,
227         vector<string> const & exc, string const & catgy)
228 {
229         LyXModule lm(modname, filename, description, pkgs, req, exc, catgy);
230         modlist_.push_back(lm);
231 }
232
233
234 LyXModuleList::const_iterator ModuleList::begin() const
235 {
236         return modlist_.begin();
237 }
238
239
240 LyXModuleList::iterator ModuleList::begin()
241 {
242         return modlist_.begin();
243 }
244
245
246 LyXModuleList::const_iterator ModuleList::end() const
247 {
248         return modlist_.end();
249 }
250
251
252 LyXModuleList::iterator ModuleList::end()
253 {
254         return modlist_.end();
255 }
256
257
258 LyXModule const * ModuleList::operator[](string const & str) const
259 {
260         LyXModuleList::const_iterator it = modlist_.begin();
261         for (; it != modlist_.end(); ++it)
262                 if (it->getID() == str) {
263                         LyXModule const & mod = *it;
264                         return &mod;
265                 }
266         return 0;
267 }
268
269
270 LyXModule * ModuleList::operator[](string const & str)
271 {
272         LyXModuleList::iterator it = modlist_.begin();
273         for (; it != modlist_.end(); ++it)
274                 if (it->getID() == str) {
275                 LyXModule & mod = *it;
276                 return &mod;
277                 }
278                 return 0;
279 }
280
281 } // namespace lyx