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