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 // FIXME This could be simplified a bit to call TextClass::read(string, ReadType).
225 FileName const tempLayout = FileName::tempName("basic_layout");
226 ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
227 // This writes a very basic class, but it also attempts to include
228 // stdclass.inc. That would give us something moderately usable.
229 ofs << "# This layout is automatically generated\n"
230 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
231 "Format " << LAYOUT_FORMAT << "\n"
232 "Input stdclass.inc\n\n"
236 // We do not know if a LaTeX class is available for this document, but setting
237 // the last parameter to true will suppress a warning message about missing
239 LayoutFile * tc = new LayoutFile(textclass, textclass,
240 "Unknown text class " + textclass, textclass + ".cls", "", true);
242 if (!tc->load(tempLayout.absFileName())) {
243 // The only way this happens is because the hardcoded layout file
244 // above is wrong or stdclass.inc cannot be found. So try again
245 // without stdclass.inc and without stdinsets.inc.
246 ofstream ofs2(tempLayout.toFilesystemEncoding().c_str());
247 ofs2 << "# This layout is automatically generated\n"
248 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
249 "Format " << LAYOUT_FORMAT << "\n"
250 "Provides stdinsets 1\n"
253 if (!tc->load(tempLayout.absFileName())) {
254 // This can only happen if the hardcoded file above is wrong
255 // or there is some weird filesystem error.
256 LASSERT(false, /* */);
260 classmap_[textclass] = tc;
265 LayoutFileIndex LayoutFileList::addLocalLayout(
266 string const & textclass, string const & path)
268 // FIXME There is a bug here: 4593
270 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
271 // NOTE: latex class name is defined in textclass.layout, which can be
272 // different from textclass
273 string fullName = addName(path, textclass + ".layout");
275 FileName const layout_file(fullName);
276 if (!layout_file.exists())
279 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
280 // Read .layout file and get description, real latex classname etc
282 // This is a C++ version of function processLayoutFile in configure.py,
283 // which uses the following regex
284 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
285 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
286 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
287 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
288 static regex const catreg("^#\\s*\\\\DeclareCategory\\{(.*)\\}");
293 bool have_declaration = false;
294 while (getline(ifs, line)) {
295 // look for the \DeclareXXXClass line
297 if (regex_match(line, sub, reg)) {
298 // returns: whole string, classtype (not used here), class name, description
299 LASSERT(sub.size() == 4, /**/);
300 // now, create a TextClass with description containing path information
301 class_name = (sub.str(2) == "" ? textclass : sub.str(2));
302 class_prereq = class_name + ".cls";
303 have_declaration = true;
305 else if (regex_match(line, sub, catreg)) {
306 category = sub.str(1);
308 if (have_declaration && !category.empty())
312 if (!have_declaration)
316 new LayoutFile(textclass, class_name, textclass, class_prereq, category, true);
317 //FIXME: The prerequisites are available from the layout file and
318 // can be extracted from the above regex, but for now this
319 // field is simply set to class_name + ".cls"
320 // This textclass is added on request so it will definitely be
321 // used. Load it now because other load() calls may fail if they
322 // are called in a context without buffer path information.
324 // There will be only one textclass with this name, even if different
325 // layout files are loaded from different directories.
326 if (haveClass(textclass)) {
327 LYXERR0("Existing textclass " << textclass << " is redefined by " << fullName);
328 delete classmap_[textclass];
330 classmap_[textclass] = tmpl;
335 bool LayoutFileList::load(string const & name, string const & buf_path)
337 if (!haveClass(name)) {
338 LYXERR0("Document class \"" << name << "\" does not exist.");
342 LayoutFile * tc = classmap_[name];
343 return tc->load(buf_path);
347 LayoutFileIndex defaultBaseclass()
349 if (LayoutFileList::get().haveClass("article"))
350 return string("article");
351 if (LayoutFileList::get().empty())
352 // we'll call it that, since this gives the user a chance to
353 // have a functioning document when things improve.
354 return string("article");
355 return LayoutFileList::get().classList().front();