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