]> git.lyx.org Git - lyx.git/blob - src/BaseClassList.cpp
Cleanup.
[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 "Counters.h"
16 #include "Floating.h"
17 #include "FloatList.h"
18 #include "Lexer.h"
19 #include "TextClass.h"
20
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/filetools.h"
24 #include "support/gettext.h"
25
26 #include <boost/bind.hpp>
27 #include <boost/regex.hpp>
28
29 #include <fstream>
30
31 using namespace std;
32 using namespace lyx::support;
33
34 namespace lyx {
35
36 using boost::bind;
37 using boost::regex;
38 using boost::smatch;
39
40 LayoutFile::LayoutFile(string const & fn, string const & cln,
41                            string const & desc, bool texClassAvail )
42 {
43         name_ = fn;
44         latexname_ = cln;
45         description_ = desc;
46         texClassAvail_ = texClassAvail;
47 }
48
49
50 BaseClassList & BaseClassList::get() 
51 {
52         static BaseClassList baseclasslist;
53         return baseclasslist;
54 }
55
56
57 bool BaseClassList::haveClass(string const & classname) const
58 {
59         ClassMap::const_iterator it = classmap_.begin();
60         ClassMap::const_iterator en = classmap_.end();
61         for (; it != en; ++it) {
62                 if (it->first == classname)
63                         return true;
64         }
65         return false;
66 }
67
68
69 LayoutFile const & BaseClassList::operator[](string const & classname) const
70 {
71         BOOST_ASSERT(haveClass(classname));
72         return classmap_[classname];
73 }
74
75
76 LayoutFile & 
77         BaseClassList::operator[](string const & classname)
78 {
79         BOOST_ASSERT(haveClass(classname));
80         return classmap_[classname];
81 }
82
83
84 // Reads LyX textclass definitions according to textclass config file
85 bool BaseClassList::read()
86 {
87         Lexer lex(0, 0);
88         FileName const real_file = libFileSearch("", "textclass.lst");
89         LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
90
91         if (real_file.empty()) {
92                 lyxerr << "BaseClassList::Read: unable to find "
93                           "textclass file  `"
94                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
95                        << "'. Exiting." << endl;
96                 return false;
97                 // This causes LyX to end... Not a desirable behaviour. Lgb
98                 // What do you propose? That the user gets a file dialog
99                 // and is allowed to hunt for the file? (Asger)
100                 // more that we have a layout for minimal.cls statically
101                 // compiled in... (Lgb)
102         }
103
104         if (!lex.setFile(real_file)) {
105                 lyxerr << "BaseClassList::Read: "
106                         "lyxlex was not able to set file: "
107                        << real_file << endl;
108         }
109
110         if (!lex.isOK()) {
111                 lyxerr << "BaseClassList::Read: unable to open "
112                           "textclass file  `"
113                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
114                        << "'\nCheck your installation. LyX can't continue."
115                        << endl;
116                 return false;
117         }
118
119         bool finished = false;
120         // Parse config-file
121         LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
122         while (lex.isOK() && !finished) {
123                 LYXERR(Debug::TCLASS, "\tline by line");
124                 switch (lex.lex()) {
125                 case Lexer::LEX_FEOF:
126                         finished = true;
127                         break;
128                 default:
129                         string const fname = lex.getString();
130                         LYXERR(Debug::TCLASS, "Fname: " << fname);
131                         if (lex.next()) {
132                                 string const clname = lex.getString();
133                                 LYXERR(Debug::TCLASS, "Clname: " << clname);
134                                 if (lex.next()) {
135                                         string const desc = lex.getString();
136                                         LYXERR(Debug::TCLASS, "Desc: " << desc);
137                                         if (lex.next()) {
138                                                 bool avail = lex.getBool();
139                                                 LYXERR(Debug::TCLASS, "Avail: " << avail);
140                                                 // This code is run when we have
141                                                 // fname, clname, desc, and avail
142                                                 LayoutFile tmpl(fname, clname, desc, avail);
143                                                 if (lyxerr.debugging(Debug::TCLASS)) {
144                                                         // only system layout files are loaded here so no
145                                                         // buffer path is needed.
146                                                         tmpl.load();
147                                                 }
148                                                 classmap_[fname] = tmpl;
149                                         }
150                                 }
151                         }
152                 }
153         }
154         LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
155
156         // lyx will start with an empty classmap_, but only reconfigure is allowed
157         // in this case. This gives users a second chance to configure lyx if
158         // initial configuration fails. (c.f. bug 2829)
159         if (classmap_.empty())
160                 lyxerr << "BaseClassList::Read: no textclasses found!"
161                        << endl;
162         return true;
163 }
164
165
166 std::vector<LayoutFileIndex> BaseClassList::classList() const
167 {
168         std::vector<LayoutFileIndex> cl;
169         ClassMap::const_iterator it = classmap_.begin();
170         ClassMap::const_iterator en = classmap_.end();
171         for (; it != en; ++it)
172                 cl.push_back(it->first);
173         return cl;
174 }
175
176
177 void BaseClassList::reset(LayoutFileIndex const & classname) {
178         BOOST_ASSERT(haveClass(classname));
179         LayoutFile const & tc = classmap_[classname];
180         LayoutFile tmpl(tc.name(), tc.latexname(), tc.description(),
181                        tc.isTeXClassAvailable());
182         classmap_[classname] = tmpl;
183 }
184
185
186 string const BaseClassList::localPrefix = "LOCAL:";
187
188
189 LayoutFileIndex 
190         BaseClassList::addLayoutFile(string const & textclass, string const & path)
191 {
192         // FIXME BUGS
193         // There be bugs here. The way this presently works, the local class gets 
194         // added to the global list of available document classes. It will then
195         // appear on the list in Document>Settings, where it could be chosen in, 
196         // say, a new document, with no real warning that the class may not be
197         // available when the document is saved, since the new document may not be
198         // in the same directory as the layout file.
199         //
200         // Another bug is this: If the Document>Settings dialog is open when a file
201         // with a local layout is opened, the dialog doesn't update.
202         //
203         // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
204         // NOTE: latex class name is defined in textclass.layout, which can be 
205         // different from textclass
206         string fullName = addName(path, textclass + ".layout");
207         string localIndex = localPrefix + textclass;
208
209         // if the local file has already been loaded, return it
210         if (haveClass(localIndex))
211                 return localIndex;
212
213         FileName const layout_file(fullName);
214         if (layout_file.exists()) {
215                 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
216                 // Read .layout file and get description, real latex classname etc
217                 //
218                 // This is a C++ version of function processLayoutFile in configure.py,
219                 // which uses the following regex
220                 //     \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
221                 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
222                 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
223                         "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
224                 string line;
225                 while (getline(ifs, line)) {
226                         // look for the \DeclareXXXClass line
227                         smatch sub;
228                         if (regex_match(line, sub, reg)) {
229                                 // returns: whole string, classtype (not used here), class name, description
230                                 BOOST_ASSERT(sub.size() == 4);
231                                 // now, create a TextClass with description containing path information
232                                 LayoutFile tmpl(textclass, sub.str(2) == "" ? textclass : sub.str(2),
233                                         sub.str(3) + " <" + path + ">", true);
234                                 // Do not add this local TextClass to classmap_ if it has
235                                 // already been loaded by, for example, a master buffer.
236                                 if (haveClass(textclass)
237                                                 // FIXME I don't understand this comment (rgh)
238                                                 // only layouts from the same directory are considered to be identical.
239                                                 && classmap_[textclass].description() == tmpl.description()
240                                    )
241                                         return textclass;
242                                 classmap_[localIndex] = tmpl;
243                                 // This textclass is added on request so it will definitely be
244                                 // used. Load it now because other load() calls may fail if they
245                                 // are called in a context without buffer path information.
246                                 classmap_[localIndex].load(path);
247                                 return localIndex;
248                         }
249                 }
250         }
251         // If .layout is not in local directory, or an invalid layout is found, return null
252         return string("");
253 }
254
255
256 LayoutFileIndex defaultBaseclass()
257 {
258         if (BaseClassList::get().haveClass("article"))
259                 return string("article");
260         else 
261                 return string("");
262 }
263
264
265
266 // Reads the style files
267 bool LyXSetStyle()
268 {
269         LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
270
271         if (!BaseClassList::get().read()) {
272                 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
273                         "during parsing.\n             Exiting.");
274                 return false;
275         }
276
277         LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");
278         return true;
279 }
280
281
282 } // namespace lyx