2 * \file lyxtextclasslist.C
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 "lyxtextclasslist.h"
15 #include "lyxtextclass.h"
19 #include "support/filetools.h"
21 #include <boost/bind.hpp>
22 #include <boost/regex.hpp>
23 #include <boost/filesystem/operations.hpp>
25 namespace fs = boost::filesystem;
27 using lyx::textclass_type;
29 using lyx::support::libFileSearch;
30 using lyx::support::makeDisplayPath;
36 #ifndef CXX_GLOBAL_CSTD
50 // Gets textclass number from name
51 pair<bool, textclass_type> const
52 LyXTextClassList::numberOfClass(string const & textclass) const
54 ClassList::const_iterator cit =
55 find_if(classlist_.begin(), classlist_.end(),
56 bind(equal_to<string>(),
57 bind(&LyXTextClass::name, _1),
60 return cit != classlist_.end() ?
61 make_pair(true, textclass_type(cit - classlist_.begin())) :
62 make_pair(false, textclass_type(0));
66 // Gets a textclass structure from number
68 LyXTextClassList::operator[](textclass_type textclass) const
70 classlist_[textclass].load();
71 if (textclass < classlist_.size())
72 return classlist_[textclass];
78 // used when sorting the textclass list.
79 class less_textclass_avail_desc
80 : public std::binary_function<LyXTextClass, LyXTextClass, int>
83 int operator()(LyXTextClass const & tc1,
84 LyXTextClass const & tc2) const
87 // 1. Availability of text class
88 // 2. Description (lexicographic)
90 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
91 (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
92 tc1.description() < tc2.description());
97 // Reads LyX textclass definitions according to textclass config file
98 bool LyXTextClassList::read()
101 string real_file = libFileSearch("", "textclass.lst");
102 lyxerr[Debug::TCLASS] << "Reading textclasses from `"
103 << real_file << '\'' << endl;
105 if (real_file.empty()) {
106 lyxerr << "LyXTextClassList::Read: unable to find "
107 "textclass file `" << makeDisplayPath(real_file, 1000)
108 << "'. Exiting." << endl;
110 // This causes LyX to end... Not a desirable behaviour. Lgb
111 // What do you propose? That the user gets a file dialog
112 // and is allowed to hunt for the file? (Asger)
113 // more that we have a layout for minimal.cls statically
114 // compiled in... (Lgb)
117 if (!lex.setFile(real_file)) {
118 lyxerr << "LyXTextClassList::Read: "
119 "lyxlex was not able to set file: "
120 << real_file << endl;
124 lyxerr << "LyXTextClassList::Read: unable to open "
125 "textclass file `" << makeDisplayPath(real_file, 1000)
126 << "'\nCheck your installation. LyX can't continue."
131 bool finished = false;
133 lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
134 while (lex.isOK() && !finished) {
135 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
137 case LyXLex::LEX_FEOF:
141 string const fname = lex.getString();
142 lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
144 string const clname = lex.getString();
145 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
147 string const desc = lex.getString();
148 lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
150 bool avail = lex.getBool();
151 lyxerr[Debug::TCLASS] << "Avail: " << avail << endl;
152 // This code is run when we have
153 // fname, clname, desc, and avail
154 LyXTextClass 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" << endl;
166 if (classlist_.empty()) {
167 lyxerr << "LyXTextClassList::Read: no textclasses found!"
171 // Ok everything loaded ok, now sort the list.
172 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
177 std::pair<bool, lyx::textclass_type> const
178 LyXTextClassList::addTextClass(std::string const & textclass, std::string const & path)
180 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
181 // NOTE: latex class name is defined in textclass.layout, which can be different from textclass
182 string layout_file = path + "/" + textclass + ".layout";
183 if (fs::exists(layout_file)) {
184 lyxerr[Debug::TCLASS] << "Adding class " << textclass << " from directory " << path << endl;
185 // Read .layout file and get description, real latex classname etc
187 // This is a C++ version of function processLayoutFile in configure.py,
188 // which uses the following regex
189 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
190 ifstream ifs(layout_file.c_str());
191 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
192 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
194 while (getline(ifs, line)) {
195 // look for the \DeclareXXXClass line
197 if (regex_match(line, sub, reg)) {
198 // returns: whole string, classtype (not used here), first option, description
199 BOOST_ASSERT(sub.size()==4);
200 // now, add the layout to textclass.
201 LyXTextClass tmpl(textclass, sub.str(2)==""?textclass:sub.str(2),
202 sub.str(3) + " <" + path + ">", true);
203 if (lyxerr.debugging(Debug::TCLASS))
205 classlist_.push_back(tmpl);
206 return make_pair(true, classlist_.size() - 1);
210 // If .layout is not in local directory, or an invalid layout is found, return false
211 return make_pair(false, textclass_type(0));
215 // Global variable: textclass table.
216 LyXTextClassList textclasslist;
219 // Reads the style files
222 lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration..." << endl;
224 if (!textclasslist.read()) {
225 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
226 "during parsing.\n Exiting." << endl;
230 lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;