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::stepMaster(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;
289 step(it->second.master(), utype);
293 void Counters::step(docstring const & ctr, UpdateType utype)
295 CounterList::iterator it = counterList_.find(ctr);
296 if (it == counterList_.end()) {
297 lyxerr << "step: Counter does not exist: "
298 << to_utf8(ctr) << endl;
303 if (utype == OutputUpdate) {
304 LBUFERR(!counter_stack_.empty());
305 counter_stack_.pop_back();
306 counter_stack_.push_back(ctr);
313 void Counters::reset()
317 current_float_.erase();
318 CounterList::iterator it = counterList_.begin();
319 CounterList::iterator const end = counterList_.end();
320 for (; it != end; ++it)
322 counter_stack_.clear();
323 counter_stack_.push_back(from_ascii(""));
324 layout_stack_.clear();
325 layout_stack_.push_back(0);
329 void Counters::reset(docstring const & match)
331 LASSERT(!match.empty(), return);
333 CounterList::iterator it = counterList_.begin();
334 CounterList::iterator end = counterList_.end();
335 for (; it != end; ++it) {
336 if (it->first.find(match) != string::npos)
342 bool Counters::remove(docstring const & cnt)
344 bool retval = counterList_.erase(cnt);
347 CounterList::iterator it = counterList_.begin();
348 CounterList::iterator end = counterList_.end();
349 for (; it != end; ++it) {
350 if (it->second.checkAndRemoveMaster(cnt))
351 LYXERR(Debug::TCLASS, "Removed master counter `" +
352 to_utf8(cnt) + "' from counter: " + to_utf8(it->first));
358 void Counters::copy(Counters & from, Counters & to, docstring const & match)
360 CounterList::iterator it = counterList_.begin();
361 CounterList::iterator end = counterList_.end();
362 for (; it != end; ++it) {
363 if (it->first.find(match) != string::npos || match == "") {
364 to.set(it->first, from.value(it->first));
372 char loweralphaCounter(int const n)
380 char alphaCounter(int const n)
388 char hebrewCounter(int const n)
390 static const char hebrew[22] = {
391 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
392 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
393 '\xf7', '\xf8', '\xf9', '\xfa'
398 return hebrew[n - 1];
402 // On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
403 // and for a list of roman numerals up to and including 3999, see
404 // http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
406 docstring const romanCounter(int const n)
408 static char const * const ones[9] = {
409 "I", "II", "III", "IV", "V",
410 "VI", "VII", "VIII", "IX"
413 static char const * const tens[9] = {
414 "X", "XX", "XXX", "XL", "L",
415 "LX", "LXX", "LXXX", "XC"
418 static char const * const hunds[9] = {
419 "C", "CC", "CCC", "CD", "D",
420 "DC", "DCC", "DCCC", "CM"
423 if (n >= 1000 || n < 1)
424 return from_ascii("??");
438 int hundreds = val / 100;
439 roman = hunds[hundreds - 1];
446 roman = roman + "XC";
450 int tensnum = val / 10;
451 roman = roman + tens[tensnum - 1];
456 roman = roman + ones[val -1];
458 return from_ascii(roman);
462 docstring const lowerromanCounter(int const n)
464 return lowercase(romanCounter(n));
468 docstring const fnsymbolCounter(int const n)
471 case 1: return docstring(1, 0x002a); //*
472 case 2: return docstring(1, 0x2020); // dagger
473 case 3: return docstring(1, 0x2021); // double dagger
474 case 4: return docstring(1, 0x00A7); // section sign
475 case 5: return docstring(1, 0x00B6); // pilcrow sign
476 case 6: return docstring(1, 0x2016); // vertical bar
477 case 7: return docstring(2, 0x002a); // two *
478 case 8: return docstring(2, 0x2020); // two daggers
479 case 9: return docstring(2, 0x2021); // two double daggers
481 return from_ascii("?");
488 docstring Counters::labelItem(docstring const & ctr,
489 docstring const & numbertype) const
491 CounterList::const_iterator const cit = counterList_.find(ctr);
492 if (cit == counterList_.end()) {
495 << " does not exist." << endl;
499 int val = cit->second.value();
501 if (numbertype == "hebrew")
502 return docstring(1, hebrewCounter(val));
504 if (numbertype == "alph")
505 return docstring(1, loweralphaCounter(val));
507 if (numbertype == "Alph")
508 return docstring(1, alphaCounter(val));
510 if (numbertype == "roman")
511 return lowerromanCounter(val);
513 if (numbertype == "Roman")
514 return romanCounter(val);
516 if (numbertype == "fnsymbol")
517 return fnsymbolCounter(val);
519 return convert<docstring>(val);
523 docstring Counters::theCounter(docstring const & counter,
524 string const & lang) const
526 CounterList::const_iterator it = counterList_.find(counter);
527 if (it == counterList_.end())
528 return from_ascii("#");
529 Counter const & ctr = it->second;
530 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
531 Counter::StringMap::iterator smit = sm.find(lang);
532 if (smit != sm.end())
533 return counterLabel(smit->second, lang);
535 vector<docstring> callers;
536 docstring const & fls = flattenLabelString(counter, appendix(),
539 return counterLabel(fls, lang);
543 docstring Counters::flattenLabelString(docstring const & counter,
546 vector<docstring> & callers) const
548 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
549 // recursion detected
550 lyxerr << "Warning: Recursion in label for counter `"
551 << counter << "' detected"
553 return from_ascii("??");
556 CounterList::const_iterator it = counterList_.find(counter);
557 if (it == counterList_.end())
558 return from_ascii("#");
559 Counter const & c = it->second;
561 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
563 callers.push_back(counter);
565 if (!c.master().empty())
566 ls = flattenLabelString(c.master(), in_appendix, lang, callers)
569 return ls + from_ascii("\\arabic{") + counter + "}";
573 //lyxerr << "ls=" << to_utf8(ls) << endl;
574 size_t const i = ls.find(from_ascii("\\the"), 0);
575 if (i == docstring::npos)
577 size_t const j = i + 4;
579 while (k < ls.size() && lowercase(ls[k]) >= 'a'
580 && lowercase(ls[k]) <= 'z')
582 docstring const newc = ls.substr(j, k - j);
583 docstring const repl = flattenLabelString(newc, in_appendix,
585 ls.replace(i, k - j + 4, repl);
593 docstring Counters::counterLabel(docstring const & format,
594 string const & lang) const
596 docstring label = format;
598 // FIXME: Using regexps would be better, but we compile boost without
599 // wide regexps currently.
600 docstring const the = from_ascii("\\the");
602 //lyxerr << "label=" << label << endl;
603 size_t const i = label.find(the, 0);
604 if (i == docstring::npos)
606 size_t const j = i + 4;
608 while (k < label.size() && lowercase(label[k]) >= 'a'
609 && lowercase(label[k]) <= 'z')
611 docstring const newc(label, j, k - j);
612 label.replace(i, k - i, theCounter(newc, lang));
615 //lyxerr << "label=" << label << endl;
617 size_t const i = label.find('\\', 0);
618 if (i == docstring::npos)
620 size_t const j = label.find('{', i + 1);
621 if (j == docstring::npos)
623 size_t const k = label.find('}', j + 1);
624 if (k == docstring::npos)
626 docstring const numbertype(label, i + 1, j - i - 1);
627 docstring const counter(label, j + 1, k - j - 1);
628 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
630 //lyxerr << "DONE! label=" << label << endl;
635 docstring Counters::prettyCounter(docstring const & name,
636 string const & lang) const
638 CounterList::const_iterator it = counterList_.find(name);
639 if (it == counterList_.end())
640 return from_ascii("#");
641 Counter const & ctr = it->second;
643 docstring const value = theCounter(name, lang);
644 docstring const & format =
645 translateIfPossible(ctr.prettyFormat(), lang);
648 return subst(format, from_ascii("##"), value);
652 docstring Counters::currentCounter() const
654 LBUFERR(!counter_stack_.empty());
655 return counter_stack_.back();
659 void Counters::setActiveLayout(Layout const & lay)
661 LASSERT(!layout_stack_.empty(), return);
662 Layout const * const lastlay = layout_stack_.back();
663 // we want to check whether the layout has changed and, if so,
664 // whether we are coming out of or going into an environment.
666 layout_stack_.pop_back();
667 layout_stack_.push_back(&lay);
668 if (lay.isEnvironment())
670 } else if (lastlay->name() != lay.name()) {
671 layout_stack_.pop_back();
672 layout_stack_.push_back(&lay);
673 if (lastlay->isEnvironment()) {
674 // we are coming out of an environment
675 // LYXERR0("Out: " << lastlay->name());
678 if (lay.isEnvironment()) {
679 // we are going into a new environment
680 // LYXERR0("In: " << lay.name());
687 void Counters::beginEnvironment()
689 counter_stack_.push_back(counter_stack_.back());
693 void Counters::endEnvironment()
695 LASSERT(!counter_stack_.empty(), return);
696 counter_stack_.pop_back();