The following patch introduces LabelString for counters. This labelstring will be used in several places:
- everytime a layout LabelString contains \thesomething, the labelString of counter something is used (currently no layout uses that) - InsetCaption also uses this for numbering. As an example, the patch modifies a few floats counters. What is missing? Not much. - use Counters::labelString in a few more places, where we use counters::value directly. - actually use it in layout files - add a LabelStringAppendix. After that the one from layouts will be irrelevant, but we will nevertheless use it when needed. - change the numbering of layouts so that when LabelString is empry, the counter's one is used instead. - fix bugs. If you load the example file, you will see how different floats with different configurations get displayed. Do not be surprised by the leading 0. in numbering: it is because section is declared as having chapter as master, which is not correct in this case. It will be fixed. Time to go to bed. JMarc
svndiff
Index: src/TextClass.cpp
===================================================================
--- src/TextClass.cpp (révision 19529)
+++ src/TextClass.cpp (copie de travail)
@@ -894,6 +894,7 @@ void TextClass::readFloat(Lexer & lexrc)
enum CounterTags {
CT_NAME = 1,
CT_WITHIN,
+ CT_LABELSTRING,
CT_END
};
@@ -901,6 +902,7 @@ void TextClass::readCounter(Lexer & lexr
{
keyword_item counterTags[] = {
{ "end", CT_END },
+ { "labelstring", CT_LABELSTRING },
{ "name", CT_NAME },
{ "within", CT_WITHIN }
};
@@ -909,6 +911,7 @@ void TextClass::readCounter(Lexer & lexr
docstring name;
docstring within;
+ docstring labelstring;
bool getout = false;
while (!getout && lexrc.isOK()) {
@@ -930,6 +933,10 @@ void TextClass::readCounter(Lexer & lexr
if (within == "none")
within.erase();
break;
+ case CT_LABELSTRING:
+ lexrc.next();
+ labelstring = lexrc.getDocString();
+ break;
case CT_END:
getout = true;
break;
@@ -937,12 +944,8 @@ void TextClass::readCounter(Lexer & lexr
}
// Here if have a full counter if getout == true
- if (getout) {
- if (within.empty())
- ctrs_->newCounter(name);
- else
- ctrs_->newCounter(name, within);
- }
+ if (getout)
+ ctrs_->newCounter(name, within, labelstring);
lexrc.popTable();
}
Index: src/insets/InsetCaption.cpp
===================================================================
--- src/insets/InsetCaption.cpp (révision 19529)
+++ src/insets/InsetCaption.cpp (copie de travail)
@@ -306,7 +306,7 @@ void InsetCaption::updateLabels(Buffer c
cnts.step(from_utf8(type));
full_label_ = bformat(from_ascii("%1$s %2$s:"),
name,
- convert<docstring>(cnts.value(from_utf8(type))));
+ cnts.theCounter(from_utf8(type)));
} else
full_label_ = bformat(from_ascii("%1$s #:"), name);
}
Index: src/buffer_funcs.cpp
===================================================================
--- src/buffer_funcs.cpp (révision 19529)
+++ src/buffer_funcs.cpp (copie de travail)
@@ -41,9 +41,7 @@
#include "frontends/alert.h"
#include "insets/InsetBibitem.h"
-#include "insets/InsetCaption.h"
#include "insets/InsetInclude.h"
-#include "insets/InsetTabular.h"
#include "support/convert.h"
#include "support/filetools.h"
@@ -537,7 +535,7 @@ void setLabel(Buffer const & buf, ParIte
counters.step(from_utf8(type));
full_label = bformat(from_ascii("%1$s %2$s:"),
name,
- convert<docstring>(counters.value(from_utf8(type))));
+ counters.theCounter(from_utf8(type)));
} else
full_label = bformat(from_ascii("%1$s #:"), name);
}
Index: src/Counters.cpp
===================================================================
--- src/Counters.cpp (révision 19529)
+++ src/Counters.cpp (copie de travail)
@@ -27,9 +27,9 @@ using std::endl;
using std::ostringstream;
using std::string;
-
namespace lyx {
+using support::lowercase;
Counter::Counter()
{
@@ -79,44 +79,35 @@ void Counter::setMaster(docstring const
}
-void Counters::newCounter(docstring const & newc)
+docstring const & Counter::labelString() const
{
- // First check if newc already exist
- CounterList::iterator const cit = counterList.find(newc);
- // if already exist give warning and return
- if (cit != counterList.end()) {
- lyxerr << "New counter already exists: "
- << to_utf8(newc)
- << endl;
- return;
- }
- counterList[newc];
+ return labelstring_;
+}
+
+
+void Counter::setLabelString(docstring const &l)
+{
+ labelstring_=l;
}
void Counters::newCounter(docstring const & newc,
- docstring const & masterc)
+ docstring const & masterc,
+ docstring const & labelstring)
{
- // First check if newc already exists
- CounterList::iterator const cit = counterList.find(newc);
- // if already existant give warning and return
- if (cit != counterList.end()) {
- lyxerr << "New counter already exists: "
- << to_utf8(newc)
- << endl;
- return;
- }
- // then check if masterc exists
- CounterList::iterator const it = counterList.find(masterc);
- // if not give warning and return
- if (it == counterList.end()) {
- lyxerr << "Master counter does not exist: "
- << to_utf8(masterc)
- << endl;
- return;
+ if (!masterc.empty()) {
+ // check if masterc exists
+ CounterList::iterator const it = counterList.find(masterc);
+ // if not give warning and return
+ if (it == counterList.end()) {
+ lyxerr << "Master counter does not exist: "
+ << to_utf8(masterc)
+ << endl;
+ return;
+ }
}
-
counterList[newc].setMaster(masterc);
+ counterList[newc].setLabelString(labelstring);
}
@@ -162,6 +153,18 @@ int Counters::value(docstring const & ct
}
+docstring Counters::labelString(docstring const & ctr) const
+{
+ CounterList::const_iterator const cit = counterList.find(ctr);
+ if (cit == counterList.end()) {
+ lyxerr << "value: Counter does not exist: "
+ << to_utf8(ctr) << endl;
+ return docstring();
+ }
+ return cit->second.labelString();
+}
+
+
void Counters::step(docstring const & ctr)
{
CounterList::iterator it = counterList.find(ctr);
@@ -185,6 +188,7 @@ void Counters::step(docstring const & ct
void Counters::reset()
{
appendix_ = false;
+ current_float_.erase();
CounterList::iterator it = counterList.begin();
CounterList::iterator const end = counterList.end();
for (; it != end; ++it) {
@@ -351,12 +355,47 @@ docstring Counters::labelItem(docstring
}
+docstring Counters::theCounter(docstring const & counter)
+{
+ docstring replace;
+ if (!hasCounter(counter))
+ return from_ascii("??");
+ Counter const & c = counterList[counter];
+
+ if (!c.labelString().empty())
+ return counterLabel(c.labelString());
+
+ if (!c.master().empty())
+ replace = theCounter(c.master()) + from_ascii(".");
+ replace += convert<docstring>(c.value());
+ return replace;
+}
+
+
docstring Counters::counterLabel(docstring const & format)
{
docstring label = format;
+
+ // Regexps are nice, but we compile boost without wide regexps anyway.
+ while (true) {
+ lyxerr << "label=" << to_utf8(label) << endl;
+ size_t const i = label.find(from_ascii("\\the"), 0);
+ if (i == docstring::npos)
+ break;
+ size_t j = i + 4;
+ size_t k = j;
+ while (lowercase(label[k]) >= 'a'
+ && lowercase(label[k]) <= 'z')
+ ++k;
+ docstring counter = label.substr(j, k - j);
+ docstring repl = theCounter(counter);
+ label.replace(i, k - j + 4, repl);
+ }
+
while (true) {
// FIXME: Using boost::regex or boost::spirit would make
// FIXME: this code a lot simpler... (Lgb)
+ lyxerr << "label=" << to_utf8(label) << endl;
size_t const i = label.find('\\', 0);
if (i == docstring::npos)
@@ -376,6 +415,7 @@ docstring Counters::counterLabel(docstri
// << numbertype << ") -> " << label << endl;
}
//lyxerr << "counterLabel: " << format << " -> " << label << endl;
+ lyxerr << "dONE! label=" << to_utf8(label) << endl;
return label;
}
Index: src/Counters.h
===================================================================
--- src/Counters.h (révision 19529)
+++ src/Counters.h (copie de travail)
@@ -41,13 +41,21 @@ public:
docstring const & master() const;
/// sets the master counter for this counter
void setMaster(docstring const & m);
+ /// Returns a LaTeX-like string to format the counter, similar
+ /// to LaTeX' \c \thesubsection.
+ docstring const & labelString() const;
+ ///
+ void setLabelString(docstring const &l);
private:
///
int value_;
/// contains master counter name; master counter is the counter
/// that, if stepped (incremented) zeroes this counter. E.g.
- /// "subparagraph"'s master is "paragraph".
+ /// "subsection"'s master is "section".
docstring master_;
+ // Contains a LaTeX-like string to format the counter, similar
+ // to LaTeX' \c \thesubsection.
+ docstring labelstring_;
};
@@ -57,9 +65,10 @@ class Counters {
public:
/// Add a new counter to array.
void newCounter(docstring const & newc);
- /// Add new counter having oldc as its master.
+ /// Add new counter having oldc as its master and ls as its label.
void newCounter(docstring const & newc,
- docstring const & oldc);
+ docstring const & oldc,
+ docstring const & ls);
///
bool hasCounter(docstring const & c) const;
///
@@ -68,6 +77,8 @@ public:
void addto(docstring const & ctr, int val);
///
int value(docstring const & ctr) const;
+ ///
+ docstring labelString(docstring const & ctr) const;
/// Step (increment by one) counter named by arg, and
/// zeroes slave counter(s) for which it is the master.
/// NOTE sub-slaves not zeroed! That happens at slave's
@@ -81,6 +92,8 @@ public:
/// the &to array of counters. Empty string matches all.
void copy(Counters & from, Counters & to,
docstring const & match = docstring());
+ /// returns the string representation of the counter.
+ docstring theCounter(docstring const & c);
/// A complete expanded label, like 2.1.4 for a subsubsection
/// according to the given format
docstring counterLabel(docstring const & format);
@@ -96,7 +109,7 @@ private:
/// A counter label's single item, 1 for subsection number in
/// the 2.1.4 subsubsection number label.
docstring labelItem(docstring const & ctr,
- docstring const & numbertype);
+ docstring const & numbertype);
/// Maps counter (layout) names to actual counters.
typedef std::map<docstring, Counter> CounterList;
/// Instantiate.
Index: lib/layouts/stdcounters.inc
===================================================================
--- lib/layouts/stdcounters.inc (révision 19529)
+++ lib/layouts/stdcounters.inc (copie de travail)
@@ -67,10 +67,13 @@ End
Counter
Name table
+ Within section
+ LabelString "\thesection.\roman{table}"
End
Counter
Name algorithm
+ Within section
End
Counter
newfile7.lyx
Description: Binary data
