3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
7 * \author Angus Leeming
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "TocBackend.h"
18 #include "BufferParams.h"
19 #include "FloatList.h"
20 #include "FuncRequest.h"
21 #include "InsetList.h"
23 #include "LyXAction.h"
24 #include "Paragraph.h"
25 #include "TextClass.h"
27 #include "insets/InsetOptArg.h"
29 #include "support/convert.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
33 #include <boost/assert.hpp>
40 ///////////////////////////////////////////////////////////////////////////
42 // TocItem implementation
44 ///////////////////////////////////////////////////////////////////////////
46 TocItem::TocItem(ParConstIterator const & par_it, int d, docstring const & s)
47 : par_it_(par_it), depth_(d), str_(s)
52 int TocItem::id() const
58 int TocItem::depth() const
64 docstring const & TocItem::str() const
70 docstring const TocItem::asString() const
72 return docstring(4 * depth_, ' ') + str_;
76 FuncRequest TocItem::action() const
78 return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
82 ///////////////////////////////////////////////////////////////////////////
84 // TocBackend implementation
86 ///////////////////////////////////////////////////////////////////////////
88 Toc const & TocBackend::toc(string const & type) const
90 // Is the type already supported?
91 TocList::const_iterator it = tocs_.find(type);
92 BOOST_ASSERT(it != tocs_.end());
98 Toc & TocBackend::toc(string const & type)
104 void TocBackend::updateItem(ParConstIterator const & par_it)
106 if (toc("tableofcontents").empty()) {
107 // FIXME: should not happen,
108 // a call to TocBackend::update() is missing somewhere
109 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
113 BufferParams const & bufparams = buffer_->params();
114 const int min_toclevel = bufparams.documentClass().min_toclevel();
116 TocIterator toc_item = item("tableofcontents", par_it);
120 // For each paragraph, traverse its insets and let them add
122 InsetList::const_iterator it = toc_item->par_it_->insetList().begin();
123 InsetList::const_iterator end = toc_item->par_it_->insetList().end();
124 for (; it != end; ++it) {
125 Inset & inset = *it->inset;
126 if (inset.lyxCode() == OPTARG_CODE) {
127 if (!tocstring.empty())
129 Paragraph const & par =
130 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
131 if (!toc_item->par_it_->labelString().empty())
132 tocstring = toc_item->par_it_->labelString() + ' ';
133 tocstring += par.asString(false);
138 int const toclevel = toc_item->par_it_->layout().toclevel;
139 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
140 && tocstring.empty())
141 tocstring = toc_item->par_it_->asString(true);
143 const_cast<TocItem &>(*toc_item).str_ = tocstring;
147 void TocBackend::update()
151 BufferParams const & bufparams = buffer_->params();
152 const int min_toclevel = bufparams.documentClass().min_toclevel();
154 Toc & toc = tocs_["tableofcontents"];
155 ParConstIterator pit = buffer_->par_iterator_begin();
156 ParConstIterator end = buffer_->par_iterator_end();
157 for (; pit != end; ++pit) {
159 // the string that goes to the toc (could be the optarg)
162 // For each paragraph, traverse its insets and let them add
164 InsetList::const_iterator it = pit->insetList().begin();
165 InsetList::const_iterator end = pit->insetList().end();
166 for (; it != end; ++it) {
167 Inset & inset = *it->inset;
168 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
170 switch (inset.lyxCode()) {
172 if (!tocstring.empty())
174 Paragraph const & par =
175 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
176 if (!pit->labelString().empty())
177 tocstring = pit->labelString() + ' ';
178 tocstring += par.asString(false);
186 /// now the toc entry for the paragraph
187 int const toclevel = pit->layout().toclevel;
188 if (toclevel != Layout::NOT_IN_TOC
189 && toclevel >= min_toclevel) {
190 // insert this into the table of contents
191 if (tocstring.empty())
192 tocstring = pit->asString(true);
193 toc.push_back(TocItem(pit, toclevel - min_toclevel,
200 TocIterator TocBackend::item(string const & type,
201 ParConstIterator const & par_it) const
203 TocList::const_iterator toclist_it = tocs_.find(type);
204 // Is the type supported?
205 BOOST_ASSERT(toclist_it != tocs_.end());
207 Toc const & toc_vector = toclist_it->second;
208 TocIterator last = toc_vector.begin();
209 TocIterator it = toc_vector.end();
215 ParConstIterator par_it_text = par_it;
216 if (par_it_text.inMathed()) {
217 // We are only interested in text so remove the math CursorSlice.
218 while (par_it_text.inMathed())
219 par_it_text.pop_back();
222 for (; it != last; --it) {
223 // We verify that we don't compare contents of two
224 // different document. This happens when you
225 // have parent and child documents.
226 if (&it->par_it_[0].inset() != &par_it_text[0].inset())
228 if (it->par_it_ <= par_it_text)
232 // We are before the first Toc Item:
237 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
239 TocList::const_iterator cit = tocs_.find(type);
240 if (cit != tocs_.end()) {
241 TocIterator ccit = cit->second.begin();
242 TocIterator end = cit->second.end();
243 for (; ccit != end; ++ccit)
244 os << ccit->asString() << from_utf8("\n");