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::FileName;
31 using support::libFileSearch;
32 using support::makeDisplayPath;
48 // Gets textclass number from name
49 pair<bool, textclass_type> const
50 LyXTextClassList::numberOfClass(string const & textclass) const
52 ClassList::const_iterator cit =
53 find_if(classlist_.begin(), classlist_.end(),
54 bind(equal_to<string>(),
55 bind(&LyXTextClass::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 LyXTextClassList::operator[](textclass_type textclass) const
68 classlist_[textclass].load();
69 if (textclass < classlist_.size())
70 return classlist_[textclass];
76 // used when sorting the textclass list.
77 class less_textclass_avail_desc
78 : public std::binary_function<LyXTextClass, LyXTextClass, int>
81 int operator()(LyXTextClass const & tc1,
82 LyXTextClass const & tc2) const
85 // 1. Availability of text class
86 // 2. Description (lexicographic)
88 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
89 (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
90 tc1.description() < tc2.description());
95 // Reads LyX textclass definitions according to textclass config file
96 bool LyXTextClassList::read()
99 support::FileName const real_file = libFileSearch("", "textclass.lst");
100 lyxerr[Debug::TCLASS] << "Reading textclasses from `"
101 << real_file << '\'' << endl;
103 if (real_file.empty()) {
104 lyxerr << "LyXTextClassList::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 << "LyXTextClassList::Read: "
118 "lyxlex was not able to set file: "
119 << real_file << endl;
123 lyxerr << "LyXTextClassList::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" << 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, 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 FileName const layout_file(path + '/' + textclass + ".layout");
183 if (fs::exists(layout_file.toFilesystemEncoding())) {
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.toFilesystemEncoding().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;