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,
44 string const & category, bool texclassavail)
49 prerequisites_ = prereq;
51 tex_class_avail_ = texclassavail;
55 LayoutFileList::~LayoutFileList()
57 ClassMap::const_iterator it = classmap_.begin();
58 ClassMap::const_iterator en = classmap_.end();
59 for (; it != en; ++it) {
65 LayoutFileList & LayoutFileList::get()
67 static LayoutFileList baseclasslist;
72 bool LayoutFileList::haveClass(string const & classname) const
74 ClassMap::const_iterator it = classmap_.begin();
75 ClassMap::const_iterator en = classmap_.end();
76 for (; it != en; ++it) {
77 if (it->first == classname)
84 LayoutFile const & LayoutFileList::operator[](string const & classname) const
86 LASSERT(haveClass(classname), /**/);
87 return *classmap_[classname];
91 LayoutFile & LayoutFileList::operator[](string const & classname)
93 LASSERT(haveClass(classname), /**/);
94 return *classmap_[classname];
98 // Reads LyX textclass definitions according to textclass config file
99 bool LayoutFileList::read()
101 bool success = false;
103 FileName const real_file = libFileSearch("", "textclass.lst");
104 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << "'.");
106 if (real_file.empty()) {
107 LYXERR0("LayoutFileList::Read: unable to find textclass file "
108 << "`textclass.lst'.");
109 } else if (!lex.setFile(real_file)) {
110 LYXERR0("LayoutFileList::Read: lyxlex was not able to set file: "
111 << real_file << '.');
112 } else if (!lex.isOK()) {
113 LYXERR0("LayoutFileList::Read: unable to open textclass file `"
114 << makeDisplayPath(real_file.absFileName(), 1000)
115 << "'\nCheck your installation.");
117 // we have a file we can read.
118 bool finished = false;
119 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
120 while (lex.isOK() && !finished) {
121 LYXERR(Debug::TCLASS, "\tline by line");
123 case Lexer::LEX_FEOF:
127 string const fname = lex.getString();
128 LYXERR(Debug::TCLASS, "Fname: " << fname);
131 string const clname = lex.getString();
132 LYXERR(Debug::TCLASS, "Clname: " << clname);
135 string const desc = lex.getString();
136 LYXERR(Debug::TCLASS, "Desc: " << desc);
139 bool avail = lex.getBool();
140 LYXERR(Debug::TCLASS, "Avail: " << avail);
143 string const prereq = lex.getString();
144 LYXERR(Debug::TCLASS, "Prereq: " << prereq);
147 string const category = lex.getString();
148 LYXERR(Debug::TCLASS, "Category: " << category);
149 // This code is run when we have
150 // fname, clname, desc, prereq, and avail
151 LayoutFile * tmpl = new LayoutFile(fname, clname, desc, prereq, category, avail);
152 if (lyxerr.debugging(Debug::TCLASS)) {
153 // only system layout files are loaded here so no
154 // buffer path is needed.
157 classmap_[fname] = tmpl;
159 } // end of while loop
160 LYXERR(Debug::TCLASS, "End parsing of textclass.lst");
164 // LyX will start with an empty classmap_. This is OK because
165 // (a) we will give the user a chance to reconfigure (see bug 2829) and
166 // (b) even if that fails, we can use addEmptyClass() to get some basic
168 if (classmap_.empty())
169 LYXERR0("LayoutFileList::Read: no textclasses found!");
174 std::vector<LayoutFileIndex> LayoutFileList::classList() const
176 std::vector<LayoutFileIndex> cl;
177 ClassMap::const_iterator it = classmap_.begin();
178 ClassMap::const_iterator en = classmap_.end();
179 for (; it != en; ++it)
180 cl.push_back(it->first);
185 void LayoutFileList::reset(LayoutFileIndex const & classname) {
186 LASSERT(haveClass(classname), /**/);
187 LayoutFile * tc = classmap_[classname];
189 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
190 tc->prerequisites(), tc->category(),
191 tc->isTeXClassAvailable());
192 classmap_[classname] = tmpl;
204 "DefaultStyle Standard\n\n"
206 " Category MainText\n"
208 " LatexType Paragraph\n"
213 " AlignPossible Block, Left, Right, Center\n"
214 " LabelType No_Label\n"
219 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
221 FileName const tempLayout = FileName::tempName("basic_layout");
222 ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
223 // This writes a very basic class, but it also attempts to include
224 // stdclass.inc. That would give us something moderately usable.
225 ofs << "# This layout is automatically generated\n"
226 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
227 "Format " << LAYOUT_FORMAT << "\n"
228 "Input stdclass.inc\n\n"
232 // We do not know if a LaTeX class is available for this document, but setting
233 // the last parameter to true will suppress a warning message about missing
235 LayoutFile * tc = new LayoutFile(textclass, textclass,
236 "Unknown text class " + textclass, textclass + ".cls", "", true);
238 if (!tc->load(tempLayout.absFileName())) {
239 // The only way this happens is because the hardcoded layout file
240 // above is wrong or stdclass.inc cannot be found. So try again
241 // without stdclass.inc and without stdinsets.inc.
242 ofstream ofs2(tempLayout.toFilesystemEncoding().c_str());
243 ofs2 << "# This layout is automatically generated\n"
244 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
245 "Format " << LAYOUT_FORMAT << "\n"
246 "Provides stdinsets 1\n"
249 if (!tc->load(tempLayout.absFileName())) {
250 // This can only happen if the hardcoded file above is wrong.
251 LASSERT(false, /* */);
255 classmap_[textclass] = tc;
261 LayoutFileList::addLocalLayout(string const & textclass, string const & path)
263 // FIXME There is a bug here: 4593
265 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
266 // NOTE: latex class name is defined in textclass.layout, which can be
267 // different from textclass
268 string fullName = addName(path, textclass + ".layout");
270 FileName const layout_file(fullName);
271 if (layout_file.exists()) {
272 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
273 // Read .layout file and get description, real latex classname etc
275 // This is a C++ version of function processLayoutFile in configure.py,
276 // which uses the following regex
277 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
278 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
279 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
280 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
281 static regex const catreg("^#\\s*\\\\DeclareCategory\\{(.*)\\}");
286 bool have_declaration = false;
287 while (getline(ifs, line)) {
288 // look for the \DeclareXXXClass line
290 if (regex_match(line, sub, reg)) {
291 // returns: whole string, classtype (not used here), class name, description
292 LASSERT(sub.size() == 4, /**/);
293 // now, create a TextClass with description containing path information
294 class_name = (sub.str(2) == "" ? textclass : sub.str(2));
295 class_prereq = class_name + ".cls";
296 have_declaration = true;
298 else if (regex_match(line, sub, catreg)) {
299 category = sub.str(1);
301 if (have_declaration && !category.empty())
304 if (have_declaration) {
306 new LayoutFile(textclass, class_name, textclass, class_prereq, category, true);
307 //FIXME: The prerequisites are available from the layout file and
308 // can be extracted from the above regex, but for now this
309 // field is simply set to class_name + ".cls"
310 // This textclass is added on request so it will definitely be
311 // used. Load it now because other load() calls may fail if they
312 // are called in a context without buffer path information.
314 // There will be only one textclass with this name, even if different
315 // layout files are loaded from different directories.
316 if (haveClass(textclass)) {
317 LYXERR0("Existing textclass " << textclass << " is redefined by " << fullName);
318 delete classmap_[textclass];
320 classmap_[textclass] = tmpl;
324 // If .layout is not in local directory, or an invalid layout is found, return null
329 bool LayoutFileList::load(string const & name, string const & buf_path)
331 if (!haveClass(name)) {
332 LYXERR0("Document class \"" << name << "\" does not exist.");
336 LayoutFile * tc = classmap_[name];
337 return tc->load(buf_path);
341 LayoutFileIndex defaultBaseclass()
343 if (LayoutFileList::get().haveClass("article"))
344 return string("article");
345 if (LayoutFileList::get().empty())
346 // we'll call it that, since this gives the user a chance to
347 // have a functioning document when things improve.
348 return string("article");
349 return LayoutFileList::get().classList().front();