]> git.lyx.org Git - lyx.git/blob - src/Counters.cpp
Paragraph:
[lyx.git] / src / Counters.cpp
1 /**
2  * \file Counters.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Martin Vermeer
8  * \author André Pönitz
9  * \author Richard Heck (roman numerals)
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "Counters.h"
17
18 #include "Lexer.h"
19
20 #include "support/convert.h"
21 #include "support/debug.h"
22 #include "support/lstrings.h"
23
24 #include "support/lassert.h"
25
26 #include <sstream>
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32
33
34 Counter::Counter()
35 {
36         reset();
37 }
38
39
40 Counter::Counter(docstring const & mc, docstring const & ls, 
41                  docstring const & lsa)
42         : master_(mc), labelstring_(ls), labelstringappendix_(lsa)
43 {
44         reset();
45 }
46
47
48 bool Counter::read(Lexer & lex)
49 {
50         enum {
51                 CT_WITHIN = 1,
52                 CT_LABELSTRING,
53                 CT_LABELSTRING_APPENDIX,
54                 CT_END
55         };
56
57         LexerKeyword counterTags[] = {
58                 { "end", CT_END },
59                 { "labelstring", CT_LABELSTRING },
60                 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
61                 { "within", CT_WITHIN }
62         };
63
64         lex.pushTable(counterTags);
65
66         bool getout = false;
67         while (!getout && lex.isOK()) {
68                 int le = lex.lex();
69                 switch (le) {
70                         case Lexer::LEX_UNDEF:
71                                 lex.printError("Unknown counter tag `$$Token'");
72                                 continue;
73                         default: 
74                                 break;
75                 }
76                 switch (le) {
77                         case CT_WITHIN:
78                                 lex.next();
79                                 master_ = lex.getDocString();
80                                 if (master_ == "none")
81                                         master_.erase();
82                                 break;
83                         case CT_LABELSTRING:
84                                 lex.next();
85                                 labelstring_ = lex.getDocString();
86                                 labelstringappendix_ = labelstring_;
87                                 break;
88                         case CT_LABELSTRING_APPENDIX:
89                                 lex.next();
90                                 labelstringappendix_ = lex.getDocString();
91                                 break;
92                         case CT_END:
93                                 getout = true;
94                                 break;
95                 }
96         }
97
98         // Here if have a full counter if getout == true
99         if (!getout)
100                 LYXERR0("No End tag found for counter!");
101         lex.popTable();
102         return getout;
103 }
104
105 void Counter::set(int v)
106 {
107         value_ = v;
108 }
109
110
111 void Counter::addto(int v)
112 {
113         value_ += v;
114 }
115
116
117 int Counter::value() const
118 {
119         return value_;
120 }
121
122
123 void Counter::step()
124 {
125         ++value_;
126 }
127
128
129 void Counter::reset()
130 {
131         value_ = 0;
132 }
133
134
135 docstring const & Counter::master() const
136 {
137         return master_;
138 }
139
140
141 docstring const & Counter::labelString() const
142 {
143         return labelstring_;
144 }
145
146
147 docstring const & Counter::labelStringAppendix() const
148 {
149         return labelstringappendix_;
150 }
151
152
153 void Counters::newCounter(docstring const & newc,
154                           docstring const & masterc, 
155                           docstring const & ls,
156                           docstring const & lsa)
157 {
158         if (!masterc.empty() && !hasCounter(masterc)) {
159                 lyxerr << "Master counter does not exist: "
160                        << to_utf8(masterc)
161                        << endl;
162                 return;
163         }
164         counterList[newc] = Counter(masterc, ls, lsa);
165 }
166
167
168 bool Counters::hasCounter(docstring const & c) const
169 {
170         return counterList.find(c) != counterList.end();
171 }
172
173
174 bool Counters::read(Lexer & lex, docstring const & name)
175 {
176         if (hasCounter(name)) {
177                 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
178                 return counterList[name].read(lex);
179         }
180         LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
181         Counter cnt;
182         bool success = cnt.read(lex);
183         if (success)
184                 counterList[name] = cnt;
185         else
186                 LYXERR0("Error reading counter `" << name << "'!");
187         return success;
188 }
189
190
191 void Counters::set(docstring const & ctr, int const val)
192 {
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;
197                 return;
198         }
199         it->second.set(val);
200 }
201
202
203 void Counters::addto(docstring const & ctr, int const val)
204 {
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;
209                 return;
210         }
211         it->second.addto(val);
212 }
213
214
215 int Counters::value(docstring const & ctr) const
216 {
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;
221                 return 0;
222         }
223         return cit->second.value();
224 }
225
226
227 void Counters::step(docstring const & ctr)
228 {
229         CounterList::iterator it = counterList.find(ctr);
230         if (it == counterList.end()) {
231                 lyxerr << "step: Counter does not exist: "
232                        << to_utf8(ctr) << endl;
233                 return;
234         }
235
236         it->second.step();
237         it = counterList.begin();
238         CounterList::iterator const end = counterList.end();
239         for (; it != end; ++it) {
240                 if (it->second.master() == ctr) {
241                         it->second.reset();
242                 }
243         }
244 }
245
246
247 void Counters::reset()
248 {
249         appendix_ = false;
250         subfloat_ = false;
251         current_float_.erase();
252         CounterList::iterator it = counterList.begin();
253         CounterList::iterator const end = counterList.end();
254         for (; it != end; ++it) {
255                 it->second.reset();
256         }
257 }
258
259
260 void Counters::reset(docstring const & match)
261 {
262         LASSERT(!match.empty(), /**/);
263
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)
268                         it->second.reset();
269         }
270 }
271
272
273 void Counters::copy(Counters & from, Counters & to, docstring const & match)
274 {
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));
280                 }
281         }
282 }
283
284
285 namespace {
286
287 char loweralphaCounter(int const n)
288 {
289         if (n < 1 || n > 26)
290                 return '?';
291         return 'a' + n - 1;
292 }
293
294
295 char alphaCounter(int const n)
296 {
297         if (n < 1 || n > 26)
298                 return '?';
299         return 'A' + n - 1;
300 }
301
302
303 char hebrewCounter(int const n)
304 {
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'
309         };
310
311         if (n < 1 || n > 22)
312                 return '?';
313         return hebrew[n - 1];
314 }
315
316
317
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
321 //for this info.)
322 docstring const romanCounter(int const n)
323 {
324         static char const * const ones[9] = {
325                 "I",   "II",  "III", "IV", "V",
326                 "VI",  "VII", "VIII", "IX"
327         };
328         
329         static char const * const tens[9] = {
330                 "X", "XX", "XXX", "XL", "L",
331                 "LX", "LXX", "LXXX", "XC"
332         };
333         
334         static char const * const hunds[9] = {
335                 "C", "CC", "CCC", "CD", "D",
336                 "DC", "DCC", "DCCC", "CM"
337         };
338         
339         if (n > 1000 || n < 1) 
340                 return from_ascii("??");
341         
342         int val = n;
343         string roman;
344         switch (n) {
345         //special cases
346         case 900: 
347                 roman = "CM";
348                 break;
349         case 400:
350                 roman = "CD";
351                 break;
352         default:
353                 if (val >= 100) {
354                         int hundreds = val / 100;
355                         roman = hunds[hundreds - 1];
356                         val = val % 100;
357                 }
358                 if (val >= 10) {
359                         switch (val) {
360                         //special case
361                         case 90:
362                                 roman = roman + "XC";
363                                 val = 0; //skip next
364                                 break;
365                         default:
366                                 int tensnum = val / 10;
367                                 roman = roman + tens[tensnum - 1];
368                                 val = val % 10;
369                         } // end switch
370                 } // end tens
371                 if (val > 0)
372                         roman = roman + ones[val -1];
373         }
374         return from_ascii(roman);
375 }
376
377
378 docstring const lowerromanCounter(int const n)
379 {
380         return lowercase(romanCounter(n));
381 }
382
383 } // namespace anon
384
385
386 docstring Counters::labelItem(docstring const & ctr,
387                               docstring const & numbertype)
388 {
389         CounterList::const_iterator const cit = counterList.find(ctr);
390         if (cit == counterList.end()) {
391                 lyxerr << "Counter "
392                        << to_utf8(ctr)
393                        << " does not exist." << endl;
394                 return docstring();
395         }
396
397         int val = cit->second.value();
398
399         if (numbertype == "hebrew")
400                 return docstring(1, hebrewCounter(val));
401
402         if (numbertype == "alph")
403                 return docstring(1, loweralphaCounter(val));
404
405         if (numbertype == "Alph")
406                 return docstring(1, alphaCounter(val));
407
408         if (numbertype == "roman")
409                 return lowerromanCounter(val);
410
411         if (numbertype == "Roman")
412                 return romanCounter(val);
413
414         return convert<docstring>(val);
415 }
416
417
418 docstring Counters::theCounter(docstring const & counter)
419 {
420         std::set<docstring> callers;
421         return theCounter(counter, callers);
422 }
423
424 docstring Counters::theCounter(docstring const & counter,
425                                std::set<docstring> & callers)
426 {
427         if (!hasCounter(counter))
428                 return from_ascii("??");
429
430         docstring label;
431
432         if (callers.find(counter) == callers.end()) {
433                 
434                 pair<std::set<docstring>::iterator, bool> result = callers.insert(counter);
435
436                 Counter const & c = counterList[counter];
437                 docstring ls = appendix() ? c.labelStringAppendix() : c.labelString();
438
439                 if (ls.empty()) {
440                         if (!c.master().empty())
441                                 ls = from_ascii("\\the") + c.master() + from_ascii(".");
442                         ls += from_ascii("\\arabic{") + counter + "}";
443                 }
444
445                 label = counterLabel(ls, &callers);
446
447                 callers.erase(result.first);
448         } else {
449                 // recursion detected
450                 lyxerr << "Warning: Recursion in label for counter `"
451                            << counter << "' detected"
452                            << endl;
453         }
454
455         return label;
456 }
457
458
459 docstring Counters::counterLabel(docstring const & format,
460                                  std::set<docstring> * callers)
461 {
462         docstring label = format;
463
464         // FIXME: Using regexps would be better, but we compile boost without
465         // wide regexps currently.
466
467         while (true) {
468                 //lyxerr << "label=" << to_utf8(label) << endl;
469                 size_t const i = label.find(from_ascii("\\the"), 0);
470                 if (i == docstring::npos)
471                         break;
472                 size_t j = i + 4;
473                 size_t k = j;
474                 while (k < label.size() && lowercase(label[k]) >= 'a' 
475                        && lowercase(label[k]) <= 'z')
476                         ++k;
477                 docstring counter = label.substr(j, k - j);
478                 docstring repl = callers? theCounter(counter, *callers): 
479                                               theCounter(counter);
480                 label.replace(i, k - j + 4, repl);
481         }
482
483         while (true) {
484                 //lyxerr << "label=" << to_utf8(label) << endl;
485
486                 size_t const i = label.find('\\', 0);
487                 if (i == docstring::npos)
488                         break;
489                 size_t const j = label.find('{', i + 1);
490                 if (j == docstring::npos)
491                         break;
492                 size_t const k = label.find('}', j + 1);
493                 if (k == docstring::npos)
494                         break;
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);
500         }
501         //lyxerr << "DONE! label=" << to_utf8(label) << endl;
502         return label;
503 }
504
505
506 } // namespace lyx