2003-08-22 09:44:59 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file Counters.cpp
|
2003-08-22 09:44:59 +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
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-22 09:44:59 +00:00
|
|
|
* \author Martin Vermeer
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
2007-08-08 15:10:55 +00:00
|
|
|
* \author Richard Heck (roman numerals)
|
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>
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Counters.h"
|
2010-01-20 19:03:17 +00:00
|
|
|
#include "Layout.h"
|
2008-10-06 14:37:28 +00:00
|
|
|
#include "Lexer.h"
|
|
|
|
|
2005-01-06 16:39:35 +00:00
|
|
|
#include "support/convert.h"
|
2020-05-01 01:56:58 +00:00
|
|
|
#include "support/counter_reps.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2009-07-12 20:09:53 +00:00
|
|
|
#include "support/gettext.h"
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2009-04-19 22:44:46 +00:00
|
|
|
#include "support/lstrings.h"
|
2000-07-17 18:27:53 +00:00
|
|
|
|
2009-04-19 23:03:50 +00:00
|
|
|
#include <algorithm>
|
2004-07-24 10:55:30 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2000-07-17 18:27:53 +00:00
|
|
|
|
2007-07-17 17:40:44 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
2000-07-17 18:27:53 +00:00
|
|
|
Counter::Counter()
|
2020-06-18 09:23:26 +00:00
|
|
|
: initial_value_(0), saved_value_(0)
|
2000-07-17 18:27:53 +00:00
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-20 14:57:45 +00:00
|
|
|
Counter::Counter(docstring const & mc, docstring const & ls,
|
2020-05-03 03:11:53 +00:00
|
|
|
docstring const & lsa, docstring const & guiname)
|
2020-06-18 09:23:26 +00:00
|
|
|
: initial_value_(0), saved_value_(0), master_(mc), labelstring_(ls),
|
2020-05-03 03:11:53 +00:00
|
|
|
labelstringappendix_(lsa), guiname_(guiname)
|
2007-08-16 11:07:00 +00:00
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-06 14:37:28 +00:00
|
|
|
bool Counter::read(Lexer & lex)
|
|
|
|
{
|
|
|
|
enum {
|
|
|
|
CT_WITHIN = 1,
|
|
|
|
CT_LABELSTRING,
|
|
|
|
CT_LABELSTRING_APPENDIX,
|
2010-01-20 20:33:36 +00:00
|
|
|
CT_PRETTYFORMAT,
|
2013-06-01 21:42:08 +00:00
|
|
|
CT_INITIALVALUE,
|
2020-05-03 03:00:17 +00:00
|
|
|
CT_GUINAME,
|
2008-10-06 14:37:28 +00:00
|
|
|
CT_END
|
|
|
|
};
|
|
|
|
|
|
|
|
LexerKeyword counterTags[] = {
|
|
|
|
{ "end", CT_END },
|
2020-05-03 03:00:17 +00:00
|
|
|
{ "guiname", CT_GUINAME },
|
|
|
|
{ "initialvalue", CT_INITIALVALUE},
|
2008-10-06 14:37:28 +00:00
|
|
|
{ "labelstring", CT_LABELSTRING },
|
|
|
|
{ "labelstringappendix", CT_LABELSTRING_APPENDIX },
|
2010-01-20 20:33:36 +00:00
|
|
|
{ "prettyformat", CT_PRETTYFORMAT },
|
2008-10-06 14:37:28 +00:00
|
|
|
{ "within", CT_WITHIN }
|
|
|
|
};
|
|
|
|
|
|
|
|
lex.pushTable(counterTags);
|
|
|
|
|
|
|
|
bool getout = false;
|
|
|
|
while (!getout && lex.isOK()) {
|
|
|
|
int le = lex.lex();
|
|
|
|
switch (le) {
|
|
|
|
case Lexer::LEX_UNDEF:
|
|
|
|
lex.printError("Unknown counter tag `$$Token'");
|
|
|
|
continue;
|
2015-02-20 14:57:45 +00:00
|
|
|
default:
|
2008-10-06 14:37:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (le) {
|
|
|
|
case CT_WITHIN:
|
|
|
|
lex.next();
|
|
|
|
master_ = lex.getDocString();
|
|
|
|
if (master_ == "none")
|
|
|
|
master_.erase();
|
|
|
|
break;
|
2013-06-01 21:42:08 +00:00
|
|
|
case CT_INITIALVALUE:
|
|
|
|
lex.next();
|
|
|
|
initial_value_ = lex.getInteger();
|
|
|
|
// getInteger() returns -1 on error, and larger
|
|
|
|
// negative values do not make much sense.
|
|
|
|
// In the other case, we subtract one, since the
|
|
|
|
// counter will be incremented before its first use.
|
|
|
|
if (initial_value_ <= -1)
|
|
|
|
initial_value_ = 0;
|
|
|
|
else
|
|
|
|
initial_value_ -= 1;
|
|
|
|
break;
|
2010-01-20 20:33:36 +00:00
|
|
|
case CT_PRETTYFORMAT:
|
|
|
|
lex.next();
|
|
|
|
prettyformat_ = lex.getDocString();
|
|
|
|
break;
|
2008-10-06 14:37:28 +00:00
|
|
|
case CT_LABELSTRING:
|
|
|
|
lex.next();
|
|
|
|
labelstring_ = lex.getDocString();
|
|
|
|
labelstringappendix_ = labelstring_;
|
|
|
|
break;
|
|
|
|
case CT_LABELSTRING_APPENDIX:
|
|
|
|
lex.next();
|
|
|
|
labelstringappendix_ = lex.getDocString();
|
|
|
|
break;
|
2020-05-03 03:00:17 +00:00
|
|
|
case CT_GUINAME:
|
|
|
|
lex.next();
|
|
|
|
guiname_ = lex.getDocString();
|
|
|
|
break;
|
2008-10-06 14:37:28 +00:00
|
|
|
case CT_END:
|
|
|
|
getout = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here if have a full counter if getout == true
|
|
|
|
if (!getout)
|
|
|
|
LYXERR0("No End tag found for counter!");
|
|
|
|
lex.popTable();
|
|
|
|
return getout;
|
|
|
|
}
|
|
|
|
|
2012-10-27 13:45:27 +00:00
|
|
|
|
2000-07-17 18:27:53 +00:00
|
|
|
void Counter::set(int v)
|
|
|
|
{
|
|
|
|
value_ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::addto(int v)
|
|
|
|
{
|
|
|
|
value_ += v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Counter::value() const
|
|
|
|
{
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-01 01:56:58 +00:00
|
|
|
void Counter::saveValue()
|
|
|
|
{
|
|
|
|
saved_value_ = value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::restoreValue()
|
|
|
|
{
|
|
|
|
value_ = saved_value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-17 18:27:53 +00:00
|
|
|
void Counter::step()
|
|
|
|
{
|
|
|
|
++value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Counter::reset()
|
|
|
|
{
|
2013-06-01 21:42:08 +00:00
|
|
|
value_ = initial_value_;
|
2000-07-17 18:27:53 +00:00
|
|
|
}
|
|
|
|
|
2002-09-06 14:48:01 +00:00
|
|
|
|
2007-01-11 22:38:37 +00:00
|
|
|
docstring const & Counter::master() const
|
2002-08-06 22:40:59 +00:00
|
|
|
{
|
|
|
|
return master_;
|
|
|
|
}
|
|
|
|
|
2002-09-06 14:48:01 +00:00
|
|
|
|
2011-01-13 01:47:41 +00:00
|
|
|
bool Counter::checkAndRemoveMaster(docstring const & cnt)
|
|
|
|
{
|
|
|
|
if (master_ != cnt)
|
|
|
|
return false;
|
|
|
|
master_ = docstring();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-19 20:05:26 +00:00
|
|
|
docstring const & Counter::labelString(bool in_appendix) const
|
2002-08-06 22:40:59 +00:00
|
|
|
{
|
2009-04-19 20:05:26 +00:00
|
|
|
return in_appendix ? labelstringappendix_ : labelstring_;
|
2002-08-06 22:40:59 +00:00
|
|
|
}
|
|
|
|
|
2000-07-17 18:27:53 +00:00
|
|
|
|
2009-07-12 20:09:53 +00:00
|
|
|
Counter::StringMap & Counter::flatLabelStrings(bool in_appendix) const
|
2000-07-17 18:27:53 +00:00
|
|
|
{
|
2009-04-19 20:05:26 +00:00
|
|
|
return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-01 18:30:36 +00:00
|
|
|
Counters::Counters() : appendix_(false), subfloat_(false), longtable_(false)
|
2010-12-02 20:22:10 +00:00
|
|
|
{
|
2020-02-28 06:48:10 +00:00
|
|
|
layout_stack_.push_back(nullptr);
|
2010-12-02 21:04:01 +00:00
|
|
|
counter_stack_.push_back(from_ascii(""));
|
2010-12-02 20:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-20 20:30:00 +00:00
|
|
|
void Counters::newCounter(docstring const & newc,
|
2015-02-20 14:57:45 +00:00
|
|
|
docstring const & masterc,
|
2007-08-16 11:07:00 +00:00
|
|
|
docstring const & ls,
|
2020-05-03 03:11:53 +00:00
|
|
|
docstring const & lsa,
|
|
|
|
docstring const & guiname)
|
2000-07-17 18:27:53 +00:00
|
|
|
{
|
2007-08-16 11:07:00 +00:00
|
|
|
if (!masterc.empty() && !hasCounter(masterc)) {
|
2006-10-20 20:30:00 +00:00
|
|
|
lyxerr << "Master counter does not exist: "
|
2006-10-21 00:16:43 +00:00
|
|
|
<< to_utf8(masterc)
|
2006-10-20 20:30:00 +00:00
|
|
|
<< endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-05-03 03:11:53 +00:00
|
|
|
counterList_[newc] = Counter(masterc, ls, lsa, guiname);
|
2000-07-17 18:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:23:30 +00:00
|
|
|
bool Counters::hasCounter(docstring const & c) const
|
|
|
|
{
|
2009-04-19 10:06:17 +00:00
|
|
|
return counterList_.find(c) != counterList_.end();
|
2007-08-11 18:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-12 14:50:01 +00:00
|
|
|
bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
|
2008-10-06 14:37:28 +00:00
|
|
|
{
|
|
|
|
if (hasCounter(name)) {
|
|
|
|
LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
|
2009-04-19 10:06:17 +00:00
|
|
|
return counterList_[name].read(lex);
|
2008-10-06 14:37:28 +00:00
|
|
|
}
|
2009-08-12 14:50:01 +00:00
|
|
|
|
2008-10-06 14:37:28 +00:00
|
|
|
LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
|
|
|
|
Counter cnt;
|
|
|
|
bool success = cnt.read(lex);
|
2009-08-12 14:50:01 +00:00
|
|
|
// if makenew is false, we will just discard what we read
|
|
|
|
if (success && makenew)
|
2009-04-19 10:06:17 +00:00
|
|
|
counterList_[name] = cnt;
|
2009-08-12 14:50:01 +00:00
|
|
|
else if (!success)
|
2008-10-06 14:37:28 +00:00
|
|
|
LYXERR0("Error reading counter `" << name << "'!");
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-20 20:30:00 +00:00
|
|
|
void Counters::set(docstring const & ctr, int const val)
|
2000-07-17 18:27:53 +00:00
|
|
|
{
|
2009-04-19 10:06:17 +00:00
|
|
|
CounterList::iterator const it = counterList_.find(ctr);
|
|
|
|
if (it == counterList_.end()) {
|
2006-10-20 20:30:00 +00:00
|
|
|
lyxerr << "set: Counter does not exist: "
|
2006-10-21 00:16:43 +00:00
|
|
|
<< to_utf8(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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-20 20:30:00 +00:00
|
|
|
void Counters::addto(docstring const & ctr, int const val)
|
2000-07-17 18:27:53 +00:00
|
|
|
{
|
2009-04-19 10:06:17 +00:00
|
|
|
CounterList::iterator const it = counterList_.find(ctr);
|
|
|
|
if (it == counterList_.end()) {
|
2006-10-20 20:30:00 +00:00
|
|
|
lyxerr << "addto: Counter does not exist: "
|
2006-10-21 00:16:43 +00:00
|
|
|
<< to_utf8(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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-20 20:30:00 +00:00
|
|
|
int Counters::value(docstring const & ctr) const
|
2000-07-17 18:27:53 +00:00
|
|
|
{
|
2009-04-19 10:06:17 +00:00
|
|
|
CounterList::const_iterator const cit = counterList_.find(ctr);
|
|
|
|
if (cit == counterList_.end()) {
|
2006-10-20 20:30:00 +00:00
|
|
|
lyxerr << "value: Counter does not exist: "
|
2006-10-21 00:16:43 +00:00
|
|
|
<< to_utf8(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
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-01 01:56:58 +00:00
|
|
|
void Counters::saveValue(docstring const & ctr) const
|
|
|
|
{
|
|
|
|
CounterList::const_iterator const cit = counterList_.find(ctr);
|
|
|
|
if (cit == counterList_.end()) {
|
|
|
|
lyxerr << "value: Counter does not exist: "
|
|
|
|
<< to_utf8(ctr) << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Counter const & cnt = cit->second;
|
|
|
|
Counter & ccnt = const_cast<Counter &>(cnt);
|
|
|
|
ccnt.saveValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Counters::restoreValue(docstring const & ctr) const
|
|
|
|
{
|
|
|
|
CounterList::const_iterator const cit = counterList_.find(ctr);
|
|
|
|
if (cit == counterList_.end()) {
|
|
|
|
lyxerr << "value: Counter does not exist: "
|
|
|
|
<< to_utf8(ctr) << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Counter const & cnt = cit->second;
|
|
|
|
Counter & ccnt = const_cast<Counter &>(cnt);
|
|
|
|
ccnt.restoreValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-28 06:48:10 +00:00
|
|
|
void Counters::resetSlaves(docstring const & count)
|
2016-04-11 10:00:19 +00:00
|
|
|
{
|
2020-02-28 06:48:10 +00:00
|
|
|
for (auto & ctr : counterList_) {
|
|
|
|
if (ctr.second.master() == count) {
|
|
|
|
ctr.second.reset();
|
|
|
|
resetSlaves(ctr.first);
|
2016-04-11 10:00:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-14 18:08:12 +00:00
|
|
|
void Counters::stepMaster(docstring const & ctr, UpdateType utype)
|
|
|
|
{
|
|
|
|
CounterList::iterator it = counterList_.find(ctr);
|
|
|
|
if (it == counterList_.end()) {
|
|
|
|
lyxerr << "step: Counter does not exist: "
|
|
|
|
<< to_utf8(ctr) << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
step(it->second.master(), utype);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-20 19:42:12 +00:00
|
|
|
void Counters::step(docstring const & ctr, UpdateType utype)
|
2000-07-17 18:27:53 +00:00
|
|
|
{
|
2009-04-19 10:06:17 +00:00
|
|
|
CounterList::iterator it = counterList_.find(ctr);
|
|
|
|
if (it == counterList_.end()) {
|
2006-10-20 20:30:00 +00:00
|
|
|
lyxerr << "step: Counter does not exist: "
|
2006-10-21 00:16:43 +00:00
|
|
|
<< to_utf8(ctr) << endl;
|
2000-07-17 18:27:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
|
|
|
|
it->second.step();
|
2010-01-20 19:42:12 +00:00
|
|
|
if (utype == OutputUpdate) {
|
2013-04-27 21:52:55 +00:00
|
|
|
LBUFERR(!counter_stack_.empty());
|
2010-01-20 19:03:17 +00:00
|
|
|
counter_stack_.pop_back();
|
|
|
|
counter_stack_.push_back(ctr);
|
|
|
|
}
|
2016-04-11 10:00:19 +00:00
|
|
|
|
|
|
|
resetSlaves(ctr);
|
2002-08-06 22:40:59 +00:00
|
|
|
}
|
|
|
|
|
2002-08-11 20:34:20 +00:00
|
|
|
|
2020-05-03 03:00:17 +00:00
|
|
|
docstring const & Counters::guiName(docstring const & cntr) const
|
|
|
|
{
|
|
|
|
CounterList::const_iterator it = counterList_.find(cntr);
|
|
|
|
if (it == counterList_.end()) {
|
|
|
|
lyxerr << "step: Counter does not exist: "
|
|
|
|
<< to_utf8(cntr) << endl;
|
|
|
|
return empty_docstring();
|
|
|
|
}
|
|
|
|
|
|
|
|
docstring const & guiname = it->second.guiName();
|
|
|
|
if (guiname.empty())
|
|
|
|
return cntr;
|
|
|
|
return guiname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-11 20:34:20 +00:00
|
|
|
void Counters::reset()
|
|
|
|
{
|
2007-07-11 12:52:50 +00:00
|
|
|
appendix_ = false;
|
2008-03-02 11:30:50 +00:00
|
|
|
subfloat_ = false;
|
2007-08-16 11:07:00 +00:00
|
|
|
current_float_.erase();
|
2020-02-28 06:48:10 +00:00
|
|
|
for (auto & ctr : counterList_)
|
|
|
|
ctr.second.reset();
|
2010-01-20 19:03:17 +00:00
|
|
|
counter_stack_.clear();
|
|
|
|
counter_stack_.push_back(from_ascii(""));
|
2010-12-02 20:22:10 +00:00
|
|
|
layout_stack_.clear();
|
2020-02-28 06:48:10 +00:00
|
|
|
layout_stack_.push_back(nullptr);
|
2002-08-11 20:34:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-20 20:30:00 +00:00
|
|
|
void Counters::reset(docstring const & match)
|
2002-08-06 22:40:59 +00:00
|
|
|
{
|
2013-04-25 21:27:10 +00:00
|
|
|
LASSERT(!match.empty(), return);
|
2002-08-11 20:34:20 +00:00
|
|
|
|
2020-02-28 06:48:10 +00:00
|
|
|
for (auto & ctr : counterList_) {
|
|
|
|
if (ctr.first.find(match) != string::npos)
|
|
|
|
ctr.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
|
|
|
|
2011-01-12 22:53:41 +00:00
|
|
|
bool Counters::remove(docstring const & cnt)
|
|
|
|
{
|
2011-01-13 01:47:41 +00:00
|
|
|
bool retval = counterList_.erase(cnt);
|
|
|
|
if (!retval)
|
|
|
|
return false;
|
2020-02-28 06:48:10 +00:00
|
|
|
for (auto & ctr : counterList_) {
|
|
|
|
if (ctr.second.checkAndRemoveMaster(cnt))
|
2011-01-13 01:47:41 +00:00
|
|
|
LYXERR(Debug::TCLASS, "Removed master counter `" +
|
2020-02-28 06:48:10 +00:00
|
|
|
to_utf8(cnt) + "' from counter: " + to_utf8(ctr.first));
|
2011-01-13 01:47:41 +00:00
|
|
|
}
|
|
|
|
return retval;
|
2011-01-12 22:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-09 15:50:24 +00:00
|
|
|
void Counters::copy(Counters const & from, Counters & to, docstring const & match)
|
2002-08-06 22:40:59 +00:00
|
|
|
{
|
2020-02-28 06:48:10 +00:00
|
|
|
for (auto const & ctr : counterList_) {
|
|
|
|
if (ctr.first.find(match) != string::npos || match == "") {
|
|
|
|
to.set(ctr.first, from.value(ctr.first));
|
2002-08-09 12:14:54 +00:00
|
|
|
}
|
|
|
|
}
|
2002-08-06 22:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-20 20:30:00 +00:00
|
|
|
docstring Counters::labelItem(docstring const & ctr,
|
2009-04-19 10:06:17 +00:00
|
|
|
docstring const & numbertype) const
|
2002-08-06 22:40:59 +00:00
|
|
|
{
|
2009-04-19 10:06:17 +00:00
|
|
|
CounterList::const_iterator const cit = counterList_.find(ctr);
|
|
|
|
if (cit == counterList_.end()) {
|
2006-10-20 20:30:00 +00:00
|
|
|
lyxerr << "Counter "
|
2006-10-21 00:16:43 +00:00
|
|
|
<< to_utf8(ctr)
|
2006-10-20 20:30:00 +00:00
|
|
|
<< " does not exist." << endl;
|
|
|
|
return docstring();
|
2002-08-06 22:40:59 +00:00
|
|
|
}
|
2002-09-05 12:58:10 +00:00
|
|
|
|
2007-01-11 22:56:21 +00:00
|
|
|
int val = cit->second.value();
|
|
|
|
|
2003-09-12 17:13:22 +00:00
|
|
|
if (numbertype == "hebrew")
|
2007-01-11 22:56:21 +00:00
|
|
|
return docstring(1, hebrewCounter(val));
|
2003-09-12 17:13:22 +00:00
|
|
|
|
|
|
|
if (numbertype == "alph")
|
2007-01-11 22:56:21 +00:00
|
|
|
return docstring(1, loweralphaCounter(val));
|
2003-09-12 17:13:22 +00:00
|
|
|
|
|
|
|
if (numbertype == "Alph")
|
2007-01-11 22:56:21 +00:00
|
|
|
return docstring(1, alphaCounter(val));
|
2003-09-12 17:13:22 +00:00
|
|
|
|
|
|
|
if (numbertype == "roman")
|
2007-01-11 22:56:21 +00:00
|
|
|
return lowerromanCounter(val);
|
2002-11-04 02:12:42 +00:00
|
|
|
|
2003-09-12 17:13:22 +00:00
|
|
|
if (numbertype == "Roman")
|
2007-01-11 22:56:21 +00:00
|
|
|
return romanCounter(val);
|
2003-09-12 17:13:22 +00:00
|
|
|
|
2014-11-14 13:53:11 +00:00
|
|
|
if (numbertype == "fnsymbol")
|
|
|
|
return fnsymbolCounter(val);
|
|
|
|
|
2007-01-11 22:56:21 +00:00
|
|
|
return convert<docstring>(val);
|
2002-08-06 22:40:59 +00:00
|
|
|
}
|
|
|
|
|
2002-08-12 20:24:10 +00:00
|
|
|
|
2009-07-12 20:09:53 +00:00
|
|
|
docstring Counters::theCounter(docstring const & counter,
|
|
|
|
string const & lang) const
|
2008-02-23 20:38:57 +00:00
|
|
|
{
|
2015-02-20 14:57:45 +00:00
|
|
|
CounterList::const_iterator it = counterList_.find(counter);
|
2009-04-19 20:05:26 +00:00
|
|
|
if (it == counterList_.end())
|
2010-11-29 19:11:38 +00:00
|
|
|
return from_ascii("#");
|
2009-07-12 20:09:53 +00:00
|
|
|
Counter const & ctr = it->second;
|
|
|
|
Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
|
|
|
|
Counter::StringMap::iterator smit = sm.find(lang);
|
|
|
|
if (smit != sm.end())
|
|
|
|
return counterLabel(smit->second, lang);
|
|
|
|
|
2010-12-03 22:42:22 +00:00
|
|
|
vector<docstring> callers;
|
2009-07-12 20:09:53 +00:00
|
|
|
docstring const & fls = flattenLabelString(counter, appendix(),
|
|
|
|
lang, callers);
|
|
|
|
sm[lang] = fls;
|
|
|
|
return counterLabel(fls, lang);
|
2008-02-23 20:38:57 +00:00
|
|
|
}
|
|
|
|
|
2009-04-19 20:05:26 +00:00
|
|
|
|
2015-02-20 14:57:45 +00:00
|
|
|
docstring Counters::flattenLabelString(docstring const & counter,
|
2009-07-12 20:09:53 +00:00
|
|
|
bool in_appendix,
|
|
|
|
string const & lang,
|
2010-12-03 22:42:22 +00:00
|
|
|
vector<docstring> & callers) const
|
2007-08-16 11:07:00 +00:00
|
|
|
{
|
2009-04-19 20:05:26 +00:00
|
|
|
if (find(callers.begin(), callers.end(), counter) != callers.end()) {
|
|
|
|
// recursion detected
|
|
|
|
lyxerr << "Warning: Recursion in label for counter `"
|
|
|
|
<< counter << "' detected"
|
|
|
|
<< endl;
|
|
|
|
return from_ascii("??");
|
|
|
|
}
|
2015-02-20 14:57:45 +00:00
|
|
|
|
|
|
|
CounterList::const_iterator it = counterList_.find(counter);
|
2009-04-19 20:05:26 +00:00
|
|
|
if (it == counterList_.end())
|
2010-11-29 19:11:38 +00:00
|
|
|
return from_ascii("#");
|
2009-04-19 20:05:26 +00:00
|
|
|
Counter const & c = it->second;
|
2008-02-23 20:38:57 +00:00
|
|
|
|
2009-07-12 20:09:53 +00:00
|
|
|
docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
|
2008-02-23 20:38:57 +00:00
|
|
|
|
2009-04-19 20:05:26 +00:00
|
|
|
callers.push_back(counter);
|
|
|
|
if (ls.empty()) {
|
|
|
|
if (!c.master().empty())
|
2015-02-20 14:57:45 +00:00
|
|
|
ls = flattenLabelString(c.master(), in_appendix, lang, callers)
|
2009-04-19 20:05:26 +00:00
|
|
|
+ from_ascii(".");
|
|
|
|
callers.pop_back();
|
|
|
|
return ls + from_ascii("\\arabic{") + counter + "}";
|
|
|
|
}
|
2008-02-23 20:38:57 +00:00
|
|
|
|
2009-04-19 20:05:26 +00:00
|
|
|
while (true) {
|
|
|
|
//lyxerr << "ls=" << to_utf8(ls) << endl;
|
|
|
|
size_t const i = ls.find(from_ascii("\\the"), 0);
|
|
|
|
if (i == docstring::npos)
|
|
|
|
break;
|
|
|
|
size_t const j = i + 4;
|
|
|
|
size_t k = j;
|
2015-02-20 14:57:45 +00:00
|
|
|
while (k < ls.size() && lowercase(ls[k]) >= 'a'
|
2009-04-19 20:05:26 +00:00
|
|
|
&& lowercase(ls[k]) <= 'z')
|
|
|
|
++k;
|
2009-04-20 10:00:54 +00:00
|
|
|
docstring const newc = ls.substr(j, k - j);
|
2009-07-12 20:09:53 +00:00
|
|
|
docstring const repl = flattenLabelString(newc, in_appendix,
|
|
|
|
lang, callers);
|
2009-04-19 20:05:26 +00:00
|
|
|
ls.replace(i, k - j + 4, repl);
|
2007-08-16 11:07:00 +00:00
|
|
|
}
|
2009-04-19 20:05:26 +00:00
|
|
|
callers.pop_back();
|
2008-02-23 20:38:57 +00:00
|
|
|
|
2009-04-19 20:05:26 +00:00
|
|
|
return ls;
|
2007-08-16 11:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-12 20:09:53 +00:00
|
|
|
docstring Counters::counterLabel(docstring const & format,
|
|
|
|
string const & lang) const
|
2002-08-06 22:40:59 +00:00
|
|
|
{
|
2006-10-20 20:30:00 +00:00
|
|
|
docstring label = format;
|
2007-08-16 11:07:00 +00:00
|
|
|
|
|
|
|
// FIXME: Using regexps would be better, but we compile boost without
|
|
|
|
// wide regexps currently.
|
2009-07-13 11:05:31 +00:00
|
|
|
docstring const the = from_ascii("\\the");
|
2007-08-16 11:07:00 +00:00
|
|
|
while (true) {
|
2009-07-13 11:05:31 +00:00
|
|
|
//lyxerr << "label=" << label << endl;
|
|
|
|
size_t const i = label.find(the, 0);
|
2007-08-16 11:07:00 +00:00
|
|
|
if (i == docstring::npos)
|
|
|
|
break;
|
2009-04-19 10:06:17 +00:00
|
|
|
size_t const j = i + 4;
|
2007-08-16 11:07:00 +00:00
|
|
|
size_t k = j;
|
2015-02-20 14:57:45 +00:00
|
|
|
while (k < label.size() && lowercase(label[k]) >= 'a'
|
2007-08-16 11:07:00 +00:00
|
|
|
&& lowercase(label[k]) <= 'z')
|
|
|
|
++k;
|
2009-07-13 11:05:31 +00:00
|
|
|
docstring const newc(label, j, k - j);
|
|
|
|
label.replace(i, k - i, theCounter(newc, lang));
|
2007-08-16 11:07:00 +00:00
|
|
|
}
|
2003-09-12 17:13:22 +00:00
|
|
|
while (true) {
|
2009-07-13 11:05:31 +00:00
|
|
|
//lyxerr << "label=" << label << endl;
|
2003-09-15 11:00:00 +00:00
|
|
|
|
2003-09-12 17:13:22 +00:00
|
|
|
size_t const i = label.find('\\', 0);
|
2006-10-20 20:30:00 +00:00
|
|
|
if (i == docstring::npos)
|
2003-09-12 17:13:22 +00:00
|
|
|
break;
|
|
|
|
size_t const j = label.find('{', i + 1);
|
2006-10-20 20:30:00 +00:00
|
|
|
if (j == docstring::npos)
|
2003-09-12 17:13:22 +00:00
|
|
|
break;
|
|
|
|
size_t const k = label.find('}', j + 1);
|
2007-01-11 22:38:37 +00:00
|
|
|
if (k == docstring::npos)
|
2003-09-12 17:13:22 +00:00
|
|
|
break;
|
2006-10-20 20:30:00 +00:00
|
|
|
docstring const numbertype(label, i + 1, j - i - 1);
|
|
|
|
docstring const counter(label, j + 1, k - j - 1);
|
2009-07-13 11:05:31 +00:00
|
|
|
label.replace(i, k + 1 - i, labelItem(counter, numbertype));
|
2003-09-12 17:13:22 +00:00
|
|
|
}
|
2009-07-13 11:05:31 +00:00
|
|
|
//lyxerr << "DONE! label=" << label << endl;
|
2003-09-12 17:13:22 +00:00
|
|
|
return label;
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2010-01-20 22:28:42 +00:00
|
|
|
docstring Counters::prettyCounter(docstring const & name,
|
2010-01-20 20:33:36 +00:00
|
|
|
string const & lang) const
|
|
|
|
{
|
2015-02-20 14:57:45 +00:00
|
|
|
CounterList::const_iterator it = counterList_.find(name);
|
2010-01-20 20:33:36 +00:00
|
|
|
if (it == counterList_.end())
|
2010-11-29 19:11:38 +00:00
|
|
|
return from_ascii("#");
|
2010-01-20 20:33:36 +00:00
|
|
|
Counter const & ctr = it->second;
|
2010-01-20 22:28:42 +00:00
|
|
|
|
|
|
|
docstring const value = theCounter(name, lang);
|
2011-04-15 11:34:06 +00:00
|
|
|
docstring const & format =
|
|
|
|
translateIfPossible(ctr.prettyFormat(), lang);
|
2010-01-20 22:28:42 +00:00
|
|
|
if (format.empty())
|
|
|
|
return value;
|
|
|
|
return subst(format, from_ascii("##"), value);
|
2010-01-20 20:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-20 19:03:17 +00:00
|
|
|
docstring Counters::currentCounter() const
|
2015-02-20 14:57:45 +00:00
|
|
|
{
|
2013-04-27 21:52:55 +00:00
|
|
|
LBUFERR(!counter_stack_.empty());
|
2015-02-20 14:57:45 +00:00
|
|
|
return counter_stack_.back();
|
2010-01-20 19:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Counters::setActiveLayout(Layout const & lay)
|
|
|
|
{
|
|
|
|
LASSERT(!layout_stack_.empty(), return);
|
|
|
|
Layout const * const lastlay = layout_stack_.back();
|
|
|
|
// we want to check whether the layout has changed and, if so,
|
|
|
|
// whether we are coming out of or going into an environment.
|
2015-02-20 14:57:45 +00:00
|
|
|
if (!lastlay) {
|
2010-01-20 19:03:17 +00:00
|
|
|
layout_stack_.pop_back();
|
|
|
|
layout_stack_.push_back(&lay);
|
|
|
|
if (lay.isEnvironment())
|
|
|
|
beginEnvironment();
|
|
|
|
} else if (lastlay->name() != lay.name()) {
|
|
|
|
layout_stack_.pop_back();
|
|
|
|
layout_stack_.push_back(&lay);
|
|
|
|
if (lastlay->isEnvironment()) {
|
|
|
|
// we are coming out of an environment
|
|
|
|
// LYXERR0("Out: " << lastlay->name());
|
|
|
|
endEnvironment();
|
|
|
|
}
|
|
|
|
if (lay.isEnvironment()) {
|
|
|
|
// we are going into a new environment
|
|
|
|
// LYXERR0("In: " << lay.name());
|
|
|
|
beginEnvironment();
|
|
|
|
}
|
2015-02-20 14:57:45 +00:00
|
|
|
}
|
2010-01-20 19:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Counters::beginEnvironment()
|
|
|
|
{
|
2010-12-02 20:22:10 +00:00
|
|
|
counter_stack_.push_back(counter_stack_.back());
|
2010-01-20 19:03:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Counters::endEnvironment()
|
|
|
|
{
|
|
|
|
LASSERT(!counter_stack_.empty(), return);
|
|
|
|
counter_stack_.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-01 01:56:58 +00:00
|
|
|
vector<docstring> Counters::listOfCounters() const {
|
|
|
|
vector<docstring> ret;
|
|
|
|
for(auto const & k : counterList_)
|
|
|
|
ret.emplace_back(k.first);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|