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,
58 LexerKeyword counterTags[] = {
60 { "labelstring", CT_LABELSTRING },
61 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
62 { "within", CT_WITHIN }
65 lex.pushTable(counterTags);
68 while (!getout && lex.isOK()) {
71 case Lexer::LEX_UNDEF:
72 lex.printError("Unknown counter tag `$$Token'");
80 master_ = lex.getDocString();
81 if (master_ == "none")
86 labelstring_ = lex.getDocString();
87 labelstringappendix_ = labelstring_;
89 case CT_LABELSTRING_APPENDIX:
91 labelstringappendix_ = lex.getDocString();
99 // Here if have a full counter if getout == true
101 LYXERR0("No End tag found for counter!");
106 void Counter::set(int v)
112 void Counter::addto(int v)
118 int Counter::value() const
130 void Counter::reset()
136 docstring const & Counter::master() const
142 docstring const & Counter::labelString(bool in_appendix) const
144 return in_appendix ? labelstringappendix_ : labelstring_;
148 Counter::StringMap & Counter::flatLabelStrings(bool in_appendix) const
150 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
154 void Counters::newCounter(docstring const & newc,
155 docstring const & masterc,
156 docstring const & ls,
157 docstring const & lsa)
159 if (!masterc.empty() && !hasCounter(masterc)) {
160 lyxerr << "Master counter does not exist: "
165 counterList_[newc] = Counter(masterc, ls, lsa);
169 bool Counters::hasCounter(docstring const & c) const
171 return counterList_.find(c) != counterList_.end();
175 bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
177 if (hasCounter(name)) {
178 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
179 return counterList_[name].read(lex);
182 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
184 bool success = cnt.read(lex);
185 // if makenew is false, we will just discard what we read
186 if (success && makenew)
187 counterList_[name] = cnt;
189 LYXERR0("Error reading counter `" << name << "'!");
194 void Counters::set(docstring const & ctr, int const val)
196 CounterList::iterator const it = counterList_.find(ctr);
197 if (it == counterList_.end()) {
198 lyxerr << "set: Counter does not exist: "
199 << to_utf8(ctr) << endl;
206 void Counters::addto(docstring const & ctr, int const val)
208 CounterList::iterator const it = counterList_.find(ctr);
209 if (it == counterList_.end()) {
210 lyxerr << "addto: Counter does not exist: "
211 << to_utf8(ctr) << endl;
214 it->second.addto(val);
218 int Counters::value(docstring const & ctr) const
220 CounterList::const_iterator const cit = counterList_.find(ctr);
221 if (cit == counterList_.end()) {
222 lyxerr << "value: Counter does not exist: "
223 << to_utf8(ctr) << endl;
226 return cit->second.value();
230 void Counters::step(docstring const & ctr, bool track_counters)
232 CounterList::iterator it = counterList_.find(ctr);
233 if (it == counterList_.end()) {
234 lyxerr << "step: Counter does not exist: "
235 << to_utf8(ctr) << endl;
240 if (track_counters) {
241 LASSERT(!counter_stack_.empty(), /* */);
242 counter_stack_.pop_back();
243 counter_stack_.push_back(ctr);
245 it = counterList_.begin();
246 CounterList::iterator const end = counterList_.end();
247 for (; it != end; ++it) {
248 if (it->second.master() == ctr) {
255 void Counters::reset()
259 current_float_.erase();
260 CounterList::iterator it = counterList_.begin();
261 CounterList::iterator const end = counterList_.end();
262 for (; it != end; ++it)
264 counter_stack_.clear();
265 counter_stack_.push_back(from_ascii(""));
266 layout_stack_.push_back(0);
270 void Counters::reset(docstring const & match)
272 LASSERT(!match.empty(), /**/);
274 CounterList::iterator it = counterList_.begin();
275 CounterList::iterator end = counterList_.end();
276 for (; it != end; ++it) {
277 if (it->first.find(match) != string::npos)
283 void Counters::copy(Counters & from, Counters & to, docstring const & match)
285 CounterList::iterator it = counterList_.begin();
286 CounterList::iterator end = counterList_.end();
287 for (; it != end; ++it) {
288 if (it->first.find(match) != string::npos || match == "") {
289 to.set(it->first, from.value(it->first));
297 char loweralphaCounter(int const n)
305 char alphaCounter(int const n)
313 char hebrewCounter(int const n)
315 static const char hebrew[22] = {
316 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
317 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
318 '\xf7', '\xf8', '\xf9', '\xfa'
323 return hebrew[n - 1];
328 // On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
329 // and for a list of roman numerals up to and including 3999, see
330 // http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
332 docstring const romanCounter(int const n)
334 static char const * const ones[9] = {
335 "I", "II", "III", "IV", "V",
336 "VI", "VII", "VIII", "IX"
339 static char const * const tens[9] = {
340 "X", "XX", "XXX", "XL", "L",
341 "LX", "LXX", "LXXX", "XC"
344 static char const * const hunds[9] = {
345 "C", "CC", "CCC", "CD", "D",
346 "DC", "DCC", "DCCC", "CM"
349 if (n > 1000 || n < 1)
350 return from_ascii("??");
364 int hundreds = val / 100;
365 roman = hunds[hundreds - 1];
372 roman = roman + "XC";
376 int tensnum = val / 10;
377 roman = roman + tens[tensnum - 1];
382 roman = roman + ones[val -1];
384 return from_ascii(roman);
388 docstring const lowerromanCounter(int const n)
390 return lowercase(romanCounter(n));
396 docstring Counters::labelItem(docstring const & ctr,
397 docstring const & numbertype) const
399 CounterList::const_iterator const cit = counterList_.find(ctr);
400 if (cit == counterList_.end()) {
403 << " does not exist." << endl;
407 int val = cit->second.value();
409 if (numbertype == "hebrew")
410 return docstring(1, hebrewCounter(val));
412 if (numbertype == "alph")
413 return docstring(1, loweralphaCounter(val));
415 if (numbertype == "Alph")
416 return docstring(1, alphaCounter(val));
418 if (numbertype == "roman")
419 return lowerromanCounter(val);
421 if (numbertype == "Roman")
422 return romanCounter(val);
424 return convert<docstring>(val);
428 docstring Counters::theCounter(docstring const & counter,
429 string const & lang) const
431 CounterList::const_iterator it = counterList_.find(counter);
432 if (it == counterList_.end())
433 return from_ascii("??");
434 Counter const & ctr = it->second;
435 Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
436 Counter::StringMap::iterator smit = sm.find(lang);
437 if (smit != sm.end())
438 return counterLabel(smit->second, lang);
440 vector<docstring> callers;
441 docstring const & fls = flattenLabelString(counter, appendix(),
444 return counterLabel(fls, lang);
448 docstring Counters::flattenLabelString(docstring const & counter,
451 vector<docstring> & callers) const
455 if (find(callers.begin(), callers.end(), counter) != callers.end()) {
456 // recursion detected
457 lyxerr << "Warning: Recursion in label for counter `"
458 << counter << "' detected"
460 return from_ascii("??");
463 CounterList::const_iterator it = counterList_.find(counter);
464 if (it == counterList_.end())
465 return from_ascii("??");
466 Counter const & c = it->second;
468 docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
470 callers.push_back(counter);
472 if (!c.master().empty())
473 ls = flattenLabelString(c.master(), in_appendix, lang, callers)
476 return ls + from_ascii("\\arabic{") + counter + "}";
480 //lyxerr << "ls=" << to_utf8(ls) << endl;
481 size_t const i = ls.find(from_ascii("\\the"), 0);
482 if (i == docstring::npos)
484 size_t const j = i + 4;
486 while (k < ls.size() && lowercase(ls[k]) >= 'a'
487 && lowercase(ls[k]) <= 'z')
489 docstring const newc = ls.substr(j, k - j);
490 docstring const repl = flattenLabelString(newc, in_appendix,
492 ls.replace(i, k - j + 4, repl);
500 docstring Counters::counterLabel(docstring const & format,
501 string const & lang) const
503 docstring label = format;
505 // FIXME: Using regexps would be better, but we compile boost without
506 // wide regexps currently.
507 docstring const the = from_ascii("\\the");
509 //lyxerr << "label=" << label << endl;
510 size_t const i = label.find(the, 0);
511 if (i == docstring::npos)
513 size_t const j = i + 4;
515 while (k < label.size() && lowercase(label[k]) >= 'a'
516 && lowercase(label[k]) <= 'z')
518 docstring const newc(label, j, k - j);
519 label.replace(i, k - i, theCounter(newc, lang));
522 //lyxerr << "label=" << label << endl;
524 size_t const i = label.find('\\', 0);
525 if (i == docstring::npos)
527 size_t const j = label.find('{', i + 1);
528 if (j == docstring::npos)
530 size_t const k = label.find('}', j + 1);
531 if (k == docstring::npos)
533 docstring const numbertype(label, i + 1, j - i - 1);
534 docstring const counter(label, j + 1, k - j - 1);
535 label.replace(i, k + 1 - i, labelItem(counter, numbertype));
537 //lyxerr << "DONE! label=" << label << endl;
542 docstring Counters::currentCounter() const
544 LASSERT(!counter_stack_.empty(), /* */);
545 return counter_stack_.back();
549 void Counters::setActiveLayout(Layout const & lay)
551 LASSERT(!layout_stack_.empty(), return);
552 Layout const * const lastlay = layout_stack_.back();
553 // we want to check whether the layout has changed and, if so,
554 // whether we are coming out of or going into an environment.
556 layout_stack_.pop_back();
557 layout_stack_.push_back(&lay);
558 if (lay.isEnvironment())
560 } else if (lastlay->name() != lay.name()) {
561 layout_stack_.pop_back();
562 layout_stack_.push_back(&lay);
563 if (lastlay->isEnvironment()) {
564 // we are coming out of an environment
565 // LYXERR0("Out: " << lastlay->name());
568 if (lay.isEnvironment()) {
569 // we are going into a new environment
570 // LYXERR0("In: " << lay.name());
577 void Counters::beginEnvironment()
579 docstring cnt = counter_stack_.back();
580 counter_stack_.push_back(cnt);
581 deque<docstring>::const_iterator it = counter_stack_.begin();
582 deque<docstring>::const_iterator en = counter_stack_.end();
584 // for (; it != en; ++it)
585 // d += " --> " + *it;
586 // LYXERR0(counter_stack_.size() << ": " << d);
590 void Counters::endEnvironment()
592 LASSERT(!counter_stack_.empty(), return);
593 counter_stack_.pop_back();
594 deque<docstring>::const_iterator it = counter_stack_.begin();
595 deque<docstring>::const_iterator en = counter_stack_.end();
597 // for (; it != en; ++it)
598 // d += " --> " + *it;
599 // LYXERR0(counter_stack_.size() << ": " << d);