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), parent_(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 parent_ = lex.getDocString();
91 if (parent_ == "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::parent() const
185 bool Counter::checkAndRemoveParent(docstring const & cnt)
189 parent_ = 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 & parentc,
215 docstring const & ls,
216 docstring const & lsa,
217 docstring const & guiname)
219 if (!parentc.empty() && !hasCounter(parentc)) {
220 lyxerr << "Parent counter does not exist: "
225 counterList_[newc] = Counter(parentc, 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::resetChildren(docstring const & count)
320 for (auto & ctr : counterList_) {
321 if (ctr.second.parent() == count) {
323 resetChildren(ctr.first);
329 void Counters::stepParent(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.parent(), 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.checkAndRemoveParent(cnt))
409 LYXERR(Debug::TCLASS, "Removed parent counter `" +
410 to_utf8(cnt) + "' from counter: " + to_utf8(ctr.first));
416 docstring Counters::labelItem(docstring const & ctr,
417 docstring const & numbertype) const
419 CounterList::const_iterator const cit = counterList_.find(ctr);
420 if (cit == counterList_.end()) {
423 << " does not exist." << endl;
427 int val = cit->second.value();
429 if (numbertype == "hebrew")
430 return docstring(1, hebrewCounter(val));
432 if (numbertype == "alph")
433 return docstring(1, loweralphaCounter(val));
435 if (numbertype == "Alph")
436 return docstring(1, alphaCounter(val));
438 if (numbertype == "roman")
439 return lowerromanCounter(val);
441 if (numbertype == "Roman")
442 return romanCounter(val);
444 if (numbertype == "fnsymbol")
445 return fnsymbolCounter(val);
447 return convert<docstring>(val);
451 docstring Counters::theCounter(docstring const & counter,
452 string const & lang) const
454 CounterList::const_iterator it = counterList_.find(counter);
455 if (it == counterList_.end())
456 return from_ascii("#");
457 Counter const & ctr = it->second;
458 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
459 Counter::StringMap::iterator smit = sm.find(lang);
460 if (smit != sm.end())
461 return counterLabel(smit->second, lang);
463 vector<docstring> callers;
464 docstring const & fls = flattenLabelString(counter, appendix(),
467 return counterLabel(fls, lang);
471 docstring Counters::flattenLabelString(docstring const & counter,
474 vector<docstring> & callers) const
476 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
477 // recursion detected
478 lyxerr << "Warning: Recursion in label for counter `"
479 << counter << "' detected"
481 return from_ascii("??");
484 CounterList::const_iterator it = counterList_.find(counter);
485 if (it == counterList_.end())
486 return from_ascii("#");
487 Counter const & c = it->second;
489 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
491 callers.push_back(counter);
493 if (!c.parent().empty())
494 ls = flattenLabelString(c.parent(), in_appendix, lang, callers)
497 return ls + from_ascii("\\arabic{") + counter + "}";
501 //lyxerr << "ls=" << to_utf8(ls) << endl;
502 size_t const i = ls.find(from_ascii("\\the"), 0);
503 if (i == docstring::npos)
505 size_t const j = i + 4;
507 while (k < ls.size() && lowercase(ls[k]) >= 'a'
508 && lowercase(ls[k]) <= 'z')
510 docstring const newc = ls.substr(j, k - j);
511 docstring const repl = flattenLabelString(newc, in_appendix,
513 ls.replace(i, k - j + 4, repl);
521 docstring Counters::counterLabel(docstring const & format,
522 string const & lang) const
524 docstring label = format;
526 // FIXME: Using regexps would be better, but we compile boost without
527 // wide regexps currently.
528 docstring const the = from_ascii("\\the");
530 //lyxerr << "label=" << label << endl;
531 size_t const i = label.find(the, 0);
532 if (i == docstring::npos)
534 size_t const j = i + 4;
536 while (k < label.size() && lowercase(label[k]) >= 'a'
537 && lowercase(label[k]) <= 'z')
539 docstring const newc(label, j, k - j);
540 label.replace(i, k - i, theCounter(newc, lang));
543 //lyxerr << "label=" << label << endl;
545 size_t const i = label.find('\\', 0);
546 if (i == docstring::npos)
548 size_t const j = label.find('{', i + 1);
549 if (j == docstring::npos)
551 size_t const k = label.find('}', j + 1);
552 if (k == docstring::npos)
554 docstring const numbertype(label, i + 1, j - i - 1);
555 docstring const counter(label, j + 1, k - j - 1);
556 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
558 //lyxerr << "DONE! label=" << label << endl;
563 docstring Counters::prettyCounter(docstring const & name,
564 string const & lang) const
566 CounterList::const_iterator it = counterList_.find(name);
567 if (it == counterList_.end())
568 return from_ascii("#");
569 Counter const & ctr = it->second;
571 docstring const value = theCounter(name, lang);
572 docstring const & format =
573 translateIfPossible(ctr.prettyFormat(), lang);
576 return subst(format, from_ascii("##"), value);
580 docstring Counters::currentCounter() const
582 LBUFERR(!counter_stack_.empty());
583 return counter_stack_.back();
587 void Counters::setActiveLayout(Layout const & lay)
589 LASSERT(!layout_stack_.empty(), return);
590 Layout const * const lastlay = layout_stack_.back();
591 // we want to check whether the layout has changed and, if so,
592 // whether we are coming out of or going into an environment.
594 layout_stack_.pop_back();
595 layout_stack_.push_back(&lay);
596 if (lay.isEnvironment())
598 } else if (lastlay->name() != lay.name()) {
599 layout_stack_.pop_back();
600 layout_stack_.push_back(&lay);
601 if (lastlay->isEnvironment()) {
602 // we are coming out of an environment
603 // LYXERR0("Out: " << lastlay->name());
606 if (lay.isEnvironment()) {
607 // we are going into a new environment
608 // LYXERR0("In: " << lay.name());
615 void Counters::beginEnvironment()
617 counter_stack_.push_back(counter_stack_.back());
621 void Counters::endEnvironment()
623 LASSERT(!counter_stack_.empty(), return);
624 counter_stack_.pop_back();
628 vector<docstring> Counters::listOfCounters() const {
629 vector<docstring> ret;
630 for(auto const & k : counterList_)
631 ret.emplace_back(k.first);