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