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"
16 #include "support/debug.h"
19 #include "support/filetools.h"
21 #include <boost/bind.hpp>
22 #include <boost/regex.hpp>
29 using support::FileName;
30 using support::addName;
31 using support::libFileSearch;
32 using support::makeDisplayPath;
40 // Gets textclass number from name
41 pair<bool, textclass_type> const
42 TextClassList::numberOfClass(string const & textclass) const
44 ClassList::const_iterator cit =
45 find_if(classlist_.begin(), classlist_.end(),
46 bind(equal_to<string>(),
47 bind(&TextClass::name, _1),
50 return cit != classlist_.end() ?
51 make_pair(true, textclass_type(cit - classlist_.begin())) :
52 make_pair(false, textclass_type(0));
56 // Gets a textclass structure from number
58 TextClassList::operator[](textclass_type textclass) const
60 if (textclass >= classlist_.size())
63 //FIXME I don't believe the following line is actually necessary (rgh)
64 classlist_[textclass].load();
65 return classlist_[textclass];
69 // used when sorting the textclass list.
70 class less_textclass_avail_desc
71 : public std::binary_function<TextClass, TextClass, int>
74 int operator()(TextClass const & tc1,
75 TextClass const & tc2) const
78 // 1. Availability of text class
79 // 2. Description (lexicographic)
81 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
82 (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
83 tc1.description() < tc2.description());
88 // Reads LyX textclass definitions according to textclass config file
89 bool TextClassList::read()
92 support::FileName const real_file = libFileSearch("", "textclass.lst");
93 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
95 if (real_file.empty()) {
96 lyxerr << "TextClassList::Read: unable to find "
98 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
99 << "'. Exiting." << endl;
101 // This causes LyX to end... Not a desirable behaviour. Lgb
102 // What do you propose? That the user gets a file dialog
103 // and is allowed to hunt for the file? (Asger)
104 // more that we have a layout for minimal.cls statically
105 // compiled in... (Lgb)
108 if (!lex.setFile(real_file)) {
109 lyxerr << "TextClassList::Read: "
110 "lyxlex was not able to set file: "
111 << real_file << endl;
115 lyxerr << "TextClassList::Read: unable to open "
117 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
118 << "'\nCheck your installation. LyX can't continue."
123 bool finished = false;
125 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
126 while (lex.isOK() && !finished) {
127 LYXERR(Debug::TCLASS, "\tline by line");
129 case Lexer::LEX_FEOF:
133 string const fname = lex.getString();
134 LYXERR(Debug::TCLASS, "Fname: " << fname);
136 string const clname = lex.getString();
137 LYXERR(Debug::TCLASS, "Clname: " << clname);
139 string const desc = lex.getString();
140 LYXERR(Debug::TCLASS, "Desc: " << desc);
142 bool avail = lex.getBool();
143 LYXERR(Debug::TCLASS, "Avail: " << avail);
144 // This code is run when we have
145 // fname, clname, desc, and avail
146 TextClass tmpl(fname, clname, desc, avail);
147 if (lyxerr.debugging(Debug::TCLASS)) {
150 classlist_.push_back(tmpl);
156 LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
158 // lyx will start with an empty classlist_, but only reconfigure is allowed
159 // in this case. This gives users a second chance to configure lyx if
160 // initial configuration fails. (c.f. bug 2829)
161 if (classlist_.empty())
162 lyxerr << "TextClassList::Read: no textclasses found!"
165 // Ok everything loaded ok, now sort the list.
166 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
171 void TextClassList::reset(textclass_type const textclass) {
172 if (textclass >= classlist_.size())
174 TextClass const & tc = classlist_[textclass];
175 TextClass tmpl(tc.name(), tc.latexname(), tc.description(),
176 tc.isTeXClassAvailable());
177 classlist_[textclass] = tmpl;
181 std::pair<bool, textclass_type> const
182 TextClassList::addTextClass(std::string const & textclass, std::string const & path)
184 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
185 // NOTE: latex class name is defined in textclass.layout, which can be different from textclass
186 FileName const layout_file(addName(path, textclass + ".layout"));
187 if (layout_file.exists()) {
188 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
189 // Read .layout file and get description, real latex classname etc
191 // This is a C++ version of function processLayoutFile in configure.py,
192 // which uses the following regex
193 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
194 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
195 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
196 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
198 while (getline(ifs, line)) {
199 // look for the \DeclareXXXClass line
201 if (regex_match(line, sub, reg)) {
202 // returns: whole string, classtype (not used here), first option, description
203 BOOST_ASSERT(sub.size()==4);
204 // now, create a TextClass with description containing path information
205 TextClass tmpl(textclass, sub.str(2)==""?textclass:sub.str(2),
206 sub.str(3) + " <" + path + ">", true);
207 if (lyxerr.debugging(Debug::TCLASS))
209 // Do not add this local TextClass to classlist_ if it has
210 // already been loaded by, for example, a master buffer.
211 pair<bool, lyx::textclass_type> pp =
212 textclasslist.numberOfClass(textclass);
213 // only layouts from the same directory are considered to be identical.
214 if (pp.first && classlist_[pp.second].description() == tmpl.description())
216 classlist_.push_back(tmpl);
217 return make_pair(true, classlist_.size() - 1);
221 // If .layout is not in local directory, or an invalid layout is found, return false
222 return make_pair(false, textclass_type(0));
226 // Global variable: textclass table.
227 TextClassList textclasslist;
230 textclass_type defaultTextclass()
232 // We want to return the article class. if `first' is
233 // true in the returned pair, then `second' is the textclass
234 // number; if it is false, second is 0. In both cases, second
236 return textclasslist.numberOfClass("article").second;
241 // Reads the style files
244 LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
246 if (!textclasslist.read()) {
247 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
248 "during parsing.\n Exiting.");
252 LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");