* de/Tutorial.lyx: minor corrections reported on lyx-docs.
[lyx.git] / src / LayoutFile.cpp
blobb7abd795d0f6f5c5cea420656fa73fb14e8f89bc
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.
6 * \author Lars Gullik Bjønnes
7 * \author John Levon
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "LayoutFile.h"
15 #include "Counters.h"
16 #include "Floating.h"
17 #include "FloatList.h"
18 #include "Lexer.h"
19 #include "TextClass.h"
21 #include "support/lassert.h"
22 #include "support/debug.h"
23 #include "support/FileName.h"
24 #include "support/filetools.h"
25 #include "support/gettext.h"
27 #include <boost/bind.hpp>
28 #include <boost/regex.hpp>
30 #include <fstream>
32 using namespace std;
33 using namespace lyx::support;
35 namespace lyx {
37 using boost::bind;
38 using boost::regex;
39 using boost::smatch;
41 LayoutFile::LayoutFile(string const & fn, string const & cln,
42 string const & desc, bool texClassAvail )
44 name_ = fn;
45 latexname_ = cln;
46 description_ = desc;
47 texClassAvail_ = texClassAvail;
50 LayoutFileList::~LayoutFileList()
52 ClassMap::const_iterator it = classmap_.begin();
53 ClassMap::const_iterator en = classmap_.end();
54 for (; it != en; ++it) {
55 delete it->second;
59 LayoutFileList & LayoutFileList::get()
61 static LayoutFileList baseclasslist;
62 return baseclasslist;
66 bool LayoutFileList::haveClass(string const & classname) const
68 ClassMap::const_iterator it = classmap_.begin();
69 ClassMap::const_iterator en = classmap_.end();
70 for (; it != en; ++it) {
71 if (it->first == classname)
72 return true;
74 return false;
78 LayoutFile const & LayoutFileList::operator[](string const & classname) const
80 LASSERT(haveClass(classname), /**/);
81 return *classmap_[classname];
85 LayoutFile & LayoutFileList::operator[](string const & classname)
87 LASSERT(haveClass(classname), /**/);
88 return *classmap_[classname];
92 // Reads LyX textclass definitions according to textclass config file
93 bool LayoutFileList::read()
95 Lexer lex;
96 FileName const real_file = libFileSearch("", "textclass.lst");
97 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
99 if (real_file.empty()) {
100 lyxerr << "LayoutFileList::Read: unable to find "
101 "textclass file `"
102 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
103 << "'. Exiting." << endl;
104 return false;
105 // This causes LyX to end... Not a desirable behaviour. Lgb
106 // What do you propose? That the user gets a file dialog
107 // and is allowed to hunt for the file? (Asger)
108 // more that we have a layout for minimal.cls statically
109 // compiled in... (Lgb)
112 if (!lex.setFile(real_file)) {
113 lyxerr << "LayoutFileList::Read: "
114 "lyxlex was not able to set file: "
115 << real_file << endl;
118 if (!lex.isOK()) {
119 lyxerr << "LayoutFileList::Read: unable to open "
120 "textclass file `"
121 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
122 << "'\nCheck your installation. LyX can't continue."
123 << endl;
124 return false;
127 bool finished = false;
128 // Parse config-file
129 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
130 while (lex.isOK() && !finished) {
131 LYXERR(Debug::TCLASS, "\tline by line");
132 switch (lex.lex()) {
133 case Lexer::LEX_FEOF:
134 finished = true;
135 break;
136 default:
137 string const fname = lex.getString();
138 LYXERR(Debug::TCLASS, "Fname: " << fname);
139 if (lex.next()) {
140 string const clname = lex.getString();
141 LYXERR(Debug::TCLASS, "Clname: " << clname);
142 if (lex.next()) {
143 string const desc = lex.getString();
144 LYXERR(Debug::TCLASS, "Desc: " << desc);
145 if (lex.next()) {
146 bool avail = lex.getBool();
147 LYXERR(Debug::TCLASS, "Avail: " << avail);
148 // This code is run when we have
149 // fname, clname, desc, and avail
150 LayoutFile * tmpl = new LayoutFile(fname, clname, desc, avail);
151 if (lyxerr.debugging(Debug::TCLASS)) {
152 // only system layout files are loaded here so no
153 // buffer path is needed.
154 tmpl->load();
156 classmap_[fname] = tmpl;
162 LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
164 // lyx will start with an empty classmap_, but only reconfigure is allowed
165 // in this case. This gives users a second chance to configure lyx if
166 // initial configuration fails. (c.f. bug 2829)
167 if (classmap_.empty())
168 lyxerr << "LayoutFileList::Read: no textclasses found!"
169 << endl;
170 return true;
174 std::vector<LayoutFileIndex> LayoutFileList::classList() const
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;
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->isTeXClassAvailable());
191 classmap_[classname] = tmpl;
192 delete tc;
196 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
198 if (haveClass(textclass))
199 return textclass;
201 FileName const tempLayout = FileName::tempName();
202 ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
203 ofs << "# This layout is automatically generated\n"
204 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
205 "Format 7\n"
206 "Input stdclass.inc\n\n"
207 "Columns 1\n"
208 "Sides 1\n"
209 "SecNumDepth 2\n"
210 "TocDepth 2\n"
211 "DefaultStyle Standard\n\n"
212 "Style Standard\n"
213 " Category MainText\n"
214 " Margin Static\n"
215 " LatexType Paragraph\n"
216 " LatexName dummy\n"
217 " ParIndent MM\n"
218 " ParSkip 0.4\n"
219 " Align Block\n"
220 " AlignPossible Block, Left, Right, Center\n"
221 " LabelType No_Label\n"
222 "End\n";
223 ofs.close();
225 // We do not know if a LaTeX class is available for this document, but setting
226 // the last parameter to true will suppress a warning message about missing
227 // tex class.
228 LayoutFile * tc = new LayoutFile(textclass, textclass, "Unknown text class " + textclass, true);
229 if (!tc->load(tempLayout.absFilename())) {
230 // The only way this happens is because the hardcoded layout file above
231 // is wrong.
232 LASSERT(false, /**/);
234 classmap_[textclass] = tc;
235 return textclass;
239 LayoutFileIndex
240 LayoutFileList::addLocalLayout(string const & textclass, string const & path)
242 // FIXME There is a bug here: 4593
244 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
245 // NOTE: latex class name is defined in textclass.layout, which can be
246 // different from textclass
247 string fullName = addName(path, textclass + ".layout");
249 FileName const layout_file(fullName);
250 if (layout_file.exists()) {
251 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
252 // Read .layout file and get description, real latex classname etc
254 // This is a C++ version of function processLayoutFile in configure.py,
255 // which uses the following regex
256 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
257 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
258 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
259 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
260 string line;
261 while (getline(ifs, line)) {
262 // look for the \DeclareXXXClass line
263 smatch sub;
264 if (regex_match(line, sub, reg)) {
265 // returns: whole string, classtype (not used here), class name, description
266 LASSERT(sub.size() == 4, /**/);
267 // now, create a TextClass with description containing path information
268 string className(sub.str(2) == "" ? textclass : sub.str(2));
269 LayoutFile * tmpl =
270 new LayoutFile(textclass, className, textclass, true);
271 // This textclass is added on request so it will definitely be
272 // used. Load it now because other load() calls may fail if they
273 // are called in a context without buffer path information.
274 tmpl->load(path);
275 // There will be only one textclass with this name, even if different
276 // layout files are loaded from different directories.
277 if (haveClass(textclass)) {
278 LYXERR0("Existing textclass " << textclass << " is redefined by " << fullName);
279 delete classmap_[textclass];
281 classmap_[textclass] = tmpl;
282 return textclass;
286 // If .layout is not in local directory, or an invalid layout is found, return null
287 return string();
291 LayoutFileIndex defaultBaseclass()
293 if (LayoutFileList::get().haveClass("article"))
294 return string("article");
295 if (LayoutFileList::get().empty())
296 return string();
297 return LayoutFileList::get().classList().front();
302 // Reads the style files
303 bool LyXSetStyle()
305 LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
307 if (!LayoutFileList::get().read()) {
308 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
309 "during parsing.\n Exiting.");
310 return false;
313 LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");
314 return true;
318 } // namespace lyx