]> git.lyx.org Git - lyx.git/blob - src/ModuleList.cpp
547f1f922072a74a4c5c19ed4b84f222bf9de0a1
[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/filetools.h"
22 #include "support/lstrings.h"
23
24 #include <algorithm>
25         
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30
31
32 //global variable: module list
33 ModuleList moduleList;
34
35
36 LyXModule::LyXModule(string const & n, string const & i, 
37                            string const & d, vector<string> const & p,
38                            vector<string> const & r, vector<string> const & e):
39         name(n), id(i), description(d), 
40         packageList(p), requiredModules(r), excludedModules(e),
41         checked(false)
42 {
43         filename = id + ".module";
44 }
45
46
47 bool LyXModule::isAvailable() {
48         if (packageList.empty())
49                 return true;
50         if (checked)
51                 return available;
52         checked = true;
53         //check whether all of the required packages are available
54         vector<string>::const_iterator it  = packageList.begin();
55         vector<string>::const_iterator end = packageList.end(); 
56         for (; it != end; ++it) {
57                 if (!LaTeXFeatures::isAvailable(*it)) {
58                         available = false;
59                         return available;
60                 }
61         }
62         available = true;
63         return available;
64 }
65
66
67 bool LyXModule::isCompatible(string const & modName) const
68 {
69         // do we exclude it?
70         if (find(excludedModules.begin(), excludedModules.end(), modName) !=
71                         excludedModules.end())
72                 return false;
73
74         LyXModule const * const lm = moduleList[modName];
75         if (!lm)
76                 return true;
77
78         // does it exclude us?
79         vector<string> const excMods = lm->getExcludedModules();
80         if (find(excMods.begin(), excMods.end(), name) != excMods.end())
81                 return false;
82
83         return true;
84 }
85
86
87 bool LyXModule::areCompatible(string const & mod1, string const & mod2)
88 {
89         LyXModule const * const lm1 = moduleList[mod1];
90         if (lm1)
91                 return lm1->isCompatible(mod2);
92         LyXModule const * const lm2 = moduleList[mod2];
93         if (lm2)
94                 return lm2->isCompatible(mod1);
95         // Can't check it either way.
96         return true;
97 }
98
99 // used when sorting the module list.
100 class ModuleSorter
101 {
102 public:
103         int operator()(LyXModule const & lm1, LyXModule const & lm2) const
104         {
105                 return lm1.getName() < lm2.getName();
106         }
107 };
108
109
110 //Much of this is borrowed from LayoutFileList::read()
111 bool ModuleList::load()
112 {
113         FileName const real_file = libFileSearch("", "lyxmodules.lst");
114         LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
115
116         if (real_file.empty()) {
117                 LYXERR0("unable to find modules file  `"
118                         << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
119                         << "'.\nNo modules will be available.");
120                 return false;
121         }
122
123         Lexer lex;
124         if (!lex.setFile(real_file)) {
125                 LYXERR0("lyxlex was not able to set file: "
126                         << real_file << ".\nNo modules will be available.");
127                 return false;
128         }
129
130         if (!lex.isOK()) {
131                 LYXERR0("unable to open modules file  `"
132                         << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
133                         << "'\nNo modules will be available.");
134                 return false;
135         }
136
137         bool finished = false;
138         // Parse modules files
139         LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
140         while (lex.isOK() && !finished) {
141                 LYXERR(Debug::TCLASS, "\tline by line");
142                 switch (lex.lex()) {
143                 case Lexer::LEX_FEOF:
144                         finished = true;
145                         break;
146                 default:
147                         string const modName = lex.getString();
148                         LYXERR(Debug::TCLASS, "Module name: " << modName);
149                         if (!lex.next())
150                                 break;
151                         string const fname = lex.getString();
152                         LYXERR(Debug::TCLASS, "Filename: " << fname);
153                         if (!lex.next())
154                                 break;
155                         string const desc = lex.getString();
156                         LYXERR(Debug::TCLASS, "Description: " << desc);
157                         //FIXME Add packages
158                         if (!lex.next())
159                                 break;
160                         string str = lex.getString();
161                         LYXERR(Debug::TCLASS, "Packages: " << str);
162                         vector<string> pkgs;
163                         while (!str.empty()) {
164                                 string p;
165                                 str = split(str, p, ',');
166                                 pkgs.push_back(p);
167                         }
168                         if (!lex.next())
169                                 break;
170                         str = lex.getString();
171                         LYXERR(Debug::TCLASS, "Required: " << str);
172                         vector<string> req;
173                         while (!str.empty()) {
174                                 string p;
175                                 str = split(str, p, '|');
176                                 req.push_back(p);
177                         }
178                         if (!lex.next())
179                                 break;
180                         str = lex.getString();
181                         LYXERR(Debug::TCLASS, "Excluded: " << str);
182                         vector<string> exc;
183                         while (!str.empty()) {
184                                 string p;
185                                 str = split(str, p, '|');
186                                 exc.push_back(p);
187                         }
188                         // This code is run when we have
189                         // modName, fname, desc, pkgs, req, and exc
190                         addLayoutModule(modName, fname, desc, pkgs, req, exc);
191                 } // end switch
192         } //end while
193         
194         LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
195
196         if (!moduleList.empty())
197                 sort(moduleList.begin(), moduleList.end(), ModuleSorter());
198         return true;
199 }
200
201
202 void ModuleList::addLayoutModule(string const & moduleName, 
203         string const & filename, string const & description,
204         vector<string> const & pkgs, vector<string> const & req,
205         vector<string> const & exc)
206 {
207         LyXModule lm(moduleName, filename, description, pkgs, req, exc);
208         modlist_.push_back(lm);
209 }
210
211
212 LyXModuleList::const_iterator ModuleList::begin() const
213 {
214         return modlist_.begin();
215 }
216
217
218 LyXModuleList::iterator ModuleList::begin()
219 {
220         return modlist_.begin();
221 }
222
223
224 LyXModuleList::const_iterator ModuleList::end() const
225 {
226         return modlist_.end();
227 }
228
229
230 LyXModuleList::iterator ModuleList::end()
231 {
232         return modlist_.end();
233 }
234
235
236 LyXModule * ModuleList::operator[](string const & str)
237 {
238         LyXModuleList::iterator it = modlist_.begin();
239         for (; it != modlist_.end(); ++it)
240                 if (it->getID() == str) {
241                         LyXModule & mod = *it;
242                         return &mod;
243                 }
244         return 0;
245 }
246
247 } // namespace lyx