]> git.lyx.org Git - lyx.git/blob - src/ModuleList.cpp
Allow boldface and font size changing commands for unicode symbols in math.
[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 // used when sorting the module list.
68 class ModuleSorter
69 {
70 public:
71         int operator()(LyXModule const & lm1, LyXModule const & lm2) const
72         {
73                 return lm1.getName() < lm2.getName();
74         }
75 };
76
77
78 //Much of this is borrowed from LayoutFileList::read()
79 bool ModuleList::load()
80 {
81         FileName const real_file = libFileSearch("", "lyxmodules.lst");
82         LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
83
84         if (real_file.empty()) {
85                 LYXERR0("unable to find modules file  `"
86                         << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
87                         << "'.\nNo modules will be available.");
88                 return false;
89         }
90
91         Lexer lex;
92         if (!lex.setFile(real_file)) {
93                 LYXERR0("lyxlex was not able to set file: "
94                         << real_file << ".\nNo modules will be available.");
95                 return false;
96         }
97
98         if (!lex.isOK()) {
99                 LYXERR0("unable to open modules file  `"
100                         << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
101                         << "'\nNo modules will be available.");
102                 return false;
103         }
104
105         bool finished = false;
106         // Parse modules files
107         LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
108         while (lex.isOK() && !finished) {
109                 LYXERR(Debug::TCLASS, "\tline by line");
110                 switch (lex.lex()) {
111                 case Lexer::LEX_FEOF:
112                         finished = true;
113                         break;
114                 default:
115                         string const modName = lex.getString();
116                         LYXERR(Debug::TCLASS, "Module name: " << modName);
117                         if (!lex.next())
118                                 break;
119                         string const fname = lex.getString();
120                         LYXERR(Debug::TCLASS, "Filename: " << fname);
121                         if (!lex.next())
122                                 break;
123                         string const desc = lex.getString();
124                         LYXERR(Debug::TCLASS, "Description: " << desc);
125                         //FIXME Add packages
126                         if (!lex.next())
127                                 break;
128                         string str = lex.getString();
129                         LYXERR(Debug::TCLASS, "Packages: " << str);
130                         vector<string> pkgs;
131                         while (!str.empty()) {
132                                 string p;
133                                 str = split(str, p, ',');
134                                 pkgs.push_back(p);
135                         }
136                         if (!lex.next())
137                                 break;
138                         str = lex.getString();
139                         LYXERR(Debug::TCLASS, "Required: " << str);
140                         vector<string> req;
141                         while (!str.empty()) {
142                                 string p;
143                                 str = split(str, p, '|');
144                                 req.push_back(p);
145                         }
146                         if (!lex.next())
147                                 break;
148                         str = lex.getString();
149                         LYXERR(Debug::TCLASS, "Excluded: " << str);
150                         vector<string> exc;
151                         while (!str.empty()) {
152                                 string p;
153                                 str = split(str, p, '|');
154                                 exc.push_back(p);
155                         }
156                         // This code is run when we have
157                         // modName, fname, desc, pkgs, req, and exc
158                         addLayoutModule(modName, fname, desc, pkgs, req, exc);
159                 } // end switch
160         } //end while
161         
162         LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
163
164         if (!moduleList.empty())
165                 sort(moduleList.begin(), moduleList.end(), ModuleSorter());
166         return true;
167 }
168
169
170 void ModuleList::addLayoutModule(string const & moduleName, 
171         string const & filename, string const & description,
172         vector<string> const & pkgs, vector<string> const & req,
173         vector<string> const & exc)
174 {
175         LyXModule lm(moduleName, filename, description, pkgs, req, exc);
176         modlist_.push_back(lm);
177 }
178
179
180 LyXModuleList::const_iterator ModuleList::begin() const
181 {
182         return modlist_.begin();
183 }
184
185
186 LyXModuleList::iterator ModuleList::begin()
187 {
188         return modlist_.begin();
189 }
190
191
192 LyXModuleList::const_iterator ModuleList::end() const
193 {
194         return modlist_.end();
195 }
196
197
198 LyXModuleList::iterator ModuleList::end()
199 {
200         return modlist_.end();
201 }
202
203
204 LyXModule * ModuleList::getModuleByName(string const & str)
205 {
206         LyXModuleList::iterator it = modlist_.begin();
207         for (; it != modlist_.end(); ++it)
208                 if (it->getName() == str) {
209                         LyXModule & mod = *it;
210                         return &mod;
211                 }
212         return 0;
213 }
214
215 LyXModule * ModuleList::operator[](string const & str)
216 {
217         LyXModuleList::iterator it = modlist_.begin();
218         for (; it != modlist_.end(); ++it)
219                 if (it->getID() == str) {
220                         LyXModule & mod = *it;
221                         return &mod;
222                 }
223         return 0;
224 }
225
226 } // namespace lyx