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/gettext.h"
24 #include "support/lassert.h"
25 #include "support/lstrings.h"
31 using namespace lyx::support;
43 Counter::Counter(docstring const & mc, docstring const & ls,
44 docstring const & lsa)
45 : initial_value_(0), master_(mc), labelstring_(ls), labelstringappendix_(lsa)
51 bool Counter::read(Lexer & lex)
56 CT_LABELSTRING_APPENDIX,
62 LexerKeyword counterTags[] = {
64 { "initialvalue", CT_INITIALVALUE},
65 { "labelstring", CT_LABELSTRING },
66 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
67 { "prettyformat", CT_PRETTYFORMAT },
68 { "within", CT_WITHIN }
71 lex.pushTable(counterTags);
74 while (!getout && lex.isOK()) {
77 case Lexer::LEX_UNDEF:
78 lex.printError("Unknown counter tag `$$Token'");
86 master_ = lex.getDocString();
87 if (master_ == "none")
92 initial_value_ = lex.getInteger();
93 // getInteger() returns -1 on error, and larger
94 // negative values do not make much sense.
95 // In the other case, we subtract one, since the
96 // counter will be incremented before its first use.
97 if (initial_value_ <= -1)
102 case CT_PRETTYFORMAT:
104 prettyformat_ = lex.getDocString();
108 labelstring_ = lex.getDocString();
109 labelstringappendix_ = labelstring_;
111 case CT_LABELSTRING_APPENDIX:
113 labelstringappendix_ = lex.getDocString();
121 // Here if have a full counter if getout == true
123 LYXERR0("No End tag found for counter!");
129 void Counter::set(int v)
135 void Counter::addto(int v)
141 int Counter::value() const
147 void Counter::saveValue()
149 saved_value_ = value_;
153 void Counter::restoreValue()
155 value_ = saved_value_;
165 void Counter::reset()
167 value_ = initial_value_;
171 docstring const & Counter::master() const
177 bool Counter::checkAndRemoveMaster(docstring const & cnt)
181 master_ = docstring();
186 docstring const & Counter::labelString(bool in_appendix) const
188 return in_appendix ? labelstringappendix_ : labelstring_;
192 Counter::StringMap & Counter::flatLabelStrings(bool in_appendix) const
194 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
198 Counters::Counters() : appendix_(false), subfloat_(false), longtable_(false)
200 layout_stack_.push_back(nullptr);
201 counter_stack_.push_back(from_ascii(""));
205 void Counters::newCounter(docstring const & newc,
206 docstring const & masterc,
207 docstring const & ls,
208 docstring const & lsa)
210 if (!masterc.empty() && !hasCounter(masterc)) {
211 lyxerr << "Master counter does not exist: "
216 counterList_[newc] = Counter(masterc, ls, lsa);
220 bool Counters::hasCounter(docstring const & c) const
222 return counterList_.find(c) != counterList_.end();
226 bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
228 if (hasCounter(name)) {
229 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
230 return counterList_[name].read(lex);
233 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
235 bool success = cnt.read(lex);
236 // if makenew is false, we will just discard what we read
237 if (success && makenew)
238 counterList_[name] = cnt;
240 LYXERR0("Error reading counter `" << name << "'!");
245 void Counters::set(docstring const & ctr, int const val)
247 CounterList::iterator const it = counterList_.find(ctr);
248 if (it == counterList_.end()) {
249 lyxerr << "set: Counter does not exist: "
250 << to_utf8(ctr) << endl;
257 void Counters::addto(docstring const & ctr, int const val)
259 CounterList::iterator const it = counterList_.find(ctr);
260 if (it == counterList_.end()) {
261 lyxerr << "addto: Counter does not exist: "
262 << to_utf8(ctr) << endl;
265 it->second.addto(val);
269 int Counters::value(docstring const & ctr) const
271 CounterList::const_iterator const cit = counterList_.find(ctr);
272 if (cit == counterList_.end()) {
273 lyxerr << "value: Counter does not exist: "
274 << to_utf8(ctr) << endl;
277 return cit->second.value();
281 void Counters::saveValue(docstring const & ctr) const
283 CounterList::const_iterator const cit = counterList_.find(ctr);
284 if (cit == counterList_.end()) {
285 lyxerr << "value: Counter does not exist: "
286 << to_utf8(ctr) << endl;
289 Counter const & cnt = cit->second;
290 Counter & ccnt = const_cast<Counter &>(cnt);
295 void Counters::restoreValue(docstring const & ctr) const
297 CounterList::const_iterator const cit = counterList_.find(ctr);
298 if (cit == counterList_.end()) {
299 lyxerr << "value: Counter does not exist: "
300 << to_utf8(ctr) << endl;
303 Counter const & cnt = cit->second;
304 Counter & ccnt = const_cast<Counter &>(cnt);
309 void Counters::resetSlaves(docstring const & count)
311 for (auto & ctr : counterList_) {
312 if (ctr.second.master() == count) {
314 resetSlaves(ctr.first);
320 void Counters::stepMaster(docstring const & ctr, UpdateType utype)
322 CounterList::iterator it = counterList_.find(ctr);
323 if (it == counterList_.end()) {
324 lyxerr << "step: Counter does not exist: "
325 << to_utf8(ctr) << endl;
328 step(it->second.master(), utype);
332 void Counters::step(docstring const & ctr, UpdateType utype)
334 CounterList::iterator it = counterList_.find(ctr);
335 if (it == counterList_.end()) {
336 lyxerr << "step: Counter does not exist: "
337 << to_utf8(ctr) << endl;
342 if (utype == OutputUpdate) {
343 LBUFERR(!counter_stack_.empty());
344 counter_stack_.pop_back();
345 counter_stack_.push_back(ctr);
352 void Counters::reset()
356 current_float_.erase();
357 for (auto & ctr : counterList_)
359 counter_stack_.clear();
360 counter_stack_.push_back(from_ascii(""));
361 layout_stack_.clear();
362 layout_stack_.push_back(nullptr);
366 void Counters::reset(docstring const & match)
368 LASSERT(!match.empty(), return);
370 for (auto & ctr : counterList_) {
371 if (ctr.first.find(match) != string::npos)
377 bool Counters::remove(docstring const & cnt)
379 bool retval = counterList_.erase(cnt);
382 for (auto & ctr : counterList_) {
383 if (ctr.second.checkAndRemoveMaster(cnt))
384 LYXERR(Debug::TCLASS, "Removed master counter `" +
385 to_utf8(cnt) + "' from counter: " + to_utf8(ctr.first));
391 void Counters::copy(Counters & from, Counters & to, docstring const & match)
393 for (auto const & ctr : counterList_) {
394 if (ctr.first.find(match) != string::npos || match == "") {
395 to.set(ctr.first, from.value(ctr.first));
401 docstring Counters::labelItem(docstring const & ctr,
402 docstring const & numbertype) const
404 CounterList::const_iterator const cit = counterList_.find(ctr);
405 if (cit == counterList_.end()) {
408 << " does not exist." << endl;
412 int val = cit->second.value();
414 if (numbertype == "hebrew")
415 return docstring(1, hebrewCounter(val));
417 if (numbertype == "alph")
418 return docstring(1, loweralphaCounter(val));
420 if (numbertype == "Alph")
421 return docstring(1, alphaCounter(val));
423 if (numbertype == "roman")
424 return lowerromanCounter(val);
426 if (numbertype == "Roman")
427 return romanCounter(val);
429 if (numbertype == "fnsymbol")
430 return fnsymbolCounter(val);
432 return convert<docstring>(val);
436 docstring Counters::theCounter(docstring const & counter,
437 string const & lang) const
439 CounterList::const_iterator it = counterList_.find(counter);
440 if (it == counterList_.end())
441 return from_ascii("#");
442 Counter const & ctr = it->second;
443 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
444 Counter::StringMap::iterator smit = sm.find(lang);
445 if (smit != sm.end())
446 return counterLabel(smit->second, lang);
448 vector<docstring> callers;
449 docstring const & fls = flattenLabelString(counter, appendix(),
452 return counterLabel(fls, lang);
456 docstring Counters::flattenLabelString(docstring const & counter,
459 vector<docstring> & callers) const
461 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
462 // recursion detected
463 lyxerr << "Warning: Recursion in label for counter `"
464 << counter << "' detected"
466 return from_ascii("??");
469 CounterList::const_iterator it = counterList_.find(counter);
470 if (it == counterList_.end())
471 return from_ascii("#");
472 Counter const & c = it->second;
474 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
476 callers.push_back(counter);
478 if (!c.master().empty())
479 ls = flattenLabelString(c.master(), in_appendix, lang, callers)
482 return ls + from_ascii("\\arabic{") + counter + "}";
486 //lyxerr << "ls=" << to_utf8(ls) << endl;
487 size_t const i = ls.find(from_ascii("\\the"), 0);
488 if (i == docstring::npos)
490 size_t const j = i + 4;
492 while (k < ls.size() && lowercase(ls[k]) >= 'a'
493 && lowercase(ls[k]) <= 'z')
495 docstring const newc = ls.substr(j, k - j);
496 docstring const repl = flattenLabelString(newc, in_appendix,
498 ls.replace(i, k - j + 4, repl);
506 docstring Counters::counterLabel(docstring const & format,
507 string const & lang) const
509 docstring label = format;
511 // FIXME: Using regexps would be better, but we compile boost without
512 // wide regexps currently.
513 docstring const the = from_ascii("\\the");
515 //lyxerr << "label=" << label << endl;
516 size_t const i = label.find(the, 0);
517 if (i == docstring::npos)
519 size_t const j = i + 4;
521 while (k < label.size() && lowercase(label[k]) >= 'a'
522 && lowercase(label[k]) <= 'z')
524 docstring const newc(label, j, k - j);
525 label.replace(i, k - i, theCounter(newc, lang));
528 //lyxerr << "label=" << label << endl;
530 size_t const i = label.find('\\', 0);
531 if (i == docstring::npos)
533 size_t const j = label.find('{', i + 1);
534 if (j == docstring::npos)
536 size_t const k = label.find('}', j + 1);
537 if (k == docstring::npos)
539 docstring const numbertype(label, i + 1, j - i - 1);
540 docstring const counter(label, j + 1, k - j - 1);
541 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
543 //lyxerr << "DONE! label=" << label << endl;
548 docstring Counters::prettyCounter(docstring const & name,
549 string const & lang) const
551 CounterList::const_iterator it = counterList_.find(name);
552 if (it == counterList_.end())
553 return from_ascii("#");
554 Counter const & ctr = it->second;
556 docstring const value = theCounter(name, lang);
557 docstring const & format =
558 translateIfPossible(ctr.prettyFormat(), lang);
561 return subst(format, from_ascii("##"), value);
565 docstring Counters::currentCounter() const
567 LBUFERR(!counter_stack_.empty());
568 return counter_stack_.back();
572 void Counters::setActiveLayout(Layout const & lay)
574 LASSERT(!layout_stack_.empty(), return);
575 Layout const * const lastlay = layout_stack_.back();
576 // we want to check whether the layout has changed and, if so,
577 // whether we are coming out of or going into an environment.
579 layout_stack_.pop_back();
580 layout_stack_.push_back(&lay);
581 if (lay.isEnvironment())
583 } else if (lastlay->name() != lay.name()) {
584 layout_stack_.pop_back();
585 layout_stack_.push_back(&lay);
586 if (lastlay->isEnvironment()) {
587 // we are coming out of an environment
588 // LYXERR0("Out: " << lastlay->name());
591 if (lay.isEnvironment()) {
592 // we are going into a new environment
593 // LYXERR0("In: " << lay.name());
600 void Counters::beginEnvironment()
602 counter_stack_.push_back(counter_stack_.back());
606 void Counters::endEnvironment()
608 LASSERT(!counter_stack_.empty(), return);
609 counter_stack_.pop_back();
613 vector<docstring> Counters::listOfCounters() const {
614 vector<docstring> ret;
615 for(auto const & k : counterList_)
616 ret.emplace_back(k.first);