diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 102b4ef690..e2185fbd89 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -2229,8 +2229,8 @@ void Buffer::getLabelList(vector & list) const list.clear(); shared_ptr toc = d->toc_backend.toc("label"); - TocIterator toc_it = toc->begin(); - TocIterator end = toc->end(); + Toc::const_iterator toc_it = toc->begin(); + Toc::const_iterator end = toc->end(); for (; toc_it != end; ++toc_it) { if (toc_it->depth() == 0) list.push_back(toc_it->str()); diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 13d4c7abad..c6c489f994 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -2407,8 +2407,8 @@ void BufferView::gotoLabel(docstring const & label) // find label shared_ptr toc = buf->tocBackend().toc("label"); - TocIterator toc_it = toc->begin(); - TocIterator end = toc->end(); + Toc::const_iterator toc_it = toc->begin(); + Toc::const_iterator end = toc->end(); for (; toc_it != end; ++toc_it) { if (label == toc_it->str()) { lyx::dispatch(toc_it->action()); diff --git a/src/Changes.cpp b/src/Changes.cpp index 832ccacd76..9fa46f92a3 100644 --- a/src/Changes.cpp +++ b/src/Changes.cpp @@ -501,7 +501,7 @@ void Changes::addToToc(DocIterator const & cdit, Buffer const & buffer, // ΒΆ U+00B6 PILCROW SIGN str.push_back(0xb6); docstring const & author = author_list.get(it->change.author).name(); - Toc::iterator it = change_list->item(0, author); + Toc::iterator it = TocBackend::findItem(*change_list, 0, author); if (it == change_list->end()) { change_list->push_back(TocItem(dit, 0, author, true)); change_list->push_back(TocItem(dit, 1, str, output_active, diff --git a/src/DocIterator.cpp b/src/DocIterator.cpp index a059ad18d2..28d23e8d11 100644 --- a/src/DocIterator.cpp +++ b/src/DocIterator.cpp @@ -229,7 +229,7 @@ CursorSlice const & DocIterator::innerTextSlice() const DocIterator DocIterator::getInnerText() const { DocIterator texted = *this; - while (!texted.inTexted()) + while (!texted.inTexted()) texted.pop_back(); return texted; } diff --git a/src/Makefile.am b/src/Makefile.am index 047188f848..bbd2746d33 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -289,6 +289,7 @@ HEADERFILESCORE = \ Text.h \ TextClass.h \ TextMetrics.h \ + Toc.h \ TocBackend.h \ Trans.h \ Undo.h \ diff --git a/src/Paragraph.h b/src/Paragraph.h index d0ed94bfb0..73de6c173b 100644 --- a/src/Paragraph.h +++ b/src/Paragraph.h @@ -49,7 +49,6 @@ class MetricsInfo; class OutputParams; class PainterInfo; class ParagraphParameters; -class Toc; class WordLangTuple; class XHTMLStream; class otexstream; diff --git a/src/Toc.h b/src/Toc.h new file mode 100644 index 0000000000..d58a09a003 --- /dev/null +++ b/src/Toc.h @@ -0,0 +1,43 @@ +// -*- C++ -*- +/** + * \file TocBackend.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Jean-Marc Lasgouttes + * \author Angus Leeming + * \author Abdelrazak Younes + * \author Guillaume Munch + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef TOC_H +#define TOC_H + +#include "support/shared_ptr.h" + +#include +#include +#include + + +namespace lyx { + +// TocItem is defined in TocBackend.h +class TocItem; + +typedef std::vector Toc; + +class TocList : public std::map > +{ +private: + // TocList should never map to null pointers. + // We forbid the following method which creates null pointers. + using std::map >::operator[]; +}; + + +} // namespace lyx + +#endif // TOC_H diff --git a/src/TocBackend.cpp b/src/TocBackend.cpp index 9bd435f2f2..6526c57537 100644 --- a/src/TocBackend.cpp +++ b/src/TocBackend.cpp @@ -107,21 +107,15 @@ FuncRequest TocItem::action() const // /////////////////////////////////////////////////////////////////////////// -TocIterator Toc::item(DocIterator const & dit) const +Toc::const_iterator TocBackend::findItem(Toc const & toc, + DocIterator const & dit) { - TocIterator last = begin(); - TocIterator it = end(); + Toc::const_iterator last = toc.begin(); + Toc::const_iterator it = toc.end(); if (it == last) return it; - --it; - - DocIterator dit_text = dit; - if (dit_text.inMathed()) { - // We are only interested in text so remove the math CursorSlice. - while (dit_text.inMathed()) - dit_text.pop_back(); - } + DocIterator dit_text = dit.getInnerText(); for (; it != last; --it) { // We verify that we don't compare contents of two @@ -138,12 +132,12 @@ TocIterator Toc::item(DocIterator const & dit) const } -Toc::iterator Toc::item(int depth, docstring const & str) +Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str) { - if (empty()) - return end(); - iterator it = begin(); - iterator itend = end(); + if (toc.empty()) + return toc.end(); + Toc::iterator it = toc.begin(); + Toc::iterator itend = toc.end(); for (; it != itend; ++it) { if (it->depth() == depth && it->str() == str) break; @@ -185,7 +179,7 @@ void TocBuilder::captionItem(DocIterator const & dit, docstring const & s, arg = "paragraph-goto " + paragraph_goto_arg((*toc_)[stack_.top().pos].dit_) + ";" + arg; FuncRequest func(LFUN_COMMAND_SEQUENCE, arg); - + if (!stack_.empty() && !stack_.top().is_captioned) { // The float we entered has not yet been assigned a caption. // Assign the caption string to it. @@ -286,7 +280,7 @@ bool TocBackend::updateItem(DocIterator const & dit_in) BufferParams const & bufparams = buffer_->params(); const int min_toclevel = bufparams.documentClass().min_toclevel(); - TocIterator toc_item = item("tableofcontents", dit); + Toc::const_iterator toc_item = item("tableofcontents", dit); docstring tocstring; @@ -336,14 +330,14 @@ void TocBackend::update(bool output_active, UpdateType utype) } -TocIterator TocBackend::item(string const & type, - DocIterator const & dit) const +Toc::const_iterator TocBackend::item(string const & type, + DocIterator const & dit) const { TocList::const_iterator toclist_it = tocs_.find(type); // Is the type supported? // We will try to make the best of it in release mode LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin()); - return toclist_it->second->item(dit); + return findItem(*toclist_it->second, dit); } @@ -352,8 +346,8 @@ void TocBackend::writePlaintextTocList(string const & type, { TocList::const_iterator cit = tocs_.find(type); if (cit != tocs_.end()) { - TocIterator ccit = cit->second->begin(); - TocIterator end = cit->second->end(); + Toc::const_iterator ccit = cit->second->begin(); + Toc::const_iterator end = cit->second->end(); for (; ccit != end; ++ccit) { os << ccit->asString() << from_utf8("\n"); if (os.str().size() > max_length) diff --git a/src/TocBackend.h b/src/TocBackend.h index cc5e4292d8..2520927406 100644 --- a/src/TocBackend.h +++ b/src/TocBackend.h @@ -18,14 +18,11 @@ #include "DocIterator.h" #include "FuncRequest.h" #include "OutputEnums.h" +#include "Toc.h" -#include "support/shared_ptr.h" #include "support/strfwd.h" -#include -#include #include -#include namespace lyx { @@ -67,7 +64,6 @@ enum TocType { */ class TocItem { - friend class Toc; friend class TocBackend; friend class TocBuilder; @@ -123,25 +119,6 @@ private: }; -/// -class Toc : public std::vector -{ -public: - // This is needed to work around a libc++ bug - // https://llvm.org/bugs/show_bug.cgi?id=24137 - Toc() {} - typedef std::vector::const_iterator const_iterator; - typedef std::vector::iterator iterator; - const_iterator item(DocIterator const & dit) const; - /// Look for a TocItem given its depth and string. - /// \return The first matching item. - /// \retval end() if no item was found. - iterator item(int depth, docstring const & str); -}; - -typedef Toc::const_iterator TocIterator; - - /// Caption-enabled TOC builders class TocBuilder { @@ -169,16 +146,6 @@ private: }; -/// The ToC list. -/// A class and no typedef because we want to forward declare it. -class TocList : public std::map > -{ -private: - // this can create null pointers - using std::map >::operator[]; -}; - - /// class TocBuilderStore { @@ -200,6 +167,12 @@ private: class TocBackend { public: + static Toc::const_iterator findItem(Toc const & toc, + DocIterator const & dit); + /// Look for a TocItem given its depth and string. + /// \return The first matching item. + /// \retval end() if no item was found. + static Toc::iterator findItem(Toc & toc, int depth, docstring const & str); /// TocBackend(Buffer const * buffer) : buffer_(buffer) {} /// @@ -216,7 +189,7 @@ public: /// nevel null shared_ptr builder(std::string const & type); /// Return the first Toc Item before the cursor - TocIterator item( + Toc::const_iterator item( std::string const & type, ///< Type of Toc. DocIterator const & dit ///< The cursor location in the document. ) const; diff --git a/src/frontends/qt4/TocModel.cpp b/src/frontends/qt4/TocModel.cpp index 7b4a38b223..7654983f09 100644 --- a/src/frontends/qt4/TocModel.cpp +++ b/src/frontends/qt4/TocModel.cpp @@ -133,7 +133,8 @@ QModelIndex TocModel::modelIndex(DocIterator const & dit) const if (toc_->empty()) return QModelIndex(); - unsigned int const toc_index = toc_->item(dit) - toc_->begin(); + unsigned int const toc_index = TocBackend::findItem(*toc_, dit) - + toc_->begin(); QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole, QVariant(toc_index), 1, @@ -329,10 +330,10 @@ TocItem const TocModels::currentItem(QString const & type, return TocItem(); } LASSERT(index.model() == it.value()->model(), return TocItem()); - + return it.value()->tocItem(index); } - + void TocModels::updateItem(QString const & type, DocIterator const & dit) { diff --git a/src/frontends/qt4/TocModel.h b/src/frontends/qt4/TocModel.h index 4d4d442719..c959aba556 100644 --- a/src/frontends/qt4/TocModel.h +++ b/src/frontends/qt4/TocModel.h @@ -12,6 +12,8 @@ #ifndef TOCMODEL_H #define TOCMODEL_H +#include "Toc.h" + #include "support/shared_ptr.h" #include @@ -22,8 +24,6 @@ namespace lyx { class Buffer; class BufferView; class DocIterator; -class Toc; -class TocItem; namespace frontend { diff --git a/src/insets/InsetTOC.h b/src/insets/InsetTOC.h index bd6ba17973..7801f439ad 100644 --- a/src/insets/InsetTOC.h +++ b/src/insets/InsetTOC.h @@ -14,11 +14,12 @@ #include "InsetCommand.h" +#include "Toc.h" + namespace lyx { class Paragraph; -class Toc; /// Used to insert table of contents and similar lists /// at present, supports only \tableofcontents and \listoflistings.