]> git.lyx.org Git - lyx.git/blob - src/toc.C
Fix bug 2485 and crash on middle mouse paste on math
[lyx.git] / src / toc.C
1 /**
2  * \file toc.C
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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "toc.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "FloatList.h"
19 #include "funcrequest.h"
20 #include "LyXAction.h"
21 #include "paragraph.h"
22
23 #include "frontends/LyXView.h"
24
25 #include "insets/insetfloat.h"
26 #include "insets/insetoptarg.h"
27 #include "insets/insetwrap.h"
28
29 #include "support/convert.h"
30
31 using std::vector;
32 using std::max;
33 using std::ostream;
34 using std::string;
35
36 namespace lyx {
37 namespace toc {
38
39 string const TocItem::asString() const
40 {
41         return string(4 * depth, ' ') + str;
42 }
43
44
45 void TocItem::goTo(LyXView & lv_) const
46 {
47         string const tmp = convert<string>(id_);
48         lv_.dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
49 }
50
51
52 FuncRequest TocItem::action() const
53 {
54         return FuncRequest(LFUN_GOTO_PARAGRAPH, convert<string>(id_));
55 }
56
57
58 string const getType(string const & cmdName)
59 {
60         // special case
61         if (cmdName == "tableofcontents")
62                 return "TOC";
63         else
64                 return cmdName;
65 }
66
67
68 string const getGuiName(string const & type, Buffer const & buffer)
69 {
70         FloatList const & floats =
71                 buffer.params().getLyXTextClass().floats();
72         if (floats.typeExist(type))
73                 return floats.getType(type).name();
74         else
75                 return type;
76 }
77
78
79 TocList const getTocList(Buffer const & buf)
80 {
81         TocList toclist;
82
83         BufferParams const & bufparams = buf.params();
84         const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
85
86         ParConstIterator pit = buf.par_iterator_begin();
87         ParConstIterator end = buf.par_iterator_end();
88         for (; pit != end; ++pit) {
89
90                 // the string that goes to the toc (could be the optarg)
91                 string tocstring;
92
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);
102                                 break;
103                         case InsetBase::WRAP_CODE:
104                                 static_cast<InsetWrap*>(it->inset)
105                                         ->addToToc(toclist, buf);
106                                 break;
107                         case InsetBase::OPTARG_CODE: {
108                                 if (!tocstring.empty())
109                                         break;
110                                 Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
111                                 if (!pit->getLabelstring().empty())
112                                         tocstring = pit->getLabelstring()
113                                                 + ' ';
114                                 tocstring += par.asString(buf, false);
115                                 break;
116                         }
117                         default:
118                                 break;
119                         }
120                 }
121
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,
131                                            tocstring);
132                         toclist["TOC"].push_back(item);
133                 }
134         }
135         return toclist;
136 }
137
138
139 vector<string> const getTypes(Buffer const & buffer)
140 {
141         vector<string> types;
142
143         TocList const tmp = getTocList(buffer);
144
145         TocList::const_iterator cit = tmp.begin();
146         TocList::const_iterator end = tmp.end();
147
148         for (; cit != end; ++cit) {
149                 types.push_back(cit->first);
150         }
151
152         return types;
153 }
154
155
156 void asciiTocList(string const & type, Buffer const & buffer, ostream & os)
157 {
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';
165         }
166 }
167
168
169 void outline(OutlineOp mode, Buffer * buf, pit_type & pit)
170 {
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();
176
177         LyXTextClass::const_iterator lit =
178                 buf->params().getLyXTextClass().begin();
179         LyXTextClass::const_iterator const lend =
180                 buf->params().getLyXTextClass().end();
181
182         int const thistoclevel = s->layout()->toclevel;
183         int toclevel;
184         switch (mode) {
185                 case UP: {
186                         if (p != end)
187                                 ++p;
188                         for (; p != end; ++p) {
189                                 toclevel = p->layout()->toclevel;
190                                 if (toclevel != LyXLayout::NOT_IN_TOC
191                                     && toclevel <= thistoclevel) {
192                                         break;
193                                 }
194                         }
195                         ParagraphList::iterator q = s;
196                         if (q != bgn)
197                                 --q;
198                         else
199                                 break;
200                         for (; q != bgn; --q) {
201                                 toclevel = q->layout()->toclevel;
202                                 if (toclevel != LyXLayout::NOT_IN_TOC
203                                     && toclevel <= thistoclevel) {
204                                         break;
205                                 }
206                         }
207                         pit_type const newpit = std::distance(pars.begin(), q);
208                         pit_type const len = std::distance(s, p);
209                         pit += len;
210                         pars.insert(q, s, p);
211                         s = boost::next(pars.begin(), pit);
212                         ParagraphList::iterator t = boost::next(s, len);
213                         pit = newpit;
214                         pars.erase(s, t);
215                 break;
216                 }
217                 case DOWN: {
218                            if (p != end)
219                                 ++p;
220                         for (; p != end; ++p) {
221                                 toclevel = p->layout()->toclevel;
222                                 if (toclevel != LyXLayout::NOT_IN_TOC
223                                     && toclevel <= thistoclevel) {
224                                         break;
225                                 }
226                         }
227                         ParagraphList::iterator q = p;
228                         if (q != end)
229                                 ++q;
230                         else
231                                 break;
232                         for (; q != end; ++q) {
233                                 toclevel = q->layout()->toclevel;
234                                 if (toclevel != LyXLayout::NOT_IN_TOC
235                                     && toclevel <= thistoclevel) {
236                                         break;
237                                 }
238                         }
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);
244                         pit = newpit - len;
245                         pars.erase(s, t);
246                 break;
247                 }
248                 case IN:
249                         for (; lit != lend; ++lit) {
250                                 if ((*lit)->toclevel == thistoclevel + 1) {
251                                         s->layout((*lit));
252                                         break;
253                                 }
254                         }
255                 break;
256                 case OUT:
257                         for (; lit != lend; ++lit) {
258                                 if ((*lit)->toclevel == thistoclevel - 1) {
259                                         s->layout((*lit));
260                                         break;
261                                 }
262                         }
263                 break;
264                 default:
265                 break;
266         }
267 }
268
269
270 } // namespace toc
271 } // namespace lyx