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/assert.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;
51 LayoutFileList & LayoutFileList::get()
53 static LayoutFileList baseclasslist;
58 bool LayoutFileList::haveClass(string const & classname) const
60 ClassMap::const_iterator it = classmap_.begin();
61 ClassMap::const_iterator en = classmap_.end();
62 for (; it != en; ++it) {
63 if (it->first == classname)
70 LayoutFile const & LayoutFileList::operator[](string const & classname) const
72 LASSERT(haveClass(classname), /**/);
73 return *classmap_[classname];
78 LayoutFileList::operator[](string const & classname)
80 LASSERT(haveClass(classname), /**/);
81 return *classmap_[classname];
85 // Reads LyX textclass definitions according to textclass config file
86 bool LayoutFileList::read()
89 FileName const real_file = libFileSearch("", "textclass.lst");
90 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
92 if (real_file.empty()) {
93 lyxerr << "LayoutFileList::Read: unable to find "
95 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
96 << "'. Exiting." << endl;
98 // This causes LyX to end... Not a desirable behaviour. Lgb
99 // What do you propose? That the user gets a file dialog
100 // and is allowed to hunt for the file? (Asger)
101 // more that we have a layout for minimal.cls statically
102 // compiled in... (Lgb)
105 if (!lex.setFile(real_file)) {
106 lyxerr << "LayoutFileList::Read: "
107 "lyxlex was not able to set file: "
108 << real_file << endl;
112 lyxerr << "LayoutFileList::Read: unable to open "
114 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
115 << "'\nCheck your installation. LyX can't continue."
120 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);
133 string const clname = lex.getString();
134 LYXERR(Debug::TCLASS, "Clname: " << clname);
136 string const desc = lex.getString();
137 LYXERR(Debug::TCLASS, "Desc: " << desc);
139 bool avail = lex.getBool();
140 LYXERR(Debug::TCLASS, "Avail: " << avail);
141 // This code is run when we have
142 // fname, clname, desc, and avail
143 LayoutFile * tmpl = new LayoutFile(fname, clname, desc, avail);
144 if (lyxerr.debugging(Debug::TCLASS)) {
145 // only system layout files are loaded here so no
146 // buffer path is needed.
149 classmap_[fname] = tmpl;
155 LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
157 // lyx will start with an empty classmap_, but only reconfigure is allowed
158 // in this case. This gives users a second chance to configure lyx if
159 // initial configuration fails. (c.f. bug 2829)
160 if (classmap_.empty())
161 lyxerr << "LayoutFileList::Read: no textclasses found!"
167 std::vector<LayoutFileIndex> LayoutFileList::classList() const
169 std::vector<LayoutFileIndex> cl;
170 ClassMap::const_iterator it = classmap_.begin();
171 ClassMap::const_iterator en = classmap_.end();
172 for (; it != en; ++it)
173 cl.push_back(it->first);
178 void LayoutFileList::reset(LayoutFileIndex const & classname) {
179 LASSERT(haveClass(classname), /**/);
180 LayoutFile * tc = classmap_[classname];
182 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
183 tc->isTeXClassAvailable());
184 classmap_[classname] = tmpl;
189 string const LayoutFileList::localPrefix = "LOCAL:";
190 string const LayoutFileList::embeddedPrefix = "EMBED:";
194 LayoutFileList::addLayoutFile(string const & textclass, string const & path,
197 // FIXME There is a bug here: 4593
199 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
200 // NOTE: latex class name is defined in textclass.layout, which can be
201 // different from textclass
202 string fullName = addName(path, textclass + ".layout");
206 localIndex = localPrefix + fullName;
207 else if (type == Embedded)
208 localIndex = embeddedPrefix + textclass;
210 // if the local file has already been loaded, return it
211 if (haveClass(localIndex))
214 FileName const layout_file(fullName);
215 if (layout_file.exists()) {
216 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
217 // Read .layout file and get description, real latex classname etc
219 // This is a C++ version of function processLayoutFile in configure.py,
220 // which uses the following regex
221 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
222 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
223 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
224 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
226 while (getline(ifs, line)) {
227 // look for the \DeclareXXXClass line
229 if (regex_match(line, sub, reg)) {
230 // returns: whole string, classtype (not used here), class name, description
231 LASSERT(sub.size() == 4, /**/);
232 // now, create a TextClass with description containing path information
233 string className(sub.str(2) == "" ? textclass : sub.str(2));
235 new LayoutFile(textclass, className, localIndex, true);
236 // This textclass is added on request so it will definitely be
237 // used. Load it now because other load() calls may fail if they
238 // are called in a context without buffer path information.
240 classmap_[localIndex] = tmpl;
245 // If .layout is not in local directory, or an invalid layout is found, return null
250 LayoutFileIndex defaultBaseclass()
252 if (LayoutFileList::get().haveClass("article"))
253 return string("article");
254 if (LayoutFileList::get().empty())
256 return LayoutFileList::get().classList().front();
261 // Reads the style files
264 LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
266 if (!LayoutFileList::get().read()) {
267 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
268 "during parsing.\n Exiting.");
272 LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");