2 * \file BaseClassList.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 "BaseClassList.h"
17 #include "FloatList.h"
19 #include "TextClass.h"
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/filetools.h"
24 #include "support/gettext.h"
26 #include <boost/bind.hpp>
27 #include <boost/regex.hpp>
32 using namespace lyx::support;
40 LayoutFile::LayoutFile(string const & fn, string const & cln,
41 string const & desc, bool texClassAvail )
46 texClassAvail_ = texClassAvail;
50 BaseClassList & BaseClassList::get()
52 static BaseClassList baseclasslist;
57 bool BaseClassList::haveClass(string const & classname) const
59 ClassMap::const_iterator it = classmap_.begin();
60 ClassMap::const_iterator en = classmap_.end();
61 for (; it != en; ++it) {
62 if (it->first == classname)
69 LayoutFile const & BaseClassList::operator[](string const & classname) const
71 BOOST_ASSERT(haveClass(classname));
72 return classmap_[classname];
76 // Gets a textclass structure from string
78 BaseClassList::operator[](string const & classname)
80 BOOST_ASSERT(haveClass(classname));
81 return classmap_[classname];
85 // Reads LyX textclass definitions according to textclass config file
86 bool BaseClassList::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 << "BaseClassList::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 << "BaseClassList::Read: "
107 "lyxlex was not able to set file: "
108 << real_file << endl;
112 lyxerr << "BaseClassList::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(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 << "BaseClassList::Read: no textclasses found!"
167 std::vector<LayoutFileIndex> BaseClassList::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 BaseClassList::reset(LayoutFileIndex const & classname) {
179 BOOST_ASSERT(haveClass(classname));
180 LayoutFile const & tc = classmap_[classname];
181 LayoutFile tmpl(tc.name(), tc.latexname(), tc.description(),
182 tc.isTeXClassAvailable());
183 classmap_[classname] = tmpl;
187 string const BaseClassList::localPrefix = "LOCAL:";
191 BaseClassList::addLayoutFile(string const & textclass, string const & path)
194 // There be bugs here. The way this presently works, the local class gets
195 // added to the global list of available document classes. It will then
196 // appear on the list in Document>Settings, where it could be chosen in,
197 // say, a new document, with no real warning that the class may not be
198 // available when the document is saved, since the new document may not be
199 // in the same directory as the layout file.
201 // Another bug is this: If the Document>Settings dialog is open when a file
202 // with a local layout is opened, the dialog doesn't update.
204 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
205 // NOTE: latex class name is defined in textclass.layout, which can be
206 // different from textclass
207 string fullName = addName(path, textclass + ".layout");
208 string localIndex = localPrefix + 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 BOOST_ASSERT(sub.size() == 4);
232 // now, create a TextClass with description containing path information
233 LayoutFile tmpl(textclass, sub.str(2) == "" ? textclass : sub.str(2),
234 sub.str(3) + " <" + path + ">", true);
235 // Do not add this local TextClass to classmap_ if it has
236 // already been loaded by, for example, a master buffer.
237 if (haveClass(textclass)
238 // FIXME I don't understand this comment (rgh)
239 // only layouts from the same directory are considered to be identical.
240 && classmap_[textclass].description() == tmpl.description()
243 classmap_[localIndex] = tmpl;
244 // This textclass is added on request so it will definitely be
245 // used. Load it now because other load() calls may fail if they
246 // are called in a context without buffer path information.
247 classmap_[localIndex].load(path);
252 // If .layout is not in local directory, or an invalid layout is found, return null
257 LayoutFileIndex defaultBaseclass()
259 if (BaseClassList::get().haveClass("article"))
260 return string("article");
267 // Reads the style files
270 LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
272 if (!BaseClassList::get().read()) {
273 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
274 "during parsing.\n Exiting.");
278 LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");