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/InsetText.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, FuncRequest action)
53 : dit_(dit), depth_(d), str_(s), output_(output_active),
59 int TocItem::id() const
61 return dit_.paragraph().id();
65 docstring const TocItem::asString() const
67 static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
68 static char_type const thin = 0x2009; // U+2009 THIN SPACE
79 // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO
80 docstring paragraph_goto_arg(DocIterator const & dit)
82 CursorSlice const & s = dit.innerTextSlice();
83 return convert<docstring>(s.paragraph().id()) + ' ' +
84 convert<docstring>(s.pos());
89 FuncRequest TocItem::action() const
91 if (action_.action() == LFUN_UNKNOWN_ACTION) {
92 return FuncRequest(LFUN_PARAGRAPH_GOTO, paragraph_goto_arg(dit_));
98 ///////////////////////////////////////////////////////////////////////////
100 // Toc implementation
102 ///////////////////////////////////////////////////////////////////////////
104 Toc::const_iterator TocBackend::findItem(Toc const & toc,
105 DocIterator const & dit)
107 Toc::const_iterator last = toc.begin();
108 Toc::const_iterator it = toc.end();
112 DocIterator dit_text = dit.getInnerText();
114 for (; it != last; --it) {
115 // We verify that we don't compare contents of two
116 // different document. This happens when you
117 // have parent and child documents.
118 if (&it->dit_[0].inset() != &dit_text[0].inset())
120 if (it->dit_ <= dit_text)
124 // We are before the first Toc Item:
129 Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str)
133 Toc::iterator it = toc.begin();
134 Toc::iterator itend = toc.end();
135 for (; it != itend; ++it) {
136 if (it->depth() == depth && it->str() == str)
143 ///////////////////////////////////////////////////////////////////////////
145 // TocBuilder implementation
147 ///////////////////////////////////////////////////////////////////////////
149 TocBuilder::TocBuilder(shared_ptr<Toc> toc)
150 : toc_(toc ? toc : make_shared<Toc>()),
156 void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
157 bool output_active, bool is_captioned)
159 toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
161 toc_->size() - 1, //pos
162 is_captioned, //is_captioned
167 void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
170 // first show the float before moving to the caption
171 docstring arg = "paragraph-goto " + paragraph_goto_arg(dit);
173 arg = "paragraph-goto " +
174 paragraph_goto_arg((*toc_)[stack_.top().pos].dit_) + ";" + arg;
175 FuncRequest func(LFUN_COMMAND_SEQUENCE, arg);
177 if (!stack_.empty() && !stack_.top().is_captioned) {
178 // The float we entered has not yet been assigned a caption.
179 // Assign the caption string to it.
180 TocItem & captionable = (*toc_)[stack_.top().pos];
182 captionable.setAction(func);
183 stack_.top().is_captioned = true;
185 // This is a new entry.
187 // the dit is at the float's level, e.g. for the contextual menu of
189 DocIterator captionable_dit = dit;
190 captionable_dit.pop_back();
191 pushItem(captionable_dit, s, output_active, true);
192 (*toc_)[stack_.top().pos].setAction(func);
196 void TocBuilder::argumentItem(docstring const & arg_str)
198 if (stack_.empty() || arg_str.empty())
200 TocItem & item = (*toc_)[stack_.top().pos];
201 docstring const & str = item.str();
202 string const & delim =
203 (str.empty() || !stack_.top().is_captioned) ? "" : ", ";
204 item.str(str + from_ascii(delim) + arg_str);
205 stack_.top().is_captioned = true;
208 void TocBuilder::pop()
216 ///////////////////////////////////////////////////////////////////////////
218 // TocBackend implementation
220 ///////////////////////////////////////////////////////////////////////////
222 shared_ptr<Toc const> TocBackend::toc(string const & type) const
224 // Is the type already supported?
225 TocList::const_iterator it = tocs_.find(type);
226 LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
231 shared_ptr<Toc> TocBackend::toc(string const & type)
233 // std::map::insert only really performs the insertion if the key is not
234 // already bound, and otherwise returns an iterator to the element already
235 // there, see manual.
236 return tocs_.insert({type, make_shared<Toc>()}).first->second;
240 TocBuilder & TocBackend::builder(string const & type)
242 auto p = make_unique<TocBuilder>(toc(type));
243 return * builders_.insert(make_pair(type, move(p))).first->second;
247 // FIXME: This function duplicates functionality from InsetText::iterateForToc.
248 // Both have their own way of computing the TocItem for "tableofcontents". The
249 // TocItem creation and update should be made in a dedicated function and
250 // updateItem should be rewritten to uniformly update the matching items from
252 bool TocBackend::updateItem(DocIterator const & dit_in)
255 DocIterator dit = dit_in.getInnerText();
257 if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
260 if (toc("tableofcontents")->empty()) {
261 // FIXME: should not happen,
262 // a call to TocBackend::update() is missing somewhere
263 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
267 BufferParams const & bufparams = buffer_->params();
268 const int min_toclevel = bufparams.documentClass().min_toclevel();
270 Toc::const_iterator toc_item = item("tableofcontents", dit);
274 // For each paragraph, traverse its insets and let them add
277 // FIXME: This is supposed to accomplish the same as the body of
278 // InsetText::iterateForToc(), probably
279 Paragraph & par = toc_item->dit_.paragraph();
280 InsetList::const_iterator it = par.insetList().begin();
281 InsetList::const_iterator end = par.insetList().end();
282 for (; it != end; ++it) {
283 Inset & inset = *it->inset;
284 if (inset.lyxCode() == ARG_CODE) {
285 tocstring = par.labelString();
286 if (!tocstring.empty())
288 inset.asInsetText()->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
293 int const toclevel = toc_item->dit_.text()->
294 getTocLevel(toc_item->dit_.pit());
295 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
296 && tocstring.empty())
297 par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
299 support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
300 const_cast<TocItem &>(*toc_item).str(tocstring);
302 buffer_->updateTocItem("tableofcontents", dit);
307 void TocBackend::update(bool output_active, UpdateType utype)
309 for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
313 if (!buffer_->isInternal()) {
315 buffer_->inset().addToToc(dit, output_active, utype);
320 Toc::const_iterator TocBackend::item(string const & type,
321 DocIterator const & dit) const
323 TocList::const_iterator toclist_it = tocs_.find(type);
324 // Is the type supported?
325 // We will try to make the best of it in release mode
326 LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
327 return findItem(*toclist_it->second, dit);
331 void TocBackend::writePlaintextTocList(string const & type,
332 odocstringstream & os, size_t max_length) const
334 TocList::const_iterator cit = tocs_.find(type);
335 if (cit != tocs_.end()) {
336 Toc::const_iterator ccit = cit->second->begin();
337 Toc::const_iterator end = cit->second->end();
338 for (; ccit != end; ++ccit) {
339 os << ccit->asString() << from_utf8("\n");
340 if (os.str().size() > max_length)
347 docstring TocBackend::outlinerName(string const & type) const
349 return translateIfPossible(
350 buffer_->params().documentClass().outlinerName(type));
354 bool TocBackend::isOther(std::string const & type)
356 return type == "graphics"
361 || type == "citation"
362 || type == "equation"
363 || type == "footnote"
364 || type == "marginalnote"
366 || type == "listings"
367 || type == "math-macro"
368 || type == "external"
369 || type == "senseless"
371 || type.substr(0,6) == "index:";