]> git.lyx.org Git - lyx.git/blob - src/BaseClassList.cpp
baseclasslist --> BaseClassList singleton. Prep for further type safety.
[lyx.git] / src / BaseClassList.cpp
1 /**
2  * \file BaseClassList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "BaseClassList.h"
15 #include "TextClass.h"
16 #include "Lexer.h"
17
18 #include "support/debug.h"
19 #include "support/FileName.h"
20 #include "support/filetools.h"
21
22 #include <boost/bind.hpp>
23 #include <boost/regex.hpp>
24
25 #include <fstream>
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32 using boost::bind;
33 using boost::regex;
34 using boost::smatch;
35
36 BaseClassList & BaseClassList::get() 
37 {
38         static BaseClassList baseclasslist;
39         return baseclasslist;
40 }
41
42
43 // Gets textclass number from name
44 pair<bool, BaseClassIndex> const
45 BaseClassList::numberOfClass(string const & textclass) const
46 {
47         ClassList::const_iterator cit =
48                 find_if(classlist_.begin(), classlist_.end(),
49                         bind(equal_to<string>(),
50                              bind(&TextClass::name, _1),
51                              textclass));
52
53         return cit != classlist_.end() ?
54                 make_pair(true, BaseClassIndex(cit - classlist_.begin())) :
55                 make_pair(false, BaseClassIndex(0));
56 }
57
58
59 // Gets a textclass structure from number
60 TextClass const &
61 BaseClassList::operator[](BaseClassIndex textclass) const
62 {
63         if (textclass >= classlist_.size())
64                 return classlist_[0];
65         
66         //FIXME I don't believe the following line is actually necessary (rgh)
67         classlist_[textclass].load();
68         return classlist_[textclass];
69 }
70
71
72 // used when sorting the textclass list.
73 class less_textclass_avail_desc
74         : public binary_function<TextClass, TextClass, int>
75 {
76 public:
77         int operator()(TextClass const & tc1,
78                        TextClass const & tc2) const
79         {
80                 // Ordering criteria:
81                 //   1. Availability of text class
82                 //   2. Description (lexicographic)
83
84                 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
85                         (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
86                          tc1.description() < tc2.description());
87         }
88 };
89
90
91 // Reads LyX textclass definitions according to textclass config file
92 bool BaseClassList::read()
93 {
94         Lexer lex(0, 0);
95         FileName const real_file = libFileSearch("", "textclass.lst");
96         LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
97
98         if (real_file.empty()) {
99                 lyxerr << "BaseClassList::Read: unable to find "
100                           "textclass file  `"
101                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
102                        << "'. Exiting." << endl;
103                 return false;
104                 // This causes LyX to end... Not a desirable behaviour. Lgb
105                 // What do you propose? That the user gets a file dialog
106                 // and is allowed to hunt for the file? (Asger)
107                 // more that we have a layout for minimal.cls statically
108                 // compiled in... (Lgb)
109         }
110
111         if (!lex.setFile(real_file)) {
112                 lyxerr << "BaseClassList::Read: "
113                         "lyxlex was not able to set file: "
114                        << real_file << endl;
115         }
116
117         if (!lex.isOK()) {
118                 lyxerr << "BaseClassList::Read: unable to open "
119                           "textclass file  `"
120                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
121                        << "'\nCheck your installation. LyX can't continue."
122                        << endl;
123                 return false;
124         }
125
126         bool finished = false;
127         // Parse config-file
128         LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
129         while (lex.isOK() && !finished) {
130                 LYXERR(Debug::TCLASS, "\tline by line");
131                 switch (lex.lex()) {
132                 case Lexer::LEX_FEOF:
133                         finished = true;
134                         break;
135                 default:
136                         string const fname = lex.getString();
137                         LYXERR(Debug::TCLASS, "Fname: " << fname);
138                         if (lex.next()) {
139                                 string const clname = lex.getString();
140                                 LYXERR(Debug::TCLASS, "Clname: " << clname);
141                                 if (lex.next()) {
142                                         string const desc = lex.getString();
143                                         LYXERR(Debug::TCLASS, "Desc: " << desc);
144                                         if (lex.next()) {
145                                                 bool avail = lex.getBool();
146                                                 LYXERR(Debug::TCLASS, "Avail: " << avail);
147                                                 // This code is run when we have
148                                                 // fname, clname, desc, and avail
149                                                 TextClass tmpl(fname, clname, desc, avail);
150                                                 if (lyxerr.debugging(Debug::TCLASS)) {
151                                                         // only system layout files are loaded here so no
152                                                         // buffer path is needed.
153                                                         tmpl.load();
154                                                 }
155                                                 classlist_.push_back(tmpl);
156                                         }
157                                 }
158                         }
159                 }
160         }
161         LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
162
163         // lyx will start with an empty classlist_, but only reconfigure is allowed
164         // in this case. This gives users a second chance to configure lyx if
165         // initial configuration fails. (c.f. bug 2829)
166         if (classlist_.empty())
167                 lyxerr << "BaseClassList::Read: no textclasses found!"
168                        << endl;
169         else 
170                 // Ok everything loaded ok, now sort the list.
171                 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
172         return true;
173 }
174
175
176 void BaseClassList::reset(BaseClassIndex const textclass) {
177         if (textclass >= classlist_.size())
178                 return;
179         TextClass const & tc = classlist_[textclass];
180         TextClass tmpl(tc.name(), tc.latexname(), tc.description(), 
181                        tc.isTeXClassAvailable());
182         classlist_[textclass] = tmpl;
183 }
184
185
186 pair<bool, BaseClassIndex> const
187 BaseClassList::addTextClass(string const & textclass, string const & path)
188 {
189         // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
190         // NOTE: latex class name is defined in textclass.layout, which can be different from textclass
191         FileName const layout_file(addName(path, textclass + ".layout"));
192         if (layout_file.exists()) {
193                 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
194                 // Read .layout file and get description, real latex classname etc
195                 //
196                 // This is a C++ version of function processLayoutFile in configure.py,
197                 // which uses the following regex
198                 //     \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
199                 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
200                 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
201                         "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
202                 string line;
203                 while (getline(ifs, line)) {
204                         // look for the \DeclareXXXClass line
205                         smatch sub;
206                         if (regex_match(line, sub, reg)) {
207                                 // returns: whole string, classtype (not used here), class name, description
208                                 BOOST_ASSERT(sub.size() == 4);
209                                 // now, create a TextClass with description containing path information
210                                 TextClass tmpl(textclass, sub.str(2) == "" ? textclass : sub.str(2),
211                                         sub.str(3) + " <" + path + ">", true);
212                                 if (lyxerr.debugging(Debug::TCLASS))
213                                         tmpl.load(path);
214                                 // Do not add this local TextClass to classlist_ if it has
215                                 // already been loaded by, for example, a master buffer.
216                                 pair<bool, lyx::BaseClassIndex> pp = numberOfClass(textclass);
217                                 // only layouts from the same directory are considered to be identical.
218                                 if (pp.first && classlist_[pp.second].description() == tmpl.description())
219                                         return pp;
220                                 classlist_.push_back(tmpl);
221                                 // This textclass is added on request so it will definitely be
222                                 // used. Load it now because other load() calls may fail if they
223                                 // are called in a context without buffer path information.
224                                 classlist_.back().load(path);
225                                 return make_pair(true, classlist_.size() - 1);
226                         }
227                 }
228         }
229         // If .layout is not in local directory, or an invalid layout is found, return false
230         return make_pair(false, BaseClassIndex(0));
231 }
232
233
234
235 BaseClassIndex defaultBaseclass()
236 {
237         // We want to return the article class. if `first' is
238         // true in the returned pair, then `second' is the textclass
239         // number; if it is false, second is 0. In both cases, second
240         // is what we want.
241         return BaseClassList::get().numberOfClass("article").second;
242 }
243
244
245
246 // Reads the style files
247 bool LyXSetStyle()
248 {
249         LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
250
251         if (!BaseClassList::get().read()) {
252                 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
253                         "during parsing.\n             Exiting.");
254                 return false;
255         }
256
257         LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");
258         return true;
259 }
260
261
262 } // namespace lyx