3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
11 * Record changes in a paragraph.
19 #include <boost/assert.hpp>
28 bool operator==(Change const & l, Change const & r)
30 return l.type == r.type && l.author == r.author
31 && l.changetime == r.changetime;
35 bool operator!=(Change const & l, Change const & r)
41 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
43 return r1.start == r2.start && r1.end == r2.end;
47 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
53 bool Changes::Range::contains(Range const & r) const
55 return r.start >= start && r.end <= end;
59 bool Changes::Range::contains(pos_type const pos) const
61 return pos >= start && pos < end;
65 bool Changes::Range::intersects(Range const & r) const
67 return r.start < end && r.end > start; // end itself is not in the range!
71 void Changes::set(Change const & change, pos_type const pos)
73 set(change, pos, pos + 1);
77 void Changes::set(Change const & change, pos_type const start, pos_type const end)
79 if (lyxerr.debugging(Debug::CHANGES)) {
80 lyxerr[Debug::CHANGES] << "setting change (type: " << change.type
81 << ", author: " << change.author << ", time: " << change.changetime
82 << ") in range (" << start << ", " << end << ")" << endl;
85 Range const newRange(start, end);
87 ChangeTable::iterator it = table_.begin();
89 for (; it != table_.end(); ) {
90 // find super change, check for equal types, and do nothing
91 if (it->range.contains(newRange) && it->change.type == change.type) {
95 // current change starts like or follows new change
96 if (it->range.start >= start) {
100 // new change intersects with existing change
101 if (it->range.end > start) {
102 pos_type oldEnd = it->range.end;
103 it->range.end = start;
104 if (lyxerr.debugging(Debug::CHANGES)) {
105 lyxerr[Debug::CHANGES] << " cutting tail of type " << it->change.type
106 << " resulting in range (" << it->range.start << ", "
107 << it->range.end << ")" << endl;
111 if (lyxerr.debugging(Debug::CHANGES)) {
112 lyxerr[Debug::CHANGES] << " inserting tail in range ("
113 << end << ", " << oldEnd << ")" << endl;
115 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
123 if (change.type != Change::UNCHANGED) {
124 if (lyxerr.debugging(Debug::CHANGES)) {
125 lyxerr[Debug::CHANGES] << " inserting change" << endl;
127 it = table_.insert(it, ChangeRange(change, Range(start, end)));
131 for (; it != table_.end(); ) {
132 // new change 'contains' existing change
133 if (newRange.contains(it->range)) {
134 if (lyxerr.debugging(Debug::CHANGES)) {
135 lyxerr[Debug::CHANGES] << " removing subrange ("
136 << it->range.start << ", " << it->range.end << ")" << endl;
138 it = table_.erase(it);
142 // new change precedes existing change
143 if (it->range.start >= end) {
147 // new change intersects with existing change
148 it->range.start = end;
149 if (lyxerr.debugging(Debug::CHANGES)) {
150 lyxerr[Debug::CHANGES] << " cutting head of type "
151 << it->change.type << " resulting in range ("
152 << end << ", " << it->range.end << ")" << endl;
154 break; // no need for another iteration
161 void Changes::erase(pos_type const pos)
163 if (lyxerr.debugging(Debug::CHANGES)) {
164 lyxerr[Debug::CHANGES] << "Erasing change at position " << pos << endl;
167 ChangeTable::iterator it = table_.begin();
168 ChangeTable::iterator end = table_.end();
170 for (; it != end; ++it) {
171 // range (pos,pos+x) becomes (pos,pos+x-1)
172 if (it->range.start > pos) {
175 // range (pos-x,pos) stays (pos-x,pos)
176 if (it->range.end > pos) {
185 void Changes::insert(Change const & change, lyx::pos_type pos)
187 if (lyxerr.debugging(Debug::CHANGES)) {
188 lyxerr[Debug::CHANGES] << "Inserting change of type " << change.type
189 << " at position " << pos << endl;
192 ChangeTable::iterator it = table_.begin();
193 ChangeTable::iterator end = table_.end();
195 for (; it != end; ++it) {
196 // range (pos,pos+x) becomes (pos+1,pos+x+1)
197 if (it->range.start >= pos) {
201 // range (pos-x,pos) stays as it is
202 if (it->range.end > pos) {
207 set(change, pos, pos + 1); // set will call merge
211 Change const Changes::lookup(pos_type const pos) const
213 if (table_.empty()) {
214 return Change(Change::UNCHANGED);
216 ChangeTable::const_iterator it = table_.begin();
217 ChangeTable::const_iterator const end = table_.end();
219 for (; it != end; ++it) {
220 if (it->range.contains(pos))
224 return Change(Change::UNCHANGED);
228 bool Changes::isChanged(pos_type const start, pos_type const end) const
230 ChangeTable::const_iterator it = table_.begin();
231 ChangeTable::const_iterator const itend = table_.end();
233 for (; it != itend; ++it) {
234 if (it->range.intersects(Range(start, end))) {
235 if (lyxerr.debugging(Debug::CHANGES)) {
236 lyxerr[Debug::CHANGES] << "found intersection of range ("
237 << start << ", " << end << ") with ("
238 << it->range.start << ", " << it->range.end
239 << ") of type " << it->change.type << endl;
248 void Changes::merge()
250 if (lyxerr.debugging(Debug::CHANGES)) {
251 lyxerr[Debug::CHANGES] << "merging changes..." << endl;
254 ChangeTable::iterator it = table_.begin();
256 while (it != table_.end()) {
257 if (lyxerr.debugging(Debug::CHANGES)) {
258 lyxerr[Debug::CHANGES] << " found change of type " << it->change.type
259 << " and range (" << it->range.start << ", " << it->range.end
263 if (it->range.start == it->range.end) {
264 if (lyxerr.debugging(Debug::CHANGES)) {
265 lyxerr[Debug::CHANGES] << " removing empty range for pos "
266 << it->range.start << endl;
275 if (it + 1 == table_.end())
278 if (it->change == (it + 1)->change && it->range.end == (it + 1)->range.start) {
279 if (lyxerr.debugging(Debug::CHANGES)) {
280 lyxerr[Debug::CHANGES] << " merging ranges (" << it->range.start << ", "
281 << it->range.end << ") and (" << (it + 1)->range.start << ", "
282 << (it + 1)->range.end << ")" << endl;
284 (it + 1)->range.start = it->range.start;
296 int Changes::latexMarkChange(odocstream & os,
297 Change::Type const old, Change::Type const change,
300 // FIXME: change tracking (MG)
301 if (!output || old == change)
304 static docstring const start(from_ascii("\\changestart{}"));
305 static docstring const end(from_ascii("\\changeend{}"));
306 static docstring const son(from_ascii("\\overstrikeon{}"));
307 static docstring const soff(from_ascii("\\overstrikeoff{}"));
311 if (old == Change::DELETED) {
313 column += soff.length();
317 case Change::UNCHANGED:
319 column += end.length();
322 case Change::DELETED:
323 if (old == Change::UNCHANGED) {
325 column += start.length();
328 column += son.length();
331 case Change::INSERTED:
332 if (old == Change::UNCHANGED) {
334 column += start.length();
343 void Changes::lyxMarkChange(std::ostream & os, int & column,
344 time_type const curtime,
345 Change const & old, Change const & change)
347 // FIXME: change tracking (MG)
353 switch (change.type) {
354 case Change::UNCHANGED:
355 os << "\n\\change_unchanged\n";
358 case Change::DELETED: {
359 time_type t = change.changetime;
362 os << "\n\\change_deleted " << change.author
368 case Change::INSERTED: {
369 time_type t = change.changetime;
372 os << "\n\\change_inserted " << change.author