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>
23 using lyx::textclass_type;
25 using lyx::support::LibFileSearch;
26 using lyx::support::MakeDisplayPath;
30 #ifndef CXX_GLOBAL_CSTD
43 // Gets textclass number from name
44 pair<bool, textclass_type> const
45 LyXTextClassList::NumberOfClass(string const & textclass) const
47 ClassList::const_iterator cit =
48 find_if(classlist_.begin(), classlist_.end(),
49 bind(equal_to<string>(),
50 bind(&LyXTextClass::name, _1),
53 return cit != classlist_.end() ?
54 make_pair(true, textclass_type(cit - classlist_.begin())) :
55 make_pair(false, textclass_type(0));
59 // Gets a textclass structure from number
61 LyXTextClassList::operator[](textclass_type textclass) const
63 classlist_[textclass].load();
64 if (textclass < classlist_.size())
65 return classlist_[textclass];
71 // used when sorting the textclass list.
72 struct less_textclass_avail_desc
73 : public std::binary_function<LyXTextClass, LyXTextClass, int>
75 int operator()(LyXTextClass const & tc1,
76 LyXTextClass const & tc2) const
79 // 1. Availability of text class
80 // 2. Description (lexicographic)
82 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
83 (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
84 tc1.description() < tc2.description());
89 // Reads LyX textclass definitions according to textclass config file
90 bool LyXTextClassList::Read()
93 string real_file = LibFileSearch("", "textclass.lst");
94 lyxerr[Debug::TCLASS] << "Reading textclasses from `"
95 << real_file << '\'' << endl;
97 if (real_file.empty()) {
98 lyxerr << "LyXTextClassList::Read: unable to find "
99 "textclass file `" << MakeDisplayPath(real_file, 1000)
100 << "'. Exiting." << endl;
102 // This causes LyX to end... Not a desirable behaviour. Lgb
103 // What do you propose? That the user gets a file dialog
104 // and is allowed to hunt for the file? (Asger)
105 // more that we have a layout for minimal.cls statically
106 // compiled in... (Lgb)
109 if (!lex.setFile(real_file)) {
110 lyxerr << "LyXTextClassList::Read: "
111 "lyxlex was not able to set file: "
112 << real_file << endl;
116 lyxerr << "LyXTextClassList::Read: unable to open "
117 "textclass file `" << MakeDisplayPath(real_file, 1000)
118 << "'\nCheck your installation. LyX can't continue."
123 bool finished = false;
125 lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
126 while (lex.isOK() && !finished) {
127 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
129 case LyXLex::LEX_FEOF:
133 string const fname = lex.getString();
134 lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
136 string const clname = lex.getString();
137 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
139 string const desc = lex.getString();
140 lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
142 bool avail = lex.getBool();
143 lyxerr[Debug::TCLASS] << "Avail: " << avail << endl;
144 // This code is run when we have
145 // fname, clname, desc, and avail
146 LyXTextClass 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" << endl;
158 if (classlist_.empty()) {
159 lyxerr << "LyXTextClassList::Read: no textclasses found!"
163 // Ok everything loaded ok, now sort the list.
164 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
169 // Global variable: textclass table.
170 LyXTextClassList textclasslist;
173 // Reads the style files
176 lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration..." << endl;
178 if (!textclasslist.Read()) {
179 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
180 "during parsing.\n Exiting." << endl;
184 lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;