commit 5fdc577badb1cb133d6a0dc7d831bb1f82576adb
Author: Guillaume Munch <[email protected]>
Date:   Mon Jan 9 17:37:50 2017 +0100

    Improve the list of equations
    
    Also convert other Tocs to TocBuilder when trivial, to make them 
customisable
---
 src/insets/InsetCitation.cpp     |    5 +++--
 src/insets/InsetExternal.cpp     |    7 +++----
 src/insets/InsetGraphics.cpp     |    4 +++-
 src/insets/InsetInclude.cpp      |   38 +++++++++++++++++---------------------
 src/insets/InsetIndex.cpp        |    4 +++-
 src/insets/InsetNomencl.cpp      |    4 +++-
 src/mathed/InsetMathHull.cpp     |   33 ++++++++++++++++++++++++++++++---
 src/mathed/MathMacroTemplate.cpp |    5 +++--
 8 files changed, 65 insertions(+), 35 deletions(-)

diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp
index 13fddd7..29a0fbf 100644
--- a/src/insets/InsetCitation.cpp
+++ b/src/insets/InsetCitation.cpp
@@ -334,8 +334,9 @@ void InsetCitation::addToToc(DocIterator const & cpit, bool 
output_active,
        // by both XHTML and plaintext output. So, if we change what goes into 
the TOC,
        // then we will also need to change that routine.
        docstring const tocitem = getParam("key");
-       shared_ptr<Toc> toc = buffer().tocBackend().toc("citation");
-       toc->push_back(TocItem(cpit, 0, tocitem, output_active));
+       TocBuilder & b = buffer().tocBackend().builder("citation");
+       b.pushItem(cpit, tocitem, output_active);
+       b.pop();
 }
 
 
diff --git a/src/insets/InsetExternal.cpp b/src/insets/InsetExternal.cpp
index 00c304a..6e0baff 100644
--- a/src/insets/InsetExternal.cpp
+++ b/src/insets/InsetExternal.cpp
@@ -506,11 +506,10 @@ bool InsetExternal::getStatus(Cursor & cur, FuncRequest 
const & cmd,
 void InsetExternal::addToToc(DocIterator const & cpit, bool output_active,
                                                         UpdateType) const
 {
-       DocIterator pit = cpit;
-       pit.push_back(CursorSlice(const_cast<InsetExternal &>(*this)));
-       shared_ptr<Toc> toc = buffer().tocBackend().toc("external");
        docstring str = screenLabel(params_, buffer());
-       toc->push_back(TocItem(pit, 0, str, output_active));
+       TocBuilder & b = buffer().tocBackend().builder("external");
+       b.pushItem(cpit, str, output_active);
+       b.pop();
 }
 
 
diff --git a/src/insets/InsetGraphics.cpp b/src/insets/InsetGraphics.cpp
index 4d850b5..d1458c0 100644
--- a/src/insets/InsetGraphics.cpp
+++ b/src/insets/InsetGraphics.cpp
@@ -1040,7 +1040,9 @@ void InsetGraphics::addToToc(DocIterator const & cpit, 
bool output_active,
 {
        //FIXME UNICODE
        docstring const str = from_utf8(params_.filename.onlyFileName());
-       buffer().tocBackend().toc("graphics")->push_back(TocItem(cpit, 0, str, 
output_active));
+       TocBuilder & b = buffer().tocBackend().builder("graphics");
+       b.pushItem(cpit, str, output_active);
+       b.pop();
 }
 
 
diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp
index 7a35edd..4e44ec8 100644
--- a/src/insets/InsetInclude.cpp
+++ b/src/insets/InsetInclude.cpp
@@ -1143,37 +1143,33 @@ void InsetInclude::addPreview(DocIterator const & 
/*inset_pos*/,
 void InsetInclude::addToToc(DocIterator const & cpit, bool output_active,
                                                        UpdateType utype) const
 {
-       TocBackend & backend = buffer().tocBackend();
-
        if (isListings(params())) {
                if (label_)
                        label_->addToToc(cpit, output_active, utype);
-
+               TocBuilder & b = buffer().tocBackend().builder("listing");
+               b.pushItem(cpit, screenLabel(), output_active);
                InsetListingsParams p(to_utf8(params()["lstparams"]));
-               string caption = p.getParamValue("caption");
-               if (caption.empty())
-                       return;
-               shared_ptr<Toc> toc = backend.toc("listing");
-               docstring str = convert<docstring>(toc->size() + 1)
-                       + ". " +  from_utf8(caption);
-               DocIterator pit = cpit;
-               toc->push_back(TocItem(pit, 0, str, output_active));
+               b.argumentItem(from_utf8(p.getParamValue("caption")));
+               b.pop();
        } else {
                Buffer const * const childbuffer = getChildBuffer();
+
+               TocBuilder & b = buffer().tocBackend().builder("child");
+               docstring str = childbuffer ? 
childbuffer->fileName().displayName()
+                       : from_ascii("?");
+               b.pushItem(cpit, str, output_active);
+               b.pop();
+
                if (!childbuffer)
                        return;
 
-               shared_ptr<Toc> toc = backend.toc("child");
-               docstring str = childbuffer->fileName().displayName();
-               toc->push_back(TocItem(cpit, 0, str, output_active));
-
+               // Include Tocs from children
                childbuffer->tocBackend().update(output_active, utype);
-               TocList const & childtoclist = childbuffer->tocBackend().tocs();
-               TocList::const_iterator it = childtoclist.begin();
-               TocList::const_iterator const end = childtoclist.end();
-               for(; it != end; ++it) {
-                       shared_ptr<Toc> toc = backend.toc(it->first);
-                       toc->insert(toc->end(), it->second->begin(), 
it->second->end());
+               for(auto const & pair : childbuffer->tocBackend().tocs()) {
+                       string const & type = pair.first;
+                       shared_ptr<Toc> child_toc = pair.second;
+                       shared_ptr<Toc> toc = buffer().tocBackend().toc(type);
+                       toc->insert(toc->end(), child_toc->begin(), 
child_toc->end());
                }
        }
 }
diff --git a/src/insets/InsetIndex.cpp b/src/insets/InsetIndex.cpp
index 455b09f..a328d27 100644
--- a/src/insets/InsetIndex.cpp
+++ b/src/insets/InsetIndex.cpp
@@ -361,9 +361,11 @@ void InsetIndex::addToToc(DocIterator const & cpit, bool 
output_active,
                type += ":" + to_utf8(params_.index);
        // this is unlikely to be terribly long
        text().forOutliner(str, INT_MAX);
-       buffer().tocBackend().toc(type)->push_back(TocItem(pit, 0, str, 
output_active));
+       TocBuilder & b = buffer().tocBackend().builder(type);
+       b.pushItem(pit, str, output_active);
        // Proceed with the rest of the inset.
        InsetCollapsable::addToToc(cpit, output_active, utype);
+       b.pop();
 }
 
 
diff --git a/src/insets/InsetNomencl.cpp b/src/insets/InsetNomencl.cpp
index 7395b6c..fe4f7fb 100644
--- a/src/insets/InsetNomencl.cpp
+++ b/src/insets/InsetNomencl.cpp
@@ -141,7 +141,9 @@ void InsetNomencl::addToToc(DocIterator const & cpit, bool 
output_active,
                                                        UpdateType) const
 {
        docstring const str = getParam("symbol");
-       buffer().tocBackend().toc("nomencl")->push_back(TocItem(cpit, 0, str, 
output_active));
+       TocBuilder & b = buffer().tocBackend().builder("nomencl");
+       b.pushItem(cpit, str, output_active);
+       b.pop();
 }
 
 
diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index 807475e..61e0210 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -344,15 +344,42 @@ void InsetMathHull::addToToc(DocIterator const & pit, 
bool output_active,
                return;
        }
 
-       shared_ptr<Toc> toc = buffer().tocBackend().toc("equation");
-
+       TocBuilder & b = buffer().tocBackend().builder("equation");
+       // compute first and last item
+       row_type first = nrows();
+       for (row_type row = 0; row != nrows(); ++row)
+               if (numbered(row)) {
+                       first = row;
+                       break;
+               }
+       if (first == nrows())
+               // no equation
+               return;
+       row_type last = nrows() - 1;
+       for (; last != 0; --last)
+               if (numbered(last))
+                       break;
+       // add equation numbers
+       b.pushItem(pit, docstring(), output_active);
+       if (first != last)
+               b.argumentItem(bformat(from_ascii("(%1$s-%2$s)"),
+                                      numbers_[first], numbers_[last]));
        for (row_type row = 0; row != nrows(); ++row) {
                if (!numbered(row))
                        continue;
                if (label_[row])
                        label_[row]->addToToc(pit, output_active, utype);
-               toc->push_back(TocItem(pit, 0, nicelabel(row), output_active));
+               docstring label = nicelabel(row);
+               if (first == last)
+                       // this is the only equation
+                       b.argumentItem(label);
+               else {
+                       // insert as sub-items
+                       b.pushItem(pit, label, output_active);
+                       b.pop();
+               }
        }
+       b.pop();
 }
 
 
diff --git a/src/mathed/MathMacroTemplate.cpp b/src/mathed/MathMacroTemplate.cpp
index 2ac8703..2c6cbf7 100644
--- a/src/mathed/MathMacroTemplate.cpp
+++ b/src/mathed/MathMacroTemplate.cpp
@@ -1389,13 +1389,14 @@ string MathMacroTemplate::contextMenuName() const
 void MathMacroTemplate::addToToc(DocIterator const & pit, bool output_active,
                                                                 UpdateType) 
const
 {
-       shared_ptr<Toc> toc = buffer().tocBackend().toc("math-macro");
        docstring str;
        if (!validMacro())
                str = bformat(_("Invalid macro! \\%1$s"), name());
        else
                str = "\\" + name();
-       toc->push_back(TocItem(pit, 0, str, output_active));
+       TocBuilder & b = buffer().tocBackend().builder("math-macro");
+       b.pushItem(pit, str, output_active);
+       b.pop();
 }
 
 

Reply via email to