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/regex.h"
31 #include "support/TempFile.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 LATTEST(haveClass(classname));
87 // safe to continue, since we will make an empty LayoutFile
88 return *classmap_[classname];
92 LayoutFile & LayoutFileList::operator[](string const & classname)
94 LATTEST(haveClass(classname));
95 // safe to continue, since we will make an empty LayoutFile
96 return *classmap_[classname];
100 // Reads LyX textclass definitions according to textclass config file
101 bool LayoutFileList::read()
103 bool success = false;
105 FileName const real_file = libFileSearch("", "textclass.lst");
106 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << "'.");
108 if (real_file.empty()) {
109 LYXERR0("LayoutFileList::Read: unable to find textclass file "
110 << "`textclass.lst'.");
111 } else if (!lex.setFile(real_file)) {
112 LYXERR0("LayoutFileList::Read: lyxlex was not able to set file: "
113 << real_file << '.');
114 } else if (!lex.isOK()) {
115 LYXERR0("LayoutFileList::Read: unable to open textclass file `"
116 << makeDisplayPath(real_file.absFileName(), 1000)
117 << "'\nCheck your installation.");
119 // we have a file we can read.
120 bool finished = false;
121 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
122 while (lex.isOK() && !finished) {
123 LYXERR(Debug::TCLASS, "\tline by line");
125 case Lexer::LEX_FEOF:
129 string const fname = lex.getString();
130 LYXERR(Debug::TCLASS, "Fname: " << fname);
133 string const clname = lex.getString();
134 LYXERR(Debug::TCLASS, "Clname: " << clname);
137 string const desc = lex.getString();
138 LYXERR(Debug::TCLASS, "Desc: " << desc);
141 bool avail = lex.getBool();
142 LYXERR(Debug::TCLASS, "Avail: " << avail);
145 string const prereq = lex.getString();
146 LYXERR(Debug::TCLASS, "Prereq: " << prereq);
149 string const category = lex.getString();
150 LYXERR(Debug::TCLASS, "Category: " << category);
151 // This code is run when we have
152 // fname, clname, desc, prereq, and avail
153 LayoutFile * tmpl = new LayoutFile(fname, clname, desc, prereq, category, avail);
154 if (lyxerr.debugging(Debug::TCLASS)) {
155 // only system layout files are loaded here so no
156 // buffer path is needed.
159 classmap_[fname] = tmpl;
161 } // end of while loop
162 LYXERR(Debug::TCLASS, "End parsing of textclass.lst");
166 // LyX will start with an empty classmap_. This is OK because
167 // (a) we will give the user a chance to reconfigure (see bug 2829) and
168 // (b) even if that fails, we can use addEmptyClass() to get some basic
170 if (classmap_.empty())
171 LYXERR0("LayoutFileList::Read: no textclasses found!");
176 std::vector<LayoutFileIndex> LayoutFileList::classList() const
178 std::vector<LayoutFileIndex> cl;
179 ClassMap::const_iterator it = classmap_.begin();
180 ClassMap::const_iterator en = classmap_.end();
181 for (; it != en; ++it)
182 cl.push_back(it->first);
187 void LayoutFileList::reset(LayoutFileIndex const & classname)
189 LATTEST(haveClass(classname));
190 // safe to continue, since we will make an empty LayoutFile
191 LayoutFile * tc = classmap_[classname];
193 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
194 tc->prerequisites(), tc->category(),
195 tc->isTeXClassAvailable());
196 classmap_[classname] = tmpl;
208 "DefaultStyle Standard\n\n"
210 " Category MainText\n"
212 " LatexType Paragraph\n"
217 " AlignPossible Block, Left, Right, Center\n"
218 " LabelType No_Label\n"
224 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
226 // FIXME This could be simplified a bit to call TextClass::read(string, ReadType).
228 TempFile tempfile("basicXXXXXX.layout");
229 FileName const tempLayout = tempfile.name();
230 ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
231 // This writes a very basic class, but it also attempts to include
232 // stdclass.inc. That would give us something moderately usable.
233 ofs << "# This layout is automatically generated\n"
234 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
235 "Format " << LAYOUT_FORMAT << "\n"
236 "Input stdclass.inc\n\n"
240 // We do not know if a LaTeX class is available for this document, but setting
241 // the last parameter to true will suppress a warning message about missing
243 LayoutFile * tc = new LayoutFile(textclass, textclass,
244 "Unknown text class " + textclass, textclass + ".cls", "", true);
246 if (!tc->load(tempLayout.absFileName())) {
247 // The only way this happens is because the hardcoded layout file
248 // above is wrong or stdclass.inc cannot be found. So try again
249 // without stdclass.inc and without stdinsets.inc.
250 ofstream ofs2(tempLayout.toFilesystemEncoding().c_str());
251 ofs2 << "# This layout is automatically generated\n"
252 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
253 "Format " << LAYOUT_FORMAT << "\n"
254 "Provides stdinsets 1\n"
257 if (!tc->load(tempLayout.absFileName())) {
258 // This can only happen if the hardcoded file above is wrong
259 // or there is some weird filesystem error.
260 LATTEST(false); // We will get an empty layout or something.
264 classmap_[textclass] = tc;
269 LayoutFileIndex LayoutFileList::addLocalLayout(
270 string const & textclass, string const & path, string const & oldpath)
272 // FIXME There is a bug here: 4593
274 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
275 // NOTE: latex class name is defined in textclass.layout, which can be
276 // different from textclass
277 string fullName = addName(path, textclass + ".layout");
279 FileName layout_file(fullName);
282 if (!layout_file.exists()) {
285 // The document has been moved to a different directory.
286 // However, oldpath always points to the right spot, unless
287 // the user also moved the layout file.
288 fullName = addName(oldpath, textclass + ".layout");
289 layout_file.set(fullName);
290 layout_file.refresh();
291 if (!layout_file.exists())
296 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
297 // Read .layout file and get description, real latex classname etc
299 // This is a C++ version of function processLayoutFile in configure.py,
300 // which uses the following regex
301 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
302 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
303 static regex const reg("^\\s*#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
304 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
305 static regex const catreg("^\\s*#\\s*\\\\DeclareCategory\\{(.*)\\}\\s*");
310 bool have_declaration = false;
311 while (getline(ifs, line)) {
312 // look for the \DeclareXXXClass line
314 if (regex_match(line, sub, reg)) {
315 // returns: whole string, classtype (not used here), class name, description
316 // LASSERT: Why would this fail?
317 LASSERT(sub.size() == 4, /**/);
318 // now, create a TextClass with description containing path information
319 class_name = (sub.str(2) == "" ? textclass : sub.str(2));
320 class_prereq = class_name + ".cls";
321 have_declaration = true;
323 else if (regex_match(line, sub, catreg)) {
324 category = sub.str(1);
326 if (have_declaration && !category.empty())
330 if (!have_declaration)
334 new LayoutFile(textclass, class_name, textclass, class_prereq, category, true);
335 //FIXME: The prerequisites are available from the layout file and
336 // can be extracted from the above regex, but for now this
337 // field is simply set to class_name + ".cls"
338 // This textclass is added on request so it will definitely be
339 // used. Load it now because other load() calls may fail if they
340 // are called in a context without buffer path information.
341 tmpl->load(moved ? oldpath : path);
342 // There will be only one textclass with this name, even if different
343 // layout files are loaded from different directories.
344 if (haveClass(textclass)) {
345 // Unconditionally issuing the warning may be confusing when
346 // saving the document with a different name, as it is exactly
347 // the same textclass that is being re-established.
348 LYXERR(Debug::TCLASS, "Existing textclass " << textclass << " is redefined by " << fullName);
349 delete classmap_[textclass];
351 classmap_[textclass] = tmpl;
352 return removeExtension(fullName);
356 bool LayoutFileList::load(string const & name, string const & buf_path)
358 if (!haveClass(name)) {
359 LYXERR0("Document class \"" << name << "\" does not exist.");
363 LayoutFile * tc = classmap_[name];
364 return tc->load(buf_path);
368 LayoutFileIndex defaultBaseclass()
370 if (LayoutFileList::get().haveClass("article"))
371 return string("article");
372 if (LayoutFileList::get().empty())
373 // we'll call it that, since this gives the user a chance to
374 // have a functioning document when things improve.
375 return string("article");
376 return LayoutFileList::get().classList().front();