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