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 // Gets textclass number from name
44 pair<bool, BaseClassIndex> const
45 BaseClassList::numberOfClass(string const & textclass) const
47 ClassList::const_iterator cit =
48 find_if(classlist_.begin(), classlist_.end(),
49 bind(equal_to<string>(),
50 bind(&TextClass::name, _1),
53 return cit != classlist_.end() ?
54 make_pair(true, BaseClassIndex(cit - classlist_.begin())) :
55 make_pair(false, BaseClassIndex(0));
59 // Gets a textclass structure from number
61 BaseClassList::operator[](BaseClassIndex textclass) const
63 if (textclass >= classlist_.size())
66 //FIXME I don't believe the following line is actually necessary (rgh)
67 classlist_[textclass].load();
68 return classlist_[textclass];
72 // used when sorting the textclass list.
73 class less_textclass_avail_desc
74 : public binary_function<TextClass, TextClass, int>
77 int operator()(TextClass const & tc1,
78 TextClass const & tc2) const
81 // 1. Availability of text class
82 // 2. Description (lexicographic)
84 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
85 (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
86 tc1.description() < tc2.description());
91 // Reads LyX textclass definitions according to textclass config file
92 bool BaseClassList::read()
95 FileName const real_file = libFileSearch("", "textclass.lst");
96 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
98 if (real_file.empty()) {
99 lyxerr << "BaseClassList::Read: unable to find "
101 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
102 << "'. Exiting." << endl;
104 // This causes LyX to end... Not a desirable behaviour. Lgb
105 // What do you propose? That the user gets a file dialog
106 // and is allowed to hunt for the file? (Asger)
107 // more that we have a layout for minimal.cls statically
108 // compiled in... (Lgb)
111 if (!lex.setFile(real_file)) {
112 lyxerr << "BaseClassList::Read: "
113 "lyxlex was not able to set file: "
114 << real_file << endl;
118 lyxerr << "BaseClassList::Read: unable to open "
120 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
121 << "'\nCheck your installation. LyX can't continue."
126 bool finished = false;
128 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
129 while (lex.isOK() && !finished) {
130 LYXERR(Debug::TCLASS, "\tline by line");
132 case Lexer::LEX_FEOF:
136 string const fname = lex.getString();
137 LYXERR(Debug::TCLASS, "Fname: " << fname);
139 string const clname = lex.getString();
140 LYXERR(Debug::TCLASS, "Clname: " << clname);
142 string const desc = lex.getString();
143 LYXERR(Debug::TCLASS, "Desc: " << desc);
145 bool avail = lex.getBool();
146 LYXERR(Debug::TCLASS, "Avail: " << avail);
147 // This code is run when we have
148 // fname, clname, desc, and avail
149 TextClass tmpl(fname, clname, desc, avail);
150 if (lyxerr.debugging(Debug::TCLASS)) {
151 // only system layout files are loaded here so no
152 // buffer path is needed.
155 classlist_.push_back(tmpl);
161 LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
163 // lyx will start with an empty classlist_, but only reconfigure is allowed
164 // in this case. This gives users a second chance to configure lyx if
165 // initial configuration fails. (c.f. bug 2829)
166 if (classlist_.empty())
167 lyxerr << "BaseClassList::Read: no textclasses found!"
170 // Ok everything loaded ok, now sort the list.
171 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
176 void BaseClassList::reset(BaseClassIndex const textclass) {
177 if (textclass >= classlist_.size())
179 TextClass const & tc = classlist_[textclass];
180 TextClass tmpl(tc.name(), tc.latexname(), tc.description(),
181 tc.isTeXClassAvailable());
182 classlist_[textclass] = tmpl;
186 pair<bool, BaseClassIndex> const
187 BaseClassList::addTextClass(string const & textclass, string const & path)
189 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
190 // NOTE: latex class name is defined in textclass.layout, which can be different from textclass
191 FileName const layout_file(addName(path, textclass + ".layout"));
192 if (layout_file.exists()) {
193 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
194 // Read .layout file and get description, real latex classname etc
196 // This is a C++ version of function processLayoutFile in configure.py,
197 // which uses the following regex
198 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
199 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
200 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
201 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
203 while (getline(ifs, line)) {
204 // look for the \DeclareXXXClass line
206 if (regex_match(line, sub, reg)) {
207 // returns: whole string, classtype (not used here), class name, description
208 BOOST_ASSERT(sub.size() == 4);
209 // now, create a TextClass with description containing path information
210 TextClass tmpl(textclass, sub.str(2) == "" ? textclass : sub.str(2),
211 sub.str(3) + " <" + path + ">", true);
212 if (lyxerr.debugging(Debug::TCLASS))
214 // Do not add this local TextClass to classlist_ if it has
215 // already been loaded by, for example, a master buffer.
216 pair<bool, lyx::BaseClassIndex> pp = numberOfClass(textclass);
217 // only layouts from the same directory are considered to be identical.
218 if (pp.first && classlist_[pp.second].description() == tmpl.description())
220 classlist_.push_back(tmpl);
221 // This textclass is added on request so it will definitely be
222 // used. Load it now because other load() calls may fail if they
223 // are called in a context without buffer path information.
224 classlist_.back().load(path);
225 return make_pair(true, classlist_.size() - 1);
229 // If .layout is not in local directory, or an invalid layout is found, return false
230 return make_pair(false, BaseClassIndex(0));
235 BaseClassIndex defaultBaseclass()
237 // We want to return the article class. if `first' is
238 // true in the returned pair, then `second' is the textclass
239 // number; if it is false, second is 0. In both cases, second
241 return BaseClassList::get().numberOfClass("article").second;
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.");