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