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 "ParIterator.h"
26 #include "TextClass.h"
28 #include "insets/InsetOptArg.h"
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
34 #include "support/lassert.h"
41 ///////////////////////////////////////////////////////////////////////////
43 // TocItem implementation
45 ///////////////////////////////////////////////////////////////////////////
47 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s)
48 : dit_(dit), depth_(d), str_(s)
53 int TocItem::id() const
55 return dit_.paragraph().id();
59 int TocItem::depth() const
65 docstring const & TocItem::str() const
71 docstring const TocItem::asString() const
73 return docstring(4 * depth_, ' ') + str_;
77 FuncRequest TocItem::action() const
79 return FuncRequest(LFUN_PARAGRAPH_GOTO, convert<string>(id()));
83 ///////////////////////////////////////////////////////////////////////////
85 // TocBackend implementation
87 ///////////////////////////////////////////////////////////////////////////
89 Toc const & TocBackend::toc(string const & type) const
91 // Is the type already supported?
92 TocList::const_iterator it = tocs_.find(type);
93 LASSERT(it != tocs_.end(), /**/);
99 Toc & TocBackend::toc(string const & type)
105 void TocBackend::updateItem(DocIterator const & dit)
107 if (toc("tableofcontents").empty()) {
108 // FIXME: should not happen,
109 // a call to TocBackend::update() is missing somewhere
110 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
114 BufferParams const & bufparams = buffer_->params();
115 const int min_toclevel = bufparams.documentClass().min_toclevel();
117 TocIterator toc_item = item("tableofcontents", dit);
121 // For each paragraph, traverse its insets and let them add
123 Paragraph & par = toc_item->dit_.paragraph();
124 InsetList::const_iterator it = par.insetList().begin();
125 InsetList::const_iterator end = par.insetList().end();
126 for (; it != end; ++it) {
127 Inset & inset = *it->inset;
128 if (inset.lyxCode() == OPTARG_CODE) {
129 if (!tocstring.empty())
131 Paragraph const & inset_par =
132 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
133 if (!par.labelString().empty())
134 tocstring = par.labelString() + ' ';
135 tocstring += inset_par.asString(false);
140 int const toclevel = par.layout().toclevel;
141 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
142 && tocstring.empty())
143 tocstring = par.asString(true);
145 const_cast<TocItem &>(*toc_item).str_ = tocstring;
149 void TocBackend::update()
153 BufferParams const & bufparams = buffer_->params();
154 const int min_toclevel = bufparams.documentClass().min_toclevel();
156 Toc & toc = tocs_["tableofcontents"];
157 ParConstIterator pit = buffer_->par_iterator_begin();
158 ParConstIterator end = buffer_->par_iterator_end();
159 for (; pit != end; ++pit) {
161 // the string that goes to the toc (could be the optarg)
164 // For each paragraph, traverse its insets and let them add
166 InsetList::const_iterator it = pit->insetList().begin();
167 InsetList::const_iterator end = pit->insetList().end();
168 for (; it != end; ++it) {
169 Inset & inset = *it->inset;
171 //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
173 switch (inset.lyxCode()) {
175 if (!tocstring.empty())
177 Paragraph const & par =
178 *static_cast<InsetOptArg&>(inset).paragraphs().begin();
179 if (!pit->labelString().empty())
180 tocstring = pit->labelString() + ' ';
181 tocstring += par.asString(false);
189 /// now the toc entry for the paragraph
190 int const toclevel = pit->layout().toclevel;
191 if (toclevel != Layout::NOT_IN_TOC
192 && toclevel >= min_toclevel) {
193 // insert this into the table of contents
194 if (tocstring.empty())
195 tocstring = pit->asString(true);
196 toc.push_back(TocItem(pit, toclevel - min_toclevel,
203 TocIterator TocBackend::item(string const & type,
204 DocIterator const & dit) const
206 TocList::const_iterator toclist_it = tocs_.find(type);
207 // Is the type supported?
208 LASSERT(toclist_it != tocs_.end(), /**/);
210 Toc const & toc_vector = toclist_it->second;
211 TocIterator last = toc_vector.begin();
212 TocIterator it = toc_vector.end();
218 ParConstIterator par_it_text(dit);
219 if (par_it_text.inMathed()) {
220 // We are only interested in text so remove the math CursorSlice.
221 while (par_it_text.inMathed())
222 par_it_text.pop_back();
225 for (; it != last; --it) {
226 // We verify that we don't compare contents of two
227 // different document. This happens when you
228 // have parent and child documents.
229 if (&it->dit_[0].inset() != &par_it_text[0].inset())
231 if (it->dit_ <= par_it_text)
235 // We are before the first Toc Item:
240 void TocBackend::writePlaintextTocList(string const & type, odocstream & os) const
242 TocList::const_iterator cit = tocs_.find(type);
243 if (cit != tocs_.end()) {
244 TocIterator ccit = cit->second.begin();
245 TocIterator end = cit->second.end();
246 for (; ccit != end; ++ccit)
247 os << ccit->asString() << from_utf8("\n");