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 "support/regex.h"
36 using namespace lyx::support;
42 LayoutFile::LayoutFile(string const & fn, string const & cln,
43 string const & desc, string const & prereq,
49 prerequisites_ = prereq;
50 tex_class_avail_ = texclassavail;
54 LayoutFileList::~LayoutFileList()
56 ClassMap::const_iterator it = classmap_.begin();
57 ClassMap::const_iterator en = classmap_.end();
58 for (; it != en; ++it) {
64 LayoutFileList & LayoutFileList::get()
66 static LayoutFileList baseclasslist;
71 bool LayoutFileList::haveClass(string const & classname) const
73 ClassMap::const_iterator it = classmap_.begin();
74 ClassMap::const_iterator en = classmap_.end();
75 for (; it != en; ++it) {
76 if (it->first == classname)
83 LayoutFile const & LayoutFileList::operator[](string const & classname) const
85 LASSERT(haveClass(classname), /**/);
86 return *classmap_[classname];
90 LayoutFile & LayoutFileList::operator[](string const & classname)
92 LASSERT(haveClass(classname), /**/);
93 return *classmap_[classname];
97 // Reads LyX textclass definitions according to textclass config file
98 bool LayoutFileList::read()
100 bool success = false;
102 FileName const real_file = libFileSearch("", "textclass.lst");
103 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << "'.");
105 if (real_file.empty()) {
106 LYXERR0("LayoutFileList::Read: unable to find textclass file "
107 << "`textclass.lst'.");
108 } else if (!lex.setFile(real_file)) {
109 LYXERR0("LayoutFileList::Read: lyxlex was not able to set file: "
110 << real_file << '.');
111 } else if (!lex.isOK()) {
112 LYXERR0("LayoutFileList::Read: unable to open textclass file `"
113 << makeDisplayPath(real_file.absFileName(), 1000)
114 << "'\nCheck your installation.");
116 // we have a file we can read.
117 bool finished = false;
118 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
119 while (lex.isOK() && !finished) {
120 LYXERR(Debug::TCLASS, "\tline by line");
122 case Lexer::LEX_FEOF:
126 string const fname = lex.getString();
127 LYXERR(Debug::TCLASS, "Fname: " << fname);
130 string const clname = lex.getString();
131 LYXERR(Debug::TCLASS, "Clname: " << clname);
134 string const desc = lex.getString();
135 LYXERR(Debug::TCLASS, "Desc: " << desc);
138 bool avail = lex.getBool();
139 LYXERR(Debug::TCLASS, "Avail: " << avail);
142 string const prereq = lex.getString();
143 LYXERR(Debug::TCLASS, "Prereq: " << prereq);
144 // This code is run when we have
145 // fname, clname, desc, prereq, and avail
146 LayoutFile * tmpl = new LayoutFile(fname, clname, desc, prereq, avail);
147 if (lyxerr.debugging(Debug::TCLASS)) {
148 // only system layout files are loaded here so no
149 // buffer path is needed.
152 classmap_[fname] = tmpl;
154 } // end of while loop
155 LYXERR(Debug::TCLASS, "End parsing of textclass.lst");
159 // LyX will start with an empty classmap_. This is OK because
160 // (a) we will give the user a chance to reconfigure (see bug 2829) and
161 // (b) even if that fails, we can use addEmptyClass() to get some basic
163 if (classmap_.empty())
164 LYXERR0("LayoutFileList::Read: no textclasses found!");
169 std::vector<LayoutFileIndex> LayoutFileList::classList() const
171 std::vector<LayoutFileIndex> cl;
172 ClassMap::const_iterator it = classmap_.begin();
173 ClassMap::const_iterator en = classmap_.end();
174 for (; it != en; ++it)
175 cl.push_back(it->first);
180 void LayoutFileList::reset(LayoutFileIndex const & classname) {
181 LASSERT(haveClass(classname), /**/);
182 LayoutFile * tc = classmap_[classname];
184 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
185 tc->prerequisites(), tc->isTeXClassAvailable());
186 classmap_[classname] = tmpl;
198 "DefaultStyle Standard\n\n"
200 " Category MainText\n"
202 " LatexType Paragraph\n"
207 " AlignPossible Block, Left, Right, Center\n"
208 " LabelType No_Label\n"
213 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
215 FileName const tempLayout = FileName::tempName();
216 ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
217 // This writes a very basic class, but it also attempts to include
218 // stdclass.inc. That would give us something moderately usable.
219 ofs << "# This layout is automatically generated\n"
220 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
221 "Format " << LAYOUT_FORMAT << "\n"
222 "Input stdclass.inc\n\n"
226 // We do not know if a LaTeX class is available for this document, but setting
227 // the last parameter to true will suppress a warning message about missing
229 LayoutFile * tc = new LayoutFile(textclass, textclass,
230 "Unknown text class " + textclass, textclass + ".cls", true);
232 if (!tc->load(tempLayout.absFileName())) {
233 // The only way this happens is because the hardcoded layout file
234 // above is wrong or stdclass.inc cannot be found. So try again
235 // without stdclass.inc.
236 ofstream ofs2(tempLayout.toFilesystemEncoding().c_str());
237 ofs2 << "# This layout is automatically generated\n"
238 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
239 "Format " << LAYOUT_FORMAT << "\n"
242 if (!tc->load(tempLayout.absFileName())) {
243 // This can only happen if the hardcoded file above is wrong.
244 LASSERT(false, /* */);
248 classmap_[textclass] = tc;
254 LayoutFileList::addLocalLayout(string const & textclass, string const & path)
256 // FIXME There is a bug here: 4593
258 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
259 // NOTE: latex class name is defined in textclass.layout, which can be
260 // different from textclass
261 string fullName = addName(path, textclass + ".layout");
263 FileName const layout_file(fullName);
264 if (layout_file.exists()) {
265 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
266 // Read .layout file and get description, real latex classname etc
268 // This is a C++ version of function processLayoutFile in configure.py,
269 // which uses the following regex
270 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
271 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
272 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
273 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
275 while (getline(ifs, line)) {
276 // look for the \DeclareXXXClass line
278 if (regex_match(line, sub, reg)) {
279 // returns: whole string, classtype (not used here), class name, description
280 LASSERT(sub.size() == 4, /**/);
281 // now, create a TextClass with description containing path information
282 string class_name(sub.str(2) == "" ? textclass : sub.str(2));
283 string class_prereq(class_name + ".cls");
285 new LayoutFile(textclass, class_name, textclass, class_prereq, true);
286 //FIXME: The prerequisites are available from the layout file and
287 // can be extracted from the above regex, but for now this
288 // field is simply set to class_name + ".cls"
289 // This textclass is added on request so it will definitely be
290 // used. Load it now because other load() calls may fail if they
291 // are called in a context without buffer path information.
293 // There will be only one textclass with this name, even if different
294 // layout files are loaded from different directories.
295 if (haveClass(textclass)) {
296 LYXERR0("Existing textclass " << textclass << " is redefined by " << fullName);
297 delete classmap_[textclass];
299 classmap_[textclass] = tmpl;
304 // If .layout is not in local directory, or an invalid layout is found, return null
309 bool LayoutFileList::load(string const & name, string const & buf_path)
311 if (!haveClass(name)) {
312 LYXERR0("Document class \"" << name << "\" does not exist.");
316 LayoutFile * tc = classmap_[name];
317 if (!tc->load(buf_path)) {
318 docstring s = bformat(_("The document class %1$s "
319 "could not be loaded."), from_utf8(name));
320 frontend::Alert::error(_("Could not load class"), s);
327 LayoutFileIndex defaultBaseclass()
329 if (LayoutFileList::get().haveClass("article"))
330 return string("article");
331 if (LayoutFileList::get().empty())
332 // we'll call it that, since this gives the user a chance to
333 // have a functioning document when things improve.
334 return string("article");
335 return LayoutFileList::get().classList().front();