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"
15 #include "TextClass.h"
18 #include "support/debug.h"
19 #include "support/FileName.h"
20 #include "support/filetools.h"
22 #include <boost/bind.hpp>
23 #include <boost/regex.hpp>
28 using namespace lyx::support;
36 BaseClassList & BaseClassList::get()
38 static BaseClassList baseclasslist;
43 bool BaseClassList::haveClass(string const & classname) const
45 ClassMap::const_iterator it = classmap_.begin();
46 ClassMap::const_iterator en = classmap_.end();
47 for (; it != en; ++it) {
48 if (it->first == classname)
55 // Gets a textclass structure from string
57 BaseClassList::operator[](string const & classname) const
59 BOOST_ASSERT(haveClass(classname));
60 return classmap_[classname];
64 // Reads LyX textclass definitions according to textclass config file
65 bool BaseClassList::read()
68 FileName const real_file = libFileSearch("", "textclass.lst");
69 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
71 if (real_file.empty()) {
72 lyxerr << "BaseClassList::Read: unable to find "
74 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
75 << "'. Exiting." << endl;
77 // This causes LyX to end... Not a desirable behaviour. Lgb
78 // What do you propose? That the user gets a file dialog
79 // and is allowed to hunt for the file? (Asger)
80 // more that we have a layout for minimal.cls statically
81 // compiled in... (Lgb)
84 if (!lex.setFile(real_file)) {
85 lyxerr << "BaseClassList::Read: "
86 "lyxlex was not able to set file: "
91 lyxerr << "BaseClassList::Read: unable to open "
93 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
94 << "'\nCheck your installation. LyX can't continue."
99 bool finished = false;
101 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
102 while (lex.isOK() && !finished) {
103 LYXERR(Debug::TCLASS, "\tline by line");
105 case Lexer::LEX_FEOF:
109 string const fname = lex.getString();
110 LYXERR(Debug::TCLASS, "Fname: " << fname);
112 string const clname = lex.getString();
113 LYXERR(Debug::TCLASS, "Clname: " << clname);
115 string const desc = lex.getString();
116 LYXERR(Debug::TCLASS, "Desc: " << desc);
118 bool avail = lex.getBool();
119 LYXERR(Debug::TCLASS, "Avail: " << avail);
120 // This code is run when we have
121 // fname, clname, desc, and avail
122 TextClass tmpl(fname, clname, desc, avail);
123 if (lyxerr.debugging(Debug::TCLASS)) {
124 // only system layout files are loaded here so no
125 // buffer path is needed.
128 classmap_[fname] = tmpl;
134 LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
136 // lyx will start with an empty classmap_, but only reconfigure is allowed
137 // in this case. This gives users a second chance to configure lyx if
138 // initial configuration fails. (c.f. bug 2829)
139 if (classmap_.empty())
140 lyxerr << "BaseClassList::Read: no textclasses found!"
146 std::vector<BaseClassIndex> BaseClassList::classList() const
148 std::vector<BaseClassIndex> cl;
149 ClassMap::const_iterator it = classmap_.begin();
150 ClassMap::const_iterator en = classmap_.end();
151 for (; it != en; ++it)
152 cl.push_back(it->first);
157 void BaseClassList::reset(BaseClassIndex const & classname) {
158 BOOST_ASSERT(haveClass(classname));
159 TextClass const & tc = classmap_[classname];
160 TextClass tmpl(tc.name(), tc.latexname(), tc.description(),
161 tc.isTeXClassAvailable());
162 classmap_[classname] = tmpl;
166 string const BaseClassList::localPrefix = "LOCAL:";
170 BaseClassList::addTextClass(string const & textclass, string const & path)
173 // There be bugs here. The way this presently works, the local class gets
174 // added to the global list of available document classes. It will then
175 // appear on the list in Document>Settings, where it could be chosen in,
176 // say, a new document, with no real warning that the class may not be
177 // available when the document is saved, since the new document may not be
178 // in the same directory as the layout file.
180 // Another bug is this: If the Document>Settings dialog is open when a file
181 // with a local layout is opened, the dialog doesn't update.
183 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
184 // NOTE: latex class name is defined in textclass.layout, which can be
185 // different from textclass
186 string fullName = addName(path, textclass + ".layout");
187 string localIndex = localPrefix + textclass;
189 // if the local file has already been loaded, return it
190 if (haveClass(localIndex))
193 FileName const layout_file(fullName);
194 if (layout_file.exists()) {
195 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
196 // Read .layout file and get description, real latex classname etc
198 // This is a C++ version of function processLayoutFile in configure.py,
199 // which uses the following regex
200 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
201 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
202 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
203 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
205 while (getline(ifs, line)) {
206 // look for the \DeclareXXXClass line
208 if (regex_match(line, sub, reg)) {
209 // returns: whole string, classtype (not used here), class name, description
210 BOOST_ASSERT(sub.size() == 4);
211 // now, create a TextClass with description containing path information
212 TextClass tmpl(textclass, sub.str(2) == "" ? textclass : sub.str(2),
213 sub.str(3) + " <" + path + ">", true);
214 // Do not add this local TextClass to classmap_ if it has
215 // already been loaded by, for example, a master buffer.
216 if (haveClass(textclass)
217 // FIXME I don't understand this comment (rgh)
218 // only layouts from the same directory are considered to be identical.
219 && classmap_[textclass].description() == tmpl.description()
222 classmap_[localIndex] = tmpl;
223 // This textclass is added on request so it will definitely be
224 // used. Load it now because other load() calls may fail if they
225 // are called in a context without buffer path information.
226 classmap_[localIndex].load(path);
231 // If .layout is not in local directory, or an invalid layout is found, return null
236 BaseClassIndex defaultBaseclass()
238 if (BaseClassList::get().haveClass("article"))
239 return string("article");
246 // Reads the style files
249 LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
251 if (!BaseClassList::get().read()) {
252 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
253 "during parsing.\n Exiting.");
257 LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");