]> git.lyx.org Git - features.git/blob - src/Counters.cpp
please Abdel
[features.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(bool in_appendix) const
142 {
143         return in_appendix ? labelstringappendix_ : labelstring_;
144 }
145
146
147 docstring const & Counter::flatLabelString(bool in_appendix) const
148 {
149         return in_appendix ? flatlabelstringappendix_ : flatlabelstring_;
150 }
151
152
153 docstring const & Counter::setFlatLabelStrings(docstring const & fls,
154                                                docstring const & flsa)
155 {
156         flatlabelstring_ = fls;
157         flatlabelstringappendix_ = flsa;
158 }
159
160
161 void Counters::newCounter(docstring const & newc,
162                           docstring const & masterc, 
163                           docstring const & ls,
164                           docstring const & lsa)
165 {
166         if (!masterc.empty() && !hasCounter(masterc)) {
167                 lyxerr << "Master counter does not exist: "
168                        << to_utf8(masterc)
169                        << endl;
170                 return;
171         }
172         counterList_[newc] = Counter(masterc, ls, lsa);
173 }
174
175
176 bool Counters::hasCounter(docstring const & c) const
177 {
178         return counterList_.find(c) != counterList_.end();
179 }
180
181
182 bool Counters::read(Lexer & lex, docstring const & name)
183 {
184         if (hasCounter(name)) {
185                 LYXERR(Debug::TCLASS, "Reading existing counter " << to_utf8(name));
186                 return counterList_[name].read(lex);
187         }
188         LYXERR(Debug::TCLASS, "Reading new counter " << to_utf8(name));
189         Counter cnt;
190         bool success = cnt.read(lex);
191         if (success)
192                 counterList_[name] = cnt;
193         else
194                 LYXERR0("Error reading counter `" << name << "'!");
195         return success;
196 }
197
198
199 void Counters::set(docstring const & ctr, int const val)
200 {
201         CounterList::iterator const it = counterList_.find(ctr);
202         if (it == counterList_.end()) {
203                 lyxerr << "set: Counter does not exist: "
204                        << to_utf8(ctr) << endl;
205                 return;
206         }
207         it->second.set(val);
208 }
209
210
211 void Counters::addto(docstring const & ctr, int const val)
212 {
213         CounterList::iterator const it = counterList_.find(ctr);
214         if (it == counterList_.end()) {
215                 lyxerr << "addto: Counter does not exist: "
216                        << to_utf8(ctr) << endl;
217                 return;
218         }
219         it->second.addto(val);
220 }
221
222
223 int Counters::value(docstring const & ctr) const
224 {
225         CounterList::const_iterator const cit = counterList_.find(ctr);
226         if (cit == counterList_.end()) {
227                 lyxerr << "value: Counter does not exist: "
228                        << to_utf8(ctr) << endl;
229                 return 0;
230         }
231         return cit->second.value();
232 }
233
234
235 void Counters::step(docstring const & ctr)
236 {
237         CounterList::iterator it = counterList_.find(ctr);
238         if (it == counterList_.end()) {
239                 lyxerr << "step: Counter does not exist: "
240                        << to_utf8(ctr) << endl;
241                 return;
242         }
243
244         it->second.step();
245         it = counterList_.begin();
246         CounterList::iterator const end = counterList_.end();
247         for (; it != end; ++it) {
248                 if (it->second.master() == ctr) {
249                         it->second.reset();
250                 }
251         }
252 }
253
254
255 void Counters::reset()
256 {
257         appendix_ = false;
258         subfloat_ = false;
259         current_float_.erase();
260         CounterList::iterator it = counterList_.begin();
261         CounterList::iterator const end = counterList_.end();
262         std::vector<docstring> callers;
263         for (; it != end; ++it) {
264                 it->second.reset();
265                 // Compute the explicit counter labels without any
266                 // \thexxx strings, in order to avoid recursion.  
267                 // It only needs to be done when the textclass is
268                 // updated, but in practice the extra work is probably
269                 // not noticeable (JMarc)
270                 docstring const & fls = flattenLabelString(it->first, false, callers);
271                 docstring const & flsa = flattenLabelString(it->first, true, callers);
272                 it->second.setFlatLabelStrings(fls, flsa);
273         }
274 }
275
276
277 void Counters::reset(docstring const & match)
278 {
279         LASSERT(!match.empty(), /**/);
280
281         CounterList::iterator it = counterList_.begin();
282         CounterList::iterator end = counterList_.end();
283         for (; it != end; ++it) {
284                 if (it->first.find(match) != string::npos)
285                         it->second.reset();
286         }
287 }
288
289
290 void Counters::copy(Counters & from, Counters & to, docstring const & match)
291 {
292         CounterList::iterator it = counterList_.begin();
293         CounterList::iterator end = counterList_.end();
294         for (; it != end; ++it) {
295                 if (it->first.find(match) != string::npos || match == "") {
296                         to.set(it->first, from.value(it->first));
297                 }
298         }
299 }
300
301
302 namespace {
303
304 char loweralphaCounter(int const n)
305 {
306         if (n < 1 || n > 26)
307                 return '?';
308         return 'a' + n - 1;
309 }
310
311
312 char alphaCounter(int const n)
313 {
314         if (n < 1 || n > 26)
315                 return '?';
316         return 'A' + n - 1;
317 }
318
319
320 char hebrewCounter(int const n)
321 {
322         static const char hebrew[22] = {
323                 '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7', '\xe8',
324                 '\xe9', '\xeb', '\xec', '\xee', '\xf0', '\xf1', '\xf2', '\xf4', '\xf6',
325                 '\xf7', '\xf8', '\xf9', '\xfa'
326         };
327
328         if (n < 1 || n > 22)
329                 return '?';
330         return hebrew[n - 1];
331 }
332
333
334
335 //On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
336 //and for a list of roman numerals up to and including 3999, see 
337 //http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
338 //for this info.)
339 docstring const romanCounter(int const n)
340 {
341         static char const * const ones[9] = {
342                 "I",   "II",  "III", "IV", "V",
343                 "VI",  "VII", "VIII", "IX"
344         };
345         
346         static char const * const tens[9] = {
347                 "X", "XX", "XXX", "XL", "L",
348                 "LX", "LXX", "LXXX", "XC"
349         };
350         
351         static char const * const hunds[9] = {
352                 "C", "CC", "CCC", "CD", "D",
353                 "DC", "DCC", "DCCC", "CM"
354         };
355         
356         if (n > 1000 || n < 1) 
357                 return from_ascii("??");
358         
359         int val = n;
360         string roman;
361         switch (n) {
362         //special cases
363         case 900: 
364                 roman = "CM";
365                 break;
366         case 400:
367                 roman = "CD";
368                 break;
369         default:
370                 if (val >= 100) {
371                         int hundreds = val / 100;
372                         roman = hunds[hundreds - 1];
373                         val = val % 100;
374                 }
375                 if (val >= 10) {
376                         switch (val) {
377                         //special case
378                         case 90:
379                                 roman = roman + "XC";
380                                 val = 0; //skip next
381                                 break;
382                         default:
383                                 int tensnum = val / 10;
384                                 roman = roman + tens[tensnum - 1];
385                                 val = val % 10;
386                         } // end switch
387                 } // end tens
388                 if (val > 0)
389                         roman = roman + ones[val -1];
390         }
391         return from_ascii(roman);
392 }
393
394
395 docstring const lowerromanCounter(int const n)
396 {
397         return lowercase(romanCounter(n));
398 }
399
400 } // namespace anon
401
402
403 docstring Counters::labelItem(docstring const & ctr,
404                               docstring const & numbertype) const
405 {
406         CounterList::const_iterator const cit = counterList_.find(ctr);
407         if (cit == counterList_.end()) {
408                 lyxerr << "Counter "
409                        << to_utf8(ctr)
410                        << " does not exist." << endl;
411                 return docstring();
412         }
413
414         int val = cit->second.value();
415
416         if (numbertype == "hebrew")
417                 return docstring(1, hebrewCounter(val));
418
419         if (numbertype == "alph")
420                 return docstring(1, loweralphaCounter(val));
421
422         if (numbertype == "Alph")
423                 return docstring(1, alphaCounter(val));
424
425         if (numbertype == "roman")
426                 return lowerromanCounter(val);
427
428         if (numbertype == "Roman")
429                 return romanCounter(val);
430
431         return convert<docstring>(val);
432 }
433
434
435 docstring Counters::theCounter(docstring const & counter) const
436 {
437         CounterList::const_iterator it = counterList_.find(counter); 
438         if (it == counterList_.end())
439                 return from_ascii("??");
440         return counterLabel(it->second.flatLabelString(appendix()));
441 }
442
443
444 docstring Counters::flattenLabelString(docstring const & counter, 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