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 if (lyxerr.debugging(Debug::CHANGES)) {
100 lyxerr[Debug::CHANGES] << "record " << change.type
101 << " at pos " << pos << " with total "
102 << table_.size() << " changes." << endl;
105 switch (change.type) {
106 case Change::INSERTED:
109 case Change::DELETED:
112 case Change::UNCHANGED:
113 set(Change::UNCHANGED, pos);
119 void Changes::set(Change change, pos_type pos)
121 set(change, pos, pos + 1);
125 void Changes::set(Change::Type type, pos_type pos)
127 set(type, pos, pos + 1);
131 void Changes::set(Change::Type type, pos_type start, pos_type end)
133 set(Change(type), start, end);
137 void Changes::set(Change change, pos_type start, pos_type end)
139 ChangeTable::iterator it = table_.begin();
141 if (lyxerr.debugging(Debug::CHANGES)) {
142 lyxerr[Debug::CHANGES] << "changeset of " << change.type
143 << " author " << change.author << " time " << change.changetime
144 << " in range " << start << "," << end << endl;
147 Range const new_range(start, end);
149 // remove all sub-ranges
150 for (; it != table_.end();) {
151 if (new_range != it->range && it->range.contained(new_range)) {
152 if (lyxerr.debugging(Debug::CHANGES)) {
153 lyxerr[Debug::CHANGES] << "Removing subrange "
154 << it->range.start << "," << it->range.end << endl;
156 it = table_.erase(it);
163 ChangeTable::iterator itend = table_.end();
165 // find a super-range
166 for (; it != itend; ++it) {
167 if (it->range.contains(new_range))
172 lyxerr[Debug::CHANGES] << "Inserting change at end" << endl;
173 table_.push_back(ChangeRange(start, end, change));
178 if (change.type == it->change.type) {
179 lyxerr[Debug::CHANGES] << "Change set already." << endl;
186 if (lyxerr.debugging(Debug::CHANGES)) {
187 lyxerr[Debug::CHANGES] << "Using change of type " << c.change.type
188 << " over " << c.range.start << "," << c.range.end << endl;
192 if (c.range.start < start) {
193 it = table_.insert(it, ChangeRange(c.range.start, start, c.change));
194 if (lyxerr.debugging(Debug::CHANGES)) {
195 lyxerr[Debug::CHANGES] << "Splitting head of type " << c.change.type
196 << " over " << c.range.start << "," << start << endl;
201 // reset this as new type
202 it->range.start = start;
205 lyxerr[Debug::CHANGES] << "Resetting to new change" << endl;
208 if (c.range.end > end) {
210 table_.insert(it, ChangeRange(end, c.range.end, c.change));
211 if (lyxerr.debugging(Debug::CHANGES)) {
212 lyxerr[Debug::CHANGES] << "Splitting tail of type " << c.change.type
213 << " over " << end << "," << c.range.end << endl;
222 void Changes::erase(pos_type pos)
224 ChangeTable::iterator it = table_.begin();
225 ChangeTable::iterator end = table_.end();
229 for (; it != end; ++it) {
230 Range & range(it->range);
232 if (lyxerr.debugging(Debug::CHANGES)) {
233 lyxerr[Debug::CHANGES] << "era:Range of type " << it->change.type << " is "
234 << it->range.start << "," << it->range.end << endl;
237 if (range.contains(pos)) {
253 void Changes::del(Change change, ChangeTable::size_type pos)
255 // this case happens when building from .lyx
256 if (table_.empty()) {
261 ChangeTable::iterator it = table_.begin();
263 for (; it != table_.end(); ++it) {
264 Range & range(it->range);
266 if (range.contains(pos)) {
267 if (it->change.type != Change::INSERTED) {
273 } else if (range.loose_contains(pos) && it + 1 == table_.end()) {
274 // this case happens when building from .lyx
282 void Changes::add(Change change, ChangeTable::size_type pos)
284 ChangeTable::iterator it = table_.begin();
285 ChangeTable::iterator end = table_.end();
289 for (; it != end; ++it) {
290 Range & range(it->range);
292 if (!found && range.loose_contains(pos)) {
294 if (lyxerr.debugging(Debug::CHANGES)) {
295 lyxerr[Debug::CHANGES] << "Found range of "
296 << range.start << "," << range.end << endl;
311 Change const Changes::lookupFull(pos_type pos) const
313 if (!table_.size()) {
314 if (lyxerr.debugging(Debug::CHANGES))
315 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
316 return Change(empty_type_);
319 ChangeTable::const_iterator it = table_.begin();
320 ChangeTable::const_iterator end = table_.end();
322 for (; it != end; ++it) {
323 if (it->range.contains(pos))
329 return Change(Change::UNCHANGED);
333 Change::Type Changes::lookup(pos_type pos) const
335 if (!table_.size()) {
336 if (lyxerr.debugging(Debug::CHANGES))
337 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
341 ChangeTable::const_iterator it = table_.begin();
342 ChangeTable::const_iterator end = table_.end();
344 for (; it != end; ++it) {
345 if (it->range.contains(pos))
346 return it->change.type;
351 return Change::UNCHANGED;
355 bool Changes::isChange(pos_type start, pos_type end) const
357 if (!table_.size()) {
358 if (lyxerr.debugging(Debug::CHANGES))
359 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
360 return empty_type_ != Change::UNCHANGED;
363 ChangeTable::const_iterator it = table_.begin();
364 ChangeTable::const_iterator itend = table_.end();
366 for (; it != itend; ++it) {
367 if (lyxerr.debugging(Debug::CHANGES)) {
368 lyxerr[Debug::CHANGES] << "Looking for " << start << ","
369 << end << " in " << it->range.start << ","
370 << it->range.end << "of type " << it->change.type << endl;
373 if (it->range.intersects(Range(start, end))
374 && it->change.type != Change::UNCHANGED) {
375 if (lyxerr.debugging(Debug::CHANGES)) {
376 lyxerr[Debug::CHANGES] << "Found intersection of "
377 << start << "," << end << " with "
378 << it->range.start << "," << it->range.end
379 << " of type " << it->change.type << endl;
389 bool Changes::isChangeEdited(lyx::pos_type start, lyx::pos_type end) const
391 if (!table_.size()) {
392 if (lyxerr.debugging(Debug::CHANGES))
393 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
394 return empty_type_ != Change::INSERTED;
397 ChangeTable::const_iterator it = table_.begin();
398 ChangeTable::const_iterator itend = table_.end();
400 for (; it != itend; ++it) {
401 if (it->range.intersects(Range(start, end ? end - 1 : 0))
402 && it->change.type != Change::INSERTED) {
410 void Changes::merge()
412 if (lyxerr.debugging(Debug::CHANGES))
413 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
415 ChangeTable::iterator it = table_.begin();
417 while (it != table_.end()) {
418 if (lyxerr.debugging(Debug::CHANGES)) {
419 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
420 << it->range.start << "," << it->range.end << endl;
423 if (it->range.start == it->range.end) {
424 if (lyxerr.debugging(Debug::CHANGES)) {
425 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
426 << it->range.start << endl;
435 if (it + 1 == table_.end())
438 if (it->change == (it + 1)->change) {
439 if (lyxerr.debugging(Debug::CHANGES)) {
440 lyxerr[Debug::CHANGES] << "Merging equal ranges "
441 << it->range.start << "," << it->range.end
442 << " and " << (it + 1)->range.start << ","
443 << (it + 1)->range.end << endl;
446 (it + 1)->range.start = it->range.start;
456 lyxerr[Debug::CHANGES] << "Merge ended" << endl;
461 void Changes::check() const
463 ChangeTable::const_iterator it = table_.begin();
464 ChangeTable::const_iterator end = table_.end();
466 bool dont_assert(true);
468 lyxerr[Debug::CHANGES] << "Changelist:" << endl;
469 for (; it != end; ++it) {
470 if (lyxerr.debugging(Debug::CHANGES)) {
471 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
472 << it->range.start << "," << it->range.end << " author "
473 << it->change.author << " time " << it->change.changetime << endl;
479 Range const & range(it->range);
480 Range const & next((it + 1)->range);
481 if (range.end != next.start)
485 if (lyxerr.debugging(Debug::CHANGES))
486 lyxerr[Debug::CHANGES] << "End" << endl;
488 lyx::Assert(dont_assert);
492 int Changes::latexMarkChange(std::ostream & os, Change::Type old, Change::Type change)
497 string const start("\\changestart{}");
498 string const end("\\changeend{}");
499 string const son("\\overstrikeon{}");
500 string const soff("\\overstrikeoff{}");
504 if (old == Change::DELETED) {
506 column += soff.length();
510 case Change::UNCHANGED:
512 column += end.length();
515 case Change::DELETED:
516 if (old == Change::UNCHANGED) {
518 column += start.length();
521 column += son.length();
524 case Change::INSERTED:
525 if (old == Change::UNCHANGED) {
527 column += start.length();
536 void Changes::lyxMarkChange(std::ostream & os, int & column, lyx::time_type curtime,
537 Change const & old, Change const & change)
544 switch (change.type) {
545 case Change::UNCHANGED:
546 os << "\n\\change_unchanged\n";
549 case Change::DELETED: {
550 lyx::time_type t(change.changetime);
553 os << "\n\\change_deleted " << change.author
559 case Change::INSERTED:
560 lyx::time_type t(change.changetime);
563 os << "\n\\change_inserted " << change.author