]> git.lyx.org Git - features.git/blob - src/TocBackend.cpp
merge recent changes from master
[features.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 "Cursor.h"
21 #include "FloatList.h"
22 #include "FuncRequest.h"
23 #include "InsetList.h"
24 #include "Layout.h"
25 #include "LyXAction.h"
26 #include "Paragraph.h"
27 #include "ParIterator.h"
28 #include "TextClass.h"
29
30 #include "insets/InsetText.h"
31
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"
38
39 using namespace std;
40
41
42 namespace lyx {
43
44
45 ///////////////////////////////////////////////////////////////////////////
46 //
47 // TocItem implementation
48 //
49 ///////////////////////////////////////////////////////////////////////////
50
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),
54           action_(action)
55 {
56 }
57
58
59 int TocItem::id() const
60 {
61         return dit_.paragraph().id();
62 }
63
64
65 docstring const TocItem::asString() const
66 {
67         static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
68         static char_type const thin = 0x2009; // U+2009 THIN SPACE
69         docstring prefix;
70         if (!output_) {
71                 prefix += cross;
72                 prefix += thin;
73         }
74         return prefix + str_;
75 }
76
77 namespace {
78
79 // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO 
80 docstring paragraph_goto_arg(DocIterator const & dit)
81 {
82         CursorSlice const & s = dit.innerTextSlice();
83         return convert<docstring>(s.paragraph().id()) + ' ' +
84                 convert<docstring>(s.pos());
85 }
86
87 } // namespace anon
88
89 FuncRequest TocItem::action() const
90 {
91         if (action_.action() == LFUN_UNKNOWN_ACTION) {
92                 return FuncRequest(LFUN_PARAGRAPH_GOTO, paragraph_goto_arg(dit_));
93         } else
94                 return action_;
95 }
96
97
98 ///////////////////////////////////////////////////////////////////////////
99 //
100 // Toc implementation
101 //
102 ///////////////////////////////////////////////////////////////////////////
103
104 Toc::const_iterator TocBackend::findItem(Toc const & toc,
105                                          DocIterator const & dit)
106 {
107         Toc::const_iterator last = toc.begin();
108         Toc::const_iterator it = toc.end();
109         if (it == last)
110                 return it;
111         --it;
112         DocIterator dit_text = dit.getInnerText();
113
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())
119                         continue;
120                 if (it->dit_ <= dit_text)
121                         return it;
122         }
123
124         // We are before the first Toc Item:
125         return last;
126 }
127
128
129 Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str)
130 {
131         if (toc.empty())
132                 return toc.end();
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)
137                         break;
138         }
139         return it;
140 }
141
142
143 ///////////////////////////////////////////////////////////////////////////
144 //
145 // TocBuilder implementation
146 //
147 ///////////////////////////////////////////////////////////////////////////
148
149 TocBuilder::TocBuilder(shared_ptr<Toc> toc)
150         : toc_(toc ? toc : make_shared<Toc>()),
151           stack_()
152 {
153         LATTEST(toc);
154 }
155
156 void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
157                           bool output_active, bool is_captioned)
158 {
159         toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
160         frame f = {
161                 toc_->size() - 1, //pos
162                 is_captioned, //is_captioned
163         };
164         stack_.push(f);
165 }
166
167 void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
168                              bool output_active)
169 {
170         // first show the float before moving to the caption
171         docstring arg = "paragraph-goto " + paragraph_goto_arg(dit);
172         if (!stack_.empty())
173                 arg = "paragraph-goto " +
174                         paragraph_goto_arg((*toc_)[stack_.top().pos].dit_) + ";" + arg;
175         FuncRequest func(LFUN_COMMAND_SEQUENCE, arg);
176
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];
181                 captionable.str(s);
182                 captionable.setAction(func);
183                 stack_.top().is_captioned = true;
184         } else {
185                 // This is a new entry.
186                 pop();
187                 // the dit is at the float's level, e.g. for the contextual menu of
188                 // outliner entries
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);
193         }
194 }
195
196 void TocBuilder::argumentItem(docstring const & arg_str)
197 {
198         if (stack_.empty() || arg_str.empty())
199                 return;
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;
206 }
207
208 void TocBuilder::pop()
209 {
210         if (!stack_.empty())
211                 stack_.pop();
212 }
213
214
215
216 ///////////////////////////////////////////////////////////////////////////
217 //
218 // TocBackend implementation
219 //
220 ///////////////////////////////////////////////////////////////////////////
221
222 shared_ptr<Toc const> TocBackend::toc(string const & type) const
223 {
224         // Is the type already supported?
225         TocList::const_iterator it = tocs_.find(type);
226         LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
227         return it->second;
228 }
229
230
231 shared_ptr<Toc> TocBackend::toc(string const & type)
232 {
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;
237 }
238
239
240 TocBuilder & TocBackend::builder(string const & type)
241 {
242         auto p = make_unique<TocBuilder>(toc(type));
243         return * builders_.insert(make_pair(type, move(p))).first->second;
244 }
245
246
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
251 // all TOCs.
252 bool TocBackend::updateItem(DocIterator const & dit_in)
253 {
254         // we need a text
255         DocIterator dit = dit_in.getInnerText();
256
257         if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
258                 return false;
259
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!");
264                 return false;
265         }
266
267         BufferParams const & bufparams = buffer_->params();
268         const int min_toclevel = bufparams.documentClass().min_toclevel();
269
270         Toc::const_iterator toc_item = item("tableofcontents", dit);
271
272         docstring tocstring;
273
274         // For each paragraph, traverse its insets and let them add
275         // their toc items
276         //
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())
287                                 tocstring += ' ';
288                         inset.asInsetText()->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
289                         break;
290                 }
291         }
292
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);
298
299         support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
300         const_cast<TocItem &>(*toc_item).str(tocstring);
301
302         buffer_->updateTocItem("tableofcontents", dit);
303         return true;
304 }
305
306
307 void TocBackend::update(bool output_active, UpdateType utype)
308 {
309         for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
310                 it->second->clear();
311         tocs_.clear();
312         builders_.clear();
313         if (!buffer_->isInternal()) {
314                 DocIterator dit;
315                 buffer_->inset().addToToc(dit, output_active, utype);
316         }
317 }
318
319
320 Toc::const_iterator TocBackend::item(string const & type,
321                                      DocIterator const & dit) const
322 {
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);
328 }
329
330
331 void TocBackend::writePlaintextTocList(string const & type,
332         odocstringstream & os, size_t max_length) const
333 {
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)
341                                 break;
342                 }
343         }
344 }
345
346
347 docstring TocBackend::outlinerName(string const & type) const
348 {
349         return translateIfPossible(
350             buffer_->params().documentClass().outlinerName(type));
351 }
352
353
354 bool TocBackend::isOther(std::string const & type)
355 {
356         // This is where having an Enum of types would have been more elegant...
357         return type == "graphics"
358                 || type == "note"
359                 || type == "branch"
360                 || type == "change"
361                 || type == "label"
362                 || type == "citation"
363                 || type == "equation"
364                 || type == "footnote"
365                 || type == "marginalnote"
366                 || type == "nomencl"
367                 || type == "listings"
368                 || type == "math-macro"
369                 || type == "external"
370                 || type == "senseless"
371                 || type == "index"
372                 || type.substr(0,6) == "index:";
373 }
374
375
376 } // namespace lyx