]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
support for Amharic, Asturian, Kannada and Khmer
[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 "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/InsetArgument.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::pop()
197 {
198         if (!stack_.empty())
199                 stack_.pop();
200 }
201
202
203
204 ///////////////////////////////////////////////////////////////////////////
205 //
206 // TocBackend implementation
207 //
208 ///////////////////////////////////////////////////////////////////////////
209
210 shared_ptr<Toc const> TocBackend::toc(string const & type) const
211 {
212         // Is the type already supported?
213         TocList::const_iterator it = tocs_.find(type);
214         LASSERT(it != tocs_.end(), { return make_shared<Toc>(); });
215         return it->second;
216 }
217
218
219 shared_ptr<Toc> TocBackend::toc(string const & type)
220 {
221         // std::map::insert only really performs the insertion if the key is not
222         // already bound, and otherwise returns an iterator to the element already
223         // there, see manual.
224         return tocs_.insert({type, make_shared<Toc>()}).first->second;
225 }
226
227
228 TocBuilder & TocBackend::builder(string const & type)
229 {
230         auto p = make_unique<TocBuilder>(toc(type));
231         return * builders_.insert(make_pair(type, move(p))).first->second;
232 }
233
234
235 // FIXME: This function duplicates functionality from InsetText::iterateForToc.
236 // Both have their own way of computing the TocItem for "tableofcontents". The
237 // TocItem creation and update should be made in a dedicated function and
238 // updateItem should be rewritten to uniformly update the matching items from
239 // all TOCs.
240 bool TocBackend::updateItem(DocIterator const & dit_in)
241 {
242         // we need a text
243         DocIterator dit = dit_in.getInnerText();
244
245         if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
246                 return false;
247
248         if (toc("tableofcontents")->empty()) {
249                 // FIXME: should not happen, 
250                 // a call to TocBackend::update() is missing somewhere
251                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
252                 return false;
253         }
254
255         BufferParams const & bufparams = buffer_->params();
256         const int min_toclevel = bufparams.documentClass().min_toclevel();
257
258         Toc::const_iterator toc_item = item("tableofcontents", dit);
259
260         docstring tocstring;
261
262         // For each paragraph, traverse its insets and let them add
263         // their toc items
264         //
265         // FIXME: This is supposed to accomplish the same as the body of
266         // InsetText::iterateForToc(), probably
267         Paragraph & par = toc_item->dit_.paragraph();
268         InsetList::const_iterator it = par.insetList().begin();
269         InsetList::const_iterator end = par.insetList().end();
270         for (; it != end; ++it) {
271                 Inset & inset = *it->inset;
272                 if (inset.lyxCode() == ARG_CODE) {
273                         tocstring = par.labelString();
274                         if (!tocstring.empty())
275                                 tocstring += ' ';
276                         inset.asInsetText()->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
277                         break;
278                 }
279         }
280
281         int const toclevel = toc_item->dit_.text()->
282                 getTocLevel(toc_item->dit_.pit());
283         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
284                 && tocstring.empty())
285                 par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
286
287         support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
288         const_cast<TocItem &>(*toc_item).str(tocstring);
289
290         buffer_->updateTocItem("tableofcontents", dit);
291         return true;
292 }
293
294
295 void TocBackend::update(bool output_active, UpdateType utype)
296 {
297         for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
298                 it->second->clear();
299         tocs_.clear();
300         builders_.clear();
301         if (!buffer_->isInternal()) {
302                 DocIterator dit;
303                 buffer_->inset().addToToc(dit, output_active, utype);
304         }
305 }
306
307
308 Toc::const_iterator TocBackend::item(string const & type,
309                                      DocIterator const & dit) const
310 {
311         TocList::const_iterator toclist_it = tocs_.find(type);
312         // Is the type supported?
313         // We will try to make the best of it in release mode
314         LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
315         return findItem(*toclist_it->second, dit);
316 }
317
318
319 void TocBackend::writePlaintextTocList(string const & type,
320         odocstringstream & os, size_t max_length) const
321 {
322         TocList::const_iterator cit = tocs_.find(type);
323         if (cit != tocs_.end()) {
324                 Toc::const_iterator ccit = cit->second->begin();
325                 Toc::const_iterator end = cit->second->end();
326                 for (; ccit != end; ++ccit) {
327                         os << ccit->asString() << from_utf8("\n");
328                         if (os.str().size() > max_length)
329                                 break;
330                 }
331         }
332 }
333
334
335 docstring TocBackend::outlinerName(string const & type) const
336 {
337         return translateIfPossible(
338             buffer_->params().documentClass().outlinerName(type));
339 }
340
341
342 } // namespace lyx