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
7 * \author Martin Vermeer
9 * \author Richard Heck (roman numerals)
11 * Full author contact details are available in file CREDITS.
20 #include "support/convert.h"
21 #include "support/counter_reps.h"
22 #include "support/debug.h"
23 #include "support/docstring.h"
24 #include "support/gettext.h"
25 #include "support/lassert.h"
26 #include "support/lstrings.h"
32 using namespace lyx::support;
38 : initial_value_(0), saved_value_(0)
44 Counter::Counter(docstring const & mc, docstring const & ls,
45 docstring const & lsa, docstring const & guiname)
46 : initial_value_(0), saved_value_(0), master_(mc), labelstring_(ls),
47 labelstringappendix_(lsa), guiname_(guiname)
53 bool Counter::read(Lexer & lex)
58 CT_LABELSTRING_APPENDIX,
65 LexerKeyword counterTags[] = {
67 { "guiname", CT_GUINAME },
68 { "initialvalue", CT_INITIALVALUE},
69 { "labelstring", CT_LABELSTRING },
70 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
71 { "prettyformat", CT_PRETTYFORMAT },
72 { "within", CT_WITHIN }
75 lex.pushTable(counterTags);
78 while (!getout && lex.isOK()) {
81 case Lexer::LEX_UNDEF:
82 lex.printError("Unknown counter tag `$$Token'");
90 master_ = lex.getDocString();
91 if (master_ == "none")
96 initial_value_ = lex.getInteger();
97 // getInteger() returns -1 on error, and larger
98 // negative values do not make much sense.
99 // In the other case, we subtract one, since the
100 // counter will be incremented before its first use.
101 if (initial_value_ <= -1)
106 case CT_PRETTYFORMAT:
108 prettyformat_ = lex.getDocString();
112 labelstring_ = lex.getDocString();
113 labelstringappendix_ = labelstring_;
115 case CT_LABELSTRING_APPENDIX:
117 labelstringappendix_ = lex.getDocString();
121 guiname_ = lex.getDocString();
129 // Here if have a full counter if getout == true
131 LYXERR0("No End tag found for counter!");
137 void Counter::set(int v)
143 void Counter::addto(int v)
149 int Counter::value() const
155 void Counter::saveValue()
157 saved_value_ = value_;
161 void Counter::restoreValue()
163 value_ = saved_value_;
173 void Counter::reset()
175 value_ = initial_value_;
179 docstring const & Counter::master() const
185 bool Counter::checkAndRemoveMaster(docstring const & cnt)
189 master_ = docstring();
194 docstring const & Counter::labelString(bool in_appendix) const
196 return in_appendix ? labelstringappendix_ : labelstring_;
200 Counter::StringMap & Counter::flatLabelStrings(bool in_appendix) const
202 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
206 Counters::Counters() : appendix_(false), subfloat_(false), longtable_(false)
208 layout_stack_.push_back(nullptr);
209 counter_stack_.push_back(from_ascii(""));
213 void Counters::newCounter(docstring const & newc,
214 docstring const & masterc,
215 docstring const & ls,
216 docstring const & lsa,
217 docstring const & guiname)
219 if (!masterc.empty() && !hasCounter(masterc)) {
220 lyxerr << "Master counter does not exist: "
225 counterList_[newc] = Counter(masterc, ls, lsa, guiname);
229 bool Counters::hasCounter(docstring const & c) const
231 return counterList_.find(c) != counterList_.end();
235 bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
237 if (hasCounter(name)) {
238 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
239 return counterList_[name].read(lex);
242 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
244 bool success = cnt.read(lex);
245 // if makenew is false, we will just discard what we read
246 if (success && makenew)
247 counterList_[name] = cnt;
249 LYXERR0("Error reading counter `" << name << "'!");
254 void Counters::set(docstring const & ctr, int const val)
256 CounterList::iterator const it = counterList_.find(ctr);
257 if (it == counterList_.end()) {
258 lyxerr << "set: Counter does not exist: "
259 << to_utf8(ctr) << endl;
266 void Counters::addto(docstring const & ctr, int const val)
268 CounterList::iterator const it = counterList_.find(ctr);
269 if (it == counterList_.end()) {
270 lyxerr << "addto: Counter does not exist: "
271 << to_utf8(ctr) << endl;
274 it->second.addto(val);
278 int Counters::value(docstring const & ctr) const
280 CounterList::const_iterator const cit = counterList_.find(ctr);
281 if (cit == counterList_.end()) {
282 lyxerr << "value: Counter does not exist: "
283 << to_utf8(ctr) << endl;
286 return cit->second.value();
290 void Counters::saveValue(docstring const & ctr) const
292 CounterList::const_iterator const cit = counterList_.find(ctr);
293 if (cit == counterList_.end()) {
294 lyxerr << "value: Counter does not exist: "
295 << to_utf8(ctr) << endl;
298 Counter const & cnt = cit->second;
299 Counter & ccnt = const_cast<Counter &>(cnt);
304 void Counters::restoreValue(docstring const & ctr) const
306 CounterList::const_iterator const cit = counterList_.find(ctr);
307 if (cit == counterList_.end()) {
308 lyxerr << "value: Counter does not exist: "
309 << to_utf8(ctr) << endl;
312 Counter const & cnt = cit->second;
313 Counter & ccnt = const_cast<Counter &>(cnt);
318 void Counters::resetSlaves(docstring const & count)
320 for (auto & ctr : counterList_) {
321 if (ctr.second.master() == count) {
323 resetSlaves(ctr.first);
329 void Counters::stepMaster(docstring const & ctr, UpdateType utype)
331 CounterList::iterator it = counterList_.find(ctr);
332 if (it == counterList_.end()) {
333 lyxerr << "step: Counter does not exist: "
334 << to_utf8(ctr) << endl;
337 step(it->second.master(), utype);
341 void Counters::step(docstring const & ctr, UpdateType utype)
343 CounterList::iterator it = counterList_.find(ctr);
344 if (it == counterList_.end()) {
345 lyxerr << "step: Counter does not exist: "
346 << to_utf8(ctr) << endl;
351 if (utype == OutputUpdate) {
352 LBUFERR(!counter_stack_.empty());
353 counter_stack_.pop_back();
354 counter_stack_.push_back(ctr);
361 docstring const & Counters::guiName(docstring const & cntr) const
363 CounterList::const_iterator it = counterList_.find(cntr);
364 if (it == counterList_.end()) {
365 lyxerr << "step: Counter does not exist: "
366 << to_utf8(cntr) << endl;
367 return empty_docstring();
370 docstring const & guiname = it->second.guiName();
377 void Counters::reset()
381 current_float_.erase();
382 for (auto & ctr : counterList_)
384 counter_stack_.clear();
385 counter_stack_.push_back(from_ascii(""));
386 layout_stack_.clear();
387 layout_stack_.push_back(nullptr);
391 void Counters::reset(docstring const & match)
393 LASSERT(!match.empty(), return);
395 for (auto & ctr : counterList_) {
396 if (ctr.first.find(match) != string::npos)
402 bool Counters::remove(docstring const & cnt)
404 bool retval = counterList_.erase(cnt);
407 for (auto & ctr : counterList_) {
408 if (ctr.second.checkAndRemoveMaster(cnt))
409 LYXERR(Debug::TCLASS, "Removed master counter `" +
410 to_utf8(cnt) + "' from counter: " + to_utf8(ctr.first));
416 void Counters::copy(Counters const & from, Counters & to, docstring const & match)
418 for (auto const & ctr : counterList_) {
419 if (ctr.first.find(match) != string::npos || match == "") {
420 to.set(ctr.first, from.value(ctr.first));
426 docstring Counters::labelItem(docstring const & ctr,
427 docstring const & numbertype) const
429 CounterList::const_iterator const cit = counterList_.find(ctr);
430 if (cit == counterList_.end()) {
433 << " does not exist." << endl;
437 int val = cit->second.value();
439 if (numbertype == "hebrew")
440 return docstring(1, hebrewCounter(val));
442 if (numbertype == "alph")
443 return docstring(1, loweralphaCounter(val));
445 if (numbertype == "Alph")
446 return docstring(1, alphaCounter(val));
448 if (numbertype == "roman")
449 return lowerromanCounter(val);
451 if (numbertype == "Roman")
452 return romanCounter(val);
454 if (numbertype == "fnsymbol")
455 return fnsymbolCounter(val);
457 return convert<docstring>(val);
461 docstring Counters::theCounter(docstring const & counter,
462 string const & lang) const
464 CounterList::const_iterator it = counterList_.find(counter);
465 if (it == counterList_.end())
466 return from_ascii("#");
467 Counter const & ctr = it->second;
468 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
469 Counter::StringMap::iterator smit = sm.find(lang);
470 if (smit != sm.end())
471 return counterLabel(smit->second, lang);
473 vector<docstring> callers;
474 docstring const & fls = flattenLabelString(counter, appendix(),
477 return counterLabel(fls, lang);
481 docstring Counters::flattenLabelString(docstring const & counter,
484 vector<docstring> & callers) const
486 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
487 // recursion detected
488 lyxerr << "Warning: Recursion in label for counter `"
489 << counter << "' detected"
491 return from_ascii("??");
494 CounterList::const_iterator it = counterList_.find(counter);
495 if (it == counterList_.end())
496 return from_ascii("#");
497 Counter const & c = it->second;
499 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
501 callers.push_back(counter);
503 if (!c.master().empty())
504 ls = flattenLabelString(c.master(), in_appendix, lang, callers)
507 return ls + from_ascii("\\arabic{") + counter + "}";
511 //lyxerr << "ls=" << to_utf8(ls) << endl;
512 size_t const i = ls.find(from_ascii("\\the"), 0);
513 if (i == docstring::npos)
515 size_t const j = i + 4;
517 while (k < ls.size() && lowercase(ls[k]) >= 'a'
518 && lowercase(ls[k]) <= 'z')
520 docstring const newc = ls.substr(j, k - j);
521 docstring const repl = flattenLabelString(newc, in_appendix,
523 ls.replace(i, k - j + 4, repl);
531 docstring Counters::counterLabel(docstring const & format,
532 string const & lang) const
534 docstring label = format;
536 // FIXME: Using regexps would be better, but we compile boost without
537 // wide regexps currently.
538 docstring const the = from_ascii("\\the");
540 //lyxerr << "label=" << label << endl;
541 size_t const i = label.find(the, 0);
542 if (i == docstring::npos)
544 size_t const j = i + 4;
546 while (k < label.size() && lowercase(label[k]) >= 'a'
547 && lowercase(label[k]) <= 'z')
549 docstring const newc(label, j, k - j);
550 label.replace(i, k - i, theCounter(newc, lang));
553 //lyxerr << "label=" << label << endl;
555 size_t const i = label.find('\\', 0);
556 if (i == docstring::npos)
558 size_t const j = label.find('{', i + 1);
559 if (j == docstring::npos)
561 size_t const k = label.find('}', j + 1);
562 if (k == docstring::npos)
564 docstring const numbertype(label, i + 1, j - i - 1);
565 docstring const counter(label, j + 1, k - j - 1);
566 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
568 //lyxerr << "DONE! label=" << label << endl;
573 docstring Counters::prettyCounter(docstring const & name,
574 string const & lang) const
576 CounterList::const_iterator it = counterList_.find(name);
577 if (it == counterList_.end())
578 return from_ascii("#");
579 Counter const & ctr = it->second;
581 docstring const value = theCounter(name, lang);
582 docstring const & format =
583 translateIfPossible(ctr.prettyFormat(), lang);
586 return subst(format, from_ascii("##"), value);
590 docstring Counters::currentCounter() const
592 LBUFERR(!counter_stack_.empty());
593 return counter_stack_.back();
597 void Counters::setActiveLayout(Layout const & lay)
599 LASSERT(!layout_stack_.empty(), return);
600 Layout const * const lastlay = layout_stack_.back();
601 // we want to check whether the layout has changed and, if so,
602 // whether we are coming out of or going into an environment.
604 layout_stack_.pop_back();
605 layout_stack_.push_back(&lay);
606 if (lay.isEnvironment())
608 } else if (lastlay->name() != lay.name()) {
609 layout_stack_.pop_back();
610 layout_stack_.push_back(&lay);
611 if (lastlay->isEnvironment()) {
612 // we are coming out of an environment
613 // LYXERR0("Out: " << lastlay->name());
616 if (lay.isEnvironment()) {
617 // we are going into a new environment
618 // LYXERR0("In: " << lay.name());
625 void Counters::beginEnvironment()
627 counter_stack_.push_back(counter_stack_.back());
631 void Counters::endEnvironment()
633 LASSERT(!counter_stack_.empty(), return);
634 counter_stack_.pop_back();
638 vector<docstring> Counters::listOfCounters() const {
639 vector<docstring> ret;
640 for(auto const & k : counterList_)
641 ret.emplace_back(k.first);