]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / TocBackend.cpp
1 /**
2  * \file TocBackend.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  * \author Angus Leeming
8  * \author Abdelrazak Younes
9  * \author Guillaume Munch
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "TocBackend.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "IndicesList.h"
21 #include "InsetList.h"
22 #include "Paragraph.h"
23 #include "TextClass.h"
24
25 #include "insets/InsetArgument.h"
26
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"
32
33 using namespace std;
34
35
36 namespace lyx {
37
38
39 ///////////////////////////////////////////////////////////////////////////
40 //
41 // TocItem implementation
42 //
43 ///////////////////////////////////////////////////////////////////////////
44
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),
48           action_(action)
49 {
50 }
51
52
53 int TocItem::id() const
54 {
55         return dit_.paragraph().id();
56 }
57
58
59 docstring const TocItem::asString() const
60 {
61         static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
62         static char_type const thin = 0x2009; // U+2009 THIN SPACE
63         docstring prefix;
64         if (!output_) {
65                 prefix += cross;
66                 prefix += thin;
67         }
68         if (missing_) {
69                 prefix += _("MISSING: ");
70         }
71         return prefix + str_;
72 }
73
74
75 FuncRequest TocItem::action() const
76 {
77         if (action_.action() == LFUN_UNKNOWN_ACTION) {
78                 return FuncRequest(LFUN_PARAGRAPH_GOTO, dit_.paragraphGotoArgument());
79         } else
80                 return action_;
81 }
82
83
84 ///////////////////////////////////////////////////////////////////////////
85 //
86 // TocBackend implementation
87 //
88 ///////////////////////////////////////////////////////////////////////////
89
90 Toc::const_iterator TocBackend::findItem(Toc const & toc,
91                                          DocIterator const & dit)
92 {
93         Toc::const_iterator last = toc.begin();
94         Toc::const_iterator it = toc.end();
95         if (it == last)
96                 return it;
97         --it;
98         DocIterator dit_text = dit.getInnerText();
99
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())
105                         continue;
106                 if (it->dit() <= dit_text)
107                         return it;
108         }
109
110         // We are before the first Toc Item:
111         return last;
112 }
113
114
115 Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str)
116 {
117         if (toc.empty())
118                 return toc.end();
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)
123                         break;
124         }
125         return it;
126 }
127
128
129 shared_ptr<Toc const> TocBackend::toc(string const & type) const
130 {
131         // Is the type already supported?
132         TocList::const_iterator it = tocs_.find(type);
133         LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
134         return it->second;
135 }
136
137
138 shared_ptr<Toc> TocBackend::toc(string const & type)
139 {
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;
144 }
145
146
147 TocBuilder & TocBackend::builder(string const & type)
148 {
149         auto p = lyx::make_unique<TocBuilder>(toc(type));
150         return * builders_.insert(make_pair(type, std::move(p))).first->second;
151 }
152
153
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
158 // all TOCs.
159 bool TocBackend::updateItem(DocIterator const & dit_in) const
160 {
161         if (dit_in.buffer() && dit_in.buffer()->isInternal())
162                 return false;
163
164         // we need a text
165         DocIterator dit = dit_in.getInnerText();
166
167         if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
168                 return false;
169
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!");
174                 return false;
175         }
176
177         BufferParams const & bufparams = buffer_->params();
178         const int min_toclevel = bufparams.documentClass().min_toclevel();
179
180         Toc::const_iterator toc_item = item("tableofcontents", dit);
181
182         docstring tocstring;
183
184         // For each paragraph, traverse its insets and let them add
185         // their toc items
186         //
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())
194                                 tocstring += ' ';
195                         arg->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
196                         break;
197                 }
198
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);
204
205         support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
206         const_cast<TocItem &>(*toc_item).str(tocstring);
207
208         buffer_->updateTocItem("tableofcontents", dit);
209         return true;
210 }
211
212
213 void TocBackend::reset()
214 {
215         for (auto const & t: tocs_)
216                 t.second->clear();
217         tocs_.clear();
218         builders_.clear();
219         resetOutlinerNames();
220 }
221
222
223 void TocBackend::update(bool output_active, UpdateType utype)
224 {
225         reset();
226         if (buffer_->isInternal())
227                 return;
228
229         DocIterator dit;
230         buffer_->inset().addToToc(dit, output_active, utype, *this);
231 }
232
233
234 Toc::const_iterator TocBackend::item(string const & type,
235                                      DocIterator const & dit) const
236 {
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);
242 }
243
244
245 void TocBackend::writePlaintextTocList(string const & type,
246         odocstringstream & os, size_t max_length) const
247 {
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)
255                                 break;
256                 }
257         }
258 }
259
260
261 docstring TocBackend::outlinerName(string const & type) const
262 {
263         map<string, docstring>::const_iterator const it
264                 = outliner_names_.find(type);
265         if (it != outliner_names_.end())
266                 return it->second;
267
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!");
274                 if (index)
275                         indextype = index->index();
276                 return support::bformat(_("Index Entries (%1$s)"), indextype);
277         }
278
279         LYXERR0("Missing OutlinerName for " << type << "!");
280         return from_utf8(type);
281 }
282
283
284 void TocBackend::resetOutlinerNames()
285 {
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));
291         // Hardcoded types
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"));
305 }
306
307
308 void TocBackend::addName(string const & type, docstring const & name)
309 {
310         if (name.empty())
311                 return;
312         // only inserts if the key does not exist
313         outliner_names_.insert({type, name});
314 }
315
316
317 bool TocBackend::isOther(std::string const & type)
318 {
319         return type == "graphics"
320                 || type == "note"
321                 || type == "branch"
322                 || type == "change"
323                 || type == "label"
324                 || type == "citation"
325                 || type == "equation"
326                 || type == "footnote"
327                 || type == "marginalnote"
328                 || type == "nomencl"
329                 || type == "listings"
330                 || type == "math-macro"
331                 || type == "external"
332                 || type == "senseless"
333                 || type == "index"
334                 || type.substr(0,6) == "index:";
335 }
336
337
338 } // namespace lyx