3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * Record changes in a paragraph.
8 * \author John Levon <levon@movementarian.org>
17 #include "support/LAssert.h"
18 #include "support/LOstream.h"
24 bool operator==(Change const & l, Change const & r)
26 return l.type == r.type && l.author == r.author
27 && l.changetime == r.changetime;
31 bool operator!=(Change const & l, Change const & r)
37 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
39 return r1.start == r2.start && r1.end == r2.end;
43 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
49 bool Changes::Range::contains(Range const & r) const
51 return r.start >= start && r.end <= end;
55 bool Changes::Range::contained(Range const & r) const
57 return r.contains(*this);
61 bool Changes::Range::contains(pos_type pos) const
63 return pos >= start && pos < end;
67 bool Changes::Range::loose_contains(pos_type pos) const
69 return pos >= start && pos <= end;
73 bool Changes::Range::intersects(Range const & r) const
75 return contained(r) || contains(r)
76 || contains(r.start) || contains(r.end);
80 Changes::Changes(Change::Type type)
91 Changes::Changes(Changes const & c)
97 void Changes::record(Change change, pos_type pos)
99 lyxerr[Debug::CHANGES] << "record " << change.type
100 << " at pos " << pos << " with total "
101 << table_.size() << " changes." << endl;
103 switch (change.type) {
104 case Change::INSERTED:
107 case Change::DELETED:
110 case Change::UNCHANGED:
111 set(Change::UNCHANGED, pos);
117 void Changes::set(Change change, pos_type pos)
119 set(change, pos, pos + 1);
123 void Changes::set(Change::Type type, pos_type pos)
125 set(type, pos, pos + 1);
129 void Changes::set(Change::Type type, pos_type start, pos_type end)
131 set(Change(type), start, end);
135 void Changes::set(Change change, pos_type start, pos_type end)
137 ChangeTable::iterator it = table_.begin();
139 lyxerr[Debug::CHANGES] << "changeset of " << change.type
140 << " author " << change.author << " time " << change.changetime
141 << " in range " << start << "," << end << endl;
143 Range const new_range(start, end);
145 // remove all sub-ranges
146 for (; it != table_.end();) {
147 if (new_range != it->range && it->range.contained(new_range)) {
148 lyxerr[Debug::CHANGES] << "Removing subrange "
149 << it->range.start << "," << it->range.end << endl;
150 it = table_.erase(it);
157 ChangeTable::iterator itend = table_.end();
159 // find a super-range
160 for (; it != itend; ++it) {
161 if (it->range.contains(new_range))
166 lyxerr[Debug::CHANGES] << "Inserting change at end" << endl;
167 table_.push_back(ChangeRange(start, end, change));
172 if (change.type == it->change.type) {
173 lyxerr[Debug::CHANGES] << "Change set already." << endl;
180 lyxerr[Debug::CHANGES] << "Using change of type " << c.change.type
181 << " over " << c.range.start << "," << c.range.end << endl;
184 if (c.range.start < start) {
185 it = table_.insert(it, ChangeRange(c.range.start, start, c.change));
186 lyxerr[Debug::CHANGES] << "Splitting head of type " << c.change.type
187 << " over " << c.range.start << "," << start << endl;
191 // reset this as new type
192 it->range.start = start;
195 lyxerr[Debug::CHANGES] << "Resetting to new change" << endl;
198 if (c.range.end > end) {
200 table_.insert(it, ChangeRange(end, c.range.end, c.change));
201 lyxerr[Debug::CHANGES] << "Splitting tail of type " << c.change.type
202 << " over " << end << "," << c.range.end << endl;
210 void Changes::erase(pos_type pos)
212 ChangeTable::iterator it = table_.begin();
213 ChangeTable::iterator end = table_.end();
217 for (; it != end; ++it) {
218 Range & range(it->range);
220 lyxerr[Debug::CHANGES] << "era:Range of type " << it->change.type << " is "
221 << it->range.start << "," << it->range.end << endl;
223 if (range.contains(pos)) {
239 void Changes::del(Change change, ChangeTable::size_type pos)
241 // this case happens when building from .lyx
242 if (table_.empty()) {
247 ChangeTable::iterator it = table_.begin();
249 for (; it != table_.end(); ++it) {
250 Range & range(it->range);
252 if (range.contains(pos)) {
253 if (it->change.type != Change::INSERTED) {
259 } else if (range.loose_contains(pos) && it + 1 == table_.end()) {
260 // this case happens when building from .lyx
268 void Changes::add(Change change, ChangeTable::size_type pos)
270 ChangeTable::iterator it = table_.begin();
271 ChangeTable::iterator end = table_.end();
275 for (; it != end; ++it) {
276 Range & range(it->range);
278 if (!found && range.loose_contains(pos)) {
280 lyxerr[Debug::CHANGES] << "Found range of "
281 << range.start << "," << range.end << endl;
295 Change const Changes::lookupFull(pos_type pos) const
297 if (!table_.size()) {
298 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
299 return Change(empty_type_);
302 ChangeTable::const_iterator it = table_.begin();
303 ChangeTable::const_iterator end = table_.end();
305 for (; it != end; ++it) {
306 if (it->range.contains(pos))
312 return Change(Change::UNCHANGED);
316 Change::Type Changes::lookup(pos_type pos) const
318 if (!table_.size()) {
319 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
323 ChangeTable::const_iterator it = table_.begin();
324 ChangeTable::const_iterator end = table_.end();
326 for (; it != end; ++it) {
327 if (it->range.contains(pos))
328 return it->change.type;
333 return Change::UNCHANGED;
337 bool Changes::isChange(pos_type start, pos_type end) const
339 if (!table_.size()) {
340 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
341 return empty_type_ != Change::UNCHANGED;
344 ChangeTable::const_iterator it = table_.begin();
345 ChangeTable::const_iterator itend = table_.end();
347 for (; it != itend; ++it) {
348 lyxerr[Debug::CHANGES] << "Looking for " << start << ","
349 << end << " in " << it->range.start << ","
350 << it->range.end << "of type " << it->change.type << endl;
351 if (it->range.intersects(Range(start, end))
352 && it->change.type != Change::UNCHANGED) {
353 lyxerr[Debug::CHANGES] << "Found intersection of "
354 << start << "," << end << " with "
355 << it->range.start << "," << it->range.end
356 << " of type " << it->change.type << endl;
365 bool Changes::isChangeEdited(lyx::pos_type start, lyx::pos_type end) const
367 if (!table_.size()) {
368 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
369 return empty_type_ != Change::INSERTED;
372 ChangeTable::const_iterator it = table_.begin();
373 ChangeTable::const_iterator itend = table_.end();
375 for (; it != itend; ++it) {
376 if (it->range.intersects(Range(start, end ? end - 1 : 0))
377 && it->change.type != Change::INSERTED) {
385 void Changes::merge()
387 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
388 ChangeTable::iterator it = table_.begin();
390 while (it != table_.end()) {
391 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
392 << it->range.start << "," << it->range.end << endl;
394 if (it->range.start == it->range.end) {
395 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
396 << it->range.start << endl;
403 if (it + 1 == table_.end())
406 if (it->change == (it + 1)->change) {
407 lyxerr[Debug::CHANGES] << "Merging equal ranges "
408 << it->range.start << "," << it->range.end
409 << " and " << (it + 1)->range.start << ","
410 << (it + 1)->range.end << endl;
411 (it + 1)->range.start = it->range.start;
421 lyxerr[Debug::CHANGES] << "Merge ended" << endl;
426 void Changes::check() const
428 ChangeTable::const_iterator it = table_.begin();
429 ChangeTable::const_iterator end = table_.end();
431 bool dont_assert(true);
433 lyxerr[Debug::CHANGES] << "Changelist:" << endl;
434 for (; it != end; ++it) {
435 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
436 << it->range.start << "," << it->range.end << " author "
437 << it->change.author << " time " << it->change.changetime << endl;
441 Range const & range(it->range);
442 Range const & next((it + 1)->range);
443 if (range.end != next.start)
446 lyxerr[Debug::CHANGES] << "End" << endl;
447 lyx::Assert(dont_assert);
451 int Changes::latexMarkChange(std::ostream & os, Change::Type old, Change::Type change)
456 string const start("\\changestart{}");
457 string const end("\\changeend{}");
458 string const son("\\overstrikeon{}");
459 string const soff("\\overstrikeoff{}");
463 if (old == Change::DELETED) {
465 column += soff.length();
469 case Change::UNCHANGED:
471 column += end.length();
474 case Change::DELETED:
475 if (old == Change::UNCHANGED) {
477 column += start.length();
480 column += son.length();
483 case Change::INSERTED:
484 if (old == Change::UNCHANGED) {
486 column += start.length();
495 void Changes::lyxMarkChange(std::ostream & os, int & column, lyx::time_type curtime,
496 Change const & old, Change const & change)
503 switch (change.type) {
504 case Change::UNCHANGED:
505 os << "\n\\change_unchanged\n";
508 case Change::DELETED: {
509 lyx::time_type t(change.changetime);
512 os << "\n\\change_deleted " << change.author
518 case Change::INSERTED:
519 lyx::time_type t(change.changetime);
522 os << "\n\\change_inserted " << change.author