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;
41 Counter::Counter(docstring const & mc, docstring const & ls,
42 docstring const & lsa)
43 : initial_value_(0), master_(mc), labelstring_(ls), labelstringappendix_(lsa)
49 bool Counter::read(Lexer & lex)
54 CT_LABELSTRING_APPENDIX,
60 LexerKeyword counterTags[] = {
62 { "initialvalue", CT_INITIALVALUE},
63 { "labelstring", CT_LABELSTRING },
64 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
65 { "prettyformat", CT_PRETTYFORMAT },
66 { "within", CT_WITHIN }
69 lex.pushTable(counterTags);
72 while (!getout && lex.isOK()) {
75 case Lexer::LEX_UNDEF:
76 lex.printError("Unknown counter tag `$$Token'");
84 master_ = lex.getDocString();
85 if (master_ == "none")
90 initial_value_ = lex.getInteger();
91 // getInteger() returns -1 on error, and larger
92 // negative values do not make much sense.
93 // In the other case, we subtract one, since the
94 // counter will be incremented before its first use.
95 if (initial_value_ <= -1)
100 case CT_PRETTYFORMAT:
102 prettyformat_ = lex.getDocString();
106 labelstring_ = lex.getDocString();
107 labelstringappendix_ = labelstring_;
109 case CT_LABELSTRING_APPENDIX:
111 labelstringappendix_ = lex.getDocString();
119 // Here if have a full counter if getout == true
121 LYXERR0("No End tag found for counter!");
127 void Counter::set(int v)
133 void Counter::addto(int v)
139 int Counter::value() const
151 void Counter::reset()
153 value_ = initial_value_;
157 docstring const & Counter::master() const
163 bool Counter::checkAndRemoveMaster(docstring const & cnt)
167 master_ = docstring();
172 docstring const & Counter::labelString(bool in_appendix) const
174 return in_appendix ? labelstringappendix_ : labelstring_;
178 Counter::StringMap & Counter::flatLabelStrings(bool in_appendix) const
180 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
184 Counters::Counters() : appendix_(false), subfloat_(false)
186 layout_stack_.push_back(0);
187 counter_stack_.push_back(from_ascii(""));
191 void Counters::newCounter(docstring const & newc,
192 docstring const & masterc,
193 docstring const & ls,
194 docstring const & lsa)
196 if (!masterc.empty() && !hasCounter(masterc)) {
197 lyxerr << "Master counter does not exist: "
202 counterList_[newc] = Counter(masterc, ls, lsa);
206 bool Counters::hasCounter(docstring const & c) const
208 return counterList_.find(c) != counterList_.end();
212 bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
214 if (hasCounter(name)) {
215 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
216 return counterList_[name].read(lex);
219 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
221 bool success = cnt.read(lex);
222 // if makenew is false, we will just discard what we read
223 if (success && makenew)
224 counterList_[name] = cnt;
226 LYXERR0("Error reading counter `" << name << "'!");
231 void Counters::set(docstring const & ctr, int const val)
233 CounterList::iterator const it = counterList_.find(ctr);
234 if (it == counterList_.end()) {
235 lyxerr << "set: Counter does not exist: "
236 << to_utf8(ctr) << endl;
243 void Counters::addto(docstring const & ctr, int const val)
245 CounterList::iterator const it = counterList_.find(ctr);
246 if (it == counterList_.end()) {
247 lyxerr << "addto: Counter does not exist: "
248 << to_utf8(ctr) << endl;
251 it->second.addto(val);
255 int Counters::value(docstring const & ctr) const
257 CounterList::const_iterator const cit = counterList_.find(ctr);
258 if (cit == counterList_.end()) {
259 lyxerr << "value: Counter does not exist: "
260 << to_utf8(ctr) << endl;
263 return cit->second.value();
267 void Counters::step(docstring const & ctr, UpdateType utype)
269 CounterList::iterator it = counterList_.find(ctr);
270 if (it == counterList_.end()) {
271 lyxerr << "step: Counter does not exist: "
272 << to_utf8(ctr) << endl;
277 if (utype == OutputUpdate) {
278 LBUFERR(!counter_stack_.empty());
279 counter_stack_.pop_back();
280 counter_stack_.push_back(ctr);
282 it = counterList_.begin();
283 CounterList::iterator const end = counterList_.end();
284 for (; it != end; ++it) {
285 if (it->second.master() == ctr) {
292 void Counters::reset()
296 current_float_.erase();
297 CounterList::iterator it = counterList_.begin();
298 CounterList::iterator const end = counterList_.end();
299 for (; it != end; ++it)
301 counter_stack_.clear();
302 counter_stack_.push_back(from_ascii(""));
303 layout_stack_.clear();
304 layout_stack_.push_back(0);
308 void Counters::reset(docstring const & match)
310 LASSERT(!match.empty(), return);
312 CounterList::iterator it = counterList_.begin();
313 CounterList::iterator end = counterList_.end();
314 for (; it != end; ++it) {
315 if (it->first.find(match) != string::npos)
321 bool Counters::remove(docstring const & cnt)
323 bool retval = counterList_.erase(cnt);
326 CounterList::iterator it = counterList_.begin();
327 CounterList::iterator end = counterList_.end();
328 for (; it != end; ++it) {
329 if (it->second.checkAndRemoveMaster(cnt))
330 LYXERR(Debug::TCLASS, "Removed master counter `" +
331 to_utf8(cnt) + "' from counter: " + to_utf8(it->first));
337 void Counters::copy(Counters & from, Counters & to, docstring const & match)
339 CounterList::iterator it = counterList_.begin();
340 CounterList::iterator end = counterList_.end();
341 for (; it != end; ++it) {
342 if (it->first.find(match) != string::npos || match == "") {
343 to.set(it->first, from.value(it->first));
351 char loweralphaCounter(int const n)
359 char alphaCounter(int const n)
367 char hebrewCounter(int const n)
369 static const char hebrew[22] = {
370 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
371 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
372 '\xf7', '\xf8', '\xf9', '\xfa'
377 return hebrew[n - 1];
381 // On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
382 // and for a list of roman numerals up to and including 3999, see
383 // http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
385 docstring const romanCounter(int const n)
387 static char const * const ones[9] = {
388 "I", "II", "III", "IV", "V",
389 "VI", "VII", "VIII", "IX"
392 static char const * const tens[9] = {
393 "X", "XX", "XXX", "XL", "L",
394 "LX", "LXX", "LXXX", "XC"
397 static char const * const hunds[9] = {
398 "C", "CC", "CCC", "CD", "D",
399 "DC", "DCC", "DCCC", "CM"
402 if (n > 1000 || n < 1)
403 return from_ascii("??");
417 int hundreds = val / 100;
418 roman = hunds[hundreds - 1];
425 roman = roman + "XC";
429 int tensnum = val / 10;
430 roman = roman + tens[tensnum - 1];
435 roman = roman + ones[val -1];
437 return from_ascii(roman);
441 docstring const lowerromanCounter(int const n)
443 return lowercase(romanCounter(n));
449 docstring Counters::labelItem(docstring const & ctr,
450 docstring const & numbertype) const
452 CounterList::const_iterator const cit = counterList_.find(ctr);
453 if (cit == counterList_.end()) {
456 << " does not exist." << endl;
460 int val = cit->second.value();
462 if (numbertype == "hebrew")
463 return docstring(1, hebrewCounter(val));
465 if (numbertype == "alph")
466 return docstring(1, loweralphaCounter(val));
468 if (numbertype == "Alph")
469 return docstring(1, alphaCounter(val));
471 if (numbertype == "roman")
472 return lowerromanCounter(val);
474 if (numbertype == "Roman")
475 return romanCounter(val);
477 return convert<docstring>(val);
481 docstring Counters::theCounter(docstring const & counter,
482 string const & lang) const
484 CounterList::const_iterator it = counterList_.find(counter);
485 if (it == counterList_.end())
486 return from_ascii("#");
487 Counter const & ctr = it->second;
488 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
489 Counter::StringMap::iterator smit = sm.find(lang);
490 if (smit != sm.end())
491 return counterLabel(smit->second, lang);
493 vector<docstring> callers;
494 docstring const & fls = flattenLabelString(counter, appendix(),
497 return counterLabel(fls, lang);
501 docstring Counters::flattenLabelString(docstring const & counter,
504 vector<docstring> & callers) const
508 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
509 // recursion detected
510 lyxerr << "Warning: Recursion in label for counter `"
511 << counter << "' detected"
513 return from_ascii("??");
516 CounterList::const_iterator it = counterList_.find(counter);
517 if (it == counterList_.end())
518 return from_ascii("#");
519 Counter const & c = it->second;
521 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
523 callers.push_back(counter);
525 if (!c.master().empty())
526 ls = flattenLabelString(c.master(), in_appendix, lang, callers)
529 return ls + from_ascii("\\arabic{") + counter + "}";
533 //lyxerr << "ls=" << to_utf8(ls) << endl;
534 size_t const i = ls.find(from_ascii("\\the"), 0);
535 if (i == docstring::npos)
537 size_t const j = i + 4;
539 while (k < ls.size() && lowercase(ls[k]) >= 'a'
540 && lowercase(ls[k]) <= 'z')
542 docstring const newc = ls.substr(j, k - j);
543 docstring const repl = flattenLabelString(newc, in_appendix,
545 ls.replace(i, k - j + 4, repl);
553 docstring Counters::counterLabel(docstring const & format,
554 string const & lang) const
556 docstring label = format;
558 // FIXME: Using regexps would be better, but we compile boost without
559 // wide regexps currently.
560 docstring const the = from_ascii("\\the");
562 //lyxerr << "label=" << label << endl;
563 size_t const i = label.find(the, 0);
564 if (i == docstring::npos)
566 size_t const j = i + 4;
568 while (k < label.size() && lowercase(label[k]) >= 'a'
569 && lowercase(label[k]) <= 'z')
571 docstring const newc(label, j, k - j);
572 label.replace(i, k - i, theCounter(newc, lang));
575 //lyxerr << "label=" << label << endl;
577 size_t const i = label.find('\\', 0);
578 if (i == docstring::npos)
580 size_t const j = label.find('{', i + 1);
581 if (j == docstring::npos)
583 size_t const k = label.find('}', j + 1);
584 if (k == docstring::npos)
586 docstring const numbertype(label, i + 1, j - i - 1);
587 docstring const counter(label, j + 1, k - j - 1);
588 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
590 //lyxerr << "DONE! label=" << label << endl;
595 docstring Counters::prettyCounter(docstring const & name,
596 string const & lang) const
598 CounterList::const_iterator it = counterList_.find(name);
599 if (it == counterList_.end())
600 return from_ascii("#");
601 Counter const & ctr = it->second;
603 docstring const value = theCounter(name, lang);
604 docstring const & format =
605 translateIfPossible(ctr.prettyFormat(), lang);
608 return subst(format, from_ascii("##"), value);
612 docstring Counters::currentCounter() const
614 LBUFERR(!counter_stack_.empty());
615 return counter_stack_.back();
619 void Counters::setActiveLayout(Layout const & lay)
621 LASSERT(!layout_stack_.empty(), return);
622 Layout const * const lastlay = layout_stack_.back();
623 // we want to check whether the layout has changed and, if so,
624 // whether we are coming out of or going into an environment.
626 layout_stack_.pop_back();
627 layout_stack_.push_back(&lay);
628 if (lay.isEnvironment())
630 } else if (lastlay->name() != lay.name()) {
631 layout_stack_.pop_back();
632 layout_stack_.push_back(&lay);
633 if (lastlay->isEnvironment()) {
634 // we are coming out of an environment
635 // LYXERR0("Out: " << lastlay->name());
638 if (lay.isEnvironment()) {
639 // we are going into a new environment
640 // LYXERR0("In: " << lay.name());
647 void Counters::beginEnvironment()
649 counter_stack_.push_back(counter_stack_.back());
653 void Counters::endEnvironment()
655 LASSERT(!counter_stack_.empty(), return);
656 counter_stack_.pop_back();