]> git.lyx.org Git - lyx.git/blob - src/LayoutFile.cpp
a dummy file while we wait for andre
[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/assert.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
51 LayoutFileList & LayoutFileList::get() 
52 {
53         static LayoutFileList baseclasslist;
54         return baseclasslist;
55 }
56
57
58 bool LayoutFileList::haveClass(string const & classname) const
59 {
60         ClassMap::const_iterator it = classmap_.begin();
61         ClassMap::const_iterator en = classmap_.end();
62         for (; it != en; ++it) {
63                 if (it->first == classname)
64                         return true;
65         }
66         return false;
67 }
68
69
70 LayoutFile const & LayoutFileList::operator[](string const & classname) const
71 {
72         LASSERT(haveClass(classname), /**/);
73         return *classmap_[classname];
74 }
75
76
77 LayoutFile & 
78         LayoutFileList::operator[](string const & classname)
79 {
80         LASSERT(haveClass(classname), /**/);
81         return *classmap_[classname];
82 }
83
84
85 // Reads LyX textclass definitions according to textclass config file
86 bool LayoutFileList::read()
87 {
88         Lexer lex;
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 << "LayoutFileList::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 << "LayoutFileList::Read: "
107                         "lyxlex was not able to set file: "
108                        << real_file << endl;
109         }
110
111         if (!lex.isOK()) {
112                 lyxerr << "LayoutFileList::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 = new LayoutFile(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 << "LayoutFileList::Read: no textclasses found!"
162                        << endl;
163         return true;
164 }
165
166
167 std::vector<LayoutFileIndex> LayoutFileList::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 LayoutFileList::reset(LayoutFileIndex const & classname) {
179         LASSERT(haveClass(classname), /**/);
180         LayoutFile * tc = classmap_[classname];
181         LayoutFile * tmpl = 
182                 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
183                                tc->isTeXClassAvailable());
184         classmap_[classname] = tmpl;
185         delete tc;
186 }
187
188
189 string const LayoutFileList::localPrefix = "LOCAL:";
190 string const LayoutFileList::embeddedPrefix = "EMBED:";
191
192
193 LayoutFileIndex 
194         LayoutFileList::addLayoutFile(string const & textclass, string const & path,
195                 Layout_Type type)
196 {
197         // FIXME  There is a bug here: 4593
198         //
199         // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
200         // NOTE: latex class name is defined in textclass.layout, which can be 
201         // different from textclass
202         string fullName = addName(path, textclass + ".layout");
203         string localIndex;
204         
205         if (type == Local)
206                 localIndex = localPrefix + fullName;
207         else if (type == Embedded)
208                 localIndex = embeddedPrefix + 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                                 LASSERT(sub.size() == 4, /**/);
232                                 // now, create a TextClass with description containing path information
233                                 string className(sub.str(2) == "" ? textclass : sub.str(2));
234                                 LayoutFile * tmpl = 
235                                         new LayoutFile(textclass, className, localIndex, true);
236                                 // This textclass is added on request so it will definitely be
237                                 // used. Load it now because other load() calls may fail if they
238                                 // are called in a context without buffer path information.
239                                 tmpl->load(path);
240                                 classmap_[localIndex] = tmpl;
241                                 return localIndex;
242                         }
243                 }
244         }
245         // If .layout is not in local directory, or an invalid layout is found, return null
246         return string();
247 }
248
249
250 LayoutFileIndex defaultBaseclass()
251 {
252         if (LayoutFileList::get().haveClass("article"))
253                 return string("article");
254         if (LayoutFileList::get().empty())
255                 return string();
256         return LayoutFileList::get().classList().front();
257 }
258
259
260
261 // Reads the style files
262 bool LyXSetStyle()
263 {
264         LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
265
266         if (!LayoutFileList::get().read()) {
267                 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
268                         "during parsing.\n             Exiting.");
269                 return false;
270         }
271
272         LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");
273         return true;
274 }
275
276
277 } // namespace lyx