2 * \file TextClassList.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 "TextClassList.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 // Gets textclass number from name
37 pair<bool, BaseClassIndex> const
38 TextClassList::numberOfClass(string const & textclass) const
40 ClassList::const_iterator cit =
41 find_if(classlist_.begin(), classlist_.end(),
42 bind(equal_to<string>(),
43 bind(&TextClass::name, _1),
46 return cit != classlist_.end() ?
47 make_pair(true, BaseClassIndex(cit - classlist_.begin())) :
48 make_pair(false, BaseClassIndex(0));
52 // Gets a textclass structure from number
54 TextClassList::operator[](BaseClassIndex textclass) const
56 if (textclass >= classlist_.size())
59 //FIXME I don't believe the following line is actually necessary (rgh)
60 classlist_[textclass].load();
61 return classlist_[textclass];
65 // used when sorting the textclass list.
66 class less_textclass_avail_desc
67 : public binary_function<TextClass, TextClass, int>
70 int operator()(TextClass const & tc1,
71 TextClass const & tc2) const
74 // 1. Availability of text class
75 // 2. Description (lexicographic)
77 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
78 (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
79 tc1.description() < tc2.description());
84 // Reads LyX textclass definitions according to textclass config file
85 bool TextClassList::read()
88 FileName const real_file = libFileSearch("", "textclass.lst");
89 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
91 if (real_file.empty()) {
92 lyxerr << "TextClassList::Read: unable to find "
94 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
95 << "'. Exiting." << endl;
97 // This causes LyX to end... Not a desirable behaviour. Lgb
98 // What do you propose? That the user gets a file dialog
99 // and is allowed to hunt for the file? (Asger)
100 // more that we have a layout for minimal.cls statically
101 // compiled in... (Lgb)
104 if (!lex.setFile(real_file)) {
105 lyxerr << "TextClassList::Read: "
106 "lyxlex was not able to set file: "
107 << real_file << endl;
111 lyxerr << "TextClassList::Read: unable to open "
113 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
114 << "'\nCheck your installation. LyX can't continue."
119 bool finished = false;
121 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
122 while (lex.isOK() && !finished) {
123 LYXERR(Debug::TCLASS, "\tline by line");
125 case Lexer::LEX_FEOF:
129 string const fname = lex.getString();
130 LYXERR(Debug::TCLASS, "Fname: " << fname);
132 string const clname = lex.getString();
133 LYXERR(Debug::TCLASS, "Clname: " << clname);
135 string const desc = lex.getString();
136 LYXERR(Debug::TCLASS, "Desc: " << desc);
138 bool avail = lex.getBool();
139 LYXERR(Debug::TCLASS, "Avail: " << avail);
140 // This code is run when we have
141 // fname, clname, desc, and avail
142 TextClass tmpl(fname, clname, desc, avail);
143 if (lyxerr.debugging(Debug::TCLASS)) {
144 // only system layout files are loaded here so no
145 // buffer path is needed.
148 classlist_.push_back(tmpl);
154 LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
156 // lyx will start with an empty classlist_, but only reconfigure is allowed
157 // in this case. This gives users a second chance to configure lyx if
158 // initial configuration fails. (c.f. bug 2829)
159 if (classlist_.empty())
160 lyxerr << "TextClassList::Read: no textclasses found!"
163 // Ok everything loaded ok, now sort the list.
164 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
169 void TextClassList::reset(BaseClassIndex const textclass) {
170 if (textclass >= classlist_.size())
172 TextClass const & tc = classlist_[textclass];
173 TextClass tmpl(tc.name(), tc.latexname(), tc.description(),
174 tc.isTeXClassAvailable());
175 classlist_[textclass] = tmpl;
179 pair<bool, BaseClassIndex> const
180 TextClassList::addTextClass(string const & textclass, string const & path)
182 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
183 // NOTE: latex class name is defined in textclass.layout, which can be different from textclass
184 FileName const layout_file(addName(path, textclass + ".layout"));
185 if (layout_file.exists()) {
186 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
187 // Read .layout file and get description, real latex classname etc
189 // This is a C++ version of function processLayoutFile in configure.py,
190 // which uses the following regex
191 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
192 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
193 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
194 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
196 while (getline(ifs, line)) {
197 // look for the \DeclareXXXClass line
199 if (regex_match(line, sub, reg)) {
200 // returns: whole string, classtype (not used here), first option, description
201 BOOST_ASSERT(sub.size()==4);
202 // now, create a TextClass with description containing path information
203 TextClass tmpl(textclass, sub.str(2)==""?textclass:sub.str(2),
204 sub.str(3) + " <" + path + ">", true);
205 if (lyxerr.debugging(Debug::TCLASS))
207 // Do not add this local TextClass to classlist_ if it has
208 // already been loaded by, for example, a master buffer.
209 pair<bool, lyx::BaseClassIndex> pp =
210 textclasslist.numberOfClass(textclass);
211 // only layouts from the same directory are considered to be identical.
212 if (pp.first && classlist_[pp.second].description() == tmpl.description())
214 classlist_.push_back(tmpl);
215 // This textclass is added on request so it will definitely be
216 // used. Load it now because other load() calls may fail if they
217 // are called in a context without buffer path information.
218 classlist_.back().load(path);
219 return make_pair(true, classlist_.size() - 1);
223 // If .layout is not in local directory, or an invalid layout is found, return false
224 return make_pair(false, BaseClassIndex(0));
228 // Global variable: textclass table.
229 TextClassList textclasslist;
232 BaseClassIndex defaultTextclass()
234 // We want to return the article class. if `first' is
235 // true in the returned pair, then `second' is the textclass
236 // number; if it is false, second is 0. In both cases, second
238 return textclasslist.numberOfClass("article").second;
243 // Reads the style files
246 LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
248 if (!textclasslist.read()) {
249 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
250 "during parsing.\n Exiting.");
254 LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");