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 "support/bind.h"
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()
102 bool success = false;
104 FileName const real_file = libFileSearch("", "textclass.lst");
105 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << "'.");
107 if (real_file.empty()) {
108 LYXERR0("LayoutFileList::Read: unable to find textclass file `"
109 << makeDisplayPath(real_file.absFileName(), 1000)
112 } else if (!lex.setFile(real_file)) {
113 LYXERR0("LayoutFileList::Read: lyxlex was not able to set file: "
114 << real_file << '.');
115 } else if (!lex.isOK()) {
116 LYXERR0("LayoutFileList::Read: unable to open textclass file `"
117 << makeDisplayPath(real_file.absFileName(), 1000)
118 << "'\nCheck your installation.");
120 // we have a file we can read.
121 bool finished = false;
122 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
123 while (lex.isOK() && !finished) {
124 LYXERR(Debug::TCLASS, "\tline by line");
126 case Lexer::LEX_FEOF:
130 string const fname = lex.getString();
131 LYXERR(Debug::TCLASS, "Fname: " << fname);
134 string const clname = lex.getString();
135 LYXERR(Debug::TCLASS, "Clname: " << clname);
138 string const desc = lex.getString();
139 LYXERR(Debug::TCLASS, "Desc: " << desc);
142 bool avail = lex.getBool();
143 LYXERR(Debug::TCLASS, "Avail: " << avail);
146 string const prereq = lex.getString();
147 LYXERR(Debug::TCLASS, "Prereq: " << prereq);
148 // This code is run when we have
149 // fname, clname, desc, prereq, and avail
150 LayoutFile * tmpl = new LayoutFile(fname, clname, desc, prereq, avail);
151 if (lyxerr.debugging(Debug::TCLASS)) {
152 // only system layout files are loaded here so no
153 // buffer path is needed.
156 classmap_[fname] = tmpl;
158 } // end of while loop
159 LYXERR(Debug::TCLASS, "End parsing of textclass.lst");
163 // LyX will start with an empty classmap_. This is OK because
164 // (a) we will give the user a chance to reconfigure (see bug 2829) and
165 // (b) even if that fails, we can use addEmptyClass() to get some basic
167 if (classmap_.empty())
168 LYXERR0("LayoutFileList::Read: no textclasses found!");
173 std::vector<LayoutFileIndex> LayoutFileList::classList() const
175 std::vector<LayoutFileIndex> cl;
176 ClassMap::const_iterator it = classmap_.begin();
177 ClassMap::const_iterator en = classmap_.end();
178 for (; it != en; ++it)
179 cl.push_back(it->first);
184 void LayoutFileList::reset(LayoutFileIndex const & classname) {
185 LASSERT(haveClass(classname), /**/);
186 LayoutFile * tc = classmap_[classname];
188 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
189 tc->prerequisites(), tc->isTeXClassAvailable());
190 classmap_[classname] = tmpl;
202 "DefaultStyle Standard\n\n"
204 " Category MainText\n"
206 " LatexType Paragraph\n"
211 " AlignPossible Block, Left, Right, Center\n"
212 " LabelType No_Label\n"
217 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
219 FileName const tempLayout = FileName::tempName();
220 ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
221 // This writes a very basic class, but it also attempts to include
222 // stdclass.inc. That would give us something moderately usable.
223 ofs << "# This layout is automatically generated\n"
224 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
226 "Input stdclass.inc\n\n"
230 // We do not know if a LaTeX class is available for this document, but setting
231 // the last parameter to true will suppress a warning message about missing
233 LayoutFile * tc = new LayoutFile(textclass, textclass,
234 "Unknown text class " + textclass, textclass + ".cls", true);
236 if (!tc->load(tempLayout.absFileName())) {
237 // The only way this happens is because the hardcoded layout file
238 // aboveis wrong or stdclass.inc cannot be found. So try again
239 // without stdclass.inc.
240 ofstream ofs2(tempLayout.toFilesystemEncoding().c_str());
241 ofs2 << "# This layout is automatically generated\n"
242 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
244 "Input stdclass.inc\n\n"
247 if (!tc->load(tempLayout.absFileName())) {
248 // This can only happen if the hardcoded file above is wrong.
249 LASSERT(false, /* */);
253 classmap_[textclass] = tc;
259 LayoutFileList::addLocalLayout(string const & textclass, string const & path)
261 // FIXME There is a bug here: 4593
263 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
264 // NOTE: latex class name is defined in textclass.layout, which can be
265 // different from textclass
266 string fullName = addName(path, textclass + ".layout");
268 FileName const layout_file(fullName);
269 if (layout_file.exists()) {
270 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
271 // Read .layout file and get description, real latex classname etc
273 // This is a C++ version of function processLayoutFile in configure.py,
274 // which uses the following regex
275 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
276 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
277 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
278 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
280 while (getline(ifs, line)) {
281 // look for the \DeclareXXXClass line
283 if (regex_match(line, sub, reg)) {
284 // returns: whole string, classtype (not used here), class name, description
285 LASSERT(sub.size() == 4, /**/);
286 // now, create a TextClass with description containing path information
287 string class_name(sub.str(2) == "" ? textclass : sub.str(2));
288 string class_prereq(class_name + ".cls");
290 new LayoutFile(textclass, class_name, textclass, class_prereq, true);
291 //FIXME: The prerequisites are available from the layout file and
292 // can be extracted from the above regex, but for now this
293 // field is simply set to class_name + ".cls"
294 // This textclass is added on request so it will definitely be
295 // used. Load it now because other load() calls may fail if they
296 // are called in a context without buffer path information.
298 // There will be only one textclass with this name, even if different
299 // layout files are loaded from different directories.
300 if (haveClass(textclass)) {
301 LYXERR0("Existing textclass " << textclass << " is redefined by " << fullName);
302 delete classmap_[textclass];
304 classmap_[textclass] = tmpl;
309 // If .layout is not in local directory, or an invalid layout is found, return null
314 bool LayoutFileList::load(string const & name, string const & buf_path)
316 if (!haveClass(name)) {
317 LYXERR0("Document class \"" << name << "\" does not exist.");
321 LayoutFile * tc = classmap_[name];
322 if (!tc->load(buf_path)) {
323 docstring s = bformat(_("The document class %1$s "
324 "could not be loaded."), from_utf8(name));
325 frontend::Alert::error(_("Could not load class"), s);
332 LayoutFileIndex defaultBaseclass()
334 if (LayoutFileList::get().haveClass("article"))
335 return string("article");
336 if (LayoutFileList::get().empty())
337 // we'll call it that, since this gives the user a chance to
338 // have a functioning document when things improve.
339 return string("article");
340 return LayoutFileList::get().classList().front();