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>
28 namespace fs = boost::filesystem;
30 using support::libFileSearch;
31 using support::makeDisplayPath;
37 #ifndef CXX_GLOBAL_CSTD
51 // Gets textclass number from name
52 pair<bool, textclass_type> const
53 LyXTextClassList::numberOfClass(string const & textclass) const
55 ClassList::const_iterator cit =
56 find_if(classlist_.begin(), classlist_.end(),
57 bind(equal_to<string>(),
58 bind(&LyXTextClass::name, _1),
61 return cit != classlist_.end() ?
62 make_pair(true, textclass_type(cit - classlist_.begin())) :
63 make_pair(false, textclass_type(0));
67 // Gets a textclass structure from number
69 LyXTextClassList::operator[](textclass_type textclass) const
71 classlist_[textclass].load();
72 if (textclass < classlist_.size())
73 return classlist_[textclass];
79 // used when sorting the textclass list.
80 class less_textclass_avail_desc
81 : public std::binary_function<LyXTextClass, LyXTextClass, int>
84 int operator()(LyXTextClass const & tc1,
85 LyXTextClass const & tc2) const
88 // 1. Availability of text class
89 // 2. Description (lexicographic)
91 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
92 (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
93 tc1.description() < tc2.description());
98 // Reads LyX textclass definitions according to textclass config file
99 bool LyXTextClassList::read()
102 string real_file = libFileSearch("", "textclass.lst");
103 lyxerr[Debug::TCLASS] << "Reading textclasses from `"
104 << real_file << '\'' << endl;
106 if (real_file.empty()) {
107 lyxerr << "LyXTextClassList::Read: unable to find "
108 "textclass file `" << to_utf8(makeDisplayPath(real_file, 1000))
109 << "'. Exiting." << endl;
111 // This causes LyX to end... Not a desirable behaviour. Lgb
112 // What do you propose? That the user gets a file dialog
113 // and is allowed to hunt for the file? (Asger)
114 // more that we have a layout for minimal.cls statically
115 // compiled in... (Lgb)
118 if (!lex.setFile(real_file)) {
119 lyxerr << "LyXTextClassList::Read: "
120 "lyxlex was not able to set file: "
121 << real_file << endl;
125 lyxerr << "LyXTextClassList::Read: unable to open "
126 "textclass file `" << to_utf8(makeDisplayPath(real_file, 1000))
127 << "'\nCheck your installation. LyX can't continue."
132 bool finished = false;
134 lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
135 while (lex.isOK() && !finished) {
136 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
138 case LyXLex::LEX_FEOF:
142 string const fname = lex.getString();
143 lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
145 string const clname = lex.getString();
146 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
148 string const desc = lex.getString();
149 lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
151 bool avail = lex.getBool();
152 lyxerr[Debug::TCLASS] << "Avail: " << avail << endl;
153 // This code is run when we have
154 // fname, clname, desc, and avail
155 LyXTextClass tmpl(fname, clname, desc, avail);
156 if (lyxerr.debugging(Debug::TCLASS)) {
159 classlist_.push_back(tmpl);
165 lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
167 if (classlist_.empty()) {
168 lyxerr << "LyXTextClassList::Read: no textclasses found!"
172 // Ok everything loaded ok, now sort the list.
173 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
178 std::pair<bool, textclass_type> const
179 LyXTextClassList::addTextClass(std::string const & textclass, std::string const & path)
181 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
182 // NOTE: latex class name is defined in textclass.layout, which can be different from textclass
183 string layout_file = path + "/" + textclass + ".layout";
184 if (fs::exists(layout_file)) {
185 lyxerr[Debug::TCLASS] << "Adding class " << textclass << " from directory " << path << endl;
186 // Read .layout file and get description, real latex classname etc
188 // This is a C++ version of function processLayoutFile in configure.py,
189 // which uses the following regex
190 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
191 ifstream ifs(layout_file.c_str());
192 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
193 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
195 while (getline(ifs, line)) {
196 // look for the \DeclareXXXClass line
198 if (regex_match(line, sub, reg)) {
199 // returns: whole string, classtype (not used here), first option, description
200 BOOST_ASSERT(sub.size()==4);
201 // now, add the layout to textclass.
202 LyXTextClass tmpl(textclass, sub.str(2)==""?textclass:sub.str(2),
203 sub.str(3) + " <" + path + ">", true);
204 if (lyxerr.debugging(Debug::TCLASS))
206 classlist_.push_back(tmpl);
207 return make_pair(true, classlist_.size() - 1);
211 // If .layout is not in local directory, or an invalid layout is found, return false
212 return make_pair(false, textclass_type(0));
216 // Global variable: textclass table.
217 LyXTextClassList textclasslist;
220 // Reads the style files
223 lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration..." << endl;
225 if (!textclasslist.read()) {
226 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
227 "during parsing.\n Exiting." << endl;
231 lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;