]> git.lyx.org Git - lyx.git/blob - src/toc.C
Remove unused symbol encoding
[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  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "toc.h"
16
17 #include "buffer.h"
18 #include "bufferparams.h"
19 #include "FloatList.h"
20 #include "funcrequest.h"
21 #include "LyXAction.h"
22 #include "paragraph.h"
23 #include "cursor.h"
24 #include "debug.h"
25 #include "undo.h"
26
27 #include "insets/insetfloat.h"
28 #include "insets/insetoptarg.h"
29 #include "insets/insetwrap.h"
30
31 #include "support/convert.h"
32
33 #include <iostream>
34 #include <map>
35
36 using std::map;
37 using std::pair;
38 using std::make_pair;
39 using std::vector;
40 using std::max;
41 using std::ostream;
42 using std::string;
43 using std::endl;
44
45 namespace lyx {
46 namespace toc {
47
48 typedef map<Buffer const *, TocBackend> TocMap;
49 static TocMap toc_backend_;
50
51 ///////////////////////////////////////////////////////////////////////////
52 // Interface to toc_backend_
53
54 void updateToc(Buffer const & buf)
55 {
56         TocMap::iterator it = toc_backend_.find(&buf);
57         if (it == toc_backend_.end()) {
58                 pair<TocMap::iterator, bool> result
59                         = toc_backend_.insert(make_pair(&buf, TocBackend(&buf)));
60                 if (!result.second)
61                         return;
62
63                 it = result.first;
64         }
65
66         it->second.update();
67 }
68
69
70 TocList const & getTocList(Buffer const & buf)
71 {
72         return toc_backend_[&buf].tocs();
73 }
74
75
76 Toc const & getToc(Buffer const & buf, std::string const & type)
77 {
78         return toc_backend_[&buf].toc(type);
79 }
80
81
82 TocIterator const getCurrentTocItem(Buffer const & buf, LCursor const & cur,
83                                                                 std::string const & type)
84 {
85         return toc_backend_[&buf].item(type, ParConstIterator(cur));
86 }
87
88
89 vector<string> const & getTypes(Buffer const & buf)
90 {
91         return toc_backend_[&buf].types();
92 }
93
94
95 void asciiTocList(string const & type, Buffer const & buf, odocstream & os)
96 {
97         toc_backend_[&buf].asciiTocList(type, os);
98 }
99
100 ///////////////////////////////////////////////////////////////////////////
101 // Other functions
102
103 string const getType(string const & cmdName)
104 {
105         // special case
106         if (cmdName == "tableofcontents")
107                 return "TOC";
108         else
109                 return cmdName;
110 }
111
112
113 string const getGuiName(string const & type, Buffer const & buffer)
114 {
115         FloatList const & floats =
116                 buffer.params().getLyXTextClass().floats();
117         if (floats.typeExist(type))
118                 return floats.getType(type).name();
119         else
120                 return type;
121 }
122
123
124 void outline(OutlineOp mode,  LCursor & cur)
125 {
126         recordUndo(cur);
127         Buffer * buf = & cur.buffer();
128         pit_type & pit = cur.pit();
129         ParagraphList & pars = buf->text().paragraphs();
130         ParagraphList::iterator bgn = pars.begin();
131         ParagraphList::iterator s = boost::next(bgn, pit);
132         ParagraphList::iterator p = s;
133         ParagraphList::iterator end = pars.end();
134
135         LyXTextClass::const_iterator lit =
136                 buf->params().getLyXTextClass().begin();
137         LyXTextClass::const_iterator const lend =
138                 buf->params().getLyXTextClass().end();
139
140         int const thistoclevel = s->layout()->toclevel;
141         int toclevel;
142         switch (mode) {
143                 case Up: {
144                         if (p != end)
145                                 ++p;
146                         for (; p != end; ++p) {
147                                 toclevel = p->layout()->toclevel;
148                                 if (toclevel != LyXLayout::NOT_IN_TOC
149                                     && toclevel <= thistoclevel) {
150                                         break;
151                                 }
152                         }
153                         ParagraphList::iterator q = s;
154                         if (q != bgn)
155                                 --q;
156                         else
157                                 break;
158                         for (; q != bgn; --q) {
159                                 toclevel = q->layout()->toclevel;
160                                 if (toclevel != LyXLayout::NOT_IN_TOC
161                                     && toclevel <= thistoclevel) {
162                                         break;
163                                 }
164                         }
165                         pit_type const newpit = std::distance(pars.begin(), q);
166                         pit_type const len = std::distance(s, p);
167                         pit += len;
168                         pars.insert(q, s, p);
169                         s = boost::next(pars.begin(), pit);
170                         ParagraphList::iterator t = boost::next(s, len);
171                         pit = newpit;
172                         pars.erase(s, t);
173                 break;
174                 }
175                 case Down: {
176                            if (p != end)
177                                 ++p;
178                         for (; p != end; ++p) {
179                                 toclevel = p->layout()->toclevel;
180                                 if (toclevel != LyXLayout::NOT_IN_TOC
181                                     && toclevel <= thistoclevel) {
182                                         break;
183                                 }
184                         }
185                         ParagraphList::iterator q = p;
186                         if (q != end)
187                                 ++q;
188                         else
189                                 break;
190                         for (; q != end; ++q) {
191                                 toclevel = q->layout()->toclevel;
192                                 if (toclevel != LyXLayout::NOT_IN_TOC
193                                     && toclevel <= thistoclevel) {
194                                         break;
195                                 }
196                         }
197                         pit_type const newpit = std::distance(pars.begin(), q);
198                         pit_type const len = std::distance(s, p);
199                         pars.insert(q, s, p);
200                         s = boost::next(pars.begin(), pit);
201                         ParagraphList::iterator t = boost::next(s, len);
202                         pit = newpit - len;
203                         pars.erase(s, t);
204                 break;
205                 }
206                 case In:
207                         for (; lit != lend; ++lit) {
208                                 if ((*lit)->toclevel == thistoclevel + 1 &&
209                                     s->layout()->labeltype == (*lit)->labeltype) {
210                                         s->layout((*lit));
211                                         break;
212                                 }
213                         }
214                 break;
215                 case Out:
216                         for (; lit != lend; ++lit) {
217                                 if ((*lit)->toclevel == thistoclevel - 1 &&
218                                     s->layout()->labeltype == (*lit)->labeltype) {
219                                         s->layout((*lit));
220                                         break;
221                                 }
222                         }
223                 break;
224                 default:
225                 break;
226         }
227 }
228
229
230 } // namespace toc
231 } // namespace lyx