]> git.lyx.org Git - lyx.git/blob - src/LayoutFile.cpp
Routines for calculating numerical labels for BibTeX citations.
[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 "frontends/alert.h"
22
23 #include "support/debug.h"
24 #include "support/FileName.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lassert.h"
28 #include "support/lstrings.h"
29
30 #include <boost/bind.hpp>
31 #include <boost/regex.hpp>
32
33 #include <fstream>
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39
40 using boost::bind;
41 using boost::regex;
42 using boost::smatch;
43
44 LayoutFile::LayoutFile(string const & fn, string const & cln,
45                            string const & desc, bool texClassAvail )
46 {
47         name_ = fn;
48         latexname_ = cln;
49         description_ = desc;
50         texClassAvail_ = texClassAvail;
51 }
52
53 LayoutFileList::~LayoutFileList()
54 {
55         ClassMap::const_iterator it = classmap_.begin();
56         ClassMap::const_iterator en = classmap_.end();
57         for (; it != en; ++it) {
58                 delete it->second;
59         }
60 }
61
62 LayoutFileList & LayoutFileList::get() 
63 {
64         static LayoutFileList baseclasslist;
65         return baseclasslist;
66 }
67
68
69 bool LayoutFileList::haveClass(string const & classname) const
70 {
71         ClassMap::const_iterator it = classmap_.begin();
72         ClassMap::const_iterator en = classmap_.end();
73         for (; it != en; ++it) {
74                 if (it->first == classname)
75                         return true;
76         }
77         return false;
78 }
79
80
81 LayoutFile const & LayoutFileList::operator[](string const & classname) const
82 {
83         LASSERT(haveClass(classname), /**/);
84         return *classmap_[classname];
85 }
86
87
88 LayoutFile & LayoutFileList::operator[](string const & classname)
89 {
90         LASSERT(haveClass(classname), /**/);
91         return *classmap_[classname];
92 }
93
94
95 // Reads LyX textclass definitions according to textclass config file
96 bool LayoutFileList::read()
97 {
98         Lexer lex;
99         FileName const real_file = libFileSearch("", "textclass.lst");
100         LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
101
102         if (real_file.empty()) {
103                 lyxerr << "LayoutFileList::Read: unable to find "
104                           "textclass file  `"
105                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
106                        << "'. Exiting." << endl;
107                 return false;
108                 // This causes LyX to end... Not a desirable behaviour. Lgb
109                 // What do you propose? That the user gets a file dialog
110                 // and is allowed to hunt for the file? (Asger)
111                 // more that we have a layout for minimal.cls statically
112                 // compiled in... (Lgb)
113         }
114
115         if (!lex.setFile(real_file)) {
116                 lyxerr << "LayoutFileList::Read: "
117                         "lyxlex was not able to set file: "
118                        << real_file << endl;
119         }
120
121         if (!lex.isOK()) {
122                 lyxerr << "LayoutFileList::Read: unable to open "
123                           "textclass file  `"
124                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
125                        << "'\nCheck your installation. LyX can't continue."
126                        << endl;
127                 return false;
128         }
129
130         bool finished = false;
131         // Parse config-file
132         LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
133         while (lex.isOK() && !finished) {
134                 LYXERR(Debug::TCLASS, "\tline by line");
135                 switch (lex.lex()) {
136                 case Lexer::LEX_FEOF:
137                         finished = true;
138                         break;
139                 default:
140                         string const fname = lex.getString();
141                         LYXERR(Debug::TCLASS, "Fname: " << fname);
142                         if (!lex.next()) 
143                                 break;
144                         string const clname = lex.getString();
145                         LYXERR(Debug::TCLASS, "Clname: " << clname);
146                         if (!lex.next()) 
147                                 break;
148                         string const desc = lex.getString();
149                         LYXERR(Debug::TCLASS, "Desc: " << desc);
150                         if (!lex.next()) 
151                                 break;
152                         bool avail = lex.getBool();
153                         LYXERR(Debug::TCLASS, "Avail: " << avail);
154                         // This code is run when we have
155                         // fname, clname, desc, and avail
156                         LayoutFile * tmpl = new LayoutFile(fname, clname, desc, avail);
157                         if (lyxerr.debugging(Debug::TCLASS)) {
158                                 // only system layout files are loaded here so no
159                                 // buffer path is needed.
160                                 tmpl->load();
161                         }
162                         classmap_[fname] = tmpl;
163                 }
164         }
165         LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
166
167         // lyx will start with an empty classmap_, but only reconfigure is allowed
168         // in this case. This gives users a second chance to configure lyx if
169         // initial configuration fails. (c.f. bug 2829)
170         if (classmap_.empty())
171                 lyxerr << "LayoutFileList::Read: no textclasses found!"
172                        << endl;
173         return true;
174 }
175
176
177 std::vector<LayoutFileIndex> LayoutFileList::classList() const
178 {
179         std::vector<LayoutFileIndex> cl;
180         ClassMap::const_iterator it = classmap_.begin();
181         ClassMap::const_iterator en = classmap_.end();
182         for (; it != en; ++it)
183                 cl.push_back(it->first);
184         return cl;
185 }
186
187
188 void LayoutFileList::reset(LayoutFileIndex const & classname) {
189         LASSERT(haveClass(classname), /**/);
190         LayoutFile * tc = classmap_[classname];
191         LayoutFile * tmpl = 
192                 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
193                                tc->isTeXClassAvailable());
194         classmap_[classname] = tmpl;
195         delete tc;
196 }
197
198
199 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
200 {
201         if (haveClass(textclass))
202                 return textclass;
203
204         FileName const tempLayout = FileName::tempName();
205         ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
206         ofs << "# This layout is automatically generated\n"
207                 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
208                 "Format         7\n"
209                 "Input stdclass.inc\n\n"
210                 "Columns                1\n"
211                 "Sides                  1\n"
212                 "SecNumDepth    2\n"
213                 "TocDepth               2\n"
214                 "DefaultStyle   Standard\n\n"
215                 "Style Standard\n"
216                 "       Category              MainText\n"
217                 "       Margin                Static\n"
218                 "       LatexType             Paragraph\n"
219                 "       LatexName             dummy\n"
220                 "       ParIndent             MM\n"
221                 "       ParSkip               0.4\n"
222                 "       Align                 Block\n"
223                 "       AlignPossible         Block, Left, Right, Center\n"
224                 "       LabelType             No_Label\n"
225                 "End\n";
226         ofs.close();
227
228         // We do not know if a LaTeX class is available for this document, but setting
229         // the last parameter to true will suppress a warning message about missing
230         // tex class.
231         LayoutFile * tc = new LayoutFile(textclass, textclass, "Unknown text class " + textclass, true);
232         if (!tc->load(tempLayout.absFilename())) {
233                 // The only way this happens is because the hardcoded layout file above
234                 // is wrong.
235                 LASSERT(false, /**/);
236         }
237         classmap_[textclass] = tc;
238         return textclass;
239 }
240
241
242 LayoutFileIndex 
243         LayoutFileList::addLocalLayout(string const & textclass, string const & path)
244 {
245         // FIXME  There is a bug here: 4593
246         //
247         // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
248         // NOTE: latex class name is defined in textclass.layout, which can be 
249         // different from textclass
250         string fullName = addName(path, textclass + ".layout");
251         
252         FileName const layout_file(fullName);
253         if (layout_file.exists()) {
254                 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
255                 // Read .layout file and get description, real latex classname etc
256                 //
257                 // This is a C++ version of function processLayoutFile in configure.py,
258                 // which uses the following regex
259                 //     \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
260                 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
261                 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
262                         "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
263                 string line;
264                 while (getline(ifs, line)) {
265                         // look for the \DeclareXXXClass line
266                         smatch sub;
267                         if (regex_match(line, sub, reg)) {
268                                 // returns: whole string, classtype (not used here), class name, description
269                                 LASSERT(sub.size() == 4, /**/);
270                                 // now, create a TextClass with description containing path information
271                                 string className(sub.str(2) == "" ? textclass : sub.str(2));
272                                 LayoutFile * tmpl = 
273                                         new LayoutFile(textclass, className, textclass, true);
274                                 // This textclass is added on request so it will definitely be
275                                 // used. Load it now because other load() calls may fail if they
276                                 // are called in a context without buffer path information.
277                                 tmpl->load(path);
278                                 // There will be only one textclass with this name, even if different
279                                 // layout files are loaded from different directories.
280                                 if (haveClass(textclass)) {
281                                         LYXERR0("Existing textclass " << textclass << " is redefined by " << fullName);
282                                         delete classmap_[textclass];
283                                 }
284                                 classmap_[textclass] = tmpl;
285                                 return textclass;
286                         }
287                 }
288         }
289         // If .layout is not in local directory, or an invalid layout is found, return null
290         return string();
291 }
292
293
294 bool LayoutFileList::load(string const & name, string const & buf_path)
295 {
296         if (!haveClass(name)) {
297                 LYXERR0("Document class \"" << name << "\" does not exist.");
298                 return false;
299         }
300
301         LayoutFile * tc = classmap_[name];
302         if (!tc->load(buf_path)) {
303                 docstring s = bformat(_("The document class %1$s "
304                                    "could not be loaded."), from_utf8(name));
305                 frontend::Alert::error(_("Could not load class"), s);
306                 return false;
307         }
308         return true;
309 }
310
311
312 LayoutFileIndex defaultBaseclass()
313 {
314         if (LayoutFileList::get().haveClass("article"))
315                 return string("article");
316         if (LayoutFileList::get().empty())
317                 return string();
318         return LayoutFileList::get().classList().front();
319 }
320
321
322
323 // Reads the style files
324 bool LyXSetStyle()
325 {
326         LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
327
328         if (!LayoutFileList::get().read()) {
329                 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
330                         "during parsing.\n             Exiting.");
331                 return false;
332         }
333
334         LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");
335         return true;
336 }
337
338
339 } // namespace lyx