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