]> git.lyx.org Git - lyx.git/blob - src/Counters.cpp
3921cf2890db3742cad8d6a6bbfd51dc6c0911d9
[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 #include "Lexer.h"
18
19 #include "support/convert.h"
20 #include "support/debug.h"
21 #include "support/lassert.h"
22 #include "support/lstrings.h"
23
24 #include <algorithm>
25 #include <sstream>
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32
33 Counter::Counter()
34 {
35         reset();
36 }
37
38
39 Counter::Counter(docstring const & mc, docstring const & ls, 
40                  docstring const & lsa)
41         : master_(mc), labelstring_(ls), labelstringappendix_(lsa)
42 {
43         reset();
44 }
45
46
47 bool Counter::read(Lexer & lex)
48 {
49         enum {
50                 CT_WITHIN = 1,
51                 CT_LABELSTRING,
52                 CT_LABELSTRING_APPENDIX,
53                 CT_END
54         };
55
56         LexerKeyword counterTags[] = {
57                 { "end", CT_END },
58                 { "labelstring", CT_LABELSTRING },
59                 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
60                 { "within", CT_WITHIN }
61         };
62
63         lex.pushTable(counterTags);
64
65         bool getout = false;
66         while (!getout && lex.isOK()) {
67                 int le = lex.lex();
68                 switch (le) {
69                         case Lexer::LEX_UNDEF:
70                                 lex.printError("Unknown counter tag `$$Token'");
71                                 continue;
72                         default: 
73                                 break;
74                 }
75                 switch (le) {
76                         case CT_WITHIN:
77                                 lex.next();
78                                 master_ = lex.getDocString();
79                                 if (master_ == "none")
80                                         master_.erase();
81                                 break;
82                         case CT_LABELSTRING:
83                                 lex.next();
84                                 labelstring_ = lex.getDocString();
85                                 labelstringappendix_ = labelstring_;
86                                 break;
87                         case CT_LABELSTRING_APPENDIX:
88                                 lex.next();
89                                 labelstringappendix_ = lex.getDocString();
90                                 break;
91                         case CT_END:
92                                 getout = true;
93                                 break;
94                 }
95         }
96
97         // Here if have a full counter if getout == true
98         if (!getout)
99                 LYXERR0("No End tag found for counter!");
100         lex.popTable();
101         return getout;
102 }
103
104 void Counter::set(int v)
105 {
106         value_ = v;
107 }
108
109
110 void Counter::addto(int v)
111 {
112         value_ += v;
113 }
114
115
116 int Counter::value() const
117 {
118         return value_;
119 }
120
121
122 void Counter::step()
123 {
124         ++value_;
125 }
126
127
128 void Counter::reset()
129 {
130         value_ = 0;
131 }
132
133
134 docstring const & Counter::master() const
135 {
136         return master_;
137 }
138
139
140 docstring const & Counter::labelString(bool in_appendix) const
141 {
142         return in_appendix ? labelstringappendix_ : labelstring_;
143 }
144
145
146 docstring const & Counter::flatLabelString(bool in_appendix) const
147 {
148         return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
149 }
150
151
152 void Counter::setFlatLabelStrings(docstring const & fls, docstring const & flsa)
153 {
154         flatlabelstring_ = fls;
155         flatlabelstringappendix_ = flsa;
156 }
157
158
159 void Counters::newCounter(docstring const & newc,
160                           docstring const & masterc, 
161                           docstring const & ls,
162                           docstring const & lsa)
163 {
164         if (!masterc.empty() && !hasCounter(masterc)) {
165                 lyxerr << "Master counter does not exist: "
166                        << to_utf8(masterc)
167                        << endl;
168                 return;
169         }
170         counterList_[newc] = Counter(masterc, ls, lsa);
171 }
172
173
174 bool Counters::hasCounter(docstring const & c) const
175 {
176         return counterList_.find(c) != counterList_.end();
177 }
178
179
180 bool Counters::read(Lexer & lex, docstring const & name)
181 {
182         if (hasCounter(name)) {
183                 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
184                 return counterList_[name].read(lex);
185         }
186         LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
187         Counter cnt;
188         bool success = cnt.read(lex);
189         if (success)
190                 counterList_[name] = cnt;
191         else
192                 LYXERR0("Error reading counter `" << name << "'!");
193         return success;
194 }
195
196
197 void Counters::set(docstring const & ctr, int const val)
198 {
199         CounterList::iterator const it = counterList_.find(ctr);
200         if (it == counterList_.end()) {
201                 lyxerr << "set: Counter does not exist: "
202                        << to_utf8(ctr) << endl;
203                 return;
204         }
205         it->second.set(val);
206 }
207
208
209 void Counters::addto(docstring const & ctr, int const val)
210 {
211         CounterList::iterator const it = counterList_.find(ctr);
212         if (it == counterList_.end()) {
213                 lyxerr << "addto: Counter does not exist: "
214                        << to_utf8(ctr) << endl;
215                 return;
216         }
217         it->second.addto(val);
218 }
219
220
221 int Counters::value(docstring const & ctr) const
222 {
223         CounterList::const_iterator const cit = counterList_.find(ctr);
224         if (cit == counterList_.end()) {
225                 lyxerr << "value: Counter does not exist: "
226                        << to_utf8(ctr) << endl;
227                 return 0;
228         }
229         return cit->second.value();
230 }
231
232
233 void Counters::step(docstring const & ctr)
234 {
235         CounterList::iterator it = counterList_.find(ctr);
236         if (it == counterList_.end()) {
237                 lyxerr << "step: Counter does not exist: "
238                        << to_utf8(ctr) << endl;
239                 return;
240         }
241
242         it->second.step();
243         it = counterList_.begin();
244         CounterList::iterator const end = counterList_.end();
245         for (; it != end; ++it) {
246                 if (it->second.master() == ctr) {
247                         it->second.reset();
248                 }
249         }
250 }
251
252
253 void Counters::reset()
254 {
255         appendix_ = false;
256         subfloat_ = false;
257         current_float_.erase();
258         CounterList::iterator it = counterList_.begin();
259         CounterList::iterator const end = counterList_.end();
260         std::vector<docstring> callers;
261         for (; it != end; ++it) {
262                 it->second.reset();
263                 // Compute the explicit counter labels without any
264                 // \thexxx strings, in order to avoid recursion.  
265                 // It only needs to be done when the textclass is
266                 // updated, but in practice the extra work is probably
267                 // not noticeable (JMarc)
268                 docstring const fls = flattenLabelString(it->first, false, callers);
269                 docstring const flsa = flattenLabelString(it->first, true, callers);
270                 it->second.setFlatLabelStrings(fls, flsa);
271         }
272 }
273
274
275 void Counters::reset(docstring const & match)
276 {
277         LASSERT(!match.empty(), /**/);
278
279         CounterList::iterator it = counterList_.begin();
280         CounterList::iterator end = counterList_.end();
281         for (; it != end; ++it) {
282                 if (it->first.find(match) != string::npos)
283                         it->second.reset();
284         }
285 }
286
287
288 void Counters::copy(Counters & from, Counters & to, docstring const & match)
289 {
290         CounterList::iterator it = counterList_.begin();
291         CounterList::iterator end = counterList_.end();
292         for (; it != end; ++it) {
293                 if (it->first.find(match) != string::npos || match == "") {
294                         to.set(it->first, from.value(it->first));
295                 }
296         }
297 }
298
299
300 namespace {
301
302 char loweralphaCounter(int const n)
303 {
304         if (n < 1 || n > 26)
305                 return '?';
306         return 'a' + n - 1;
307 }
308
309
310 char alphaCounter(int const n)
311 {
312         if (n < 1 || n > 26)
313                 return '?';
314         return 'A' + n - 1;
315 }
316
317
318 char hebrewCounter(int const n)
319 {
320         static const char hebrew[22] = {
321                 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
322                 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
323                 '\xf7', '\xf8', '\xf9', '\xfa'
324         };
325
326         if (n < 1 || n > 22)
327                 return '?';
328         return hebrew[n - 1];
329 }
330
331
332
333 // On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
334 // and for a list of roman numerals up to and including 3999, see 
335 // http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
336 // for this info.)
337 docstring const romanCounter(int const n)
338 {
339         static char const * const ones[9] = {
340                 "I",   "II",  "III", "IV", "V",
341                 "VI",  "VII", "VIII", "IX"
342         };
343         
344         static char const * const tens[9] = {
345                 "X", "XX", "XXX", "XL", "L",
346                 "LX", "LXX", "LXXX", "XC"
347         };
348         
349         static char const * const hunds[9] = {
350                 "C", "CC", "CCC", "CD", "D",
351                 "DC", "DCC", "DCCC", "CM"
352         };
353         
354         if (n > 1000 || n < 1) 
355                 return from_ascii("??");
356         
357         int val = n;
358         string roman;
359         switch (n) {
360         //special cases
361         case 900: 
362                 roman = "CM";
363                 break;
364         case 400:
365                 roman = "CD";
366                 break;
367         default:
368                 if (val >= 100) {
369                         int hundreds = val / 100;
370                         roman = hunds[hundreds - 1];
371                         val = val % 100;
372                 }
373                 if (val >= 10) {
374                         switch (val) {
375                         //special case
376                         case 90:
377                                 roman = roman + "XC";
378                                 val = 0; //skip next
379                                 break;
380                         default:
381                                 int tensnum = val / 10;
382                                 roman = roman + tens[tensnum - 1];
383                                 val = val % 10;
384                         } // end switch
385                 } // end tens
386                 if (val > 0)
387                         roman = roman + ones[val -1];
388         }
389         return from_ascii(roman);
390 }
391
392
393 docstring const lowerromanCounter(int const n)
394 {
395         return lowercase(romanCounter(n));
396 }
397
398 } // namespace anon
399
400
401 docstring Counters::labelItem(docstring const & ctr,
402                               docstring const & numbertype) const
403 {
404         CounterList::const_iterator const cit = counterList_.find(ctr);
405         if (cit == counterList_.end()) {
406                 lyxerr << "Counter "
407                        << to_utf8(ctr)
408                        << " does not exist." << endl;
409                 return docstring();
410         }
411
412         int val = cit->second.value();
413
414         if (numbertype == "hebrew")
415                 return docstring(1, hebrewCounter(val));
416
417         if (numbertype == "alph")
418                 return docstring(1, loweralphaCounter(val));
419
420         if (numbertype == "Alph")
421                 return docstring(1, alphaCounter(val));
422
423         if (numbertype == "roman")
424                 return lowerromanCounter(val);
425
426         if (numbertype == "Roman")
427                 return romanCounter(val);
428
429         return convert<docstring>(val);
430 }
431
432
433 docstring Counters::theCounter(docstring const & counter) const
434 {
435         CounterList::const_iterator it = counterList_.find(counter); 
436         if (it == counterList_.end())
437                 return from_ascii("??");
438         // FIXME: this should get translated.
439         return counterLabel(it->second.flatLabelString(appendix()));
440 }
441
442
443 docstring Counters::flattenLabelString(docstring const & counter, 
444                                        bool in_appendix, 
445                                        vector<docstring> & callers) const
446 {
447         docstring label;
448
449         if (find(callers.begin(), callers.end(), counter) != callers.end()) {
450                 // recursion detected
451                 lyxerr << "Warning: Recursion in label for counter `"
452                        << counter << "' detected"
453                        << endl;
454                 return from_ascii("??");
455         }
456                 
457         CounterList::const_iterator it = counterList_.find(counter); 
458         if (it == counterList_.end())
459                 return from_ascii("??");
460         Counter const & c = it->second;
461
462         docstring ls = c.labelString(in_appendix);
463
464         callers.push_back(counter);
465         if (ls.empty()) {
466                 if (!c.master().empty())
467                         ls = flattenLabelString(c.master(), in_appendix, callers) 
468                                 + from_ascii(".");
469                 callers.pop_back();
470                 return ls + from_ascii("\\arabic{") + counter + "}";
471         }
472
473         while (true) {
474                 //lyxerr << "ls=" << to_utf8(ls) << endl;
475                 size_t const i = ls.find(from_ascii("\\the"), 0);
476                 if (i == docstring::npos)
477                         break;
478                 size_t const j = i + 4;
479                 size_t k = j;
480                 while (k < ls.size() && lowercase(ls[k]) >= 'a' 
481                        && lowercase(ls[k]) <= 'z')
482                         ++k;
483                 docstring const newc = ls.substr(j, k - j);
484                 docstring const repl = flattenLabelString(newc, in_appendix, callers);
485                 ls.replace(i, k - j + 4, repl);
486         }
487         callers.pop_back();
488
489         return ls;
490 }
491
492
493 docstring Counters::counterLabel(docstring const & format) const
494 {
495         docstring label = format;
496
497         // FIXME: Using regexps would be better, but we compile boost without
498         // wide regexps currently.
499         while (true) {
500                 //lyxerr << "label=" << to_utf8(label) << endl;
501                 size_t const i = label.find(from_ascii("\\the"), 0);
502                 if (i == docstring::npos)
503                         break;
504                 size_t const j = i + 4;
505                 size_t k = j;
506                 while (k < label.size() && lowercase(label[k]) >= 'a' 
507                        && lowercase(label[k]) <= 'z')
508                         ++k;
509                 docstring const newc = label.substr(j, k - j);
510                 docstring const repl = theCounter(newc);
511                 label.replace(i, k - j + 4, repl);
512         }
513         while (true) {
514                 //lyxerr << "label=" << to_utf8(label) << endl;
515
516                 size_t const i = label.find('\\', 0);
517                 if (i == docstring::npos)
518                         break;
519                 size_t const j = label.find('{', i + 1);
520                 if (j == docstring::npos)
521                         break;
522                 size_t const k = label.find('}', j + 1);
523                 if (k == docstring::npos)
524                         break;
525                 docstring const numbertype(label, i + 1, j - i - 1);
526                 docstring const counter(label, j + 1, k - j - 1);
527                 docstring const rep = labelItem(counter, numbertype);
528                 label = docstring(label, 0, i) + rep
529                         + docstring(label, k + 1, docstring::npos);
530         }
531         //lyxerr << "DONE! label=" << to_utf8(label) << endl;
532         return label;
533 }
534
535
536 } // namespace lyx