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"
20 #include "IndicesList.h"
21 #include "InsetList.h"
22 #include "Paragraph.h"
23 #include "TextClass.h"
25 #include "insets/InsetArgument.h"
27 #include "support/debug.h"
28 #include "support/docstream.h"
29 #include "support/gettext.h"
30 #include "support/lassert.h"
31 #include "support/lstrings.h"
39 ///////////////////////////////////////////////////////////////////////////
41 // TocItem implementation
43 ///////////////////////////////////////////////////////////////////////////
45 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
46 bool output_active, bool missing, FuncRequest const & action)
47 : dit_(dit), depth_(d), str_(s), output_(output_active), missing_(missing),
53 int TocItem::id() const
55 return dit_.paragraph().id();
59 docstring const TocItem::asString() const
61 static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
62 static char_type const thin = 0x2009; // U+2009 THIN SPACE
69 prefix += _("MISSING: ");
75 FuncRequest TocItem::action() const
77 if (action_.action() == LFUN_UNKNOWN_ACTION) {
78 return FuncRequest(LFUN_PARAGRAPH_GOTO, dit_.paragraphGotoArgument());
84 ///////////////////////////////////////////////////////////////////////////
86 // TocBackend implementation
88 ///////////////////////////////////////////////////////////////////////////
90 Toc::const_iterator TocBackend::findItem(Toc const & toc,
91 DocIterator const & dit)
93 Toc::const_iterator last = toc.begin();
94 Toc::const_iterator it = toc.end();
98 DocIterator dit_text = dit.getInnerText();
100 for (; it != last; --it) {
101 // We verify that we don't compare contents of two
102 // different document. This happens when you
103 // have parent and child documents.
104 if (&it->dit()[0].inset() != &dit_text[0].inset())
106 if (it->dit() <= dit_text)
110 // We are before the first Toc Item:
115 Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str)
119 Toc::iterator it = toc.begin();
120 Toc::iterator itend = toc.end();
121 for (; it != itend; ++it) {
122 if (it->depth() == depth && it->str() == str)
129 shared_ptr<Toc const> TocBackend::toc(string const & type) const
131 // Is the type already supported?
132 TocList::const_iterator it = tocs_.find(type);
133 LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
138 shared_ptr<Toc> TocBackend::toc(string const & type)
140 // std::map::insert only really performs the insertion if the key is not
141 // already bound, and otherwise returns an iterator to the element already
142 // there, see manual.
143 return tocs_.insert({type, make_shared<Toc>()}).first->second;
147 TocBuilder & TocBackend::builder(string const & type)
149 auto p = lyx::make_unique<TocBuilder>(toc(type));
150 return * builders_.insert(make_pair(type, move(p))).first->second;
154 // FIXME: This function duplicates functionality from InsetText::iterateForToc.
155 // Both have their own way of computing the TocItem for "tableofcontents". The
156 // TocItem creation and update should be made in a dedicated function and
157 // updateItem should be rewritten to uniformly update the matching items from
159 bool TocBackend::updateItem(DocIterator const & dit_in) const
161 if (dit_in.buffer() && dit_in.buffer()->isInternal())
165 DocIterator dit = dit_in.getInnerText();
167 if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
170 if (toc("tableofcontents")->empty()) {
171 // FIXME: should not happen,
172 // a call to TocBackend::update() is missing somewhere
173 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
177 BufferParams const & bufparams = buffer_->params();
178 const int min_toclevel = bufparams.documentClass().min_toclevel();
180 Toc::const_iterator toc_item = item("tableofcontents", dit);
184 // For each paragraph, traverse its insets and let them add
187 // FIXME: This is supposed to accomplish the same as the body of
188 // InsetText::iterateForToc(), probably
189 Paragraph const & par = toc_item->dit().paragraph();
190 for (auto const & table : par.insetList())
191 if (InsetArgument const * arg = table.inset->asInsetArgument()) {
192 tocstring = par.labelString();
193 if (!tocstring.empty())
195 arg->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
199 int const toclevel = toc_item->dit().text()->
200 getTocLevel(toc_item->dit().pit());
201 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
202 && tocstring.empty())
203 par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
205 support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
206 const_cast<TocItem &>(*toc_item).str(tocstring);
208 buffer_->updateTocItem("tableofcontents", dit);
213 void TocBackend::reset()
215 for (auto const & t: tocs_)
219 resetOutlinerNames();
223 void TocBackend::update(bool output_active, UpdateType utype)
226 if (buffer_->isInternal())
230 buffer_->inset().addToToc(dit, output_active, utype, *this);
234 Toc::const_iterator TocBackend::item(string const & type,
235 DocIterator const & dit) const
237 TocList::const_iterator toclist_it = tocs_.find(type);
238 // Is the type supported?
239 // We will try to make the best of it in release mode
240 LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
241 return findItem(*toclist_it->second, dit);
245 void TocBackend::writePlaintextTocList(string const & type,
246 odocstringstream & os, size_t max_length) const
248 TocList::const_iterator cit = tocs_.find(type);
249 if (cit != tocs_.end()) {
250 Toc::const_iterator ccit = cit->second->begin();
251 Toc::const_iterator end = cit->second->end();
252 for (; ccit != end; ++ccit) {
253 os << ccit->asString() << from_utf8("\n");
254 if (os.str().size() > max_length)
261 docstring TocBackend::outlinerName(string const & type) const
263 map<string, docstring>::const_iterator const it
264 = outliner_names_.find(type);
265 if (it != outliner_names_.end())
268 // Legacy treatment of index:... type
269 if (support::prefixIs(type, "index:")) {
270 string const itype = support::split(type, ':');
271 IndicesList const & indiceslist = buffer_->params().indiceslist();
272 Index const * index = indiceslist.findShortcut(from_utf8(itype));
273 docstring indextype = _("unknown type!");
275 indextype = index->index();
276 return support::bformat(_("Index Entries (%1$s)"), indextype);
279 LYXERR0("Missing OutlinerName for " << type << "!");
280 return from_utf8(type);
284 void TocBackend::resetOutlinerNames()
286 outliner_names_.clear();
287 // names from this document class
288 for (auto const & name
289 : buffer_->params().documentClass().outlinerNames())
290 addName(name.first, translateIfPossible(name.second));
292 addName("tableofcontents", _("Table of Contents"));
293 addName("change", _("Changes"));
294 addName("senseless", _("Senseless"));
295 addName("citation", _("Citations"));
296 addName("label", _("Labels and References"));
297 addName("brokenrefs", _("Broken References and Citations"));
298 // Customizable, but the corresponding insets have no layout definition
299 addName("child", _("Child Documents"));
300 addName("graphics", _("Graphics[[listof]]"));
301 addName("equation", _("Equations"));
302 addName("external", _("External Material"));
303 addName("math-macro", _("Math Macros"));
304 addName("nomencl", _("Nomenclature Entries"));
308 void TocBackend::addName(string const & type, docstring const & name)
312 // only inserts if the key does not exist
313 outliner_names_.insert({type, name});
317 bool TocBackend::isOther(std::string const & type)
319 return type == "graphics"
324 || type == "citation"
325 || type == "equation"
326 || type == "footnote"
327 || type == "marginalnote"
329 || type == "listings"
330 || type == "math-macro"
331 || type == "external"
332 || type == "senseless"
334 || type.substr(0,6) == "index:";