]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
Back to development
[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, FuncRequest const & action)
47         : dit_(dit), depth_(d), str_(s), output_(output_active),
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         return prefix + str_;
69 }
70
71
72 FuncRequest TocItem::action() const
73 {
74         if (action_.action() == LFUN_UNKNOWN_ACTION) {
75                 return FuncRequest(LFUN_PARAGRAPH_GOTO, dit_.paragraphGotoArgument());
76         } else
77                 return action_;
78 }
79
80
81 ///////////////////////////////////////////////////////////////////////////
82 //
83 // TocBackend implementation
84 //
85 ///////////////////////////////////////////////////////////////////////////
86
87 Toc::const_iterator TocBackend::findItem(Toc const & toc,
88                                          DocIterator const & dit)
89 {
90         Toc::const_iterator last = toc.begin();
91         Toc::const_iterator it = toc.end();
92         if (it == last)
93                 return it;
94         --it;
95         DocIterator dit_text = dit.getInnerText();
96
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())
102                         continue;
103                 if (it->dit() <= dit_text)
104                         return it;
105         }
106
107         // We are before the first Toc Item:
108         return last;
109 }
110
111
112 Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str)
113 {
114         if (toc.empty())
115                 return toc.end();
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)
120                         break;
121         }
122         return it;
123 }
124
125
126 shared_ptr<Toc const> TocBackend::toc(string const & type) const
127 {
128         // Is the type already supported?
129         TocList::const_iterator it = tocs_.find(type);
130         LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
131         return it->second;
132 }
133
134
135 shared_ptr<Toc> TocBackend::toc(string const & type)
136 {
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;
141 }
142
143
144 TocBuilder & TocBackend::builder(string const & type)
145 {
146         auto p = lyx::make_unique<TocBuilder>(toc(type));
147         return * builders_.insert(make_pair(type, move(p))).first->second;
148 }
149
150
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
155 // all TOCs.
156 bool TocBackend::updateItem(DocIterator const & dit_in) const
157 {
158         if (dit_in.buffer() && dit_in.buffer()->isInternal())
159                 return false;
160
161         // we need a text
162         DocIterator dit = dit_in.getInnerText();
163
164         if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
165                 return false;
166
167         if (toc("tableofcontents")->empty()) {
168                 // FIXME: should not happen,
169                 // a call to TocBackend::update() is missing somewhere
170                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
171                 return false;
172         }
173
174         BufferParams const & bufparams = buffer_->params();
175         const int min_toclevel = bufparams.documentClass().min_toclevel();
176
177         Toc::const_iterator toc_item = item("tableofcontents", dit);
178
179         docstring tocstring;
180
181         // For each paragraph, traverse its insets and let them add
182         // their toc items
183         //
184         // FIXME: This is supposed to accomplish the same as the body of
185         // InsetText::iterateForToc(), probably
186         Paragraph const & par = toc_item->dit().paragraph();
187         for (auto const & table : par.insetList())
188                 if (InsetArgument const * arg = table.inset->asInsetArgument()) {
189                         tocstring = par.labelString();
190                         if (!tocstring.empty())
191                                 tocstring += ' ';
192                         arg->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
193                         break;
194                 }
195
196         int const toclevel = toc_item->dit().text()->
197                 getTocLevel(toc_item->dit().pit());
198         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
199                 && tocstring.empty())
200                 par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
201
202         support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
203         const_cast<TocItem &>(*toc_item).str(tocstring);
204
205         buffer_->updateTocItem("tableofcontents", dit);
206         return true;
207 }
208
209
210 void TocBackend::reset()
211 {
212         for (auto const & t: tocs_)
213                 t.second->clear();
214         tocs_.clear();
215         builders_.clear();
216         resetOutlinerNames();
217 }
218
219
220 void TocBackend::update(bool output_active, UpdateType utype)
221 {
222         reset();
223         if (buffer_->isInternal())
224                 return;
225
226         DocIterator dit;
227         buffer_->inset().addToToc(dit, output_active, utype, *this);
228 }
229
230
231 Toc::const_iterator TocBackend::item(string const & type,
232                                      DocIterator const & dit) const
233 {
234         TocList::const_iterator toclist_it = tocs_.find(type);
235         // Is the type supported?
236         // We will try to make the best of it in release mode
237         LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
238         return findItem(*toclist_it->second, dit);
239 }
240
241
242 void TocBackend::writePlaintextTocList(string const & type,
243         odocstringstream & os, size_t max_length) const
244 {
245         TocList::const_iterator cit = tocs_.find(type);
246         if (cit != tocs_.end()) {
247                 Toc::const_iterator ccit = cit->second->begin();
248                 Toc::const_iterator end = cit->second->end();
249                 for (; ccit != end; ++ccit) {
250                         os << ccit->asString() << from_utf8("\n");
251                         if (os.str().size() > max_length)
252                                 break;
253                 }
254         }
255 }
256
257
258 docstring TocBackend::outlinerName(string const & type) const
259 {
260         map<string, docstring>::const_iterator const it
261                 = outliner_names_.find(type);
262         if (it != outliner_names_.end())
263                 return it->second;
264
265         // Legacy treatment of index:... type
266         if (support::prefixIs(type, "index:")) {
267                 string const itype = support::split(type, ':');
268                 IndicesList const & indiceslist = buffer_->params().indiceslist();
269                 Index const * index = indiceslist.findShortcut(from_utf8(itype));
270                 docstring indextype = _("unknown type!");
271                 if (index)
272                         indextype = index->index();
273                 return support::bformat(_("Index Entries (%1$s)"), indextype);
274         }
275
276         LYXERR0("Missing OutlinerName for " << type << "!");
277         return from_utf8(type);
278 }
279
280
281 void TocBackend::resetOutlinerNames()
282 {
283         outliner_names_.clear();
284         // names from this document class
285         for (auto const & name
286                      : buffer_->params().documentClass().outlinerNames())
287                 addName(name.first, translateIfPossible(name.second));
288         // Hardcoded types
289         addName("tableofcontents", _("Table of Contents"));
290         addName("change", _("Changes"));
291         addName("senseless", _("Senseless"));
292         addName("citation", _("Citations"));
293         addName("label", _("Labels and References"));
294         addName("brokenrefs", _("Broken References and Citations"));
295         // Customizable, but the corresponding insets have no layout definition
296         addName("child", _("Child Documents"));
297         addName("graphics", _("Graphics[[listof]]"));
298         addName("equation", _("Equations"));
299         addName("external", _("External Material"));
300         addName("math-macro", _("Math Macros"));
301         addName("nomencl", _("Nomenclature Entries"));
302 }
303
304
305 void TocBackend::addName(string const & type, docstring const & name)
306 {
307         if (name.empty())
308                 return;
309         // only inserts if the key does not exist
310         outliner_names_.insert({type, name});
311 }
312
313
314 bool TocBackend::isOther(std::string const & type)
315 {
316         return type == "graphics"
317                 || type == "note"
318                 || type == "branch"
319                 || type == "change"
320                 || type == "label"
321                 || type == "citation"
322                 || type == "equation"
323                 || type == "footnote"
324                 || type == "marginalnote"
325                 || type == "nomencl"
326                 || type == "listings"
327                 || type == "math-macro"
328                 || type == "external"
329                 || type == "senseless"
330                 || type == "index"
331                 || type.substr(0,6) == "index:";
332 }
333
334
335 } // namespace lyx