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 "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>
33 using namespace lyx::support
;
41 LayoutFile::LayoutFile(string
const & fn
, string
const & cln
,
42 string
const & desc
, bool texClassAvail
)
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
) {
59 LayoutFileList
& LayoutFileList::get()
61 static LayoutFileList 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
)
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()
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 "
102 << to_utf8(makeDisplayPath(real_file
.absFilename(), 1000))
103 << "'. Exiting." << endl
;
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
;
119 lyxerr
<< "LayoutFileList::Read: unable to open "
121 << to_utf8(makeDisplayPath(real_file
.absFilename(), 1000))
122 << "'\nCheck your installation. LyX can't continue."
127 bool finished
= false;
129 LYXERR(Debug::TCLASS
, "Starting parsing of textclass.lst");
130 while (lex
.isOK() && !finished
) {
131 LYXERR(Debug::TCLASS
, "\tline by line");
133 case Lexer::LEX_FEOF
:
137 string
const fname
= lex
.getString();
138 LYXERR(Debug::TCLASS
, "Fname: " << fname
);
140 string
const clname
= lex
.getString();
141 LYXERR(Debug::TCLASS
, "Clname: " << clname
);
143 string
const desc
= lex
.getString();
144 LYXERR(Debug::TCLASS
, "Desc: " << desc
);
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.
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!"
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
);
185 void LayoutFileList::reset(LayoutFileIndex
const & classname
) {
186 LASSERT(haveClass(classname
), /**/);
187 LayoutFile
* tc
= classmap_
[classname
];
189 new LayoutFile(tc
->name(), tc
->latexname(), tc
->description(),
190 tc
->isTeXClassAvailable());
191 classmap_
[classname
] = tmpl
;
196 LayoutFileIndex
LayoutFileList::addEmptyClass(string
const & textclass
)
198 if (haveClass(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"
206 "Input stdclass.inc\n\n"
211 "DefaultStyle Standard\n\n"
213 " Category MainText\n"
215 " LatexType Paragraph\n"
220 " AlignPossible Block, Left, Right, Center\n"
221 " LabelType No_Label\n"
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
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
232 LASSERT(false, /**/);
234 classmap_
[textclass
] = tc
;
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*");
261 while (getline(ifs
, line
)) {
262 // look for the \DeclareXXXClass line
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));
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.
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
;
286 // If .layout is not in local directory, or an invalid layout is found, return null
291 LayoutFileIndex
defaultBaseclass()
293 if (LayoutFileList::get().haveClass("article"))
294 return string("article");
295 if (LayoutFileList::get().empty())
297 return LayoutFileList::get().classList().front();
302 // Reads the style files
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.");
313 LYXERR(Debug::TCLASS
, "LyXSetStyle: configuration parsed.");