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 : master_(mc), labelstring_(ls), labelstringappendix_(lsa)
49 bool Counter::read(Lexer & lex)
54 CT_LABELSTRING_APPENDIX,
59 LexerKeyword counterTags[] = {
61 { "labelstring", CT_LABELSTRING },
62 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
63 { "prettyformat", CT_PRETTYFORMAT },
64 { "within", CT_WITHIN }
67 lex.pushTable(counterTags);
70 while (!getout && lex.isOK()) {
73 case Lexer::LEX_UNDEF:
74 lex.printError("Unknown counter tag `$$Token'");
82 master_ = lex.getDocString();
83 if (master_ == "none")
88 prettyformat_ = lex.getDocString();
92 labelstring_ = lex.getDocString();
93 labelstringappendix_ = labelstring_;
95 case CT_LABELSTRING_APPENDIX:
97 labelstringappendix_ = lex.getDocString();
105 // Here if have a full counter if getout == true
107 LYXERR0("No End tag found for counter!");
112 void Counter::set(int v)
118 void Counter::addto(int v)
124 int Counter::value() const
136 void Counter::reset()
142 docstring const & Counter::master() const
148 docstring const & Counter::labelString(bool in_appendix) const
150 return in_appendix ? labelstringappendix_ : labelstring_;
154 Counter::StringMap & Counter::flatLabelStrings(bool in_appendix) const
156 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
160 Counters::Counters() : appendix_(false), subfloat_(false)
162 layout_stack_.push_back(0);
163 counter_stack_.push_back(from_ascii(""));
167 void Counters::newCounter(docstring const & newc,
168 docstring const & masterc,
169 docstring const & ls,
170 docstring const & lsa)
172 if (!masterc.empty() && !hasCounter(masterc)) {
173 lyxerr << "Master counter does not exist: "
178 counterList_[newc] = Counter(masterc, ls, lsa);
182 bool Counters::hasCounter(docstring const & c) const
184 return counterList_.find(c) != counterList_.end();
188 bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
190 if (hasCounter(name)) {
191 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
192 return counterList_[name].read(lex);
195 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
197 bool success = cnt.read(lex);
198 // if makenew is false, we will just discard what we read
199 if (success && makenew)
200 counterList_[name] = cnt;
202 LYXERR0("Error reading counter `" << name << "'!");
207 void Counters::set(docstring const & ctr, int const val)
209 CounterList::iterator const it = counterList_.find(ctr);
210 if (it == counterList_.end()) {
211 lyxerr << "set: Counter does not exist: "
212 << to_utf8(ctr) << endl;
219 void Counters::addto(docstring const & ctr, int const val)
221 CounterList::iterator const it = counterList_.find(ctr);
222 if (it == counterList_.end()) {
223 lyxerr << "addto: Counter does not exist: "
224 << to_utf8(ctr) << endl;
227 it->second.addto(val);
231 int Counters::value(docstring const & ctr) const
233 CounterList::const_iterator const cit = counterList_.find(ctr);
234 if (cit == counterList_.end()) {
235 lyxerr << "value: Counter does not exist: "
236 << to_utf8(ctr) << endl;
239 return cit->second.value();
243 void Counters::step(docstring const & ctr, UpdateType utype)
245 CounterList::iterator it = counterList_.find(ctr);
246 if (it == counterList_.end()) {
247 lyxerr << "step: Counter does not exist: "
248 << to_utf8(ctr) << endl;
253 if (utype == OutputUpdate) {
254 LASSERT(!counter_stack_.empty(), /* */);
255 counter_stack_.pop_back();
256 counter_stack_.push_back(ctr);
258 it = counterList_.begin();
259 CounterList::iterator const end = counterList_.end();
260 for (; it != end; ++it) {
261 if (it->second.master() == ctr) {
268 void Counters::reset()
272 current_float_.erase();
273 CounterList::iterator it = counterList_.begin();
274 CounterList::iterator const end = counterList_.end();
275 for (; it != end; ++it)
277 counter_stack_.clear();
278 counter_stack_.push_back(from_ascii(""));
279 layout_stack_.clear();
280 layout_stack_.push_back(0);
284 void Counters::reset(docstring const & match)
286 LASSERT(!match.empty(), /**/);
288 CounterList::iterator it = counterList_.begin();
289 CounterList::iterator end = counterList_.end();
290 for (; it != end; ++it) {
291 if (it->first.find(match) != string::npos)
297 bool Counters::remove(docstring const & cnt)
299 return counterList_.erase(cnt);
303 void Counters::copy(Counters & from, Counters & to, docstring const & match)
305 CounterList::iterator it = counterList_.begin();
306 CounterList::iterator end = counterList_.end();
307 for (; it != end; ++it) {
308 if (it->first.find(match) != string::npos || match == "") {
309 to.set(it->first, from.value(it->first));
317 char loweralphaCounter(int const n)
325 char alphaCounter(int const n)
333 char hebrewCounter(int const n)
335 static const char hebrew[22] = {
336 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
337 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
338 '\xf7', '\xf8', '\xf9', '\xfa'
343 return hebrew[n - 1];
348 // On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
349 // and for a list of roman numerals up to and including 3999, see
350 // http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
352 docstring const romanCounter(int const n)
354 static char const * const ones[9] = {
355 "I", "II", "III", "IV", "V",
356 "VI", "VII", "VIII", "IX"
359 static char const * const tens[9] = {
360 "X", "XX", "XXX", "XL", "L",
361 "LX", "LXX", "LXXX", "XC"
364 static char const * const hunds[9] = {
365 "C", "CC", "CCC", "CD", "D",
366 "DC", "DCC", "DCCC", "CM"
369 if (n > 1000 || n < 1)
370 return from_ascii("??");
384 int hundreds = val / 100;
385 roman = hunds[hundreds - 1];
392 roman = roman + "XC";
396 int tensnum = val / 10;
397 roman = roman + tens[tensnum - 1];
402 roman = roman + ones[val -1];
404 return from_ascii(roman);
408 docstring const lowerromanCounter(int const n)
410 return lowercase(romanCounter(n));
416 docstring Counters::labelItem(docstring const & ctr,
417 docstring const & numbertype) const
419 CounterList::const_iterator const cit = counterList_.find(ctr);
420 if (cit == counterList_.end()) {
423 << " does not exist." << endl;
427 int val = cit->second.value();
429 if (numbertype == "hebrew")
430 return docstring(1, hebrewCounter(val));
432 if (numbertype == "alph")
433 return docstring(1, loweralphaCounter(val));
435 if (numbertype == "Alph")
436 return docstring(1, alphaCounter(val));
438 if (numbertype == "roman")
439 return lowerromanCounter(val);
441 if (numbertype == "Roman")
442 return romanCounter(val);
444 return convert<docstring>(val);
448 docstring Counters::theCounter(docstring const & counter,
449 string const & lang) const
451 CounterList::const_iterator it = counterList_.find(counter);
452 if (it == counterList_.end())
453 return from_ascii("#");
454 Counter const & ctr = it->second;
455 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
456 Counter::StringMap::iterator smit = sm.find(lang);
457 if (smit != sm.end())
458 return counterLabel(smit->second, lang);
460 vector<docstring> callers;
461 docstring const & fls = flattenLabelString(counter, appendix(),
464 return counterLabel(fls, lang);
468 docstring Counters::flattenLabelString(docstring const & counter,
471 vector<docstring> & callers) const
475 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
476 // recursion detected
477 lyxerr << "Warning: Recursion in label for counter `"
478 << counter << "' detected"
480 return from_ascii("??");
483 CounterList::const_iterator it = counterList_.find(counter);
484 if (it == counterList_.end())
485 return from_ascii("#");
486 Counter const & c = it->second;
488 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
490 callers.push_back(counter);
492 if (!c.master().empty())
493 ls = flattenLabelString(c.master(), in_appendix, lang, callers)
496 return ls + from_ascii("\\arabic{") + counter + "}";
500 //lyxerr << "ls=" << to_utf8(ls) << endl;
501 size_t const i = ls.find(from_ascii("\\the"), 0);
502 if (i == docstring::npos)
504 size_t const j = i + 4;
506 while (k < ls.size() && lowercase(ls[k]) >= 'a'
507 && lowercase(ls[k]) <= 'z')
509 docstring const newc = ls.substr(j, k - j);
510 docstring const repl = flattenLabelString(newc, in_appendix,
512 ls.replace(i, k - j + 4, repl);
520 docstring Counters::counterLabel(docstring const & format,
521 string const & lang) const
523 docstring label = format;
525 // FIXME: Using regexps would be better, but we compile boost without
526 // wide regexps currently.
527 docstring const the = from_ascii("\\the");
529 //lyxerr << "label=" << label << endl;
530 size_t const i = label.find(the, 0);
531 if (i == docstring::npos)
533 size_t const j = i + 4;
535 while (k < label.size() && lowercase(label[k]) >= 'a'
536 && lowercase(label[k]) <= 'z')
538 docstring const newc(label, j, k - j);
539 label.replace(i, k - i, theCounter(newc, lang));
542 //lyxerr << "label=" << label << endl;
544 size_t const i = label.find('\\', 0);
545 if (i == docstring::npos)
547 size_t const j = label.find('{', i + 1);
548 if (j == docstring::npos)
550 size_t const k = label.find('}', j + 1);
551 if (k == docstring::npos)
553 docstring const numbertype(label, i + 1, j - i - 1);
554 docstring const counter(label, j + 1, k - j - 1);
555 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
557 //lyxerr << "DONE! label=" << label << endl;
562 docstring Counters::prettyCounter(docstring const & name,
563 string const & lang) const
565 CounterList::const_iterator it = counterList_.find(name);
566 if (it == counterList_.end())
567 return from_ascii("#");
568 Counter const & ctr = it->second;
570 docstring const value = theCounter(name, lang);
571 docstring const & format = ctr.prettyFormat();
574 return subst(format, from_ascii("##"), value);
578 docstring Counters::currentCounter() const
580 LASSERT(!counter_stack_.empty(), /* */);
581 return counter_stack_.back();
585 void Counters::setActiveLayout(Layout const & lay)
587 LASSERT(!layout_stack_.empty(), return);
588 Layout const * const lastlay = layout_stack_.back();
589 // we want to check whether the layout has changed and, if so,
590 // whether we are coming out of or going into an environment.
592 layout_stack_.pop_back();
593 layout_stack_.push_back(&lay);
594 if (lay.isEnvironment())
596 } else if (lastlay->name() != lay.name()) {
597 layout_stack_.pop_back();
598 layout_stack_.push_back(&lay);
599 if (lastlay->isEnvironment()) {
600 // we are coming out of an environment
601 // LYXERR0("Out: " << lastlay->name());
604 if (lay.isEnvironment()) {
605 // we are going into a new environment
606 // LYXERR0("In: " << lay.name());
613 void Counters::beginEnvironment()
615 counter_stack_.push_back(counter_stack_.back());
619 void Counters::endEnvironment()
621 LASSERT(!counter_stack_.empty(), return);
622 counter_stack_.pop_back();