2 * \file LayoutFileList.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
14 #include "LayoutFile.h"
17 #include "FloatList.h"
19 #include "TextClass.h"
21 #include "frontends/alert.h"
23 #include "support/debug.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lassert.h"
28 #include "support/lstrings.h"
30 #include <boost/bind.hpp>
31 #include <boost/regex.hpp>
36 using namespace lyx::support;
44 LayoutFile::LayoutFile(string const & fn, string const & cln,
45 string const & desc, string const & prereq,
51 prerequisites_ = prereq;
52 tex_class_avail_ = texclassavail;
56 LayoutFileList::~LayoutFileList()
58 ClassMap::const_iterator it = classmap_.begin();
59 ClassMap::const_iterator en = classmap_.end();
60 for (; it != en; ++it) {
66 LayoutFileList & LayoutFileList::get()
68 static LayoutFileList baseclasslist;
73 bool LayoutFileList::haveClass(string const & classname) const
75 ClassMap::const_iterator it = classmap_.begin();
76 ClassMap::const_iterator en = classmap_.end();
77 for (; it != en; ++it) {
78 if (it->first == classname)
85 LayoutFile const & LayoutFileList::operator[](string const & classname) const
87 LASSERT(haveClass(classname), /**/);
88 return *classmap_[classname];
92 LayoutFile & LayoutFileList::operator[](string const & classname)
94 LASSERT(haveClass(classname), /**/);
95 return *classmap_[classname];
99 // Reads LyX textclass definitions according to textclass config file
100 bool LayoutFileList::read()
103 FileName const real_file = libFileSearch("", "textclass.lst");
104 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
106 if (real_file.empty()) {
107 lyxerr << "LayoutFileList::Read: unable to find "
109 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
110 << "'. Exiting." << endl;
112 // This causes LyX to end... Not a desirable behaviour. Lgb
113 // What do you propose? That the user gets a file dialog
114 // and is allowed to hunt for the file? (Asger)
115 // more that we have a layout for minimal.cls statically
116 // compiled in... (Lgb)
119 if (!lex.setFile(real_file)) {
120 lyxerr << "LayoutFileList::Read: "
121 "lyxlex was not able to set file: "
122 << real_file << endl;
126 lyxerr << "LayoutFileList::Read: unable to open "
128 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
129 << "'\nCheck your installation. LyX can't continue."
134 bool finished = false;
136 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
137 while (lex.isOK() && !finished) {
138 LYXERR(Debug::TCLASS, "\tline by line");
140 case Lexer::LEX_FEOF:
144 string const fname = lex.getString();
145 LYXERR(Debug::TCLASS, "Fname: " << fname);
148 string const clname = lex.getString();
149 LYXERR(Debug::TCLASS, "Clname: " << clname);
152 string const desc = lex.getString();
153 LYXERR(Debug::TCLASS, "Desc: " << desc);
156 bool avail = lex.getBool();
157 LYXERR(Debug::TCLASS, "Avail: " << avail);
160 string const prereq = lex.getString();
161 LYXERR(Debug::TCLASS, "Prereq: " << prereq);
162 // This code is run when we have
163 // fname, clname, desc, prereq, and avail
164 LayoutFile * tmpl = new LayoutFile(fname, clname, desc, prereq, avail);
165 if (lyxerr.debugging(Debug::TCLASS)) {
166 // only system layout files are loaded here so no
167 // buffer path is needed.
170 classmap_[fname] = tmpl;
173 LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
175 // lyx will start with an empty classmap_, but only reconfigure is allowed
176 // in this case. This gives users a second chance to configure lyx if
177 // initial configuration fails. (c.f. bug 2829)
178 if (classmap_.empty())
179 lyxerr << "LayoutFileList::Read: no textclasses found!"
185 std::vector<LayoutFileIndex> LayoutFileList::classList() const
187 std::vector<LayoutFileIndex> cl;
188 ClassMap::const_iterator it = classmap_.begin();
189 ClassMap::const_iterator en = classmap_.end();
190 for (; it != en; ++it)
191 cl.push_back(it->first);
196 void LayoutFileList::reset(LayoutFileIndex const & classname) {
197 LASSERT(haveClass(classname), /**/);
198 LayoutFile * tc = classmap_[classname];
200 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
201 tc->prerequisites(), tc->isTeXClassAvailable());
202 classmap_[classname] = tmpl;
207 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
209 if (haveClass(textclass))
212 FileName const tempLayout = FileName::tempName();
213 ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
214 ofs << "# This layout is automatically generated\n"
215 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
217 "Input stdclass.inc\n\n"
222 "DefaultStyle Standard\n\n"
224 " Category MainText\n"
226 " LatexType Paragraph\n"
231 " AlignPossible Block, Left, Right, Center\n"
232 " LabelType No_Label\n"
236 // We do not know if a LaTeX class is available for this document, but setting
237 // the last parameter to true will suppress a warning message about missing
239 LayoutFile * tc = new LayoutFile(textclass, textclass,
240 "Unknown text class " + textclass, textclass + ".cls", true);
241 if (!tc->load(tempLayout.absFilename())) {
242 // The only way this happens is because the hardcoded layout file above
244 LASSERT(false, /**/);
246 classmap_[textclass] = tc;
252 LayoutFileList::addLocalLayout(string const & textclass, string const & path)
254 // FIXME There is a bug here: 4593
256 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
257 // NOTE: latex class name is defined in textclass.layout, which can be
258 // different from textclass
259 string fullName = addName(path, textclass + ".layout");
261 FileName const layout_file(fullName);
262 if (layout_file.exists()) {
263 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
264 // Read .layout file and get description, real latex classname etc
266 // This is a C++ version of function processLayoutFile in configure.py,
267 // which uses the following regex
268 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
269 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
270 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
271 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
273 while (getline(ifs, line)) {
274 // look for the \DeclareXXXClass line
276 if (regex_match(line, sub, reg)) {
277 // returns: whole string, classtype (not used here), class name, description
278 LASSERT(sub.size() == 4, /**/);
279 // now, create a TextClass with description containing path information
280 string class_name(sub.str(2) == "" ? textclass : sub.str(2));
281 string class_prereq(class_name + ".cls");
283 new LayoutFile(textclass, class_name, textclass, class_prereq, true);
284 //FIXME: The prerequisites are available from the layout file and
285 // can be extracted from the above regex, but for now this
286 // field is simply set to class_name + ".cls"
287 // This textclass is added on request so it will definitely be
288 // used. Load it now because other load() calls may fail if they
289 // are called in a context without buffer path information.
291 // There will be only one textclass with this name, even if different
292 // layout files are loaded from different directories.
293 if (haveClass(textclass)) {
294 LYXERR0("Existing textclass " << textclass << " is redefined by " << fullName);
295 delete classmap_[textclass];
297 classmap_[textclass] = tmpl;
302 // If .layout is not in local directory, or an invalid layout is found, return null
307 bool LayoutFileList::load(string const & name, string const & buf_path)
309 if (!haveClass(name)) {
310 LYXERR0("Document class \"" << name << "\" does not exist.");
314 LayoutFile * tc = classmap_[name];
315 if (!tc->load(buf_path)) {
316 docstring s = bformat(_("The document class %1$s "
317 "could not be loaded."), from_utf8(name));
318 frontend::Alert::error(_("Could not load class"), s);
325 LayoutFileIndex defaultBaseclass()
327 if (LayoutFileList::get().haveClass("article"))
328 return string("article");
329 if (LayoutFileList::get().empty())
331 return LayoutFileList::get().classList().front();
336 // Reads the style files
339 LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
341 if (!LayoutFileList::get().read()) {
342 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
343 "during parsing.\n Exiting.");
347 LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");