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