move LyXerr QString specialisation to support/qstring_helpers
[lyx.git] / src / LayoutFile.cpp
blob8b4bba35532f5ad543bf4657cda73c689237e185
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 &
86 LayoutFileList::operator[](string const & classname)
88 LASSERT(haveClass(classname), /**/);
89 return *classmap_[classname];
93 // Reads LyX textclass definitions according to textclass config file
94 bool LayoutFileList::read()
96 Lexer lex;
97 FileName const real_file = libFileSearch("", "textclass.lst");
98 LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
100 if (real_file.empty()) {
101 lyxerr << "LayoutFileList::Read: unable to find "
102 "textclass file `"
103 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
104 << "'. Exiting." << endl;
105 return false;
106 // This causes LyX to end... Not a desirable behaviour. Lgb
107 // What do you propose? That the user gets a file dialog
108 // and is allowed to hunt for the file? (Asger)
109 // more that we have a layout for minimal.cls statically
110 // compiled in... (Lgb)
113 if (!lex.setFile(real_file)) {
114 lyxerr << "LayoutFileList::Read: "
115 "lyxlex was not able to set file: "
116 << real_file << endl;
119 if (!lex.isOK()) {
120 lyxerr << "LayoutFileList::Read: unable to open "
121 "textclass file `"
122 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
123 << "'\nCheck your installation. LyX can't continue."
124 << endl;
125 return false;
128 bool finished = false;
129 // Parse config-file
130 LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
131 while (lex.isOK() && !finished) {
132 LYXERR(Debug::TCLASS, "\tline by line");
133 switch (lex.lex()) {
134 case Lexer::LEX_FEOF:
135 finished = true;
136 break;
137 default:
138 string const fname = lex.getString();
139 LYXERR(Debug::TCLASS, "Fname: " << fname);
140 if (lex.next()) {
141 string const clname = lex.getString();
142 LYXERR(Debug::TCLASS, "Clname: " << clname);
143 if (lex.next()) {
144 string const desc = lex.getString();
145 LYXERR(Debug::TCLASS, "Desc: " << desc);
146 if (lex.next()) {
147 bool avail = lex.getBool();
148 LYXERR(Debug::TCLASS, "Avail: " << avail);
149 // This code is run when we have
150 // fname, clname, desc, and avail
151 LayoutFile * tmpl = new LayoutFile(fname, clname, desc, 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();
157 classmap_[fname] = tmpl;
163 LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
165 // lyx will start with an empty classmap_, but only reconfigure is allowed
166 // in this case. This gives users a second chance to configure lyx if
167 // initial configuration fails. (c.f. bug 2829)
168 if (classmap_.empty())
169 lyxerr << "LayoutFileList::Read: no textclasses found!"
170 << endl;
171 return true;
175 std::vector<LayoutFileIndex> LayoutFileList::classList() const
177 std::vector<LayoutFileIndex> cl;
178 ClassMap::const_iterator it = classmap_.begin();
179 ClassMap::const_iterator en = classmap_.end();
180 for (; it != en; ++it)
181 cl.push_back(it->first);
182 return cl;
186 void LayoutFileList::reset(LayoutFileIndex const & classname) {
187 LASSERT(haveClass(classname), /**/);
188 LayoutFile * tc = classmap_[classname];
189 LayoutFile * tmpl =
190 new LayoutFile(tc->name(), tc->latexname(), tc->description(),
191 tc->isTeXClassAvailable());
192 classmap_[classname] = tmpl;
193 delete tc;
197 LayoutFileIndex LayoutFileList::addEmptyClass(string const & textclass)
199 if (haveClass(textclass))
200 return textclass;
202 FileName const tempLayout = FileName::tempName();
203 ofstream ofs(tempLayout.toFilesystemEncoding().c_str());
204 ofs << "# This layout is automatically generated\n"
205 "# \\DeclareLaTeXClass{" << textclass << "}\n\n"
206 "Format 7\n"
207 "Input stdclass.inc\n\n"
208 "Columns 1\n"
209 "Sides 1\n"
210 "SecNumDepth 2\n"
211 "TocDepth 2\n"
212 "DefaultStyle Standard\n\n"
213 "Style Standard\n"
214 " Category MainText\n"
215 " Margin Static\n"
216 " LatexType Paragraph\n"
217 " LatexName dummy\n"
218 " ParIndent MM\n"
219 " ParSkip 0.4\n"
220 " Align Block\n"
221 " AlignPossible Block, Left, Right, Center\n"
222 " LabelType No_Label\n"
223 "End\n";
224 ofs.close();
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, "Unknown text class " + textclass, true);
230 if (!tc->load(tempLayout.absFilename()))
231 // The only way this happens is because the hardcoded layout file above
232 // is wrong.
233 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("Exisint 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