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,
63 LexerKeyword counterTags[] = {
65 { "guiname", CT_GUINAME },
66 { "initialvalue", CT_INITIALVALUE},
67 { "labelstring", CT_LABELSTRING },
68 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
69 { "prettyformat", CT_PRETTYFORMAT },
70 { "within", CT_WITHIN }
73 lex.pushTable(counterTags);
76 while (!getout && lex.isOK()) {
79 case Lexer::LEX_UNDEF:
80 lex.printError("Unknown counter tag `$$Token'");
88 master_ = lex.getDocString();
89 if (master_ == "none")
94 initial_value_ = lex.getInteger();
95 // getInteger() returns -1 on error, and larger
96 // negative values do not make much sense.
97 // In the other case, we subtract one, since the
98 // counter will be incremented before its first use.
99 if (initial_value_ <= -1)
104 case CT_PRETTYFORMAT:
106 prettyformat_ = lex.getDocString();
110 labelstring_ = lex.getDocString();
111 labelstringappendix_ = labelstring_;
113 case CT_LABELSTRING_APPENDIX:
115 labelstringappendix_ = lex.getDocString();
119 guiname_ = lex.getDocString();
127 // Here if have a full counter if getout == true
129 LYXERR0("No End tag found for counter!");
135 void Counter::set(int v)
141 void Counter::addto(int v)
147 int Counter::value() const
153 void Counter::saveValue()
155 saved_value_ = value_;
159 void Counter::restoreValue()
161 value_ = saved_value_;
171 void Counter::reset()
173 value_ = initial_value_;
177 docstring const & Counter::master() const
183 bool Counter::checkAndRemoveMaster(docstring const & cnt)
187 master_ = docstring();
192 docstring const & Counter::labelString(bool in_appendix) const
194 return in_appendix ? labelstringappendix_ : labelstring_;
198 Counter::StringMap & Counter::flatLabelStrings(bool in_appendix) const
200 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
204 Counters::Counters() : appendix_(false), subfloat_(false), longtable_(false)
206 layout_stack_.push_back(nullptr);
207 counter_stack_.push_back(from_ascii(""));
211 void Counters::newCounter(docstring const & newc,
212 docstring const & masterc,
213 docstring const & ls,
214 docstring const & lsa)
216 if (!masterc.empty() && !hasCounter(masterc)) {
217 lyxerr << "Master counter does not exist: "
222 counterList_[newc] = Counter(masterc, ls, lsa);
226 bool Counters::hasCounter(docstring const & c) const
228 return counterList_.find(c) != counterList_.end();
232 bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
234 if (hasCounter(name)) {
235 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
236 return counterList_[name].read(lex);
239 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
241 bool success = cnt.read(lex);
242 // if makenew is false, we will just discard what we read
243 if (success && makenew)
244 counterList_[name] = cnt;
246 LYXERR0("Error reading counter `" << name << "'!");
251 void Counters::set(docstring const & ctr, int const val)
253 CounterList::iterator const it = counterList_.find(ctr);
254 if (it == counterList_.end()) {
255 lyxerr << "set: Counter does not exist: "
256 << to_utf8(ctr) << endl;
263 void Counters::addto(docstring const & ctr, int const val)
265 CounterList::iterator const it = counterList_.find(ctr);
266 if (it == counterList_.end()) {
267 lyxerr << "addto: Counter does not exist: "
268 << to_utf8(ctr) << endl;
271 it->second.addto(val);
275 int Counters::value(docstring const & ctr) const
277 CounterList::const_iterator const cit = counterList_.find(ctr);
278 if (cit == counterList_.end()) {
279 lyxerr << "value: Counter does not exist: "
280 << to_utf8(ctr) << endl;
283 return cit->second.value();
287 void Counters::saveValue(docstring const & ctr) const
289 CounterList::const_iterator const cit = counterList_.find(ctr);
290 if (cit == counterList_.end()) {
291 lyxerr << "value: Counter does not exist: "
292 << to_utf8(ctr) << endl;
295 Counter const & cnt = cit->second;
296 Counter & ccnt = const_cast<Counter &>(cnt);
301 void Counters::restoreValue(docstring const & ctr) const
303 CounterList::const_iterator const cit = counterList_.find(ctr);
304 if (cit == counterList_.end()) {
305 lyxerr << "value: Counter does not exist: "
306 << to_utf8(ctr) << endl;
309 Counter const & cnt = cit->second;
310 Counter & ccnt = const_cast<Counter &>(cnt);
315 void Counters::resetSlaves(docstring const & count)
317 for (auto & ctr : counterList_) {
318 if (ctr.second.master() == count) {
320 resetSlaves(ctr.first);
326 void Counters::stepMaster(docstring const & ctr, UpdateType utype)
328 CounterList::iterator it = counterList_.find(ctr);
329 if (it == counterList_.end()) {
330 lyxerr << "step: Counter does not exist: "
331 << to_utf8(ctr) << endl;
334 step(it->second.master(), utype);
338 void Counters::step(docstring const & ctr, UpdateType utype)
340 CounterList::iterator it = counterList_.find(ctr);
341 if (it == counterList_.end()) {
342 lyxerr << "step: Counter does not exist: "
343 << to_utf8(ctr) << endl;
348 if (utype == OutputUpdate) {
349 LBUFERR(!counter_stack_.empty());
350 counter_stack_.pop_back();
351 counter_stack_.push_back(ctr);
358 docstring const & Counters::guiName(docstring const & cntr) const
360 CounterList::const_iterator it = counterList_.find(cntr);
361 if (it == counterList_.end()) {
362 lyxerr << "step: Counter does not exist: "
363 << to_utf8(cntr) << endl;
364 return empty_docstring();
367 docstring const & guiname = it->second.guiName();
374 void Counters::reset()
378 current_float_.erase();
379 for (auto & ctr : counterList_)
381 counter_stack_.clear();
382 counter_stack_.push_back(from_ascii(""));
383 layout_stack_.clear();
384 layout_stack_.push_back(nullptr);
388 void Counters::reset(docstring const & match)
390 LASSERT(!match.empty(), return);
392 for (auto & ctr : counterList_) {
393 if (ctr.first.find(match) != string::npos)
399 bool Counters::remove(docstring const & cnt)
401 bool retval = counterList_.erase(cnt);
404 for (auto & ctr : counterList_) {
405 if (ctr.second.checkAndRemoveMaster(cnt))
406 LYXERR(Debug::TCLASS, "Removed master counter `" +
407 to_utf8(cnt) + "' from counter: " + to_utf8(ctr.first));
413 void Counters::copy(Counters & from, Counters & to, docstring const & match)
415 for (auto const & ctr : counterList_) {
416 if (ctr.first.find(match) != string::npos || match == "") {
417 to.set(ctr.first, from.value(ctr.first));
423 docstring Counters::labelItem(docstring const & ctr,
424 docstring const & numbertype) const
426 CounterList::const_iterator const cit = counterList_.find(ctr);
427 if (cit == counterList_.end()) {
430 << " does not exist." << endl;
434 int val = cit->second.value();
436 if (numbertype == "hebrew")
437 return docstring(1, hebrewCounter(val));
439 if (numbertype == "alph")
440 return docstring(1, loweralphaCounter(val));
442 if (numbertype == "Alph")
443 return docstring(1, alphaCounter(val));
445 if (numbertype == "roman")
446 return lowerromanCounter(val);
448 if (numbertype == "Roman")
449 return romanCounter(val);
451 if (numbertype == "fnsymbol")
452 return fnsymbolCounter(val);
454 return convert<docstring>(val);
458 docstring Counters::theCounter(docstring const & counter,
459 string const & lang) const
461 CounterList::const_iterator it = counterList_.find(counter);
462 if (it == counterList_.end())
463 return from_ascii("#");
464 Counter const & ctr = it->second;
465 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
466 Counter::StringMap::iterator smit = sm.find(lang);
467 if (smit != sm.end())
468 return counterLabel(smit->second, lang);
470 vector<docstring> callers;
471 docstring const & fls = flattenLabelString(counter, appendix(),
474 return counterLabel(fls, lang);
478 docstring Counters::flattenLabelString(docstring const & counter,
481 vector<docstring> & callers) const
483 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
484 // recursion detected
485 lyxerr << "Warning: Recursion in label for counter `"
486 << counter << "' detected"
488 return from_ascii("??");
491 CounterList::const_iterator it = counterList_.find(counter);
492 if (it == counterList_.end())
493 return from_ascii("#");
494 Counter const & c = it->second;
496 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
498 callers.push_back(counter);
500 if (!c.master().empty())
501 ls = flattenLabelString(c.master(), in_appendix, lang, callers)
504 return ls + from_ascii("\\arabic{") + counter + "}";
508 //lyxerr << "ls=" << to_utf8(ls) << endl;
509 size_t const i = ls.find(from_ascii("\\the"), 0);
510 if (i == docstring::npos)
512 size_t const j = i + 4;
514 while (k < ls.size() && lowercase(ls[k]) >= 'a'
515 && lowercase(ls[k]) <= 'z')
517 docstring const newc = ls.substr(j, k - j);
518 docstring const repl = flattenLabelString(newc, in_appendix,
520 ls.replace(i, k - j + 4, repl);
528 docstring Counters::counterLabel(docstring const & format,
529 string const & lang) const
531 docstring label = format;
533 // FIXME: Using regexps would be better, but we compile boost without
534 // wide regexps currently.
535 docstring const the = from_ascii("\\the");
537 //lyxerr << "label=" << label << endl;
538 size_t const i = label.find(the, 0);
539 if (i == docstring::npos)
541 size_t const j = i + 4;
543 while (k < label.size() && lowercase(label[k]) >= 'a'
544 && lowercase(label[k]) <= 'z')
546 docstring const newc(label, j, k - j);
547 label.replace(i, k - i, theCounter(newc, lang));
550 //lyxerr << "label=" << label << endl;
552 size_t const i = label.find('\\', 0);
553 if (i == docstring::npos)
555 size_t const j = label.find('{', i + 1);
556 if (j == docstring::npos)
558 size_t const k = label.find('}', j + 1);
559 if (k == docstring::npos)
561 docstring const numbertype(label, i + 1, j - i - 1);
562 docstring const counter(label, j + 1, k - j - 1);
563 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
565 //lyxerr << "DONE! label=" << label << endl;
570 docstring Counters::prettyCounter(docstring const & name,
571 string const & lang) const
573 CounterList::const_iterator it = counterList_.find(name);
574 if (it == counterList_.end())
575 return from_ascii("#");
576 Counter const & ctr = it->second;
578 docstring const value = theCounter(name, lang);
579 docstring const & format =
580 translateIfPossible(ctr.prettyFormat(), lang);
583 return subst(format, from_ascii("##"), value);
587 docstring Counters::currentCounter() const
589 LBUFERR(!counter_stack_.empty());
590 return counter_stack_.back();
594 void Counters::setActiveLayout(Layout const & lay)
596 LASSERT(!layout_stack_.empty(), return);
597 Layout const * const lastlay = layout_stack_.back();
598 // we want to check whether the layout has changed and, if so,
599 // whether we are coming out of or going into an environment.
601 layout_stack_.pop_back();
602 layout_stack_.push_back(&lay);
603 if (lay.isEnvironment())
605 } else if (lastlay->name() != lay.name()) {
606 layout_stack_.pop_back();
607 layout_stack_.push_back(&lay);
608 if (lastlay->isEnvironment()) {
609 // we are coming out of an environment
610 // LYXERR0("Out: " << lastlay->name());
613 if (lay.isEnvironment()) {
614 // we are going into a new environment
615 // LYXERR0("In: " << lay.name());
622 void Counters::beginEnvironment()
624 counter_stack_.push_back(counter_stack_.back());
628 void Counters::endEnvironment()
630 LASSERT(!counter_stack_.empty(), return);
631 counter_stack_.pop_back();
635 vector<docstring> Counters::listOfCounters() const {
636 vector<docstring> ret;
637 for(auto const & k : counterList_)
638 ret.emplace_back(k.first);