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 "support/lassert.h"
22 #include "support/debug.h"
23 #include "support/FileName.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
27 #include <boost/bind.hpp>
28 #include <boost/regex.hpp>
33 using namespace lyx::support;
41 LayoutFile::LayoutFile(string const & fn, string const & cln,
42 string const & desc, bool texClassAvail )
47 texClassAvail_ = texClassAvail;
50 LayoutFileList::~LayoutFileList()
52 ClassMap::const_iterator it = classmap_.begin();
53 ClassMap::const_iterator en = classmap_.end();
54 for (; it != en; ++it) {
59 LayoutFileList & LayoutFileList::get()
61 static LayoutFileList baseclasslist;
66 bool LayoutFileList::haveClass(string const & classname) const
68 ClassMap::const_iterator it = classmap_.begin();
69 ClassMap::const_iterator en = classmap_.end();
70 for (; it != en; ++it) {
71 if (it->first == classname)
78 LayoutFile const & LayoutFileList::operator[](string const & classname) const
80 LASSERT(haveClass(classname), /**/);
81 return *classmap_[classname];
86 LayoutFileList::operator[](string const & classname)
88 LASSERT(haveClass(classname), /**/);
89 return *classmap_[classname];
93 // Reads LyX textclass definitions according to textclass config file
94 bool LayoutFileList::read()
97 FileName const real_file = libFileSearch("", "textclass.lst");
98 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
100 if (real_file.empty()) {
101 lyxerr << "LayoutFileList::Read: unable to find "
103 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
104 << "'. Exiting." << endl;
106 // This causes LyX to end... Not a desirable behaviour. Lgb
107 // What do you propose? That the user gets a file dialog
108 // and is allowed to hunt for the file? (Asger)
109 // more that we have a layout for minimal.cls statically
110 // compiled in... (Lgb)
113 if (!lex.setFile(real_file)) {
114 lyxerr << "LayoutFileList::Read: "
115 "lyxlex was not able to set file: "
116 << real_file << endl;
120 lyxerr << "LayoutFileList::Read: unable to open "
122 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
123 << "'\nCheck your installation. LyX can't continue."
128 bool finished = false;
130 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
131 while (lex.isOK() && !finished) {
132 LYXERR(Debug::TCLASS, "\tline by line");
134 case Lexer::LEX_FEOF:
138 string const fname = lex.getString();
139 LYXERR(Debug::TCLASS, "Fname: " << fname);
141 string const clname = lex.getString();
142 LYXERR(Debug::TCLASS, "Clname: " << clname);
144 string const desc = lex.getString();
145 LYXERR(Debug::TCLASS, "Desc: " << desc);
147 bool avail = lex.getBool();
148 LYXERR(Debug::TCLASS, "Avail: " << avail);
149 // This code is run when we have
150 // fname, clname, desc, and avail
151 LayoutFile * tmpl = new LayoutFile(fname, clname, desc, 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;
163 LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
165 // lyx will start with an empty classmap_, but only reconfigure is allowed
166 // in this case. This gives users a second chance to configure lyx if
167 // initial configuration fails. (c.f. bug 2829)
168 if (classmap_.empty())
169 lyxerr << "LayoutFileList::Read: no textclasses found!"
175 std::vector<LayoutFileIndex> LayoutFileList::classList() const
177 std::vector<LayoutFileIndex> cl;
178 ClassMap::const_iterator it = classmap_.begin();
179 ClassMap::const_iterator en = classmap_.end();
180 for (; it != en; ++it)
181 cl.push_back(it->first);
186 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->isTeXClassAvailable());
192 classmap_[classname] = tmpl;
197 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
199 if (haveClass(textclass))
202 FileName const tempLayout = FileName::tempName();
203 ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
204 ofs << "# This layout is automatically generated\n"
205 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
207 "Input stdclass.inc\n\n"
212 "DefaultStyle Standard\n\n"
214 " Category MainText\n"
216 " LatexType Paragraph\n"
221 " AlignPossible Block, Left, Right, Center\n"
222 " LabelType No_Label\n"
226 // We do not know if a LaTeX class is available for this document, but setting
227 // the last parameter to true will suppress a warning message about missing
229 LayoutFile * tc = new LayoutFile(textclass, textclass, "Unknown text class " + textclass, true);
230 if (!tc->load(tempLayout.absFilename()))
231 // The only way this happens is because the hardcoded layout file above
233 LASSERT(false, /**/);
234 classmap_[textclass] = tc;
240 LayoutFileList::addLocalLayout(string const & textclass, string const & path)
242 // FIXME There is a bug here: 4593
244 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
245 // NOTE: latex class name is defined in textclass.layout, which can be
246 // different from textclass
247 string fullName = addName(path, textclass + ".layout");
249 FileName const layout_file(fullName);
250 if (layout_file.exists()) {
251 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
252 // Read .layout file and get description, real latex classname etc
254 // This is a C++ version of function processLayoutFile in configure.py,
255 // which uses the following regex
256 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
257 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
258 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
259 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
261 while (getline(ifs, line)) {
262 // look for the \DeclareXXXClass line
264 if (regex_match(line, sub, reg)) {
265 // returns: whole string, classtype (not used here), class name, description
266 LASSERT(sub.size() == 4, /**/);
267 // now, create a TextClass with description containing path information
268 string className(sub.str(2) == "" ? textclass : sub.str(2));
270 new LayoutFile(textclass, className, textclass, true);
271 // This textclass is added on request so it will definitely be
272 // used. Load it now because other load() calls may fail if they
273 // are called in a context without buffer path information.
275 // There will be only one textclass with this name, even if different
276 // layout files are loaded from different directories.
277 if (haveClass(textclass)) {
278 LYXERR0("Existing textclass " << textclass << " is redefined by " << fullName);
279 delete classmap_[textclass];
281 classmap_[textclass] = tmpl;
286 // If .layout is not in local directory, or an invalid layout is found, return null
291 LayoutFileIndex defaultBaseclass()
293 if (LayoutFileList::get().haveClass("article"))
294 return string("article");
295 if (LayoutFileList::get().empty())
297 return LayoutFileList::get().classList().front();
302 // Reads the style files
305 LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
307 if (!LayoutFileList::get().read()) {
308 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
309 "during parsing.\n Exiting.");
313 LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");