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/InsetText.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, FuncRequest action)
47 : dit_(dit), depth_(d), str_(s), output_(output_active),
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
72 FuncRequest TocItem::action() const
74 if (action_.action() == LFUN_UNKNOWN_ACTION) {
75 return FuncRequest(LFUN_PARAGRAPH_GOTO, dit_.paragraphGotoArgument());
81 ///////////////////////////////////////////////////////////////////////////
83 // TocBackend implementation
85 ///////////////////////////////////////////////////////////////////////////
87 Toc::const_iterator TocBackend::findItem(Toc const & toc,
88 DocIterator const & dit)
90 Toc::const_iterator last = toc.begin();
91 Toc::const_iterator it = toc.end();
95 DocIterator dit_text = dit.getInnerText();
97 for (; it != last; --it) {
98 // We verify that we don't compare contents of two
99 // different document. This happens when you
100 // have parent and child documents.
101 if (&it->dit()[0].inset() != &dit_text[0].inset())
103 if (it->dit() <= dit_text)
107 // We are before the first Toc Item:
112 Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str)
116 Toc::iterator it = toc.begin();
117 Toc::iterator itend = toc.end();
118 for (; it != itend; ++it) {
119 if (it->depth() == depth && it->str() == str)
126 shared_ptr<Toc const> TocBackend::toc(string const & type) const
128 // Is the type already supported?
129 TocList::const_iterator it = tocs_.find(type);
130 LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
135 shared_ptr<Toc> TocBackend::toc(string const & type)
137 // std::map::insert only really performs the insertion if the key is not
138 // already bound, and otherwise returns an iterator to the element already
139 // there, see manual.
140 return tocs_.insert({type, make_shared<Toc>()}).first->second;
144 TocBuilder & TocBackend::builder(string const & type)
146 auto p = make_unique<TocBuilder>(toc(type));
147 return * builders_.insert(make_pair(type, move(p))).first->second;
151 // FIXME: This function duplicates functionality from InsetText::iterateForToc.
152 // Both have their own way of computing the TocItem for "tableofcontents". The
153 // TocItem creation and update should be made in a dedicated function and
154 // updateItem should be rewritten to uniformly update the matching items from
156 bool TocBackend::updateItem(DocIterator const & dit_in)
159 DocIterator dit = dit_in.getInnerText();
161 if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
164 if (toc("tableofcontents")->empty()) {
165 // FIXME: should not happen,
166 // a call to TocBackend::update() is missing somewhere
167 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
171 BufferParams const & bufparams = buffer_->params();
172 const int min_toclevel = bufparams.documentClass().min_toclevel();
174 Toc::const_iterator toc_item = item("tableofcontents", dit);
178 // For each paragraph, traverse its insets and let them add
181 // FIXME: This is supposed to accomplish the same as the body of
182 // InsetText::iterateForToc(), probably
183 Paragraph & par = toc_item->dit().paragraph();
184 InsetList::const_iterator it = par.insetList().begin();
185 InsetList::const_iterator end = par.insetList().end();
186 for (; it != end; ++it) {
187 Inset & inset = *it->inset;
188 if (inset.lyxCode() == ARG_CODE) {
189 tocstring = par.labelString();
190 if (!tocstring.empty())
192 inset.asInsetText()->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
197 int const toclevel = toc_item->dit().text()->
198 getTocLevel(toc_item->dit().pit());
199 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
200 && tocstring.empty())
201 par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
203 support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
204 const_cast<TocItem &>(*toc_item).str(tocstring);
206 buffer_->updateTocItem("tableofcontents", dit);
211 void TocBackend::update(bool output_active, UpdateType utype)
213 for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
217 resetOutlinerNames();
218 if (!buffer_->isInternal()) {
220 buffer_->inset().addToToc(dit, output_active, utype, *this);
225 Toc::const_iterator TocBackend::item(string const & type,
226 DocIterator const & dit) const
228 TocList::const_iterator toclist_it = tocs_.find(type);
229 // Is the type supported?
230 // We will try to make the best of it in release mode
231 LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
232 return findItem(*toclist_it->second, dit);
236 void TocBackend::writePlaintextTocList(string const & type,
237 odocstringstream & os, size_t max_length) const
239 TocList::const_iterator cit = tocs_.find(type);
240 if (cit != tocs_.end()) {
241 Toc::const_iterator ccit = cit->second->begin();
242 Toc::const_iterator end = cit->second->end();
243 for (; ccit != end; ++ccit) {
244 os << ccit->asString() << from_utf8("\n");
245 if (os.str().size() > max_length)
252 docstring TocBackend::outlinerName(string const & type) const
254 map<string, docstring>::const_iterator const it
255 = outliner_names_.find(type);
256 if (it != outliner_names_.end())
259 // Legacy treatment of index:... type
260 if (support::prefixIs(type, "index:")) {
261 string const itype = support::split(type, ':');
262 IndicesList const & indiceslist = buffer_->params().indiceslist();
263 Index const * index = indiceslist.findShortcut(from_utf8(itype));
264 docstring indextype = _("unknown type!");
266 indextype = index->index();
267 return support::bformat(_("Index Entries (%1$s)"), indextype);
270 LYXERR0("Missing OutlinerName for " << type << "!");
271 return from_utf8(type);
275 void TocBackend::resetOutlinerNames()
277 outliner_names_.clear();
278 // names from this document class
279 for (pair<string, docstring> const & name
280 : buffer_->params().documentClass().outlinerNames())
281 addName(name.first, translateIfPossible(name.second));
283 addName("tableofcontents", _("Table of Contents"));
284 addName("change", _("Changes"));
285 addName("senseless", _("Senseless"));
286 addName("citation", _("Citations"));
287 addName("label", _("Labels and References"));
288 // Customizable, but the corresponding insets have no layout definition
289 addName("child", _("Child Documents"));
290 addName("graphics", _("Graphics"));
291 addName("equation", _("Equations"));
292 addName("external", _("External Material"));
293 addName("math-macro", _("Math Macros"));
294 addName("nomencl", _("Nomenclature Entries"));
298 void TocBackend::addName(string const & type, docstring const & name)
302 // only inserts if the key does not exist
303 outliner_names_.insert({type, name});
307 bool TocBackend::isOther(std::string const & type)
309 return type == "graphics"
314 || type == "citation"
315 || type == "equation"
316 || type == "footnote"
317 || type == "marginalnote"
319 || type == "listings"
320 || type == "math-macro"
321 || type == "external"
322 || type == "senseless"
324 || type.substr(0,6) == "index:";