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
9 * Full author contact details are available in file CREDITS.
17 #include "bufferparams.h"
18 #include "FloatList.h"
19 #include "funcrequest.h"
20 #include "LyXAction.h"
21 #include "paragraph.h"
23 #include "frontends/LyXView.h"
25 #include "insets/insetfloat.h"
26 #include "insets/insetoptarg.h"
27 #include "insets/insetwrap.h"
29 #include "support/convert.h"
39 string const TocItem::asString() const
41 return string(4 * depth, ' ') + str;
45 void TocItem::goTo(LyXView & lv_) const
47 string const tmp = convert<string>(id_);
48 lv_.dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
52 FuncRequest TocItem::action() const
54 return FuncRequest(LFUN_GOTO_PARAGRAPH, convert<string>(id_));
58 string const getType(string const & cmdName)
61 if (cmdName == "tableofcontents")
68 string const getGuiName(string const & type, Buffer const & buffer)
70 FloatList const & floats =
71 buffer.params().getLyXTextClass().floats();
72 if (floats.typeExist(type))
73 return floats.getType(type).name();
79 TocList const getTocList(Buffer const & buf)
83 BufferParams const & bufparams = buf.params();
84 const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
86 ParConstIterator pit = buf.par_iterator_begin();
87 ParConstIterator end = buf.par_iterator_end();
88 for (; pit != end; ++pit) {
90 // the string that goes to the toc (could be the optarg)
93 // For each paragraph, traverse its insets and look for
94 // FLOAT_CODE or WRAP_CODE
95 InsetList::const_iterator it = pit->insetlist.begin();
96 InsetList::const_iterator end = pit->insetlist.end();
97 for (; it != end; ++it) {
98 switch (it->inset->lyxCode()) {
99 case InsetBase::FLOAT_CODE:
100 static_cast<InsetFloat*>(it->inset)
101 ->addToToc(toclist, buf);
103 case InsetBase::WRAP_CODE:
104 static_cast<InsetWrap*>(it->inset)
105 ->addToToc(toclist, buf);
107 case InsetBase::OPTARG_CODE: {
108 if (!tocstring.empty())
110 Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
111 if (!pit->getLabelstring().empty())
112 tocstring = pit->getLabelstring()
114 tocstring += par.asString(buf, false);
122 /// now the toc entry for the paragraph
123 int const toclevel = pit->layout()->toclevel;
124 if (toclevel != LyXLayout::NOT_IN_TOC
125 && toclevel >= min_toclevel
126 && toclevel <= bufparams.tocdepth) {
127 // insert this into the table of contents
128 if (tocstring.empty())
129 tocstring = pit->asString(buf, true);
130 TocItem const item(pit->id(), toclevel - min_toclevel,
132 toclist["TOC"].push_back(item);
139 vector<string> const getTypes(Buffer const & buffer)
141 vector<string> types;
143 TocList const tmp = getTocList(buffer);
145 TocList::const_iterator cit = tmp.begin();
146 TocList::const_iterator end = tmp.end();
148 for (; cit != end; ++cit) {
149 types.push_back(cit->first);
156 void asciiTocList(string const & type, Buffer const & buffer, ostream & os)
158 TocList const toc_list = getTocList(buffer);
159 TocList::const_iterator cit = toc_list.find(type);
160 if (cit != toc_list.end()) {
161 Toc::const_iterator ccit = cit->second.begin();
162 Toc::const_iterator end = cit->second.end();
163 for (; ccit != end; ++ccit)
164 os << ccit->asString() << '\n';
169 void Outline(OutlineOp mode, Buffer * buf, pit_type & pit)
171 ParagraphList & pars = buf->text().paragraphs();
172 ParagraphList::iterator bgn = pars.begin();
173 ParagraphList::iterator s = boost::next(bgn, pit);
174 ParagraphList::iterator p = s;
175 ParagraphList::iterator end = pars.end();
177 LyXTextClass::const_iterator lit =
178 buf->params().getLyXTextClass().begin();
179 LyXTextClass::const_iterator const lend =
180 buf->params().getLyXTextClass().end();
182 int const thistoclevel = s->layout()->toclevel;
188 for (; p != end; ++p) {
189 toclevel = p->layout()->toclevel;
190 if (toclevel != LyXLayout::NOT_IN_TOC
191 && toclevel <= thistoclevel) {
195 ParagraphList::iterator q = s;
200 for (; q != bgn; --q) {
201 toclevel = q->layout()->toclevel;
202 if (toclevel != LyXLayout::NOT_IN_TOC
203 && toclevel <= thistoclevel) {
207 pit_type const newpit = std::distance(pars.begin(), q);
208 pit_type const len = std::distance(s, p);
210 pars.insert(q, s, p);
211 s = boost::next(pars.begin(), pit);
212 ParagraphList::iterator t = boost::next(s, len);
220 for (; p != end; ++p) {
221 toclevel = p->layout()->toclevel;
222 if (toclevel != LyXLayout::NOT_IN_TOC
223 && toclevel <= thistoclevel) {
227 ParagraphList::iterator q = p;
232 for (; q != end; ++q) {
233 toclevel = q->layout()->toclevel;
234 if (toclevel != LyXLayout::NOT_IN_TOC
235 && toclevel <= thistoclevel) {
239 pit_type const newpit = std::distance(pars.begin(), q);
240 pit_type const len = std::distance(s, p);
241 pars.insert(q, s, p);
242 s = boost::next(pars.begin(), pit);
243 ParagraphList::iterator t = boost::next(s, len);
249 for (; lit != lend; ++lit) {
250 if ((*lit)->toclevel == thistoclevel + 1) {
257 for (; lit != lend; ++lit) {
258 if ((*lit)->toclevel == thistoclevel - 1) {