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/debug.h"
22 #include "support/gettext.h"
23 #include "support/lassert.h"
24 #include "support/lstrings.h"
30 using namespace lyx::support;
42 Counter::Counter(docstring const & mc, docstring const & ls,
43 docstring const & lsa)
44 : initial_value_(0), master_(mc), labelstring_(ls), labelstringappendix_(lsa)
50 bool Counter::read(Lexer & lex)
55 CT_LABELSTRING_APPENDIX,
61 LexerKeyword counterTags[] = {
63 { "initialvalue", CT_INITIALVALUE},
64 { "labelstring", CT_LABELSTRING },
65 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
66 { "prettyformat", CT_PRETTYFORMAT },
67 { "within", CT_WITHIN }
70 lex.pushTable(counterTags);
73 while (!getout && lex.isOK()) {
76 case Lexer::LEX_UNDEF:
77 lex.printError("Unknown counter tag `$$Token'");
85 master_ = lex.getDocString();
86 if (master_ == "none")
91 initial_value_ = lex.getInteger();
92 // getInteger() returns -1 on error, and larger
93 // negative values do not make much sense.
94 // In the other case, we subtract one, since the
95 // counter will be incremented before its first use.
96 if (initial_value_ <= -1)
101 case CT_PRETTYFORMAT:
103 prettyformat_ = lex.getDocString();
107 labelstring_ = lex.getDocString();
108 labelstringappendix_ = labelstring_;
110 case CT_LABELSTRING_APPENDIX:
112 labelstringappendix_ = lex.getDocString();
120 // Here if have a full counter if getout == true
122 LYXERR0("No End tag found for counter!");
128 void Counter::set(int v)
134 void Counter::addto(int v)
140 int Counter::value() const
152 void Counter::reset()
154 value_ = initial_value_;
158 docstring const & Counter::master() const
164 bool Counter::checkAndRemoveMaster(docstring const & cnt)
168 master_ = docstring();
173 docstring const & Counter::labelString(bool in_appendix) const
175 return in_appendix ? labelstringappendix_ : labelstring_;
179 Counter::StringMap & Counter::flatLabelStrings(bool in_appendix) const
181 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
185 Counters::Counters() : appendix_(false), subfloat_(false), longtable_(false)
187 layout_stack_.push_back(0);
188 counter_stack_.push_back(from_ascii(""));
192 void Counters::newCounter(docstring const & newc,
193 docstring const & masterc,
194 docstring const & ls,
195 docstring const & lsa)
197 if (!masterc.empty() && !hasCounter(masterc)) {
198 lyxerr << "Master counter does not exist: "
203 counterList_[newc] = Counter(masterc, ls, lsa);
207 bool Counters::hasCounter(docstring const & c) const
209 return counterList_.find(c) != counterList_.end();
213 bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
215 if (hasCounter(name)) {
216 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
217 return counterList_[name].read(lex);
220 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
222 bool success = cnt.read(lex);
223 // if makenew is false, we will just discard what we read
224 if (success && makenew)
225 counterList_[name] = cnt;
227 LYXERR0("Error reading counter `" << name << "'!");
232 void Counters::set(docstring const & ctr, int const val)
234 CounterList::iterator const it = counterList_.find(ctr);
235 if (it == counterList_.end()) {
236 lyxerr << "set: Counter does not exist: "
237 << to_utf8(ctr) << endl;
244 void Counters::addto(docstring const & ctr, int const val)
246 CounterList::iterator const it = counterList_.find(ctr);
247 if (it == counterList_.end()) {
248 lyxerr << "addto: Counter does not exist: "
249 << to_utf8(ctr) << endl;
252 it->second.addto(val);
256 int Counters::value(docstring const & ctr) const
258 CounterList::const_iterator const cit = counterList_.find(ctr);
259 if (cit == counterList_.end()) {
260 lyxerr << "value: Counter does not exist: "
261 << to_utf8(ctr) << endl;
264 return cit->second.value();
268 void Counters::resetSlaves(docstring const & ctr)
270 CounterList::iterator it = counterList_.begin();
271 CounterList::iterator const end = counterList_.end();
272 for (; it != end; ++it) {
273 if (it->second.master() == ctr) {
275 resetSlaves(it->first);
281 void Counters::step(docstring const & ctr, UpdateType utype)
283 CounterList::iterator it = counterList_.find(ctr);
284 if (it == counterList_.end()) {
285 lyxerr << "step: Counter does not exist: "
286 << to_utf8(ctr) << endl;
291 if (utype == OutputUpdate) {
292 LBUFERR(!counter_stack_.empty());
293 counter_stack_.pop_back();
294 counter_stack_.push_back(ctr);
301 void Counters::reset()
305 current_float_.erase();
306 CounterList::iterator it = counterList_.begin();
307 CounterList::iterator const end = counterList_.end();
308 for (; it != end; ++it)
310 counter_stack_.clear();
311 counter_stack_.push_back(from_ascii(""));
312 layout_stack_.clear();
313 layout_stack_.push_back(0);
317 void Counters::reset(docstring const & match)
319 LASSERT(!match.empty(), return);
321 CounterList::iterator it = counterList_.begin();
322 CounterList::iterator end = counterList_.end();
323 for (; it != end; ++it) {
324 if (it->first.find(match) != string::npos)
330 bool Counters::remove(docstring const & cnt)
332 bool retval = counterList_.erase(cnt);
335 CounterList::iterator it = counterList_.begin();
336 CounterList::iterator end = counterList_.end();
337 for (; it != end; ++it) {
338 if (it->second.checkAndRemoveMaster(cnt))
339 LYXERR(Debug::TCLASS, "Removed master counter `" +
340 to_utf8(cnt) + "' from counter: " + to_utf8(it->first));
346 void Counters::copy(Counters & from, Counters & to, docstring const & match)
348 CounterList::iterator it = counterList_.begin();
349 CounterList::iterator end = counterList_.end();
350 for (; it != end; ++it) {
351 if (it->first.find(match) != string::npos || match == "") {
352 to.set(it->first, from.value(it->first));
360 char loweralphaCounter(int const n)
368 char alphaCounter(int const n)
376 char hebrewCounter(int const n)
378 static const char hebrew[22] = {
379 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
380 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
381 '\xf7', '\xf8', '\xf9', '\xfa'
386 return hebrew[n - 1];
390 // On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
391 // and for a list of roman numerals up to and including 3999, see
392 // http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
394 docstring const romanCounter(int const n)
396 static char const * const ones[9] = {
397 "I", "II", "III", "IV", "V",
398 "VI", "VII", "VIII", "IX"
401 static char const * const tens[9] = {
402 "X", "XX", "XXX", "XL", "L",
403 "LX", "LXX", "LXXX", "XC"
406 static char const * const hunds[9] = {
407 "C", "CC", "CCC", "CD", "D",
408 "DC", "DCC", "DCCC", "CM"
411 if (n >= 1000 || n < 1)
412 return from_ascii("??");
426 int hundreds = val / 100;
427 roman = hunds[hundreds - 1];
434 roman = roman + "XC";
438 int tensnum = val / 10;
439 roman = roman + tens[tensnum - 1];
444 roman = roman + ones[val -1];
446 return from_ascii(roman);
450 docstring const lowerromanCounter(int const n)
452 return lowercase(romanCounter(n));
456 docstring const fnsymbolCounter(int const n)
459 case 1: return docstring(1, 0x002a); //*
460 case 2: return docstring(1, 0x2020); // dagger
461 case 3: return docstring(1, 0x2021); // double dagger
462 case 4: return docstring(1, 0x00A7); // section sign
463 case 5: return docstring(1, 0x00B6); // pilcrow sign
464 case 6: return docstring(1, 0x2016); // vertical bar
465 case 7: return docstring(2, 0x002a); // two *
466 case 8: return docstring(2, 0x2020); // two daggers
467 case 9: return docstring(2, 0x2021); // two double daggers
469 return from_ascii("?");
476 docstring Counters::labelItem(docstring const & ctr,
477 docstring const & numbertype) const
479 CounterList::const_iterator const cit = counterList_.find(ctr);
480 if (cit == counterList_.end()) {
483 << " does not exist." << endl;
487 int val = cit->second.value();
489 if (numbertype == "hebrew")
490 return docstring(1, hebrewCounter(val));
492 if (numbertype == "alph")
493 return docstring(1, loweralphaCounter(val));
495 if (numbertype == "Alph")
496 return docstring(1, alphaCounter(val));
498 if (numbertype == "roman")
499 return lowerromanCounter(val);
501 if (numbertype == "Roman")
502 return romanCounter(val);
504 if (numbertype == "fnsymbol")
505 return fnsymbolCounter(val);
507 return convert<docstring>(val);
511 docstring Counters::theCounter(docstring const & counter,
512 string const & lang) const
514 CounterList::const_iterator it = counterList_.find(counter);
515 if (it == counterList_.end())
516 return from_ascii("#");
517 Counter const & ctr = it->second;
518 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
519 Counter::StringMap::iterator smit = sm.find(lang);
520 if (smit != sm.end())
521 return counterLabel(smit->second, lang);
523 vector<docstring> callers;
524 docstring const & fls = flattenLabelString(counter, appendix(),
527 return counterLabel(fls, lang);
531 docstring Counters::flattenLabelString(docstring const & counter,
534 vector<docstring> & callers) const
536 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
537 // recursion detected
538 lyxerr << "Warning: Recursion in label for counter `"
539 << counter << "' detected"
541 return from_ascii("??");
544 CounterList::const_iterator it = counterList_.find(counter);
545 if (it == counterList_.end())
546 return from_ascii("#");
547 Counter const & c = it->second;
549 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
551 callers.push_back(counter);
553 if (!c.master().empty())
554 ls = flattenLabelString(c.master(), in_appendix, lang, callers)
557 return ls + from_ascii("\\arabic{") + counter + "}";
561 //lyxerr << "ls=" << to_utf8(ls) << endl;
562 size_t const i = ls.find(from_ascii("\\the"), 0);
563 if (i == docstring::npos)
565 size_t const j = i + 4;
567 while (k < ls.size() && lowercase(ls[k]) >= 'a'
568 && lowercase(ls[k]) <= 'z')
570 docstring const newc = ls.substr(j, k - j);
571 docstring const repl = flattenLabelString(newc, in_appendix,
573 ls.replace(i, k - j + 4, repl);
581 docstring Counters::counterLabel(docstring const & format,
582 string const & lang) const
584 docstring label = format;
586 // FIXME: Using regexps would be better, but we compile boost without
587 // wide regexps currently.
588 docstring const the = from_ascii("\\the");
590 //lyxerr << "label=" << label << endl;
591 size_t const i = label.find(the, 0);
592 if (i == docstring::npos)
594 size_t const j = i + 4;
596 while (k < label.size() && lowercase(label[k]) >= 'a'
597 && lowercase(label[k]) <= 'z')
599 docstring const newc(label, j, k - j);
600 label.replace(i, k - i, theCounter(newc, lang));
603 //lyxerr << "label=" << label << endl;
605 size_t const i = label.find('\\', 0);
606 if (i == docstring::npos)
608 size_t const j = label.find('{', i + 1);
609 if (j == docstring::npos)
611 size_t const k = label.find('}', j + 1);
612 if (k == docstring::npos)
614 docstring const numbertype(label, i + 1, j - i - 1);
615 docstring const counter(label, j + 1, k - j - 1);
616 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
618 //lyxerr << "DONE! label=" << label << endl;
623 docstring Counters::prettyCounter(docstring const & name,
624 string const & lang) const
626 CounterList::const_iterator it = counterList_.find(name);
627 if (it == counterList_.end())
628 return from_ascii("#");
629 Counter const & ctr = it->second;
631 docstring const value = theCounter(name, lang);
632 docstring const & format =
633 translateIfPossible(ctr.prettyFormat(), lang);
636 return subst(format, from_ascii("##"), value);
640 docstring Counters::currentCounter() const
642 LBUFERR(!counter_stack_.empty());
643 return counter_stack_.back();
647 void Counters::setActiveLayout(Layout const & lay)
649 LASSERT(!layout_stack_.empty(), return);
650 Layout const * const lastlay = layout_stack_.back();
651 // we want to check whether the layout has changed and, if so,
652 // whether we are coming out of or going into an environment.
654 layout_stack_.pop_back();
655 layout_stack_.push_back(&lay);
656 if (lay.isEnvironment())
658 } else if (lastlay->name() != lay.name()) {
659 layout_stack_.pop_back();
660 layout_stack_.push_back(&lay);
661 if (lastlay->isEnvironment()) {
662 // we are coming out of an environment
663 // LYXERR0("Out: " << lastlay->name());
666 if (lay.isEnvironment()) {
667 // we are going into a new environment
668 // LYXERR0("In: " << lay.name());
675 void Counters::beginEnvironment()
677 counter_stack_.push_back(counter_stack_.back());
681 void Counters::endEnvironment()
683 LASSERT(!counter_stack_.empty(), return);
684 counter_stack_.pop_back();