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)
187 LASSERT(haveClass(classname), /**/);
188 LayoutFile * tc = classmap_[classname];
190 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
191 tc->prerequisites(), tc->category(),
192 tc->isTeXClassAvailable());
193 classmap_[classname] = tmpl;
205 "DefaultStyle Standard\n\n"
207 " Category MainText\n"
209 " LatexType Paragraph\n"
214 " AlignPossible Block, Left, Right, Center\n"
215 " LabelType No_Label\n"
221 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
223 FileName const tempLayout = FileName::tempName("basic_layout");
224 ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
225 // This writes a very basic class, but it also attempts to include
226 // stdclass.inc. That would give us something moderately usable.
227 ofs << "# This layout is automatically generated\n"
228 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
229 "Format " << LAYOUT_FORMAT << "\n"
230 "Input stdclass.inc\n\n"
234 // We do not know if a LaTeX class is available for this document, but setting
235 // the last parameter to true will suppress a warning message about missing
237 LayoutFile * tc = new LayoutFile(textclass, textclass,
238 "Unknown text class " + textclass, textclass + ".cls", "", true);
240 if (!tc->load(tempLayout.absFileName())) {
241 // The only way this happens is because the hardcoded layout file
242 // above is wrong or stdclass.inc cannot be found. So try again
243 // without stdclass.inc and without stdinsets.inc.
244 ofstream ofs2(tempLayout.toFilesystemEncoding().c_str());
245 ofs2 << "# This layout is automatically generated\n"
246 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
247 "Format " << LAYOUT_FORMAT << "\n"
248 "Provides stdinsets 1\n"
251 if (!tc->load(tempLayout.absFileName())) {
252 // This can only happen if the hardcoded file above is wrong.
253 LASSERT(false, /* */);
257 classmap_[textclass] = tc;
262 LayoutFileIndex LayoutFileList::addLocalLayout(
263 string const & textclass, string const & path)
265 // FIXME There is a bug here: 4593
267 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
268 // NOTE: latex class name is defined in textclass.layout, which can be
269 // different from textclass
270 string fullName = addName(path, textclass + ".layout");
272 FileName const layout_file(fullName);
273 if (layout_file.exists()) {
274 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
275 // Read .layout file and get description, real latex classname etc
277 // This is a C++ version of function processLayoutFile in configure.py,
278 // which uses the following regex
279 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
280 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
281 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
282 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
283 static regex const catreg("^#\\s*\\\\DeclareCategory\\{(.*)\\}");
288 bool have_declaration = false;
289 while (getline(ifs, line)) {
290 // look for the \DeclareXXXClass line
292 if (regex_match(line, sub, reg)) {
293 // returns: whole string, classtype (not used here), class name, description
294 LASSERT(sub.size() == 4, /**/);
295 // now, create a TextClass with description containing path information
296 class_name = (sub.str(2) == "" ? textclass : sub.str(2));
297 class_prereq = class_name + ".cls";
298 have_declaration = true;
300 else if (regex_match(line, sub, catreg)) {
301 category = sub.str(1);
303 if (have_declaration && !category.empty())
306 if (have_declaration) {
308 new LayoutFile(textclass, class_name, textclass, class_prereq, category, true);
309 //FIXME: The prerequisites are available from the layout file and
310 // can be extracted from the above regex, but for now this
311 // field is simply set to class_name + ".cls"
312 // This textclass is added on request so it will definitely be
313 // used. Load it now because other load() calls may fail if they
314 // are called in a context without buffer path information.
316 // There will be only one textclass with this name, even if different
317 // layout files are loaded from different directories.
318 if (haveClass(textclass)) {
319 LYXERR0("Existing textclass " << textclass << " is redefined by " << fullName);
320 delete classmap_[textclass];
322 classmap_[textclass] = tmpl;
326 // If .layout is not in local directory, or an invalid layout
327 // is found, return null
332 bool LayoutFileList::load(string const & name, string const & buf_path)
334 if (!haveClass(name)) {
335 LYXERR0("Document class \"" << name << "\" does not exist.");
339 LayoutFile * tc = classmap_[name];
340 return tc->load(buf_path);
344 LayoutFileIndex defaultBaseclass()
346 if (LayoutFileList::get().haveClass("article"))
347 return string("article");
348 if (LayoutFileList::get().empty())
349 // we'll call it that, since this gives the user a chance to
350 // have a functioning document when things improve.
351 return string("article");
352 return LayoutFileList::get().classList().front();