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
9 * Full author contact details are available in file CREDITS.
14 #include "LayoutFile.h"
17 #include "FloatList.h"
19 #include "TextClass.h"
21 #include "frontends/alert.h"
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"
30 #include <boost/bind.hpp>
31 #include <boost/regex.hpp>
36 using namespace lyx::support
;
44 LayoutFile::LayoutFile(string
const & fn
, string
const & cln
,
45 string
const & desc
, bool texClassAvail
)
50 texClassAvail_
= texClassAvail
;
53 LayoutFileList::~LayoutFileList()
55 ClassMap::const_iterator it
= classmap_
.begin();
56 ClassMap::const_iterator en
= classmap_
.end();
57 for (; it
!= en
; ++it
) {
62 LayoutFileList
& LayoutFileList::get()
64 static LayoutFileList baseclasslist
;
69 bool LayoutFileList::haveClass(string
const & classname
) const
71 ClassMap::const_iterator it
= classmap_
.begin();
72 ClassMap::const_iterator en
= classmap_
.end();
73 for (; it
!= en
; ++it
) {
74 if (it
->first
== classname
)
81 LayoutFile
const & LayoutFileList::operator[](string
const & classname
) const
83 LASSERT(haveClass(classname
), /**/);
84 return *classmap_
[classname
];
88 LayoutFile
& LayoutFileList::operator[](string
const & classname
)
90 LASSERT(haveClass(classname
), /**/);
91 return *classmap_
[classname
];
95 // Reads LyX textclass definitions according to textclass config file
96 bool LayoutFileList::read()
99 FileName
const real_file
= libFileSearch("", "textclass.lst");
100 LYXERR(Debug::TCLASS
, "Reading textclasses from `" << real_file
<< '\'');
102 if (real_file
.empty()) {
103 lyxerr
<< "LayoutFileList::Read: unable to find "
105 << to_utf8(makeDisplayPath(real_file
.absFilename(), 1000))
106 << "'. Exiting." << endl
;
108 // This causes LyX to end... Not a desirable behaviour. Lgb
109 // What do you propose? That the user gets a file dialog
110 // and is allowed to hunt for the file? (Asger)
111 // more that we have a layout for minimal.cls statically
112 // compiled in... (Lgb)
115 if (!lex
.setFile(real_file
)) {
116 lyxerr
<< "LayoutFileList::Read: "
117 "lyxlex was not able to set file: "
118 << real_file
<< endl
;
122 lyxerr
<< "LayoutFileList::Read: unable to open "
124 << to_utf8(makeDisplayPath(real_file
.absFilename(), 1000))
125 << "'\nCheck your installation. LyX can't continue."
130 bool finished
= false;
132 LYXERR(Debug::TCLASS
, "Starting parsing of textclass.lst");
133 while (lex
.isOK() && !finished
) {
134 LYXERR(Debug::TCLASS
, "\tline by line");
136 case Lexer::LEX_FEOF
:
140 string
const fname
= lex
.getString();
141 LYXERR(Debug::TCLASS
, "Fname: " << fname
);
144 string
const clname
= lex
.getString();
145 LYXERR(Debug::TCLASS
, "Clname: " << clname
);
148 string
const desc
= lex
.getString();
149 LYXERR(Debug::TCLASS
, "Desc: " << desc
);
152 bool avail
= lex
.getBool();
153 LYXERR(Debug::TCLASS
, "Avail: " << avail
);
154 // This code is run when we have
155 // fname, clname, desc, and avail
156 LayoutFile
* tmpl
= new LayoutFile(fname
, clname
, desc
, avail
);
157 if (lyxerr
.debugging(Debug::TCLASS
)) {
158 // only system layout files are loaded here so no
159 // buffer path is needed.
162 classmap_
[fname
] = tmpl
;
165 LYXERR(Debug::TCLASS
, "End of parsing of textclass.lst");
167 // lyx will start with an empty classmap_, but only reconfigure is allowed
168 // in this case. This gives users a second chance to configure lyx if
169 // initial configuration fails. (c.f. bug 2829)
170 if (classmap_
.empty())
171 lyxerr
<< "LayoutFileList::Read: no textclasses found!"
177 std::vector
<LayoutFileIndex
> LayoutFileList::classList() const
179 std::vector
<LayoutFileIndex
> cl
;
180 ClassMap::const_iterator it
= classmap_
.begin();
181 ClassMap::const_iterator en
= classmap_
.end();
182 for (; it
!= en
; ++it
)
183 cl
.push_back(it
->first
);
188 void LayoutFileList::reset(LayoutFileIndex
const & classname
) {
189 LASSERT(haveClass(classname
), /**/);
190 LayoutFile
* tc
= classmap_
[classname
];
192 new LayoutFile(tc
->name(), tc
->latexname(), tc
->description(),
193 tc
->isTeXClassAvailable());
194 classmap_
[classname
] = tmpl
;
199 LayoutFileIndex
LayoutFileList::addEmptyClass(string
const & textclass
)
201 if (haveClass(textclass
))
204 FileName
const tempLayout
= FileName::tempName();
205 ofstream
ofs(tempLayout
.toFilesystemEncoding().c_str());
206 ofs
<< "# This layout is automatically generated\n"
207 "# \\DeclareLaTeXClass{" << textclass
<< "}\n\n"
209 "Input stdclass.inc\n\n"
214 "DefaultStyle Standard\n\n"
216 " Category MainText\n"
218 " LatexType Paragraph\n"
223 " AlignPossible Block, Left, Right, Center\n"
224 " LabelType No_Label\n"
228 // We do not know if a LaTeX class is available for this document, but setting
229 // the last parameter to true will suppress a warning message about missing
231 LayoutFile
* tc
= new LayoutFile(textclass
, textclass
, "Unknown text class " + textclass
, true);
232 if (!tc
->load(tempLayout
.absFilename())) {
233 // The only way this happens is because the hardcoded layout file above
235 LASSERT(false, /**/);
237 classmap_
[textclass
] = tc
;
243 LayoutFileList::addLocalLayout(string
const & textclass
, string
const & path
)
245 // FIXME There is a bug here: 4593
247 // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
248 // NOTE: latex class name is defined in textclass.layout, which can be
249 // different from textclass
250 string fullName
= addName(path
, textclass
+ ".layout");
252 FileName
const layout_file(fullName
);
253 if (layout_file
.exists()) {
254 LYXERR(Debug::TCLASS
, "Adding class " << textclass
<< " from directory " << path
);
255 // Read .layout file and get description, real latex classname etc
257 // This is a C++ version of function processLayoutFile in configure.py,
258 // which uses the following regex
259 // \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
260 ifstream
ifs(layout_file
.toFilesystemEncoding().c_str());
261 static regex
const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
262 "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
264 while (getline(ifs
, line
)) {
265 // look for the \DeclareXXXClass line
267 if (regex_match(line
, sub
, reg
)) {
268 // returns: whole string, classtype (not used here), class name, description
269 LASSERT(sub
.size() == 4, /**/);
270 // now, create a TextClass with description containing path information
271 string
className(sub
.str(2) == "" ? textclass
: sub
.str(2));
273 new LayoutFile(textclass
, className
, textclass
, true);
274 // This textclass is added on request so it will definitely be
275 // used. Load it now because other load() calls may fail if they
276 // are called in a context without buffer path information.
278 // There will be only one textclass with this name, even if different
279 // layout files are loaded from different directories.
280 if (haveClass(textclass
)) {
281 LYXERR0("Existing textclass " << textclass
<< " is redefined by " << fullName
);
282 delete classmap_
[textclass
];
284 classmap_
[textclass
] = tmpl
;
289 // If .layout is not in local directory, or an invalid layout is found, return null
294 bool LayoutFileList::load(string
const & name
, string
const & buf_path
)
296 if (!haveClass(name
)) {
297 LYXERR0("Document class \"" << name
<< "\" does not exist.");
301 LayoutFile
* tc
= classmap_
[name
];
302 if (!tc
->load(buf_path
)) {
303 docstring s
= bformat(_("The document class %1$s "
304 "could not be loaded."), from_utf8(name
));
305 frontend::Alert::error(_("Could not load class"), s
);
312 LayoutFileIndex
defaultBaseclass()
314 if (LayoutFileList::get().haveClass("article"))
315 return string("article");
316 if (LayoutFileList::get().empty())
318 return LayoutFileList::get().classList().front();
323 // Reads the style files
326 LYXERR(Debug::TCLASS
, "LyXSetStyle: parsing configuration...");
328 if (!LayoutFileList::get().read()) {
329 LYXERR(Debug::TCLASS
, "LyXSetStyle: an error occured "
330 "during parsing.\n Exiting.");
334 LYXERR(Debug::TCLASS
, "LyXSetStyle: configuration parsed.");