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
9 * \author Guillaume Munch
11 * Full author contact details are available in file CREDITS.
16 #include "TocBackend.h"
19 #include "BufferParams.h"
21 #include "FloatList.h"
22 #include "FuncRequest.h"
23 #include "InsetList.h"
25 #include "LyXAction.h"
26 #include "Paragraph.h"
27 #include "ParIterator.h"
28 #include "TextClass.h"
30 #include "insets/InsetArgument.h"
32 #include "support/convert.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lassert.h"
37 #include "support/lstrings.h"
45 ///////////////////////////////////////////////////////////////////////////
47 // TocItem implementation
49 ///////////////////////////////////////////////////////////////////////////
51 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
52 bool output_active, docstring const & t, FuncRequest action) :
53 dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active),
59 int TocItem::id() const
61 return dit_.paragraph().id();
65 docstring const & TocItem::tooltip() const
67 return tooltip_.empty() ? str_ : tooltip_;
71 docstring const TocItem::asString() const
73 static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
74 static char_type const thin = 0x2009; // U+2009 THIN SPACE
85 // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO
86 docstring paragraph_goto_arg(DocIterator const & dit)
88 CursorSlice const & s = dit.innerTextSlice();
89 return convert<docstring>(s.paragraph().id()) + ' ' +
90 convert<docstring>(s.pos());
95 FuncRequest TocItem::action() const
97 if (action_.action() == LFUN_UNKNOWN_ACTION) {
98 return FuncRequest(LFUN_PARAGRAPH_GOTO, paragraph_goto_arg(dit_));
104 ///////////////////////////////////////////////////////////////////////////
106 // Toc implementation
108 ///////////////////////////////////////////////////////////////////////////
110 Toc::const_iterator TocBackend::findItem(Toc const & toc,
111 DocIterator const & dit)
113 Toc::const_iterator last = toc.begin();
114 Toc::const_iterator it = toc.end();
118 DocIterator dit_text = dit.getInnerText();
120 for (; it != last; --it) {
121 // We verify that we don't compare contents of two
122 // different document. This happens when you
123 // have parent and child documents.
124 if (&it->dit_[0].inset() != &dit_text[0].inset())
126 if (it->dit_ <= dit_text)
130 // We are before the first Toc Item:
135 Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str)
139 Toc::iterator it = toc.begin();
140 Toc::iterator itend = toc.end();
141 for (; it != itend; ++it) {
142 if (it->depth() == depth && it->str() == str)
149 ///////////////////////////////////////////////////////////////////////////
151 // TocBuilder implementation
153 ///////////////////////////////////////////////////////////////////////////
155 TocBuilder::TocBuilder(shared_ptr<Toc> toc)
156 : toc_(toc ? toc : make_shared<Toc>()),
162 void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
163 bool output_active, bool is_captioned)
165 toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
167 toc_->size() - 1, //pos
168 is_captioned, //is_captioned
173 void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
176 // first show the float before moving to the caption
177 docstring arg = "paragraph-goto " + paragraph_goto_arg(dit);
179 arg = "paragraph-goto " +
180 paragraph_goto_arg((*toc_)[stack_.top().pos].dit_) + ";" + arg;
181 FuncRequest func(LFUN_COMMAND_SEQUENCE, arg);
183 if (!stack_.empty() && !stack_.top().is_captioned) {
184 // The float we entered has not yet been assigned a caption.
185 // Assign the caption string to it.
186 TocItem & captionable = (*toc_)[stack_.top().pos];
188 captionable.setAction(func);
189 stack_.top().is_captioned = true;
191 // This is a new entry.
193 // the dit is at the float's level, e.g. for the contextual menu of
195 DocIterator captionable_dit = dit;
196 captionable_dit.pop_back();
197 pushItem(captionable_dit, s, output_active, true);
198 (*toc_)[stack_.top().pos].setAction(func);
202 void TocBuilder::pop()
210 ///////////////////////////////////////////////////////////////////////////
212 // TocBuilderStore implementation
214 ///////////////////////////////////////////////////////////////////////////
216 shared_ptr<TocBuilder> TocBuilderStore::get(string const & type,
219 map_t::const_iterator it = map_.find(type);
220 if (it == map_.end())
221 it = map_.insert(make_pair(type, make_shared<TocBuilder>(toc))).first;
227 ///////////////////////////////////////////////////////////////////////////
229 // TocBackend implementation
231 ///////////////////////////////////////////////////////////////////////////
233 shared_ptr<Toc const> TocBackend::toc(string const & type) const
235 // Is the type already supported?
236 TocList::const_iterator it = tocs_.find(type);
237 LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
242 shared_ptr<Toc> TocBackend::toc(string const & type)
244 TocList::const_iterator it = tocs_.find(type);
245 if (it == tocs_.end())
246 it = tocs_.insert(make_pair(type, make_shared<Toc>())).first;
251 shared_ptr<TocBuilder> TocBackend::builder(string const & type)
253 return builders_.get(type, toc(type));
257 // FIXME: This function duplicates functionality from InsetText::iterateForToc.
258 // Both have their own way of computing the TocItem for "tableofcontents". The
259 // TocItem creation and update should be made in a dedicated function and
260 // updateItem should be rewritten to uniformly update the matching items from
262 bool TocBackend::updateItem(DocIterator const & dit_in)
265 DocIterator dit = dit_in.getInnerText();
267 if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
270 if (toc("tableofcontents")->empty()) {
271 // FIXME: should not happen,
272 // a call to TocBackend::update() is missing somewhere
273 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
277 BufferParams const & bufparams = buffer_->params();
278 const int min_toclevel = bufparams.documentClass().min_toclevel();
280 Toc::const_iterator toc_item = item("tableofcontents", dit);
284 // For each paragraph, traverse its insets and let them add
287 // FIXME: This is supposed to accomplish the same as the body of
288 // InsetText::iterateForToc(), probably
289 Paragraph & par = toc_item->dit_.paragraph();
290 InsetList::const_iterator it = par.insetList().begin();
291 InsetList::const_iterator end = par.insetList().end();
292 for (; it != end; ++it) {
293 Inset & inset = *it->inset;
294 if (inset.lyxCode() == ARG_CODE) {
295 tocstring = par.labelString();
296 if (!tocstring.empty())
298 inset.asInsetText()->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
303 int const toclevel = toc_item->dit_.text()->
304 getTocLevel(toc_item->dit_.pit());
305 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
306 && tocstring.empty())
307 par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
309 support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
310 const_cast<TocItem &>(*toc_item).str(tocstring);
312 buffer_->updateTocItem("tableofcontents", dit);
317 void TocBackend::update(bool output_active, UpdateType utype)
319 for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
323 if (!buffer_->isInternal()) {
325 buffer_->inset().addToToc(dit, output_active, utype);
330 Toc::const_iterator TocBackend::item(string const & type,
331 DocIterator const & dit) const
333 TocList::const_iterator toclist_it = tocs_.find(type);
334 // Is the type supported?
335 // We will try to make the best of it in release mode
336 LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
337 return findItem(*toclist_it->second, dit);
341 void TocBackend::writePlaintextTocList(string const & type,
342 odocstringstream & os, size_t max_length) const
344 TocList::const_iterator cit = tocs_.find(type);
345 if (cit != tocs_.end()) {
346 Toc::const_iterator ccit = cit->second->begin();
347 Toc::const_iterator end = cit->second->end();
348 for (; ccit != end; ++ccit) {
349 os << ccit->asString() << from_utf8("\n");
350 if (os.str().size() > max_length)
357 docstring TocBackend::outlinerName(string const & type) const
359 return translateIfPossible(
360 buffer_->params().documentClass().outlinerName(type));