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 bool Counter::checkAndRemoveMaster(docstring const & cnt)
152 master_ = docstring();
157 docstring const & Counter::labelString(bool in_appendix) const
159 return in_appendix ? labelstringappendix_ : labelstring_;
163 Counter::StringMap & Counter::flatLabelStrings(bool in_appendix) const
165 return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
169 Counters::Counters() : appendix_(false), subfloat_(false)
171 layout_stack_.push_back(0);
172 counter_stack_.push_back(from_ascii(""));
176 void Counters::newCounter(docstring const & newc,
177 docstring const & masterc,
178 docstring const & ls,
179 docstring const & lsa)
181 if (!masterc.empty() && !hasCounter(masterc)) {
182 lyxerr << "Master counter does not exist: "
187 counterList_[newc] = Counter(masterc, ls, lsa);
191 bool Counters::hasCounter(docstring const & c) const
193 return counterList_.find(c) != counterList_.end();
197 bool Counters::read(Lexer & lex, docstring const & name, bool makenew)
199 if (hasCounter(name)) {
200 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
201 return counterList_[name].read(lex);
204 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
206 bool success = cnt.read(lex);
207 // if makenew is false, we will just discard what we read
208 if (success && makenew)
209 counterList_[name] = cnt;
211 LYXERR0("Error reading counter `" << name << "'!");
216 void Counters::set(docstring const & ctr, int const val)
218 CounterList::iterator const it = counterList_.find(ctr);
219 if (it == counterList_.end()) {
220 lyxerr << "set: Counter does not exist: "
221 << to_utf8(ctr) << endl;
228 void Counters::addto(docstring const & ctr, int const val)
230 CounterList::iterator const it = counterList_.find(ctr);
231 if (it == counterList_.end()) {
232 lyxerr << "addto: Counter does not exist: "
233 << to_utf8(ctr) << endl;
236 it->second.addto(val);
240 int Counters::value(docstring const & ctr) const
242 CounterList::const_iterator const cit = counterList_.find(ctr);
243 if (cit == counterList_.end()) {
244 lyxerr << "value: Counter does not exist: "
245 << to_utf8(ctr) << endl;
248 return cit->second.value();
252 void Counters::step(docstring const & ctr, UpdateType utype)
254 CounterList::iterator it = counterList_.find(ctr);
255 if (it == counterList_.end()) {
256 lyxerr << "step: Counter does not exist: "
257 << to_utf8(ctr) << endl;
262 if (utype == OutputUpdate) {
263 LASSERT(!counter_stack_.empty(), /* */);
264 counter_stack_.pop_back();
265 counter_stack_.push_back(ctr);
267 it = counterList_.begin();
268 CounterList::iterator const end = counterList_.end();
269 for (; it != end; ++it) {
270 if (it->second.master() == ctr) {
277 void Counters::reset()
281 current_float_.erase();
282 CounterList::iterator it = counterList_.begin();
283 CounterList::iterator const end = counterList_.end();
284 for (; it != end; ++it)
286 counter_stack_.clear();
287 counter_stack_.push_back(from_ascii(""));
288 layout_stack_.clear();
289 layout_stack_.push_back(0);
293 void Counters::reset(docstring const & match)
295 LASSERT(!match.empty(), /**/);
297 CounterList::iterator it = counterList_.begin();
298 CounterList::iterator end = counterList_.end();
299 for (; it != end; ++it) {
300 if (it->first.find(match) != string::npos)
306 bool Counters::remove(docstring const & cnt)
308 bool retval = counterList_.erase(cnt);
311 CounterList::iterator it = counterList_.begin();
312 CounterList::iterator end = counterList_.end();
313 for (; it != end; ++it) {
314 if (it->second.checkAndRemoveMaster(cnt))
315 LYXERR(Debug::TCLASS, "Removed master counter `" +
316 to_utf8(cnt) + "' from counter: " + to_utf8(it->first));
322 void Counters::copy(Counters & from, Counters & to, docstring const & match)
324 CounterList::iterator it = counterList_.begin();
325 CounterList::iterator end = counterList_.end();
326 for (; it != end; ++it) {
327 if (it->first.find(match) != string::npos || match == "") {
328 to.set(it->first, from.value(it->first));
336 char loweralphaCounter(int const n)
344 char alphaCounter(int const n)
352 char hebrewCounter(int const n)
354 static const char hebrew[22] = {
355 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
356 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
357 '\xf7', '\xf8', '\xf9', '\xfa'
362 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();