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, TextClassIndex> 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, TextClassIndex(cit - classlist_.begin())) :
48 make_pair(false, TextClassIndex(0));
52 // Gets a textclass structure from number
54 TextClassList::operator[](TextClassIndex 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];
64 TextClass & TextClassList::at(TextClassIndex textclass)
66 if (textclass >= classlist_.size())
69 //FIXME I don't believe the following line is actually necessary (rgh)
70 classlist_[textclass].load();
71 return classlist_[textclass];
75 // used when sorting the textclass list.
76 class less_textclass_avail_desc
77 : public binary_function<TextClass, TextClass, int>
80 int operator()(TextClass const & tc1,
81 TextClass const & tc2) const
84 // 1. Availability of text class
85 // 2. Description (lexicographic)
87 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
88 (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
89 tc1.description() < tc2.description());
94 // Reads LyX textclass definitions according to textclass config file
95 bool TextClassList::read()
98 FileName const real_file = libFileSearch("", "textclass.lst");
99 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
101 if (real_file.empty()) {
102 lyxerr << "TextClassList::Read: unable to find "
104 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
105 << "'. Exiting." << endl;
107 // This causes LyX to end... Not a desirable behaviour. Lgb
108 // What do you propose? That the user gets a file dialog
109 // and is allowed to hunt for the file? (Asger)
110 // more that we have a layout for minimal.cls statically
111 // compiled in... (Lgb)
114 if (!lex.setFile(real_file)) {
115 lyxerr << "TextClassList::Read: "
116 "lyxlex was not able to set file: "
117 << real_file << endl;
121 lyxerr << "TextClassList::Read: unable to open "
123 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
124 << "'\nCheck your installation. LyX can't continue."
129 bool finished = false;
131 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
132 while (lex.isOK() && !finished) {
133 LYXERR(Debug::TCLASS, "\tline by line");
135 case Lexer::LEX_FEOF:
139 string const fname = lex.getString();
140 LYXERR(Debug::TCLASS, "Fname: " << fname);
142 string const clname = lex.getString();
143 LYXERR(Debug::TCLASS, "Clname: " << clname);
145 string const desc = lex.getString();
146 LYXERR(Debug::TCLASS, "Desc: " << desc);
148 bool avail = lex.getBool();
149 LYXERR(Debug::TCLASS, "Avail: " << avail);
150 // This code is run when we have
151 // fname, clname, desc, and avail
152 TextClass tmpl(fname, clname, desc, avail);
153 if (lyxerr.debugging(Debug::TCLASS)) {
156 classlist_.push_back(tmpl);
162 LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
164 // lyx will start with an empty classlist_, but only reconfigure is allowed
165 // in this case. This gives users a second chance to configure lyx if
166 // initial configuration fails. (c.f. bug 2829)
167 if (classlist_.empty())
168 lyxerr << "TextClassList::Read: no textclasses found!"
171 // Ok everything loaded ok, now sort the list.
172 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
177 void TextClassList::reset(TextClassIndex const & textclass)
179 if (textclass >= classlist_.size())
181 TextClass const & tc = classlist_[textclass];
182 TextClass tmpl(tc.name(), tc.latexname(), tc.description(),
183 tc.isTeXClassAvailable());
184 classlist_[textclass] = tmpl;
188 pair<bool, TextClassIndex> const
189 TextClassList::addTextClass(string const & textclass, string const & path)
191 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
192 // NOTE: latex class name is defined in textclass.layout, which can be different from textclass
193 FileName const layout_file(addName(path, textclass + ".layout"));
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), first option, 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 if (lyxerr.debugging(Debug::TCLASS))
216 // Do not add this local TextClass to classlist_ if it has
217 // already been loaded by, for example, a master buffer.
218 pair<bool, lyx::TextClassIndex> pp =
219 textclasslist.numberOfClass(textclass);
220 // only layouts from the same directory are considered to be identical.
221 if (pp.first && classlist_[pp.second].description() == tmpl.description())
223 classlist_.push_back(tmpl);
224 return make_pair(true, classlist_.size() - 1);
228 // If .layout is not in local directory, or an invalid layout is found, return false
229 return make_pair(false, TextClassIndex(0));
233 // Global variable: textclass table.
234 TextClassList textclasslist;
237 TextClassIndex defaultTextclass()
239 // We want to return the article class. if `first' is
240 // true in the returned pair, then `second' is the textclass
241 // number; if it is false, second is 0. In both cases, second
243 return textclasslist.numberOfClass("article").second;
248 // Reads the style files
251 LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
253 if (!textclasslist.read()) {
254 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
255 "during parsing.\n Exiting.");
259 LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");