2003-08-23 00:17:00 +00:00
|
|
|
|
/**
|
2007-04-29 19:53:54 +00:00
|
|
|
|
* \file TextClassList.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>
|
|
|
|
|
|
2007-04-29 19:53:54 +00:00
|
|
|
|
#include "TextClassList.h"
|
|
|
|
|
#include "TextClass.h"
|
2001-12-28 13:26:54 +00:00
|
|
|
|
#include "debug.h"
|
2007-04-26 11:30:54 +00:00
|
|
|
|
#include "Lexer.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>
|
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
|
#include <fstream>
|
2004-11-06 16:14:22 +00:00
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
namespace lyx {
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
|
2006-11-29 21:47:37 +00:00
|
|
|
|
using support::FileName;
|
2007-02-20 19:38:15 +00:00
|
|
|
|
using support::addName;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
using support::libFileSearch;
|
|
|
|
|
using support::makeDisplayPath;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
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
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
using std::endl;
|
2004-11-06 16:14:22 +00:00
|
|
|
|
using std::equal_to;
|
2002-01-07 14:17:54 +00:00
|
|
|
|
using std::find_if;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::make_pair;
|
2002-01-07 14:17:54 +00:00
|
|
|
|
using std::sort;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::pair;
|
2006-04-09 04:35:24 +00:00
|
|
|
|
using std::ifstream;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
|
|
|
|
// Gets textclass number from name
|
|
|
|
|
pair<bool, textclass_type> const
|
2007-04-29 19:53:54 +00:00
|
|
|
|
TextClassList::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() ?
|
|
|
|
|
make_pair(true, textclass_type(cit - classlist_.begin())) :
|
2001-12-28 13:26:54 +00:00
|
|
|
|
make_pair(false, textclass_type(0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Gets a textclass structure from number
|
2007-04-29 19:53:54 +00:00
|
|
|
|
TextClass const &
|
|
|
|
|
TextClassList::operator[](textclass_type 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-04-29 19:53:54 +00:00
|
|
|
|
: public std::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
|
2007-04-29 19:53:54 +00:00
|
|
|
|
bool TextClassList::read()
|
2001-12-28 13:26:54 +00:00
|
|
|
|
{
|
2007-04-26 11:30:54 +00:00
|
|
|
|
Lexer lex(0, 0);
|
2006-11-26 21:30:39 +00:00
|
|
|
|
support::FileName const real_file = libFileSearch("", "textclass.lst");
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "Reading textclasses from `"
|
2002-11-27 10:30:28 +00:00
|
|
|
|
<< real_file << '\'' << endl;
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
|
|
|
|
if (real_file.empty()) {
|
2007-04-29 19:53:54 +00:00
|
|
|
|
lyxerr << "TextClassList::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)) {
|
2007-04-29 19:53:54 +00:00
|
|
|
|
lyxerr << "TextClassList::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()) {
|
2007-04-29 19:53:54 +00:00
|
|
|
|
lyxerr << "TextClassList::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-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "Starting parsing of textclass.lst" << endl;
|
2001-12-28 13:26:54 +00:00
|
|
|
|
while (lex.isOK() && !finished) {
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "\tline by line" << endl;
|
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-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "Fname: " << fname << endl;
|
2001-12-28 13:26:54 +00:00
|
|
|
|
if (lex.next()) {
|
|
|
|
|
string const clname = lex.getString();
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "Clname: " << clname << endl;
|
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-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "Desc: " << desc << endl;
|
2003-05-03 19:24:36 +00:00
|
|
|
|
if (lex.next()) {
|
|
|
|
|
bool avail = lex.getBool();
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "Avail: " << avail << endl;
|
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)) {
|
|
|
|
|
tmpl.load();
|
|
|
|
|
}
|
|
|
|
|
classlist_.push_back(tmpl);
|
2002-05-29 13:28:11 +00:00
|
|
|
|
}
|
2001-12-28 13:26:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "End of parsing of textclass.lst" << endl;
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
2002-07-21 21:21:06 +00:00
|
|
|
|
if (classlist_.empty()) {
|
2007-04-29 19:53:54 +00:00
|
|
|
|
lyxerr << "TextClassList::Read: no textclasses found!"
|
2001-12-28 13:26:54 +00:00
|
|
|
|
<< endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Ok everything loaded ok, now sort the list.
|
2003-05-03 19:24:36 +00:00
|
|
|
|
sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
|
2001-12-28 13:26:54 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-09-11 16:42:22 +00:00
|
|
|
|
void TextClassList::reset(textclass_type const textclass) {
|
|
|
|
|
if (textclass >= classlist_.size())
|
|
|
|
|
return;
|
|
|
|
|
TextClass const & tc = classlist_[textclass];
|
|
|
|
|
TextClass tmpl(tc.name(), tc.latexname(), tc.description(),
|
|
|
|
|
tc.isTeXClassAvailable());
|
|
|
|
|
classlist_[textclass] = tmpl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
std::pair<bool, textclass_type> const
|
2007-04-29 19:53:54 +00:00
|
|
|
|
TextClassList::addTextClass(std::string const & textclass, std::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"));
|
2006-11-29 21:47:37 +00:00
|
|
|
|
if (fs::exists(layout_file.toFilesystemEncoding())) {
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "Adding class " << textclass << " from directory " << path << endl;
|
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)) {
|
|
|
|
|
// returns: whole string, classtype (not used here), first option, description
|
|
|
|
|
BOOST_ASSERT(sub.size()==4);
|
|
|
|
|
// now, add the layout to textclass.
|
2007-05-28 22:27:45 +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);
|
|
|
|
|
classlist_.push_back(tmpl);
|
|
|
|
|
return make_pair(true, classlist_.size() - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If .layout is not in local directory, or an invalid layout is found, return false
|
|
|
|
|
return make_pair(false, textclass_type(0));
|
|
|
|
|
}
|
2007-05-28 22:27:45 +00:00
|
|
|
|
|
2006-04-09 04:35:24 +00:00
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
// Global variable: textclass table.
|
2007-04-29 19:53:54 +00:00
|
|
|
|
TextClassList textclasslist;
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
2003-08-02 11:30:30 +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-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "LyXSetStyle: parsing configuration..." << endl;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2006-04-09 00:26:19 +00:00
|
|
|
|
if (!textclasslist.read()) {
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "LyXSetStyle: an error occured "
|
2001-12-28 13:26:54 +00:00
|
|
|
|
"during parsing.\n Exiting." << endl;
|
2006-07-05 17:01:26 +00:00
|
|
|
|
return false;
|
2001-12-28 13:26:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::TCLASS) << "LyXSetStyle: configuration parsed." << endl;
|
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
|