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!");
113 void Counter::set(int v)
119 void Counter::addto(int v)
125 int Counter::value() const
137 void Counter::reset()
143 docstring const & Counter::master() const
149 bool Counter::checkAndRemoveMaster(docstring const & cnt)
153 master_ = docstring();
158 docstring const & Counter::labelString(bool in_appendix) const
160 return in_appendix ? labelstringappendix_ : labelstring_;
164 Counter::StringMap & Counter::flatLabelStrings(bool in_appendix) const
166 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
170 Counters::Counters() : appendix_(false), subfloat_(false)
172 layout_stack_.push_back(0);
173 counter_stack_.push_back(from_ascii(""));
177 void Counters::newCounter(docstring const & newc,
178 docstring const & masterc,
179 docstring const & ls,
180 docstring const & lsa)
182 if (!masterc.empty() && !hasCounter(masterc)) {
183 lyxerr << "Master counter does not exist: "
188 counterList_[newc] = Counter(masterc, ls, lsa);
192 bool Counters::hasCounter(docstring const & c) const
194 return counterList_.find(c) != counterList_.end();
198 bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
200 if (hasCounter(name)) {
201 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
202 return counterList_[name].read(lex);
205 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
207 bool success = cnt.read(lex);
208 // if makenew is false, we will just discard what we read
209 if (success && makenew)
210 counterList_[name] = cnt;
212 LYXERR0("Error reading counter `" << name << "'!");
217 void Counters::set(docstring const & ctr, int const val)
219 CounterList::iterator const it = counterList_.find(ctr);
220 if (it == counterList_.end()) {
221 lyxerr << "set: Counter does not exist: "
222 << to_utf8(ctr) << endl;
229 void Counters::addto(docstring const & ctr, int const val)
231 CounterList::iterator const it = counterList_.find(ctr);
232 if (it == counterList_.end()) {
233 lyxerr << "addto: Counter does not exist: "
234 << to_utf8(ctr) << endl;
237 it->second.addto(val);
241 int Counters::value(docstring const & ctr) const
243 CounterList::const_iterator const cit = counterList_.find(ctr);
244 if (cit == counterList_.end()) {
245 lyxerr << "value: Counter does not exist: "
246 << to_utf8(ctr) << endl;
249 return cit->second.value();
253 void Counters::step(docstring const & ctr, UpdateType utype)
255 CounterList::iterator it = counterList_.find(ctr);
256 if (it == counterList_.end()) {
257 lyxerr << "step: Counter does not exist: "
258 << to_utf8(ctr) << endl;
263 if (utype == OutputUpdate) {
264 LASSERT(!counter_stack_.empty(), /* */);
265 counter_stack_.pop_back();
266 counter_stack_.push_back(ctr);
268 it = counterList_.begin();
269 CounterList::iterator const end = counterList_.end();
270 for (; it != end; ++it) {
271 if (it->second.master() == ctr) {
278 void Counters::reset()
282 current_float_.erase();
283 CounterList::iterator it = counterList_.begin();
284 CounterList::iterator const end = counterList_.end();
285 for (; it != end; ++it)
287 counter_stack_.clear();
288 counter_stack_.push_back(from_ascii(""));
289 layout_stack_.clear();
290 layout_stack_.push_back(0);
294 void Counters::reset(docstring const & match)
296 LASSERT(!match.empty(), /**/);
298 CounterList::iterator it = counterList_.begin();
299 CounterList::iterator end = counterList_.end();
300 for (; it != end; ++it) {
301 if (it->first.find(match) != string::npos)
307 bool Counters::remove(docstring const & cnt)
309 bool retval = counterList_.erase(cnt);
312 CounterList::iterator it = counterList_.begin();
313 CounterList::iterator end = counterList_.end();
314 for (; it != end; ++it) {
315 if (it->second.checkAndRemoveMaster(cnt))
316 LYXERR(Debug::TCLASS, "Removed master counter `" +
317 to_utf8(cnt) + "' from counter: " + to_utf8(it->first));
323 void Counters::copy(Counters & from, Counters & to, docstring const & match)
325 CounterList::iterator it = counterList_.begin();
326 CounterList::iterator end = counterList_.end();
327 for (; it != end; ++it) {
328 if (it->first.find(match) != string::npos || match == "") {
329 to.set(it->first, from.value(it->first));
337 char loweralphaCounter(int const n)
345 char alphaCounter(int const n)
353 char hebrewCounter(int const n)
355 static const char hebrew[22] = {
356 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
357 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
358 '\xf7', '\xf8', '\xf9', '\xfa'
363 return hebrew[n - 1];
367 // On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
368 // and for a list of roman numerals up to and including 3999, see
369 // http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
371 docstring const romanCounter(int const n)
373 static char const * const ones[9] = {
374 "I", "II", "III", "IV", "V",
375 "VI", "VII", "VIII", "IX"
378 static char const * const tens[9] = {
379 "X", "XX", "XXX", "XL", "L",
380 "LX", "LXX", "LXXX", "XC"
383 static char const * const hunds[9] = {
384 "C", "CC", "CCC", "CD", "D",
385 "DC", "DCC", "DCCC", "CM"
388 if (n > 1000 || n < 1)
389 return from_ascii("??");
403 int hundreds = val / 100;
404 roman = hunds[hundreds - 1];
411 roman = roman + "XC";
415 int tensnum = val / 10;
416 roman = roman + tens[tensnum - 1];
421 roman = roman + ones[val -1];
423 return from_ascii(roman);
427 docstring const lowerromanCounter(int const n)
429 return lowercase(romanCounter(n));
435 docstring Counters::labelItem(docstring const & ctr,
436 docstring const & numbertype) const
438 CounterList::const_iterator const cit = counterList_.find(ctr);
439 if (cit == counterList_.end()) {
442 << " does not exist." << endl;
446 int val = cit->second.value();
448 if (numbertype == "hebrew")
449 return docstring(1, hebrewCounter(val));
451 if (numbertype == "alph")
452 return docstring(1, loweralphaCounter(val));
454 if (numbertype == "Alph")
455 return docstring(1, alphaCounter(val));
457 if (numbertype == "roman")
458 return lowerromanCounter(val);
460 if (numbertype == "Roman")
461 return romanCounter(val);
463 return convert<docstring>(val);
467 docstring Counters::theCounter(docstring const & counter,
468 string const & lang) const
470 CounterList::const_iterator it = counterList_.find(counter);
471 if (it == counterList_.end())
472 return from_ascii("#");
473 Counter const & ctr = it->second;
474 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
475 Counter::StringMap::iterator smit = sm.find(lang);
476 if (smit != sm.end())
477 return counterLabel(smit->second, lang);
479 vector<docstring> callers;
480 docstring const & fls = flattenLabelString(counter, appendix(),
483 return counterLabel(fls, lang);
487 docstring Counters::flattenLabelString(docstring const & counter,
490 vector<docstring> & callers) const
494 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
495 // recursion detected
496 lyxerr << "Warning: Recursion in label for counter `"
497 << counter << "' detected"
499 return from_ascii("??");
502 CounterList::const_iterator it = counterList_.find(counter);
503 if (it == counterList_.end())
504 return from_ascii("#");
505 Counter const & c = it->second;
507 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
509 callers.push_back(counter);
511 if (!c.master().empty())
512 ls = flattenLabelString(c.master(), in_appendix, lang, callers)
515 return ls + from_ascii("\\arabic{") + counter + "}";
519 //lyxerr << "ls=" << to_utf8(ls) << endl;
520 size_t const i = ls.find(from_ascii("\\the"), 0);
521 if (i == docstring::npos)
523 size_t const j = i + 4;
525 while (k < ls.size() && lowercase(ls[k]) >= 'a'
526 && lowercase(ls[k]) <= 'z')
528 docstring const newc = ls.substr(j, k - j);
529 docstring const repl = flattenLabelString(newc, in_appendix,
531 ls.replace(i, k - j + 4, repl);
539 docstring Counters::counterLabel(docstring const & format,
540 string const & lang) const
542 docstring label = format;
544 // FIXME: Using regexps would be better, but we compile boost without
545 // wide regexps currently.
546 docstring const the = from_ascii("\\the");
548 //lyxerr << "label=" << label << endl;
549 size_t const i = label.find(the, 0);
550 if (i == docstring::npos)
552 size_t const j = i + 4;
554 while (k < label.size() && lowercase(label[k]) >= 'a'
555 && lowercase(label[k]) <= 'z')
557 docstring const newc(label, j, k - j);
558 label.replace(i, k - i, theCounter(newc, lang));
561 //lyxerr << "label=" << label << endl;
563 size_t const i = label.find('\\', 0);
564 if (i == docstring::npos)
566 size_t const j = label.find('{', i + 1);
567 if (j == docstring::npos)
569 size_t const k = label.find('}', j + 1);
570 if (k == docstring::npos)
572 docstring const numbertype(label, i + 1, j - i - 1);
573 docstring const counter(label, j + 1, k - j - 1);
574 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
576 //lyxerr << "DONE! label=" << label << endl;
581 docstring Counters::prettyCounter(docstring const & name,
582 string const & lang) const
584 CounterList::const_iterator it = counterList_.find(name);
585 if (it == counterList_.end())
586 return from_ascii("#");
587 Counter const & ctr = it->second;
589 docstring const value = theCounter(name, lang);
590 docstring const & format =
591 translateIfPossible(ctr.prettyFormat(), lang);
594 return subst(format, from_ascii("##"), value);
598 docstring Counters::currentCounter() const
600 LASSERT(!counter_stack_.empty(), /* */);
601 return counter_stack_.back();
605 void Counters::setActiveLayout(Layout const & lay)
607 LASSERT(!layout_stack_.empty(), return);
608 Layout const * const lastlay = layout_stack_.back();
609 // we want to check whether the layout has changed and, if so,
610 // whether we are coming out of or going into an environment.
612 layout_stack_.pop_back();
613 layout_stack_.push_back(&lay);
614 if (lay.isEnvironment())
616 } else if (lastlay->name() != lay.name()) {
617 layout_stack_.pop_back();
618 layout_stack_.push_back(&lay);
619 if (lastlay->isEnvironment()) {
620 // we are coming out of an environment
621 // LYXERR0("Out: " << lastlay->name());
624 if (lay.isEnvironment()) {
625 // we are going into a new environment
626 // LYXERR0("In: " << lay.name());
633 void Counters::beginEnvironment()
635 counter_stack_.push_back(counter_stack_.back());
639 void Counters::endEnvironment()
641 LASSERT(!counter_stack_.empty(), return);
642 counter_stack_.pop_back();