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