2003-08-23 00:17:00 +00:00
|
|
|
|
/**
|
2008-02-24 16:59:49 +00:00
|
|
|
|
* \file BaseClassList.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author John Levon
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2001-12-28 13:26:54 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2008-02-24 16:59:49 +00:00
|
|
|
|
#include "BaseClassList.h"
|
2007-04-29 19:53:54 +00:00
|
|
|
|
#include "TextClass.h"
|
2007-04-26 11:30:54 +00:00
|
|
|
|
#include "Lexer.h"
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
|
#include "support/debug.h"
|
2007-12-17 16:04:46 +00:00
|
|
|
|
#include "support/FileName.h"
|
2001-12-28 13:26:54 +00:00
|
|
|
|
#include "support/filetools.h"
|
|
|
|
|
|
2004-11-06 16:14:22 +00:00
|
|
|
|
#include <boost/bind.hpp>
|
2006-04-09 04:35:24 +00:00
|
|
|
|
#include <boost/regex.hpp>
|
2007-10-18 19:29:32 +00:00
|
|
|
|
|
2006-04-09 04:35:24 +00:00
|
|
|
|
#include <fstream>
|
2004-11-06 16:14:22 +00:00
|
|
|
|
|
2007-12-12 18:57:56 +00:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace lyx::support;
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
|
2004-11-06 16:14:22 +00:00
|
|
|
|
using boost::bind;
|
2006-04-09 04:35:24 +00:00
|
|
|
|
using boost::regex;
|
|
|
|
|
using boost::smatch;
|
2004-11-06 16:14:22 +00:00
|
|
|
|
|
2008-02-28 14:49:01 +00:00
|
|
|
|
BaseClassList & BaseClassList::get()
|
|
|
|
|
{
|
|
|
|
|
static BaseClassList baseclasslist;
|
|
|
|
|
return baseclasslist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
// Gets textclass number from name
|
2008-02-24 16:29:40 +00:00
|
|
|
|
pair<bool, BaseClassIndex> const
|
2008-02-24 16:59:49 +00:00
|
|
|
|
BaseClassList::numberOfClass(string const & textclass) const
|
2001-12-28 13:26:54 +00:00
|
|
|
|
{
|
|
|
|
|
ClassList::const_iterator cit =
|
2002-07-21 21:21:06 +00:00
|
|
|
|
find_if(classlist_.begin(), classlist_.end(),
|
2004-11-06 16:14:22 +00:00
|
|
|
|
bind(equal_to<string>(),
|
2007-04-29 19:53:54 +00:00
|
|
|
|
bind(&TextClass::name, _1),
|
2004-11-06 16:14:22 +00:00
|
|
|
|
textclass));
|
|
|
|
|
|
2002-07-21 21:21:06 +00:00
|
|
|
|
return cit != classlist_.end() ?
|
2008-02-24 16:29:40 +00:00
|
|
|
|
make_pair(true, BaseClassIndex(cit - classlist_.begin())) :
|
|
|
|
|
make_pair(false, BaseClassIndex(0));
|
2001-12-28 13:26:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Gets a textclass structure from number
|
2007-04-29 19:53:54 +00:00
|
|
|
|
TextClass const &
|
2008-02-24 16:59:49 +00:00
|
|
|
|
BaseClassList::operator[](BaseClassIndex textclass) const
|
2001-12-28 13:26:54 +00:00
|
|
|
|
{
|
2007-09-11 16:42:22 +00:00
|
|
|
|
if (textclass >= classlist_.size())
|
2007-09-08 17:49:37 +00:00
|
|
|
|
return classlist_[0];
|
2007-09-11 16:42:22 +00:00
|
|
|
|
|
|
|
|
|
//FIXME I don't believe the following line is actually necessary (rgh)
|
|
|
|
|
classlist_[textclass].load();
|
|
|
|
|
return classlist_[textclass];
|
2001-12-28 13:26:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// used when sorting the textclass list.
|
2005-01-19 15:03:31 +00:00
|
|
|
|
class less_textclass_avail_desc
|
2007-12-12 19:28:07 +00:00
|
|
|
|
: public binary_function<TextClass, TextClass, int>
|
2004-01-31 15:30:24 +00:00
|
|
|
|
{
|
2005-01-19 15:03:31 +00:00
|
|
|
|
public:
|
2007-04-29 19:53:54 +00:00
|
|
|
|
int operator()(TextClass const & tc1,
|
|
|
|
|
TextClass const & tc2) const
|
2004-01-31 15:30:24 +00:00
|
|
|
|
{
|
2003-05-03 19:24:36 +00:00
|
|
|
|
// Ordering criteria:
|
|
|
|
|
// 1. Availability of text class
|
|
|
|
|
// 2. Description (lexicographic)
|
|
|
|
|
|
|
|
|
|
return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
|
2004-01-31 15:30:24 +00:00
|
|
|
|
(tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
|
|
|
|
|
tc1.description() < tc2.description());
|
2001-12-28 13:26:54 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Reads LyX textclass definitions according to textclass config file
|
2008-02-24 16:59:49 +00:00
|
|
|
|
bool BaseClassList::read()
|
2001-12-28 13:26:54 +00:00
|
|
|
|
{
|
2007-04-26 11:30:54 +00:00
|
|
|
|
Lexer lex(0, 0);
|
2007-12-12 19:57:42 +00:00
|
|
|
|
FileName const real_file = libFileSearch("", "textclass.lst");
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
|
|
|
|
if (real_file.empty()) {
|
2008-02-24 16:59:49 +00:00
|
|
|
|
lyxerr << "BaseClassList::Read: unable to find "
|
2007-05-28 22:27:45 +00:00
|
|
|
|
"textclass file `"
|
2006-11-26 21:30:39 +00:00
|
|
|
|
<< to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
|
2001-12-28 13:26:54 +00:00
|
|
|
|
<< "'. Exiting." << endl;
|
|
|
|
|
return false;
|
|
|
|
|
// This causes LyX to end... Not a desirable behaviour. Lgb
|
|
|
|
|
// What do you propose? That the user gets a file dialog
|
|
|
|
|
// and is allowed to hunt for the file? (Asger)
|
|
|
|
|
// more that we have a layout for minimal.cls statically
|
|
|
|
|
// compiled in... (Lgb)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!lex.setFile(real_file)) {
|
2008-02-24 16:59:49 +00:00
|
|
|
|
lyxerr << "BaseClassList::Read: "
|
2001-12-28 13:26:54 +00:00
|
|
|
|
"lyxlex was not able to set file: "
|
|
|
|
|
<< real_file << endl;
|
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
if (!lex.isOK()) {
|
2008-02-24 16:59:49 +00:00
|
|
|
|
lyxerr << "BaseClassList::Read: unable to open "
|
2007-05-28 22:27:45 +00:00
|
|
|
|
"textclass file `"
|
2006-11-26 21:30:39 +00:00
|
|
|
|
<< to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
|
2001-12-28 13:26:54 +00:00
|
|
|
|
<< "'\nCheck your installation. LyX can't continue."
|
|
|
|
|
<< endl;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
return false;
|
2001-12-28 13:26:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool finished = false;
|
|
|
|
|
// Parse config-file
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
|
2001-12-28 13:26:54 +00:00
|
|
|
|
while (lex.isOK() && !finished) {
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "\tline by line");
|
2001-12-28 13:26:54 +00:00
|
|
|
|
switch (lex.lex()) {
|
2007-04-26 11:30:54 +00:00
|
|
|
|
case Lexer::LEX_FEOF:
|
2001-12-28 13:26:54 +00:00
|
|
|
|
finished = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
string const fname = lex.getString();
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "Fname: " << fname);
|
2001-12-28 13:26:54 +00:00
|
|
|
|
if (lex.next()) {
|
|
|
|
|
string const clname = lex.getString();
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "Clname: " << clname);
|
2001-12-28 13:26:54 +00:00
|
|
|
|
if (lex.next()) {
|
2002-05-29 13:28:11 +00:00
|
|
|
|
string const desc = lex.getString();
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "Desc: " << desc);
|
2003-05-03 19:24:36 +00:00
|
|
|
|
if (lex.next()) {
|
|
|
|
|
bool avail = lex.getBool();
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "Avail: " << avail);
|
2003-05-03 19:24:36 +00:00
|
|
|
|
// This code is run when we have
|
|
|
|
|
// fname, clname, desc, and avail
|
2007-04-29 19:53:54 +00:00
|
|
|
|
TextClass tmpl(fname, clname, desc, avail);
|
2003-05-03 19:24:36 +00:00
|
|
|
|
if (lyxerr.debugging(Debug::TCLASS)) {
|
2008-02-24 06:14:48 +00:00
|
|
|
|
// only system layout files are loaded here so no
|
|
|
|
|
// buffer path is needed.
|
2003-05-03 19:24:36 +00:00
|
|
|
|
tmpl.load();
|
|
|
|
|
}
|
|
|
|
|
classlist_.push_back(tmpl);
|
2002-05-29 13:28:11 +00:00
|
|
|
|
}
|
2001-12-28 13:26:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
2007-09-26 19:36:09 +00:00
|
|
|
|
// lyx will start with an empty classlist_, but only reconfigure is allowed
|
|
|
|
|
// in this case. This gives users a second chance to configure lyx if
|
|
|
|
|
// initial configuration fails. (c.f. bug 2829)
|
|
|
|
|
if (classlist_.empty())
|
2008-02-24 16:59:49 +00:00
|
|
|
|
lyxerr << "BaseClassList::Read: no textclasses found!"
|
2001-12-28 13:26:54 +00:00
|
|
|
|
<< endl;
|
2007-09-26 19:36:09 +00:00
|
|
|
|
else
|
|
|
|
|
// Ok everything loaded ok, now sort the list.
|
|
|
|
|
sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
|
2001-12-28 13:26:54 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-02-24 16:59:49 +00:00
|
|
|
|
void BaseClassList::reset(BaseClassIndex const textclass) {
|
2007-09-11 16:42:22 +00:00
|
|
|
|
if (textclass >= classlist_.size())
|
|
|
|
|
return;
|
|
|
|
|
TextClass const & tc = classlist_[textclass];
|
|
|
|
|
TextClass tmpl(tc.name(), tc.latexname(), tc.description(),
|
|
|
|
|
tc.isTeXClassAvailable());
|
|
|
|
|
classlist_[textclass] = tmpl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-02-24 16:29:40 +00:00
|
|
|
|
pair<bool, BaseClassIndex> const
|
2008-02-24 16:59:49 +00:00
|
|
|
|
BaseClassList::addTextClass(string const & textclass, string const & path)
|
2006-04-09 04:35:24 +00:00
|
|
|
|
{
|
|
|
|
|
// only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
|
|
|
|
|
// NOTE: latex class name is defined in textclass.layout, which can be different from textclass
|
2007-02-20 19:38:15 +00:00
|
|
|
|
FileName const layout_file(addName(path, textclass + ".layout"));
|
2007-10-18 19:29:32 +00:00
|
|
|
|
if (layout_file.exists()) {
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
|
2006-04-09 04:35:24 +00:00
|
|
|
|
// Read .layout file and get description, real latex classname etc
|
|
|
|
|
//
|
|
|
|
|
// This is a C++ version of function processLayoutFile in configure.py,
|
|
|
|
|
// which uses the following regex
|
2006-08-23 12:55:23 +00:00
|
|
|
|
// \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
|
2006-11-29 21:47:37 +00:00
|
|
|
|
ifstream ifs(layout_file.toFilesystemEncoding().c_str());
|
2006-08-23 12:55:23 +00:00
|
|
|
|
static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
|
2006-04-09 04:35:24 +00:00
|
|
|
|
"(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
|
|
|
|
|
string line;
|
|
|
|
|
while (getline(ifs, line)) {
|
|
|
|
|
// look for the \DeclareXXXClass line
|
|
|
|
|
smatch sub;
|
|
|
|
|
if (regex_match(line, sub, reg)) {
|
2008-02-28 04:05:38 +00:00
|
|
|
|
// returns: whole string, classtype (not used here), class name, description
|
|
|
|
|
BOOST_ASSERT(sub.size() == 4);
|
2007-12-05 22:28:16 +00:00
|
|
|
|
// now, create a TextClass with description containing path information
|
2008-02-28 04:05:38 +00:00
|
|
|
|
TextClass tmpl(textclass, sub.str(2) == "" ? textclass : sub.str(2),
|
2006-04-09 04:35:24 +00:00
|
|
|
|
sub.str(3) + " <" + path + ">", true);
|
|
|
|
|
if (lyxerr.debugging(Debug::TCLASS))
|
|
|
|
|
tmpl.load(path);
|
2007-12-05 22:28:16 +00:00
|
|
|
|
// Do not add this local TextClass to classlist_ if it has
|
|
|
|
|
// already been loaded by, for example, a master buffer.
|
2008-02-28 14:49:01 +00:00
|
|
|
|
pair<bool, lyx::BaseClassIndex> pp = numberOfClass(textclass);
|
2007-12-05 22:28:16 +00:00
|
|
|
|
// only layouts from the same directory are considered to be identical.
|
|
|
|
|
if (pp.first && classlist_[pp.second].description() == tmpl.description())
|
|
|
|
|
return pp;
|
2006-04-09 04:35:24 +00:00
|
|
|
|
classlist_.push_back(tmpl);
|
2008-02-24 06:14:48 +00:00
|
|
|
|
// This textclass is added on request so it will definitely be
|
|
|
|
|
// used. Load it now because other load() calls may fail if they
|
|
|
|
|
// are called in a context without buffer path information.
|
|
|
|
|
classlist_.back().load(path);
|
2006-04-09 04:35:24 +00:00
|
|
|
|
return make_pair(true, classlist_.size() - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If .layout is not in local directory, or an invalid layout is found, return false
|
2008-02-24 16:29:40 +00:00
|
|
|
|
return make_pair(false, BaseClassIndex(0));
|
2006-04-09 04:35:24 +00:00
|
|
|
|
}
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2006-04-09 04:35:24 +00:00
|
|
|
|
|
2003-08-02 11:30:30 +00:00
|
|
|
|
|
2008-02-24 16:59:49 +00:00
|
|
|
|
BaseClassIndex defaultBaseclass()
|
2007-10-21 10:50:56 +00:00
|
|
|
|
{
|
|
|
|
|
// We want to return the article class. if `first' is
|
|
|
|
|
// true in the returned pair, then `second' is the textclass
|
|
|
|
|
// number; if it is false, second is 0. In both cases, second
|
|
|
|
|
// is what we want.
|
2008-02-28 14:49:01 +00:00
|
|
|
|
return BaseClassList::get().numberOfClass("article").second;
|
2007-10-21 10:50:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
// Reads the style files
|
2006-07-05 17:01:26 +00:00
|
|
|
|
bool LyXSetStyle()
|
2001-12-28 13:26:54 +00:00
|
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2008-02-28 14:49:01 +00:00
|
|
|
|
if (!BaseClassList::get().read()) {
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
|
|
|
|
|
"during parsing.\n Exiting.");
|
2006-07-05 17:01:26 +00:00
|
|
|
|
return false;
|
2001-12-28 13:26:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
|
LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");
|
2006-07-05 17:01:26 +00:00
|
|
|
|
return true;
|
2001-12-28 13:26:54 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|