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)
46 name_ = onlyFileName(fn);
47 path_ = fn.rfind('/') == string::npos ? string() : onlyPath(fn);
50 prerequisites_ = prereq;
52 tex_class_avail_ = texclassavail;
56 LayoutFileList::~LayoutFileList()
58 ClassMap::const_iterator it = classmap_.begin();
59 ClassMap::const_iterator en = classmap_.end();
60 for (; it != en; ++it) {
66 LayoutFileList & LayoutFileList::get()
68 static LayoutFileList baseclasslist;
73 bool LayoutFileList::haveClass(string const & classname) const
75 ClassMap::const_iterator it = classmap_.begin();
76 ClassMap::const_iterator en = classmap_.end();
77 for (; it != en; ++it) {
78 if (it->first == classname)
85 LayoutFile const & LayoutFileList::operator[](string const & classname) const
87 LATTEST(haveClass(classname));
88 // safe to continue, since we will make an empty LayoutFile
89 return *classmap_[classname];
93 LayoutFile & LayoutFileList::operator[](string const & classname)
95 LATTEST(haveClass(classname));
96 // safe to continue, since we will make an empty LayoutFile
97 return *classmap_[classname];
101 // Reads LyX textclass definitions according to textclass config file
102 bool LayoutFileList::read()
104 bool success = false;
106 FileName const real_file = libFileSearch("", "textclass.lst");
107 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << "'.");
109 if (real_file.empty()) {
110 LYXERR0("LayoutFileList::Read: unable to find textclass file "
111 << "`textclass.lst'.");
112 } else if (!lex.setFile(real_file)) {
113 LYXERR0("LayoutFileList::Read: lyxlex was not able to set file: "
114 << real_file << '.');
115 } else if (!lex.isOK()) {
116 LYXERR0("LayoutFileList::Read: unable to open textclass file `"
117 << makeDisplayPath(real_file.absFileName(), 1000)
118 << "'\nCheck your installation.");
120 // we have a file we can read.
121 bool finished = false;
122 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
123 while (lex.isOK() && !finished) {
124 LYXERR(Debug::TCLASS, "\tline by line");
126 case Lexer::LEX_FEOF:
130 string const fname = lex.getString();
131 LYXERR(Debug::TCLASS, "Fname: " << fname);
134 string const clname = lex.getString();
135 LYXERR(Debug::TCLASS, "Clname: " << clname);
138 string const desc = lex.getString();
139 LYXERR(Debug::TCLASS, "Desc: " << desc);
142 bool avail = lex.getBool();
143 LYXERR(Debug::TCLASS, "Avail: " << avail);
146 string const prereq = lex.getString();
147 LYXERR(Debug::TCLASS, "Prereq: " << prereq);
150 string const category = lex.getString();
151 LYXERR(Debug::TCLASS, "Category: " << category);
152 // This code is run when we have
153 // fname, clname, desc, prereq, and avail
154 LayoutFile * tmpl = new LayoutFile(fname, clname, desc, prereq, category, avail);
155 if (lyxerr.debugging(Debug::TCLASS)) {
156 // only system layout files are loaded here so no
157 // buffer path is needed.
160 classmap_[fname] = tmpl;
162 } // end of while loop
163 LYXERR(Debug::TCLASS, "End parsing of textclass.lst");
167 // LyX will start with an empty classmap_. This is OK because
168 // (a) we will give the user a chance to reconfigure (see bug 2829) and
169 // (b) even if that fails, we can use addEmptyClass() to get some basic
171 if (classmap_.empty())
172 LYXERR0("LayoutFileList::Read: no textclasses found!");
177 std::vector<LayoutFileIndex> LayoutFileList::classList() const
179 std::vector<LayoutFileIndex> cl;
180 ClassMap::const_iterator it = classmap_.begin();
181 ClassMap::const_iterator en = classmap_.end();
182 for (; it != en; ++it)
183 cl.push_back(it->first);
188 void LayoutFileList::reset(LayoutFileIndex const & classname)
190 LATTEST(haveClass(classname));
191 // safe to continue, since we will make an empty LayoutFile
192 LayoutFile * tc = classmap_[classname];
194 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
195 tc->prerequisites(), tc->category(),
196 tc->isTeXClassAvailable());
197 classmap_[classname] = tmpl;
209 "DefaultStyle Standard\n\n"
211 " Category MainText\n"
213 " LatexType Paragraph\n"
218 " AlignPossible Block, Left, Right, Center\n"
219 " LabelType No_Label\n"
225 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
227 // FIXME This could be simplified a bit to call TextClass::read(string, ReadType).
229 TempFile tempfile("basicXXXXXX.layout");
230 FileName const tempLayout = tempfile.name();
231 ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
232 // This writes a very basic class, but it also attempts to include
233 // stdclass.inc. That would give us something moderately usable.
234 ofs << "# This layout is automatically generated\n"
235 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
236 "Format " << LAYOUT_FORMAT << "\n"
237 "Input stdclass.inc\n\n"
241 // We do not know if a LaTeX class is available for this document, but setting
242 // the last parameter to true will suppress a warning message about missing
244 LayoutFile * tc = new LayoutFile(textclass, textclass,
245 "Unknown text class " + textclass, textclass + ".cls", "", true);
247 if (!tc->load(tempLayout.absFileName())) {
248 // The only way this happens is because the hardcoded layout file
249 // above is wrong or stdclass.inc cannot be found. So try again
250 // without stdclass.inc and without stdinsets.inc.
251 ofstream ofs2(tempLayout.toFilesystemEncoding().c_str());
252 ofs2 << "# This layout is automatically generated\n"
253 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
254 "Format " << LAYOUT_FORMAT << "\n"
255 "Provides stdinsets 1\n"
258 if (!tc->load(tempLayout.absFileName())) {
259 // This can only happen if the hardcoded file above is wrong
260 // or there is some weird filesystem error.
261 LATTEST(false); // We will get an empty layout or something.
265 classmap_[textclass] = tc;
270 LayoutFileIndex LayoutFileList::addLocalLayout(
271 string const & textclass, string const & path, string const & oldpath)
273 // FIXME There is a bug here: 4593
275 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
276 // NOTE: latex class name is defined in textclass.layout, which can be
277 // different from textclass
278 string fullName = addName(path, textclass + ".layout");
280 FileName layout_file(fullName);
283 if (!layout_file.exists()) {
286 // The document has been moved to a different directory.
287 // However, oldpath always points to the right spot, unless
288 // the user also moved the layout file.
289 fullName = addName(oldpath, textclass + ".layout");
290 layout_file.set(fullName);
291 layout_file.refresh();
292 if (!layout_file.exists())
297 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
298 // Read .layout file and get description, real latex classname etc
300 // This is a C++ version of function processLayoutFile in configure.py,
301 // which uses the following regex
302 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
303 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
304 static regex const reg("^\\s*#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
305 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
306 static regex const catreg("^\\s*#\\s*\\\\DeclareCategory\\{(.*)\\}\\s*");
311 bool have_declaration = false;
312 while (getline(ifs, line)) {
313 // look for the \DeclareXXXClass line
315 if (regex_match(line, sub, reg)) {
316 // returns: whole string, classtype (not used here), class name, description
317 // LASSERT: Why would this fail?
318 LASSERT(sub.size() == 4, /**/);
319 // now, create a TextClass with description containing path information
320 class_name = (sub.str(2) == "" ? textclass : sub.str(2));
321 class_prereq = class_name + ".cls";
322 have_declaration = true;
324 else if (regex_match(line, sub, catreg)) {
325 category = sub.str(1);
327 if (have_declaration && !category.empty())
331 if (!have_declaration)
335 new LayoutFile(addName(moved ? oldpath : path, textclass),
336 class_name, textclass, class_prereq, category, true);
337 //FIXME: The prerequisites are available from the layout file and
338 // can be extracted from the above regex, but for now this
339 // field is simply set to class_name + ".cls"
340 // This textclass is added on request so it will definitely be
341 // used. Load it now because other load() calls may fail if they
342 // are called in a context without buffer path information.
343 tmpl->load(moved ? oldpath : path);
344 // There will be only one textclass with this name, even if different
345 // layout files are loaded from different directories.
346 if (haveClass(textclass)) {
347 // Unconditionally issuing the warning may be confusing when
348 // saving the document with a different name, as it is exactly
349 // the same textclass that is being re-established.
350 LYXERR(Debug::TCLASS, "Existing textclass " << textclass << " is redefined by " << fullName);
351 delete classmap_[textclass];
353 classmap_[textclass] = tmpl;
354 return removeExtension(fullName);
358 bool LayoutFileList::load(string const & name, string const & buf_path)
360 if (!haveClass(name)) {
361 LYXERR0("Document class \"" << name << "\" does not exist.");
365 LayoutFile * tc = classmap_[name];
366 return tc->load(buf_path);
370 LayoutFileIndex defaultBaseclass()
372 if (LayoutFileList::get().haveClass("article"))
373 return string("article");
374 if (LayoutFileList::get().empty())
375 // we'll call it that, since this gives the user a chance to
376 // have a functioning document when things improve.
377 return string("article");
378 return LayoutFileList::get().classList().front();