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/lstrings.h"
24 #include "support/lassert.h"
29 using namespace lyx::support;
40 Counter::Counter(docstring const & mc, docstring const & ls,
41 docstring const & lsa)
42 : master_(mc), labelstring_(ls), labelstringappendix_(lsa)
48 bool Counter::read(Lexer & lex)
53 CT_LABELSTRING_APPENDIX,
57 LexerKeyword counterTags[] = {
59 { "labelstring", CT_LABELSTRING },
60 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
61 { "within", CT_WITHIN }
64 lex.pushTable(counterTags);
67 while (!getout && lex.isOK()) {
70 case Lexer::LEX_UNDEF:
71 lex.printError("Unknown counter tag `$$Token'");
79 master_ = lex.getDocString();
80 if (master_ == "none")
85 labelstring_ = lex.getDocString();
86 labelstringappendix_ = labelstring_;
88 case CT_LABELSTRING_APPENDIX:
90 labelstringappendix_ = lex.getDocString();
98 // Here if have a full counter if getout == true
100 LYXERR0("No End tag found for counter!");
105 void Counter::set(int v)
111 void Counter::addto(int v)
117 int Counter::value() const
129 void Counter::reset()
135 docstring const & Counter::master() const
141 docstring const & Counter::labelString() const
147 docstring const & Counter::labelStringAppendix() const
149 return labelstringappendix_;
153 void Counters::newCounter(docstring const & newc,
154 docstring const & masterc,
155 docstring const & ls,
156 docstring const & lsa)
158 if (!masterc.empty() && !hasCounter(masterc)) {
159 lyxerr << "Master counter does not exist: "
164 counterList[newc] = Counter(masterc, ls, lsa);
168 bool Counters::hasCounter(docstring const & c) const
170 return counterList.find(c) != counterList.end();
174 bool Counters::read(Lexer & lex, docstring const & name)
176 if (hasCounter(name)) {
177 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
178 return counterList[name].read(lex);
180 LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
182 bool success = cnt.read(lex);
184 counterList[name] = cnt;
186 LYXERR0("Error reading counter `" << name << "'!");
191 void Counters::set(docstring const & ctr, int const val)
193 CounterList::iterator const it = counterList.find(ctr);
194 if (it == counterList.end()) {
195 lyxerr << "set: Counter does not exist: "
196 << to_utf8(ctr) << endl;
203 void Counters::addto(docstring const & ctr, int const val)
205 CounterList::iterator const it = counterList.find(ctr);
206 if (it == counterList.end()) {
207 lyxerr << "addto: Counter does not exist: "
208 << to_utf8(ctr) << endl;
211 it->second.addto(val);
215 int Counters::value(docstring const & ctr) const
217 CounterList::const_iterator const cit = counterList.find(ctr);
218 if (cit == counterList.end()) {
219 lyxerr << "value: Counter does not exist: "
220 << to_utf8(ctr) << endl;
223 return cit->second.value();
227 void Counters::step(docstring const & ctr)
229 CounterList::iterator it = counterList.find(ctr);
230 if (it == counterList.end()) {
231 lyxerr << "step: Counter does not exist: "
232 << to_utf8(ctr) << endl;
237 it = counterList.begin();
238 CounterList::iterator const end = counterList.end();
239 for (; it != end; ++it) {
240 if (it->second.master() == ctr) {
247 void Counters::reset()
251 current_float_.erase();
252 CounterList::iterator it = counterList.begin();
253 CounterList::iterator const end = counterList.end();
254 for (; it != end; ++it) {
260 void Counters::reset(docstring const & match)
262 LASSERT(!match.empty(), /**/);
264 CounterList::iterator it = counterList.begin();
265 CounterList::iterator end = counterList.end();
266 for (; it != end; ++it) {
267 if (it->first.find(match) != string::npos)
273 void Counters::copy(Counters & from, Counters & to, docstring const & match)
275 CounterList::iterator it = counterList.begin();
276 CounterList::iterator end = counterList.end();
277 for (; it != end; ++it) {
278 if (it->first.find(match) != string::npos || match == "") {
279 to.set(it->first, from.value(it->first));
287 char loweralphaCounter(int const n)
295 char alphaCounter(int const n)
303 char hebrewCounter(int const n)
305 static const char hebrew[22] = {
306 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
307 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
308 '\xf7', '\xf8', '\xf9', '\xfa'
313 return hebrew[n - 1];
318 //On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
319 //and for a list of roman numerals up to and including 3999, see
320 //http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
322 docstring const romanCounter(int const n)
324 static char const * const ones[9] = {
325 "I", "II", "III", "IV", "V",
326 "VI", "VII", "VIII", "IX"
329 static char const * const tens[9] = {
330 "X", "XX", "XXX", "XL", "L",
331 "LX", "LXX", "LXXX", "XC"
334 static char const * const hunds[9] = {
335 "C", "CC", "CCC", "CD", "D",
336 "DC", "DCC", "DCCC", "CM"
339 if (n > 1000 || n < 1)
340 return from_ascii("??");
354 int hundreds = val / 100;
355 roman = hunds[hundreds - 1];
362 roman = roman + "XC";
366 int tensnum = val / 10;
367 roman = roman + tens[tensnum - 1];
372 roman = roman + ones[val -1];
374 return from_ascii(roman);
378 docstring const lowerromanCounter(int const n)
380 return lowercase(romanCounter(n));
386 docstring Counters::labelItem(docstring const & ctr,
387 docstring const & numbertype)
389 CounterList::const_iterator const cit = counterList.find(ctr);
390 if (cit == counterList.end()) {
393 << " does not exist." << endl;
397 int val = cit->second.value();
399 if (numbertype == "hebrew")
400 return docstring(1, hebrewCounter(val));
402 if (numbertype == "alph")
403 return docstring(1, loweralphaCounter(val));
405 if (numbertype == "Alph")
406 return docstring(1, alphaCounter(val));
408 if (numbertype == "roman")
409 return lowerromanCounter(val);
411 if (numbertype == "Roman")
412 return romanCounter(val);
414 return convert<docstring>(val);
418 docstring Counters::theCounter(docstring const & counter)
420 std::set<docstring> callers;
421 return theCounter(counter, callers);
424 docstring Counters::theCounter(docstring const & counter,
425 std::set<docstring> & callers)
427 if (!hasCounter(counter))
428 return from_ascii("??");
432 if (callers.find(counter) == callers.end()) {
434 pair<std::set<docstring>::iterator, bool> result = callers.insert(counter);
436 Counter const & c = counterList[counter];
437 docstring ls = appendix() ? c.labelStringAppendix() : c.labelString();
440 if (!c.master().empty())
441 ls = from_ascii("\\the") + c.master() + from_ascii(".");
442 ls += from_ascii("\\arabic{") + counter + "}";
445 label = counterLabel(ls, &callers);
447 callers.erase(result.first);
449 // recursion detected
450 lyxerr << "Warning: Recursion in label for counter `"
451 << counter << "' detected"
459 docstring Counters::counterLabel(docstring const & format,
460 std::set<docstring> * callers)
462 docstring label = format;
464 // FIXME: Using regexps would be better, but we compile boost without
465 // wide regexps currently.
468 //lyxerr << "label=" << to_utf8(label) << endl;
469 size_t const i = label.find(from_ascii("\\the"), 0);
470 if (i == docstring::npos)
474 while (k < label.size() && lowercase(label[k]) >= 'a'
475 && lowercase(label[k]) <= 'z')
477 docstring counter = label.substr(j, k - j);
478 docstring repl = callers? theCounter(counter, *callers):
480 label.replace(i, k - j + 4, repl);
484 //lyxerr << "label=" << to_utf8(label) << endl;
486 size_t const i = label.find('\\', 0);
487 if (i == docstring::npos)
489 size_t const j = label.find('{', i + 1);
490 if (j == docstring::npos)
492 size_t const k = label.find('}', j + 1);
493 if (k == docstring::npos)
495 docstring const numbertype(label, i + 1, j - i - 1);
496 docstring const counter(label, j + 1, k - j - 1);
497 docstring const rep = labelItem(counter, numbertype);
498 label = docstring(label, 0, i) + rep
499 + docstring(label, k + 1, docstring::npos);
501 //lyxerr << "DONE! label=" << to_utf8(label) << endl;