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 void Counters::newCounter(docstring const & newc,
161 docstring const & masterc,
162 docstring const & ls,
163 docstring const & lsa)
165 if (!masterc.empty() && !hasCounter(masterc)) {
166 lyxerr << "Master counter does not exist: "
171 counterList_[newc] = Counter(masterc, ls, lsa);
175 bool Counters::hasCounter(docstring const & c) const
177 return counterList_.find(c) != counterList_.end();
181 bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
183 if (hasCounter(name)) {
184 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
185 return counterList_[name].read(lex);
188 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
190 bool success = cnt.read(lex);
191 // if makenew is false, we will just discard what we read
192 if (success && makenew)
193 counterList_[name] = cnt;
195 LYXERR0("Error reading counter `" << name << "'!");
200 void Counters::set(docstring const & ctr, int const val)
202 CounterList::iterator const it = counterList_.find(ctr);
203 if (it == counterList_.end()) {
204 lyxerr << "set: Counter does not exist: "
205 << to_utf8(ctr) << endl;
212 void Counters::addto(docstring const & ctr, int const val)
214 CounterList::iterator const it = counterList_.find(ctr);
215 if (it == counterList_.end()) {
216 lyxerr << "addto: Counter does not exist: "
217 << to_utf8(ctr) << endl;
220 it->second.addto(val);
224 int Counters::value(docstring const & ctr) const
226 CounterList::const_iterator const cit = counterList_.find(ctr);
227 if (cit == counterList_.end()) {
228 lyxerr << "value: Counter does not exist: "
229 << to_utf8(ctr) << endl;
232 return cit->second.value();
236 void Counters::step(docstring const & ctr, UpdateType utype)
238 CounterList::iterator it = counterList_.find(ctr);
239 if (it == counterList_.end()) {
240 lyxerr << "step: Counter does not exist: "
241 << to_utf8(ctr) << endl;
246 if (utype == OutputUpdate) {
247 LASSERT(!counter_stack_.empty(), /* */);
248 counter_stack_.pop_back();
249 counter_stack_.push_back(ctr);
251 it = counterList_.begin();
252 CounterList::iterator const end = counterList_.end();
253 for (; it != end; ++it) {
254 if (it->second.master() == ctr) {
261 void Counters::reset()
265 current_float_.erase();
266 CounterList::iterator it = counterList_.begin();
267 CounterList::iterator const end = counterList_.end();
268 for (; it != end; ++it)
270 counter_stack_.clear();
271 counter_stack_.push_back(from_ascii(""));
272 layout_stack_.push_back(0);
276 void Counters::reset(docstring const & match)
278 LASSERT(!match.empty(), /**/);
280 CounterList::iterator it = counterList_.begin();
281 CounterList::iterator end = counterList_.end();
282 for (; it != end; ++it) {
283 if (it->first.find(match) != string::npos)
289 void Counters::copy(Counters & from, Counters & to, docstring const & match)
291 CounterList::iterator it = counterList_.begin();
292 CounterList::iterator end = counterList_.end();
293 for (; it != end; ++it) {
294 if (it->first.find(match) != string::npos || match == "") {
295 to.set(it->first, from.value(it->first));
303 char loweralphaCounter(int const n)
311 char alphaCounter(int const n)
319 char hebrewCounter(int const n)
321 static const char hebrew[22] = {
322 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
323 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
324 '\xf7', '\xf8', '\xf9', '\xfa'
329 return hebrew[n - 1];
334 // On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
335 // and for a list of roman numerals up to and including 3999, see
336 // http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
338 docstring const romanCounter(int const n)
340 static char const * const ones[9] = {
341 "I", "II", "III", "IV", "V",
342 "VI", "VII", "VIII", "IX"
345 static char const * const tens[9] = {
346 "X", "XX", "XXX", "XL", "L",
347 "LX", "LXX", "LXXX", "XC"
350 static char const * const hunds[9] = {
351 "C", "CC", "CCC", "CD", "D",
352 "DC", "DCC", "DCCC", "CM"
355 if (n > 1000 || n < 1)
356 return from_ascii("??");
370 int hundreds = val / 100;
371 roman = hunds[hundreds - 1];
378 roman = roman + "XC";
382 int tensnum = val / 10;
383 roman = roman + tens[tensnum - 1];
388 roman = roman + ones[val -1];
390 return from_ascii(roman);
394 docstring const lowerromanCounter(int const n)
396 return lowercase(romanCounter(n));
402 docstring Counters::labelItem(docstring const & ctr,
403 docstring const & numbertype) const
405 CounterList::const_iterator const cit = counterList_.find(ctr);
406 if (cit == counterList_.end()) {
409 << " does not exist." << endl;
413 int val = cit->second.value();
415 if (numbertype == "hebrew")
416 return docstring(1, hebrewCounter(val));
418 if (numbertype == "alph")
419 return docstring(1, loweralphaCounter(val));
421 if (numbertype == "Alph")
422 return docstring(1, alphaCounter(val));
424 if (numbertype == "roman")
425 return lowerromanCounter(val);
427 if (numbertype == "Roman")
428 return romanCounter(val);
430 return convert<docstring>(val);
434 docstring Counters::theCounter(docstring const & counter,
435 string const & lang) const
437 CounterList::const_iterator it = counterList_.find(counter);
438 if (it == counterList_.end())
439 return from_ascii("??");
440 Counter const & ctr = it->second;
441 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
442 Counter::StringMap::iterator smit = sm.find(lang);
443 if (smit != sm.end())
444 return counterLabel(smit->second, lang);
446 vector<docstring> callers;
447 docstring const & fls = flattenLabelString(counter, appendix(),
450 return counterLabel(fls, lang);
454 docstring Counters::flattenLabelString(docstring const & counter,
457 vector<docstring> & callers) const
461 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
462 // recursion detected
463 lyxerr << "Warning: Recursion in label for counter `"
464 << counter << "' detected"
466 return from_ascii("??");
469 CounterList::const_iterator it = counterList_.find(counter);
470 if (it == counterList_.end())
471 return from_ascii("??");
472 Counter const & c = it->second;
474 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
476 callers.push_back(counter);
478 if (!c.master().empty())
479 ls = flattenLabelString(c.master(), in_appendix, lang, callers)
482 return ls + from_ascii("\\arabic{") + counter + "}";
486 //lyxerr << "ls=" << to_utf8(ls) << endl;
487 size_t const i = ls.find(from_ascii("\\the"), 0);
488 if (i == docstring::npos)
490 size_t const j = i + 4;
492 while (k < ls.size() && lowercase(ls[k]) >= 'a'
493 && lowercase(ls[k]) <= 'z')
495 docstring const newc = ls.substr(j, k - j);
496 docstring const repl = flattenLabelString(newc, in_appendix,
498 ls.replace(i, k - j + 4, repl);
506 docstring Counters::counterLabel(docstring const & format,
507 string const & lang) const
509 docstring label = format;
511 // FIXME: Using regexps would be better, but we compile boost without
512 // wide regexps currently.
513 docstring const the = from_ascii("\\the");
515 //lyxerr << "label=" << label << endl;
516 size_t const i = label.find(the, 0);
517 if (i == docstring::npos)
519 size_t const j = i + 4;
521 while (k < label.size() && lowercase(label[k]) >= 'a'
522 && lowercase(label[k]) <= 'z')
524 docstring const newc(label, j, k - j);
525 label.replace(i, k - i, theCounter(newc, lang));
528 //lyxerr << "label=" << label << endl;
530 size_t const i = label.find('\\', 0);
531 if (i == docstring::npos)
533 size_t const j = label.find('{', i + 1);
534 if (j == docstring::npos)
536 size_t const k = label.find('}', j + 1);
537 if (k == docstring::npos)
539 docstring const numbertype(label, i + 1, j - i - 1);
540 docstring const counter(label, j + 1, k - j - 1);
541 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
543 //lyxerr << "DONE! label=" << label << endl;
548 docstring Counters::prettyCounter(docstring const & name,
549 string const & lang) const
551 CounterList::const_iterator it = counterList_.find(name);
552 if (it == counterList_.end())
553 return from_ascii("??");
554 Counter const & ctr = it->second;
556 docstring const value = theCounter(name, lang);
557 docstring const & format = ctr.prettyFormat();
560 return subst(format, from_ascii("##"), value);
564 docstring Counters::currentCounter() const
566 LASSERT(!counter_stack_.empty(), /* */);
567 return counter_stack_.back();
571 void Counters::setActiveLayout(Layout const & lay)
573 LASSERT(!layout_stack_.empty(), return);
574 Layout const * const lastlay = layout_stack_.back();
575 // we want to check whether the layout has changed and, if so,
576 // whether we are coming out of or going into an environment.
578 layout_stack_.pop_back();
579 layout_stack_.push_back(&lay);
580 if (lay.isEnvironment())
582 } else if (lastlay->name() != lay.name()) {
583 layout_stack_.pop_back();
584 layout_stack_.push_back(&lay);
585 if (lastlay->isEnvironment()) {
586 // we are coming out of an environment
587 // LYXERR0("Out: " << lastlay->name());
590 if (lay.isEnvironment()) {
591 // we are going into a new environment
592 // LYXERR0("In: " << lay.name());
599 void Counters::beginEnvironment()
601 docstring cnt = counter_stack_.back();
602 counter_stack_.push_back(cnt);
603 deque<docstring>::const_iterator it = counter_stack_.begin();
604 deque<docstring>::const_iterator en = counter_stack_.end();
606 // for (; it != en; ++it)
607 // d += " --> " + *it;
608 // LYXERR0(counter_stack_.size() << ": " << d);
612 void Counters::endEnvironment()
614 LASSERT(!counter_stack_.empty(), return);
615 counter_stack_.pop_back();
616 deque<docstring>::const_iterator it = counter_stack_.begin();
617 deque<docstring>::const_iterator en = counter_stack_.end();
619 // for (; it != en; ++it)
620 // d += " --> " + *it;
621 // LYXERR0(counter_stack_.size() << ": " << d);