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 class less_textclass_avail_desc
73 : public std::binary_function<LyXTextClass, LyXTextClass, int>
76 int operator()(LyXTextClass const & tc1,
77 LyXTextClass const & tc2) const
80 // 1. Availability of text class
81 // 2. Description (lexicographic)
83 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
84 (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
85 tc1.description() < tc2.description());
90 // Reads LyX textclass definitions according to textclass config file
91 bool LyXTextClassList::Read()
94 string real_file = LibFileSearch("", "textclass.lst");
95 lyxerr[Debug::TCLASS] << "Reading textclasses from `"
96 << real_file << '\'' << endl;
98 if (real_file.empty()) {
99 lyxerr << "LyXTextClassList::Read: unable to find "
100 "textclass file `" << MakeDisplayPath(real_file, 1000)
101 << "'. Exiting." << endl;
103 // This causes LyX to end... Not a desirable behaviour. Lgb
104 // What do you propose? That the user gets a file dialog
105 // and is allowed to hunt for the file? (Asger)
106 // more that we have a layout for minimal.cls statically
107 // compiled in... (Lgb)
110 if (!lex.setFile(real_file)) {
111 lyxerr << "LyXTextClassList::Read: "
112 "lyxlex was not able to set file: "
113 << real_file << endl;
117 lyxerr << "LyXTextClassList::Read: unable to open "
118 "textclass file `" << MakeDisplayPath(real_file, 1000)
119 << "'\nCheck your installation. LyX can't continue."
124 bool finished = false;
126 lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
127 while (lex.isOK() && !finished) {
128 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
130 case LyXLex::LEX_FEOF:
134 string const fname = lex.getString();
135 lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
137 string const clname = lex.getString();
138 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
140 string const desc = lex.getString();
141 lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
143 bool avail = lex.getBool();
144 lyxerr[Debug::TCLASS] << "Avail: " << avail << endl;
145 // This code is run when we have
146 // fname, clname, desc, and avail
147 LyXTextClass tmpl(fname, clname, desc, avail);
148 if (lyxerr.debugging(Debug::TCLASS)) {
151 classlist_.push_back(tmpl);
157 lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
159 if (classlist_.empty()) {
160 lyxerr << "LyXTextClassList::Read: no textclasses found!"
164 // Ok everything loaded ok, now sort the list.
165 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
170 // Global variable: textclass table.
171 LyXTextClassList textclasslist;
174 // Reads the style files
177 lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration..." << endl;
179 if (!textclasslist.Read()) {
180 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
181 "during parsing.\n Exiting." << endl;
185 lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;