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 Kimberly 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), parent_(mc), labelstring_(ls),
47 labelstringappendix_(lsa), guiname_(guiname)
53 bool Counter::read(Lexer & lex)
58 CT_LABELSTRING_APPENDIX,
66 LexerKeyword counterTags[] = {
68 { "guiname", CT_GUINAME },
69 { "initialvalue", CT_INITIALVALUE},
70 { "labelstring", CT_LABELSTRING },
71 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
72 { "latexname", CT_LATEXNAME },
73 { "prettyformat", CT_PRETTYFORMAT },
74 { "within", CT_WITHIN }
77 lex.pushTable(counterTags);
80 while (!getout && lex.isOK()) {
83 case Lexer::LEX_UNDEF:
84 lex.printError("Unknown counter tag `$$Token'");
92 parent_ = lex.getDocString();
93 if (parent_ == "none")
98 initial_value_ = lex.getInteger();
99 // getInteger() returns -1 on error, and larger
100 // negative values do not make much sense.
101 // In the other case, we subtract one, since the
102 // counter will be incremented before its first use.
103 if (initial_value_ <= -1)
108 case CT_PRETTYFORMAT:
110 prettyformat_ = lex.getDocString();
114 labelstring_ = lex.getDocString();
115 labelstringappendix_ = labelstring_;
117 case CT_LABELSTRING_APPENDIX:
119 labelstringappendix_ = lex.getDocString();
123 guiname_ = lex.getDocString();
127 latexname_ = lex.getDocString();
135 // Here if have a full counter if getout == true
137 LYXERR0("No End tag found for counter!");
143 void Counter::set(int v)
149 void Counter::addto(int v)
155 int Counter::value() const
161 void Counter::saveValue()
163 saved_value_ = value_;
167 void Counter::restoreValue()
169 value_ = saved_value_;
179 void Counter::reset()
181 value_ = initial_value_;
185 docstring const & Counter::parent() const
191 bool Counter::checkAndRemoveParent(docstring const & cnt)
195 parent_ = docstring();
200 docstring const & Counter::labelString(bool in_appendix) const
202 return in_appendix ? labelstringappendix_ : labelstring_;
206 Counter::StringMap & Counter::flatLabelStrings(bool in_appendix) const
208 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
212 Counters::Counters() : appendix_(false), subfloat_(false), longtable_(false)
214 layout_stack_.push_back(nullptr);
215 counter_stack_.push_back(from_ascii(""));
219 void Counters::newCounter(docstring const & newc,
220 docstring const & parentc,
221 docstring const & ls,
222 docstring const & lsa,
223 docstring const & guiname)
225 if (!parentc.empty() && !hasCounter(parentc)) {
226 lyxerr << "Parent counter does not exist: "
231 counterList_[newc] = Counter(parentc, ls, lsa, guiname);
235 bool Counters::hasCounter(docstring const & c) const
237 return counterList_.find(c) != counterList_.end();
241 bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
243 if (hasCounter(name)) {
244 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
245 return counterList_[name].read(lex);
248 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
250 bool success = cnt.read(lex);
251 // if makenew is false, we will just discard what we read
252 if (success && makenew)
253 counterList_[name] = cnt;
255 LYXERR0("Error reading counter `" << name << "'!");
260 void Counters::set(docstring const & ctr, int const val)
262 CounterList::iterator const it = counterList_.find(ctr);
263 if (it == counterList_.end()) {
264 lyxerr << "set: Counter does not exist: "
265 << to_utf8(ctr) << endl;
272 void Counters::addto(docstring const & ctr, int const val)
274 CounterList::iterator const it = counterList_.find(ctr);
275 if (it == counterList_.end()) {
276 lyxerr << "addto: Counter does not exist: "
277 << to_utf8(ctr) << endl;
280 it->second.addto(val);
284 int Counters::value(docstring const & ctr) const
286 CounterList::const_iterator const cit = counterList_.find(ctr);
287 if (cit == counterList_.end()) {
288 lyxerr << "value: Counter does not exist: "
289 << to_utf8(ctr) << endl;
292 return cit->second.value();
296 void Counters::saveValue(docstring const & ctr) const
298 CounterList::const_iterator const cit = counterList_.find(ctr);
299 if (cit == counterList_.end()) {
300 lyxerr << "value: Counter does not exist: "
301 << to_utf8(ctr) << endl;
304 Counter const & cnt = cit->second;
305 Counter & ccnt = const_cast<Counter &>(cnt);
310 void Counters::restoreValue(docstring const & ctr) const
312 CounterList::const_iterator const cit = counterList_.find(ctr);
313 if (cit == counterList_.end()) {
314 lyxerr << "value: Counter does not exist: "
315 << to_utf8(ctr) << endl;
318 Counter const & cnt = cit->second;
319 Counter & ccnt = const_cast<Counter &>(cnt);
324 void Counters::resetChildren(docstring const & count)
326 for (auto & ctr : counterList_) {
327 if (ctr.second.parent() == count) {
329 resetChildren(ctr.first);
335 void Counters::stepParent(docstring const & ctr, UpdateType utype)
337 CounterList::iterator it = counterList_.find(ctr);
338 if (it == counterList_.end()) {
339 lyxerr << "step: Counter does not exist: "
340 << to_utf8(ctr) << endl;
343 step(it->second.parent(), utype);
347 void Counters::step(docstring const & ctr, UpdateType utype)
349 CounterList::iterator it = counterList_.find(ctr);
350 if (it == counterList_.end()) {
351 lyxerr << "step: Counter does not exist: "
352 << to_utf8(ctr) << endl;
357 if (utype == OutputUpdate) {
358 LBUFERR(!counter_stack_.empty());
359 counter_stack_.pop_back();
360 counter_stack_.push_back(ctr);
367 docstring const & Counters::guiName(docstring const & cntr) const
369 CounterList::const_iterator it = counterList_.find(cntr);
370 if (it == counterList_.end()) {
371 lyxerr << "step: Counter does not exist: "
372 << to_utf8(cntr) << endl;
373 return empty_docstring();
376 docstring const & guiname = it->second.guiName();
383 docstring const & Counters::latexName(docstring const & cntr) const
385 CounterList::const_iterator it = counterList_.find(cntr);
386 if (it == counterList_.end()) {
387 lyxerr << "step: Counter does not exist: "
388 << to_utf8(cntr) << endl;
389 return empty_docstring();
392 docstring const & latexname = it->second.latexName();
393 if (latexname.empty())
399 void Counters::reset()
403 current_float_.erase();
404 for (auto & ctr : counterList_)
406 counter_stack_.clear();
407 counter_stack_.push_back(from_ascii(""));
408 layout_stack_.clear();
409 layout_stack_.push_back(nullptr);
413 void Counters::reset(docstring const & match)
415 LASSERT(!match.empty(), return);
417 for (auto & ctr : counterList_) {
418 if (ctr.first.find(match) != string::npos)
424 bool Counters::copy(docstring const & cnt, docstring const & newcnt)
426 auto const it = counterList_.find(cnt);
427 if (it == counterList_.end())
429 counterList_[newcnt] = it->second;
434 bool Counters::remove(docstring const & cnt)
436 bool retval = counterList_.erase(cnt);
439 for (auto & ctr : counterList_) {
440 if (ctr.second.checkAndRemoveParent(cnt))
441 LYXERR(Debug::TCLASS, "Removed parent counter `" +
442 to_utf8(cnt) + "' from counter: " + to_utf8(ctr.first));
448 docstring Counters::labelItem(docstring const & ctr,
449 docstring const & numbertype) const
451 CounterList::const_iterator const cit = counterList_.find(ctr);
452 if (cit == counterList_.end()) {
455 << " does not exist." << endl;
459 int val = cit->second.value();
461 if (numbertype == "hebrew")
462 return docstring(1, hebrewCounter(val));
464 if (numbertype == "alph")
465 return docstring(1, loweralphaCounter(val));
467 if (numbertype == "Alph")
468 return docstring(1, alphaCounter(val));
470 if (numbertype == "roman")
471 return lowerromanCounter(val);
473 if (numbertype == "Roman")
474 return romanCounter(val);
476 if (numbertype == "fnsymbol")
477 return fnsymbolCounter(val);
479 if (numbertype == "superarabic")
480 return superarabicCounter(val);
482 return convert<docstring>(val);
486 docstring Counters::theCounter(docstring const & counter,
487 string const & lang) const
489 CounterList::const_iterator it = counterList_.find(counter);
490 if (it == counterList_.end())
491 return from_ascii("#");
492 Counter const & ctr = it->second;
493 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
494 Counter::StringMap::iterator smit = sm.find(lang);
495 if (smit != sm.end())
496 return counterLabel(smit->second, lang);
498 vector<docstring> callers;
499 docstring const & fls = flattenLabelString(counter, appendix(),
502 return counterLabel(fls, lang);
506 docstring Counters::flattenLabelString(docstring const & counter,
509 vector<docstring> & callers) const
511 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
512 // recursion detected
513 lyxerr << "Warning: Recursion in label for counter `"
514 << counter << "' detected"
516 return from_ascii("??");
519 CounterList::const_iterator it = counterList_.find(counter);
520 if (it == counterList_.end())
521 return from_ascii("#");
522 Counter const & c = it->second;
524 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
526 callers.push_back(counter);
528 if (!c.parent().empty())
529 ls = flattenLabelString(c.parent(), in_appendix, lang, callers)
532 return ls + from_ascii("\\arabic{") + counter + "}";
536 //lyxerr << "ls=" << to_utf8(ls) << endl;
537 size_t const i = ls.find(from_ascii("\\the"), 0);
538 if (i == docstring::npos)
540 size_t const j = i + 4;
542 while (k < ls.size() && lowercase(ls[k]) >= 'a'
543 && lowercase(ls[k]) <= 'z')
545 docstring const newc = ls.substr(j, k - j);
546 docstring const repl = flattenLabelString(newc, in_appendix,
548 ls.replace(i, k - j + 4, repl);
556 docstring Counters::counterLabel(docstring const & format,
557 string const & lang) const
559 docstring label = format;
561 // FIXME: Using regexps would be better, but we compile boost without
562 // wide regexps currently.
563 docstring const the = from_ascii("\\the");
565 //lyxerr << "label=" << label << endl;
566 size_t const i = label.find(the, 0);
567 if (i == docstring::npos)
569 size_t const j = i + 4;
571 while (k < label.size() && lowercase(label[k]) >= 'a'
572 && lowercase(label[k]) <= 'z')
574 docstring const newc(label, j, k - j);
575 label.replace(i, k - i, theCounter(newc, lang));
578 //lyxerr << "label=" << label << endl;
580 size_t const i = label.find('\\', 0);
581 if (i == docstring::npos)
583 size_t const j = label.find('{', i + 1);
584 if (j == docstring::npos)
586 size_t const k = label.find('}', j + 1);
587 if (k == docstring::npos)
589 docstring const numbertype(label, i + 1, j - i - 1);
590 docstring const counter(label, j + 1, k - j - 1);
591 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
593 //lyxerr << "DONE! label=" << label << endl;
598 docstring Counters::prettyCounter(docstring const & name,
599 string const & lang) const
601 CounterList::const_iterator it = counterList_.find(name);
602 if (it == counterList_.end())
603 return from_ascii("#");
604 Counter const & ctr = it->second;
606 docstring const value = theCounter(name, lang);
607 docstring const & format =
608 translateIfPossible(ctr.prettyFormat(), lang);
611 return subst(format, from_ascii("##"), value);
615 docstring Counters::currentCounter() const
617 LBUFERR(!counter_stack_.empty());
618 return counter_stack_.back();
622 void Counters::setActiveLayout(Layout const & lay)
624 LASSERT(!layout_stack_.empty(), return);
625 Layout const * const lastlay = layout_stack_.back();
626 // we want to check whether the layout has changed and, if so,
627 // whether we are coming out of or going into an environment.
629 layout_stack_.pop_back();
630 layout_stack_.push_back(&lay);
631 if (lay.isEnvironment())
633 } else if (lastlay->name() != lay.name()) {
634 layout_stack_.pop_back();
635 layout_stack_.push_back(&lay);
636 if (lastlay->isEnvironment()) {
637 // we are coming out of an environment
638 // LYXERR0("Out: " << lastlay->name());
641 if (lay.isEnvironment()) {
642 // we are going into a new environment
643 // LYXERR0("In: " << lay.name());
650 void Counters::beginEnvironment()
652 counter_stack_.push_back(counter_stack_.back());
656 void Counters::endEnvironment()
658 LASSERT(!counter_stack_.empty(), return);
659 counter_stack_.pop_back();
663 vector<docstring> Counters::listOfCounters() const {
664 vector<docstring> ret;
665 for(auto const & k : counterList_)
666 ret.emplace_back(k.first);