]> git.lyx.org Git - lyx.git/blob - src/InsetList.cpp
Disable CheckTeX while buffer is processed
[lyx.git] / src / InsetList.cpp
1 /**
2  * \file InsetList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Martin Vermeer
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetList.h"
15
16 #include "insets/Inset.h"
17
18 #include "support/debug.h"
19
20 #include <algorithm>
21
22 using namespace std;
23
24 namespace lyx {
25
26
27 namespace {
28
29 typedef InsetList::InsetTable Table;
30
31 struct InsetTablePosLess
32 {
33         bool operator()(Table const & t1, Table const & t2) const
34         {
35                 return t1.pos < t2.pos;
36         }
37 };
38
39 } // namespace
40
41
42 InsetList::InsetList(InsetList const & il) : list_(il.list_)
43 {
44         List::iterator it = list_.begin();
45         List::iterator end = list_.end();
46         for (; it != end; ++it)
47                 it->inset = it->inset->clone();
48 }
49
50
51 InsetList::InsetList(InsetList const & il, pos_type beg, pos_type end)
52 {
53         InsetList::const_iterator cit = il.begin();
54         InsetList::const_iterator cend = il.end();
55         for (; cit != cend; ++cit) {
56                 if (cit->pos < beg)
57                         continue;
58                 if (cit->pos >= end)
59                         break;
60                 // Add a new entry in the insetlist_.
61                 insert(cit->inset->clone(), cit->pos - beg);
62         }
63 }
64
65
66 InsetList::~InsetList()
67 {
68         List::iterator it = list_.begin();
69         List::iterator end = list_.end();
70         for (; it != end; ++it)
71                 delete it->inset;
72 }
73
74
75 void InsetList::setBuffer(Buffer & b)
76 {
77         List::iterator it = list_.begin();
78         List::iterator end = list_.end();
79         for (; it != end; ++it)
80                 it->inset->setBuffer(b);
81 }
82
83
84 void InsetList::resetBuffer()
85 {
86         List::iterator it = list_.begin();
87         List::iterator end = list_.end();
88         for (; it != end; ++it)
89                 it->inset->resetBuffer();
90 }
91
92
93 InsetList::iterator InsetList::insetIterator(pos_type pos)
94 {
95         InsetTable search_elem(pos, 0);
96         return lower_bound(list_.begin(), list_.end(), search_elem,
97                            InsetTablePosLess());
98 }
99
100
101 InsetList::const_iterator InsetList::insetIterator(pos_type pos) const
102 {
103         InsetTable search_elem(pos, 0);
104         return lower_bound(list_.begin(), list_.end(), search_elem,
105                            InsetTablePosLess());
106 }
107
108
109 void InsetList::insert(Inset * inset, pos_type pos)
110 {
111         List::iterator end = list_.end();
112         List::iterator it = insetIterator(pos);
113         if (it != end && it->pos == pos) {
114                 LYXERR0("ERROR (InsetList::insert): "
115                        << "There is an inset in position: " << pos);
116         } else {
117                 list_.insert(it, InsetTable(pos, inset));
118         }
119 }
120
121
122 void InsetList::erase(pos_type pos)
123 {
124         List::iterator end = list_.end();
125         List::iterator it = insetIterator(pos);
126         if (it != end && it->pos == pos) {
127                 delete it->inset;
128                 list_.erase(it);
129         }
130 }
131
132
133 Inset * InsetList::release(pos_type pos)
134 {
135         List::iterator end = list_.end();
136         List::iterator it = insetIterator(pos);
137         if (it != end && it->pos == pos) {
138                 Inset * tmp = it->inset;
139                 it->inset = 0;
140                 return tmp;
141         }
142         return 0;
143 }
144
145
146 Inset * InsetList::get(pos_type pos) const
147 {
148         List::const_iterator end = list_.end();
149         List::const_iterator it = insetIterator(pos);
150         if (it != end && it->pos == pos)
151                 return it->inset;
152         return 0;
153 }
154
155
156 void InsetList::increasePosAfterPos(pos_type pos)
157 {
158         List::iterator end = list_.end();
159         List::iterator it = insetIterator(pos);
160         for (; it != end; ++it)
161                 ++it->pos;
162 }
163
164
165 void InsetList::decreasePosAfterPos(pos_type pos)
166 {
167         List::iterator end = list_.end();
168         List::iterator it = insetIterator(pos);
169         for (; it != end; ++it)
170                 --it->pos;
171 }
172
173
174 pos_type InsetList::find(InsetCode code, pos_type startpos) const
175 {
176         List::const_iterator it = insetIterator(startpos);
177         List::const_iterator end = list_.end();
178         for (; it != end ; ++it) {
179                 if (it->inset->lyxCode() == code)
180                         return it->pos;
181         }
182         return -1;
183 }
184
185
186 int InsetList::count(InsetCode code, pos_type startpos) const
187 {
188         int num = 0;
189         List::const_iterator it = insetIterator(startpos);
190         List::const_iterator end = list_.end();
191         for (; it != end ; ++it) {
192                 if (it->inset->lyxCode() == code)
193                         ++num;
194         }
195         return num;
196 }
197
198 } // namespace lyx