+docstring Counters::theCounter(docstring const & counter,
+ string const & lang) const
+{
+ CounterList::const_iterator it = counterList_.find(counter);
+ if (it == counterList_.end())
+ return from_ascii("#");
+ Counter const & ctr = it->second;
+ Counter::StringMap & sm = ctr.flatLabelStrings(appendix());
+ Counter::StringMap::iterator smit = sm.find(lang);
+ if (smit != sm.end())
+ return counterLabel(smit->second, lang);
+
+ vector<docstring> callers;
+ docstring const & fls = flattenLabelString(counter, appendix(),
+ lang, callers);
+ sm[lang] = fls;
+ return counterLabel(fls, lang);
+}
+
+
+docstring Counters::flattenLabelString(docstring const & counter,
+ bool in_appendix,
+ string const & lang,
+ vector<docstring> & callers) const
+{
+ if (find(callers.begin(), callers.end(), counter) != callers.end()) {
+ // recursion detected
+ lyxerr << "Warning: Recursion in label for counter `"
+ << counter << "' detected"
+ << endl;
+ return from_ascii("??");
+ }
+
+ CounterList::const_iterator it = counterList_.find(counter);
+ if (it == counterList_.end())
+ return from_ascii("#");
+ Counter const & c = it->second;
+
+ docstring ls = translateIfPossible(c.labelString(in_appendix), lang);
+
+ callers.push_back(counter);
+ if (ls.empty()) {
+ if (!c.master().empty())
+ ls = flattenLabelString(c.master(), in_appendix, lang, callers)
+ + from_ascii(".");
+ callers.pop_back();
+ return ls + from_ascii("\\arabic{") + counter + "}";
+ }
+
+ while (true) {
+ //lyxerr << "ls=" << to_utf8(ls) << endl;
+ size_t const i = ls.find(from_ascii("\\the"), 0);
+ if (i == docstring::npos)
+ break;
+ size_t const j = i + 4;
+ size_t k = j;
+ while (k < ls.size() && lowercase(ls[k]) >= 'a'
+ && lowercase(ls[k]) <= 'z')
+ ++k;
+ docstring const newc = ls.substr(j, k - j);
+ docstring const repl = flattenLabelString(newc, in_appendix,
+ lang, callers);
+ ls.replace(i, k - j + 4, repl);
+ }
+ callers.pop_back();
+
+ return ls;
+}
+
+
+docstring Counters::counterLabel(docstring const & format,
+ string const & lang) const