2003-08-22 09:44:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file counters.C
|
|
|
|
|
* 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-22 09:44:59 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Martin Vermeer
|
2000-07-24 13:53:19 +00:00
|
|
|
|
*
|
2003-08-22 09:44:59 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
2000-07-24 13:53:19 +00:00
|
|
|
|
|
2000-07-17 18:27:53 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "counters.h"
|
|
|
|
|
#include "debug.h"
|
2003-09-05 17:23:11 +00:00
|
|
|
|
#include "support/std_sstream.h"
|
2002-09-06 14:48:01 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
#include "support/lstrings.h"
|
2002-08-11 20:34:20 +00:00
|
|
|
|
#include "support/LAssert.h"
|
2000-07-17 18:27:53 +00:00
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
using namespace lyx::support;
|
|
|
|
|
|
2000-07-17 18:27:53 +00:00
|
|
|
|
using std::endl;
|
2002-08-06 22:40:59 +00:00
|
|
|
|
using std::vector;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Counter::Counter()
|
|
|
|
|
{
|
|
|
|
|
reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::set(int v)
|
|
|
|
|
{
|
|
|
|
|
value_ = v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::addto(int v)
|
|
|
|
|
{
|
|
|
|
|
value_ += v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int Counter::value() const
|
|
|
|
|
{
|
|
|
|
|
return value_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::step()
|
|
|
|
|
{
|
|
|
|
|
++value_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::reset()
|
|
|
|
|
{
|
|
|
|
|
value_ = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-06 14:48:01 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
string Counter::master() const
|
|
|
|
|
{
|
|
|
|
|
return master_;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-06 14:48:01 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
void Counter::setMaster(string const & m)
|
|
|
|
|
{
|
|
|
|
|
master_ = m;
|
|
|
|
|
}
|
|
|
|
|
|
2000-07-17 18:27:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counters::newCounter(string const & newc)
|
|
|
|
|
{
|
|
|
|
|
// First check if newc already exist
|
2000-09-26 13:54:57 +00:00
|
|
|
|
CounterList::iterator cit = counterList.find(newc);
|
2002-08-06 22:40:59 +00:00
|
|
|
|
// if already exist give warning and return
|
2000-07-17 18:27:53 +00:00
|
|
|
|
if (cit != counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "The new counter already exists." << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
counterList[newc];
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
void Counters::newCounter(string const & newc, string const & masterc)
|
2000-07-17 18:27:53 +00:00
|
|
|
|
{
|
2002-08-06 22:40:59 +00:00
|
|
|
|
// First check if newc already exists
|
2000-09-26 13:54:57 +00:00
|
|
|
|
CounterList::iterator cit = counterList.find(newc);
|
2000-07-17 18:27:53 +00:00
|
|
|
|
// if already existant give warning and return
|
|
|
|
|
if (cit != counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "The new counter already exists." << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
// then check if masterc exists
|
|
|
|
|
CounterList::iterator it = counterList.find(masterc);
|
2000-07-17 18:27:53 +00:00
|
|
|
|
// if not give warning and return
|
|
|
|
|
if (it == counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "The master counter does not exist." << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-06 14:48:01 +00:00
|
|
|
|
counterList[newc].setMaster(masterc);
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
void Counters::set(string const & ctr, int val)
|
2000-07-17 18:27:53 +00:00
|
|
|
|
{
|
|
|
|
|
CounterList::iterator it = counterList.find(ctr);
|
|
|
|
|
if (it == counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "set: Counter does not exist: " << ctr << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
it->second.set(val);
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counters::addto(string const & ctr, int val)
|
|
|
|
|
{
|
|
|
|
|
CounterList::iterator it = counterList.find(ctr);
|
|
|
|
|
if (it == counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "addto: Counter does not exist: " << ctr << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
it->second.addto(val);
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
int Counters::value(string const & ctr) const
|
2000-07-17 18:27:53 +00:00
|
|
|
|
{
|
|
|
|
|
CounterList::const_iterator cit = counterList.find(ctr);
|
|
|
|
|
if (cit == counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "value: Counter does not exist: " << ctr << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
return cit->second.value();
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Counters::step(string const & ctr)
|
|
|
|
|
{
|
|
|
|
|
CounterList::iterator it = counterList.find(ctr);
|
|
|
|
|
if (it == counterList.end()) {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
lyxerr << "step: Counter does not exist: " << ctr << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
|
|
|
|
|
it->second.step();
|
|
|
|
|
it = counterList.begin();
|
|
|
|
|
CounterList::iterator end = counterList.end();
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
if (it->second.master() == ctr) {
|
|
|
|
|
it->second.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-11 20:34:20 +00:00
|
|
|
|
|
|
|
|
|
void Counters::reset()
|
|
|
|
|
{
|
|
|
|
|
CounterList::iterator it = counterList.begin();
|
|
|
|
|
CounterList::iterator end = counterList.end();
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
it->second.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
void Counters::reset(string const & match)
|
|
|
|
|
{
|
2003-06-30 23:56:22 +00:00
|
|
|
|
Assert(!match.empty());
|
2002-08-11 20:34:20 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
CounterList::iterator it = counterList.begin();
|
|
|
|
|
CounterList::iterator end = counterList.end();
|
|
|
|
|
for (; it != end; ++it) {
|
2002-08-11 20:34:20 +00:00
|
|
|
|
if (it->first.find(match) != string::npos)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
it->second.reset();
|
2002-08-09 12:14:54 +00:00
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
void Counters::copy(Counters & from, Counters & to, string const & match)
|
|
|
|
|
{
|
|
|
|
|
CounterList::iterator it = counterList.begin();
|
|
|
|
|
CounterList::iterator end = counterList.end();
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
|
if (it->first.find(match) != string::npos || match == "") {
|
|
|
|
|
to.set(it->first, from.value(it->first));
|
2002-08-09 12:14:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
2002-08-09 12:14:54 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
inline
|
|
|
|
|
char loweralphaCounter(int n)
|
|
|
|
|
{
|
|
|
|
|
if (n < 1 || n > 26)
|
|
|
|
|
return '?';
|
|
|
|
|
else
|
|
|
|
|
return 'a' + n - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
inline
|
|
|
|
|
char alphaCounter(int n)
|
|
|
|
|
{
|
|
|
|
|
if (n < 1 || n > 26)
|
|
|
|
|
return '?';
|
|
|
|
|
else
|
|
|
|
|
return 'A' + n - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
inline
|
|
|
|
|
char hebrewCounter(int n)
|
|
|
|
|
{
|
|
|
|
|
static const char hebrew[22] = {
|
|
|
|
|
'<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>',
|
|
|
|
|
'<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>',
|
|
|
|
|
'<EFBFBD>', '<EFBFBD>', '<EFBFBD>', '<EFBFBD>'
|
|
|
|
|
};
|
|
|
|
|
if (n < 1 || n > 22)
|
|
|
|
|
return '?';
|
|
|
|
|
else
|
|
|
|
|
return hebrew[n-1];
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
inline
|
|
|
|
|
string const romanCounter(int n)
|
|
|
|
|
{
|
|
|
|
|
static char const * roman[20] = {
|
|
|
|
|
"i", "ii", "iii", "iv", "v",
|
|
|
|
|
"vi", "vii", "viii", "ix", "x",
|
|
|
|
|
"xi", "xii", "xiii", "xiv", "xv",
|
|
|
|
|
"xvi", "xvii", "xviii", "xix", "xx"
|
|
|
|
|
};
|
|
|
|
|
if (n < 1 || n > 20)
|
|
|
|
|
return "??";
|
|
|
|
|
else
|
|
|
|
|
return roman[n-1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
string Counters::labelItem(string const & ctr,
|
2002-08-12 20:24:10 +00:00
|
|
|
|
string const & numbertype,
|
|
|
|
|
string const & langtype,
|
|
|
|
|
bool first)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
{
|
2002-09-05 12:58:10 +00:00
|
|
|
|
ostringstream s;
|
|
|
|
|
ostringstream o;
|
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
CounterList::iterator it = counterList.find(ctr);
|
|
|
|
|
if (it == counterList.end()) {
|
|
|
|
|
lyxerr << "Counter does not exist." << endl;
|
2002-11-27 10:30:28 +00:00
|
|
|
|
return string();
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
2002-09-05 12:58:10 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
if (!first) {
|
2002-11-27 10:30:28 +00:00
|
|
|
|
s << '.' << value(ctr);
|
2002-08-06 22:40:59 +00:00
|
|
|
|
} else {
|
|
|
|
|
if (numbertype == "sectioning" || numbertype == "appendix") {
|
|
|
|
|
if (numbertype == "appendix") {
|
|
|
|
|
if (langtype == "hebrew") {
|
|
|
|
|
o << hebrewCounter(value(ctr));
|
|
|
|
|
} else {
|
|
|
|
|
o << alphaCounter(value(ctr));
|
|
|
|
|
}
|
|
|
|
|
} else o << value(ctr);
|
|
|
|
|
}
|
|
|
|
|
s << o.str();
|
|
|
|
|
}
|
2002-11-04 02:12:42 +00:00
|
|
|
|
|
|
|
|
|
return STRCONV(s.str());
|
2002-08-06 22:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
string Counters::numberLabel(string const & ctr,
|
2002-08-12 20:24:10 +00:00
|
|
|
|
string const & numbertype,
|
|
|
|
|
string const & langtype,
|
|
|
|
|
int head)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
{
|
2002-11-04 02:12:42 +00:00
|
|
|
|
ostringstream s;
|
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
if (numbertype == "sectioning" || numbertype == "appendix") {
|
|
|
|
|
if (ctr == "chapter" && head == 0) {
|
|
|
|
|
s << labelItem("chapter", numbertype, langtype, true);
|
|
|
|
|
} else if (ctr == "section" && head <= 1) {
|
|
|
|
|
s << numberLabel("chapter", numbertype, langtype, head)
|
|
|
|
|
<< labelItem("section", numbertype, langtype, head == 1);
|
|
|
|
|
} else if (ctr == "subsection" && head <= 2) {
|
2002-08-09 12:14:54 +00:00
|
|
|
|
s << numberLabel("section", numbertype, langtype, head)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
<< labelItem("subsection", numbertype, langtype, head == 2);
|
|
|
|
|
} else if (ctr == "subsubsection" && head <= 3) {
|
2002-08-09 12:14:54 +00:00
|
|
|
|
s << numberLabel("subsection", numbertype, langtype, head)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
<< labelItem("subsubsection", numbertype, langtype, head == 3);
|
|
|
|
|
} else if (ctr == "paragraph" && head <= 4) {
|
2002-08-09 12:14:54 +00:00
|
|
|
|
s << numberLabel("subsubsection", numbertype, langtype, head)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
<< labelItem("paragraph", numbertype, langtype, head == 4);
|
|
|
|
|
} else if (ctr == "subparagraph" && head <= 5) {
|
2002-08-09 12:14:54 +00:00
|
|
|
|
s << numberLabel("paragraph", numbertype, langtype, head)
|
2002-08-06 22:40:59 +00:00
|
|
|
|
<< labelItem("subparagraph", numbertype, langtype, head == 5);
|
2002-09-05 12:58:10 +00:00
|
|
|
|
} else if (ctr == "figure" || ctr == "table") {
|
2002-08-06 22:40:59 +00:00
|
|
|
|
// figure, table, ...
|
|
|
|
|
lyxerr << "Counter:" << ctr << endl;
|
|
|
|
|
s << numberLabel("chapter", numbertype, langtype, head)
|
|
|
|
|
<< labelItem(ctr, numbertype, langtype, head == 1);
|
|
|
|
|
}
|
2002-08-09 12:14:54 +00:00
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
} else if (numbertype == "enumeration") {
|
2002-09-06 14:48:01 +00:00
|
|
|
|
ostringstream ei;
|
|
|
|
|
ostringstream eii;
|
|
|
|
|
ostringstream eiii;
|
|
|
|
|
ostringstream eiv;
|
|
|
|
|
|
2002-08-06 22:40:59 +00:00
|
|
|
|
if (langtype == "hebrew") {
|
|
|
|
|
ei << '.' << value("enumi");
|
|
|
|
|
eii << '(' << hebrewCounter(value("enumii")) << ')';
|
|
|
|
|
eiii << '.' << romanCounter(value("enumiii"));
|
|
|
|
|
eiv << '.' << alphaCounter(value("enumiv"));
|
|
|
|
|
} else {
|
|
|
|
|
ei << value("enumi") << '.';
|
|
|
|
|
eii << '(' << loweralphaCounter(value("enumii")) << ')';
|
|
|
|
|
eiii << romanCounter(value("enumiii")) << '.';
|
|
|
|
|
eiv << alphaCounter(value("enumiv")) << '.';
|
|
|
|
|
}
|
|
|
|
|
if (ctr == "enumii") {
|
2002-08-09 12:14:54 +00:00
|
|
|
|
s << eii.str();
|
2002-08-06 22:40:59 +00:00
|
|
|
|
} else if (ctr == "enumi") {
|
2002-08-09 12:14:54 +00:00
|
|
|
|
s << ei.str();
|
2002-08-06 22:40:59 +00:00
|
|
|
|
} else if (ctr == "enumiii") {
|
|
|
|
|
s << eiii.str();
|
|
|
|
|
} else if (ctr == "enumiv") {
|
|
|
|
|
s << eiv.str();
|
|
|
|
|
}
|
2002-08-09 12:14:54 +00:00
|
|
|
|
}
|
2002-11-04 02:12:42 +00:00
|
|
|
|
|
|
|
|
|
return STRCONV(s.str());
|
2000-07-17 18:27:53 +00:00
|
|
|
|
}
|