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"
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;
48 // Gets textclass number from name
49 pair<bool, textclass_type> const
50 TextClassList::numberOfClass(string const & textclass) const
52 ClassList::const_iterator cit =
53 find_if(classlist_.begin(), classlist_.end(),
54 bind(equal_to<string>(),
55 bind(&TextClass::name, _1),
58 return cit != classlist_.end() ?
59 make_pair(true, textclass_type(cit - classlist_.begin())) :
60 make_pair(false, textclass_type(0));
64 // Gets a textclass structure from number
66 TextClassList::operator[](textclass_type textclass) const
68 if (textclass >= classlist_.size())
71 //FIXME I don't believe the following line is actually necessary (rgh)
72 classlist_[textclass].load();
73 return classlist_[textclass];
77 // used when sorting the textclass list.
78 class less_textclass_avail_desc
79 : public std::binary_function<TextClass, TextClass, int>
82 int operator()(TextClass const & tc1,
83 TextClass const & tc2) const
86 // 1. Availability of text class
87 // 2. Description (lexicographic)
89 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
90 (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
91 tc1.description() < tc2.description());
96 // Reads LyX textclass definitions according to textclass config file
97 bool TextClassList::read()
100 support::FileName const real_file = libFileSearch("", "textclass.lst");
101 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
103 if (real_file.empty()) {
104 lyxerr << "TextClassList::Read: unable to find "
106 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
107 << "'. Exiting." << endl;
109 // This causes LyX to end... Not a desirable behaviour. Lgb
110 // What do you propose? That the user gets a file dialog
111 // and is allowed to hunt for the file? (Asger)
112 // more that we have a layout for minimal.cls statically
113 // compiled in... (Lgb)
116 if (!lex.setFile(real_file)) {
117 lyxerr << "TextClassList::Read: "
118 "lyxlex was not able to set file: "
119 << real_file << endl;
123 lyxerr << "TextClassList::Read: unable to open "
125 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
126 << "'\nCheck your installation. LyX can't continue."
131 bool finished = false;
133 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
134 while (lex.isOK() && !finished) {
135 LYXERR(Debug::TCLASS, "\tline by line");
137 case Lexer::LEX_FEOF:
141 string const fname = lex.getString();
142 LYXERR(Debug::TCLASS, "Fname: " << fname);
144 string const clname = lex.getString();
145 LYXERR(Debug::TCLASS, "Clname: " << clname);
147 string const desc = lex.getString();
148 LYXERR(Debug::TCLASS, "Desc: " << desc);
150 bool avail = lex.getBool();
151 LYXERR(Debug::TCLASS, "Avail: " << avail);
152 // This code is run when we have
153 // fname, clname, desc, and avail
154 TextClass tmpl(fname, clname, desc, avail);
155 if (lyxerr.debugging(Debug::TCLASS)) {
158 classlist_.push_back(tmpl);
164 LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
166 // lyx will start with an empty classlist_, but only reconfigure is allowed
167 // in this case. This gives users a second chance to configure lyx if
168 // initial configuration fails. (c.f. bug 2829)
169 if (classlist_.empty())
170 lyxerr << "TextClassList::Read: no textclasses found!"
173 // Ok everything loaded ok, now sort the list.
174 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
179 void TextClassList::reset(textclass_type const textclass) {
180 if (textclass >= classlist_.size())
182 TextClass const & tc = classlist_[textclass];
183 TextClass tmpl(tc.name(), tc.latexname(), tc.description(),
184 tc.isTeXClassAvailable());
185 classlist_[textclass] = tmpl;
189 std::pair<bool, textclass_type> const
190 TextClassList::addTextClass(std::string const & textclass, std::string const & path)
192 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
193 // NOTE: latex class name is defined in textclass.layout, which can be different from textclass
194 FileName const layout_file(addName(path, textclass + ".layout"));
195 if (layout_file.exists()) {
196 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
197 // Read .layout file and get description, real latex classname etc
199 // This is a C++ version of function processLayoutFile in configure.py,
200 // which uses the following regex
201 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
202 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
203 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
204 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
206 while (getline(ifs, line)) {
207 // look for the \DeclareXXXClass line
209 if (regex_match(line, sub, reg)) {
210 // returns: whole string, classtype (not used here), first option, description
211 BOOST_ASSERT(sub.size()==4);
212 // now, add the layout to textclass.
213 TextClass tmpl(textclass, sub.str(2)==""?textclass:sub.str(2),
214 sub.str(3) + " <" + path + ">", true);
215 if (lyxerr.debugging(Debug::TCLASS))
217 classlist_.push_back(tmpl);
218 return make_pair(true, classlist_.size() - 1);
222 // If .layout is not in local directory, or an invalid layout is found, return false
223 return make_pair(false, textclass_type(0));
227 // Global variable: textclass table.
228 TextClassList textclasslist;
231 textclass_type defaultTextclass()
233 // We want to return the article class. if `first' is
234 // true in the returned pair, then `second' is the textclass
235 // number; if it is false, second is 0. In both cases, second
237 return textclasslist.numberOfClass("article").second;
242 // Reads the style files
245 LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
247 if (!textclasslist.read()) {
248 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
249 "during parsing.\n Exiting.");
253 LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");