]> git.lyx.org Git - lyx.git/blob - src/LayoutFile.cpp
4337956170496b3c3715f58945377657b4d96fe7
[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         LASSERT(haveClass(classname), /**/);
187         LayoutFile * tc = classmap_[classname];
188         LayoutFile * tmpl = 
189                 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
190                                tc->prerequisites(), tc->category(),
191                                tc->isTeXClassAvailable());
192         classmap_[classname] = tmpl;
193         delete tc;
194 }
195
196
197 namespace {
198
199 string layoutpost =                     
200                 "Columns      1\n"
201                 "Sides        1\n"
202                 "SecNumDepth  2\n"
203                 "TocDepth     2\n"
204                 "DefaultStyle   Standard\n\n"
205                 "Style Standard\n"
206                 "       Category              MainText\n"
207                 "       Margin                Static\n"
208                 "       LatexType             Paragraph\n"
209                 "       LatexName             dummy\n"
210                 "       ParIndent             MM\n"
211                 "       ParSkip               0.4\n"
212                 "       Align                 Block\n"
213                 "       AlignPossible         Block, Left, Right, Center\n"
214                 "       LabelType             No_Label\n"
215                 "End\n";
216         
217 }
218
219 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
220 {
221         FileName const tempLayout = FileName::tempName("basic_layout");
222         ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
223         // This writes a very basic class, but it also attempts to include 
224         // stdclass.inc. That would give us something moderately usable.
225         ofs << "# This layout is automatically generated\n"
226                "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
227                "Format " << LAYOUT_FORMAT << "\n"
228                "Input stdclass.inc\n\n"
229             << layoutpost;
230         ofs.close();
231
232         // We do not know if a LaTeX class is available for this document, but setting
233         // the last parameter to true will suppress a warning message about missing
234         // tex class.
235         LayoutFile * tc = new LayoutFile(textclass, textclass, 
236                         "Unknown text class " + textclass, textclass + ".cls", "", true);
237
238         if (!tc->load(tempLayout.absFileName())) {
239                 // The only way this happens is because the hardcoded layout file 
240                 // above is wrong or stdclass.inc cannot be found. So try again 
241                 // without stdclass.inc and without stdinsets.inc. 
242                 ofstream ofs2(tempLayout.toFilesystemEncoding().c_str());
243                 ofs2 << "# This layout is automatically generated\n"
244                         "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
245                         "Format " << LAYOUT_FORMAT << "\n"
246                         "Provides stdinsets 1\n"
247                      << layoutpost;
248                 ofs2.close();
249                 if (!tc->load(tempLayout.absFileName())) {
250                         // This can only happen if the hardcoded file above is wrong.
251                         LASSERT(false, /* */);
252                 }
253         }
254
255         classmap_[textclass] = tc;
256         return textclass;
257 }
258
259
260 LayoutFileIndex 
261         LayoutFileList::addLocalLayout(string const & textclass, string const & path)
262 {
263         // FIXME  There is a bug here: 4593
264         //
265         // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
266         // NOTE: latex class name is defined in textclass.layout, which can be 
267         // different from textclass
268         string fullName = addName(path, textclass + ".layout");
269         
270         FileName const layout_file(fullName);
271         if (layout_file.exists()) {
272                 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
273                 // Read .layout file and get description, real latex classname etc
274                 //
275                 // This is a C++ version of function processLayoutFile in configure.py,
276                 // which uses the following regex
277                 //     \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
278                 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
279                 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
280                         "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
281                 static regex const catreg("^#\\s*\\\\DeclareCategory\\{(.*)\\}");
282                 string line;
283                 string class_name;
284                 string class_prereq;
285                 string category;
286                 bool have_declaration = false;
287                 while (getline(ifs, line)) {
288                         // look for the \DeclareXXXClass line
289                         smatch sub;
290                         if (regex_match(line, sub, reg)) {
291                                 // returns: whole string, classtype (not used here), class name, description
292                                 LASSERT(sub.size() == 4, /**/);
293                                 // now, create a TextClass with description containing path information
294                                 class_name = (sub.str(2) == "" ? textclass : sub.str(2));
295                                 class_prereq = class_name + ".cls";
296                                 have_declaration = true;
297                         }
298                         else if (regex_match(line, sub, catreg)) {
299                                 category = sub.str(1);
300                         }
301                         if (have_declaration && !category.empty())
302                                 break;
303                 }
304                 if (have_declaration) {
305                         LayoutFile * tmpl = 
306                                 new LayoutFile(textclass, class_name, textclass, class_prereq, category, true);
307                         //FIXME: The prerequisites are available from the layout file and
308                         //       can be extracted from the above regex, but for now this
309                         //       field is simply set to class_name + ".cls"
310                         // This textclass is added on request so it will definitely be
311                         // used. Load it now because other load() calls may fail if they
312                         // are called in a context without buffer path information.
313                         tmpl->load(path);
314                         // There will be only one textclass with this name, even if different
315                         // layout files are loaded from different directories.
316                         if (haveClass(textclass)) {
317                                 LYXERR0("Existing textclass " << textclass << " is redefined by " << fullName);
318                                 delete classmap_[textclass];
319                         }
320                         classmap_[textclass] = tmpl;
321                         return textclass;
322                 }
323         }
324         // If .layout is not in local directory, or an invalid layout is found, return null
325         return string();
326 }
327
328
329 bool LayoutFileList::load(string const & name, string const & buf_path)
330 {
331         if (!haveClass(name)) {
332                 LYXERR0("Document class \"" << name << "\" does not exist.");
333                 return false;
334         }
335
336         LayoutFile * tc = classmap_[name];
337         return tc->load(buf_path);
338 }
339
340
341 LayoutFileIndex defaultBaseclass()
342 {
343         if (LayoutFileList::get().haveClass("article"))
344                 return string("article");
345         if (LayoutFileList::get().empty())
346                 // we'll call it that, since this gives the user a chance to
347                 // have a functioning document when things improve.
348                 return string("article");
349         return LayoutFileList::get().classList().front();
350 }
351
352 } // namespace lyx