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"
20 using namespace lyx::support;
26 bool operator==(Change const & l, Change const & r)
28 return l.type == r.type && l.author == r.author
29 && l.changetime == r.changetime;
33 bool operator!=(Change const & l, Change const & r)
39 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
41 return r1.start == r2.start && r1.end == r2.end;
45 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
51 bool Changes::Range::contains(Range const & r) const
53 return r.start >= start && r.end <= end;
57 bool Changes::Range::contained(Range const & r) const
59 return r.contains(*this);
63 bool Changes::Range::contains(pos_type pos) const
65 return pos >= start && pos < end;
69 bool Changes::Range::loose_contains(pos_type pos) const
71 return pos >= start && pos <= end;
75 bool Changes::Range::intersects(Range const & r) const
77 return contained(r) || contains(r)
78 || contains(r.start) || contains(r.end);
82 Changes::Changes(Change::Type type)
93 Changes::Changes(Changes const & c)
99 void Changes::record(Change change, pos_type pos)
101 if (lyxerr.debugging(Debug::CHANGES)) {
102 lyxerr[Debug::CHANGES] << "record " << change.type
103 << " at pos " << pos << " with total "
104 << table_.size() << " changes." << endl;
107 switch (change.type) {
108 case Change::INSERTED:
111 case Change::DELETED:
114 case Change::UNCHANGED:
115 set(Change::UNCHANGED, pos);
121 void Changes::set(Change change, pos_type pos)
123 set(change, pos, pos + 1);
127 void Changes::set(Change::Type type, pos_type pos)
129 set(type, pos, pos + 1);
133 void Changes::set(Change::Type type, pos_type start, pos_type end)
135 set(Change(type), start, end);
139 void Changes::set(Change change, pos_type start, pos_type end)
141 ChangeTable::iterator it = table_.begin();
143 if (lyxerr.debugging(Debug::CHANGES)) {
144 lyxerr[Debug::CHANGES] << "changeset of " << change.type
145 << " author " << change.author << " time " << change.changetime
146 << " in range " << start << "," << end << endl;
149 Range const new_range(start, end);
151 // remove all sub-ranges
152 for (; it != table_.end();) {
153 if (new_range != it->range && it->range.contained(new_range)) {
154 if (lyxerr.debugging(Debug::CHANGES)) {
155 lyxerr[Debug::CHANGES] << "Removing subrange "
156 << it->range.start << "," << it->range.end << endl;
158 it = table_.erase(it);
165 ChangeTable::iterator itend = table_.end();
167 // find a super-range
168 for (; it != itend; ++it) {
169 if (it->range.contains(new_range))
174 lyxerr[Debug::CHANGES] << "Inserting change at end" << endl;
175 table_.push_back(ChangeRange(start, end, change));
180 if (change.type == it->change.type) {
181 lyxerr[Debug::CHANGES] << "Change set already." << endl;
188 if (lyxerr.debugging(Debug::CHANGES)) {
189 lyxerr[Debug::CHANGES] << "Using change of type " << c.change.type
190 << " over " << c.range.start << "," << c.range.end << endl;
194 if (c.range.start < start) {
195 it = table_.insert(it, ChangeRange(c.range.start, start, c.change));
196 if (lyxerr.debugging(Debug::CHANGES)) {
197 lyxerr[Debug::CHANGES] << "Splitting head of type " << c.change.type
198 << " over " << c.range.start << "," << start << endl;
203 // reset this as new type
204 it->range.start = start;
207 lyxerr[Debug::CHANGES] << "Resetting to new change" << endl;
210 if (c.range.end > end) {
212 table_.insert(it, ChangeRange(end, c.range.end, c.change));
213 if (lyxerr.debugging(Debug::CHANGES)) {
214 lyxerr[Debug::CHANGES] << "Splitting tail of type " << c.change.type
215 << " over " << end << "," << c.range.end << endl;
224 void Changes::erase(pos_type pos)
226 ChangeTable::iterator it = table_.begin();
227 ChangeTable::iterator end = table_.end();
231 for (; it != end; ++it) {
232 Range & range(it->range);
234 if (lyxerr.debugging(Debug::CHANGES)) {
235 lyxerr[Debug::CHANGES] << "era:Range of type " << it->change.type << " is "
236 << it->range.start << "," << it->range.end << endl;
239 if (range.contains(pos)) {
255 void Changes::del(Change change, ChangeTable::size_type pos)
257 // this case happens when building from .lyx
258 if (table_.empty()) {
263 ChangeTable::iterator it = table_.begin();
265 for (; it != table_.end(); ++it) {
266 Range & range(it->range);
268 if (range.contains(pos)) {
269 if (it->change.type != Change::INSERTED) {
275 } else if (range.loose_contains(pos) && it + 1 == table_.end()) {
276 // this case happens when building from .lyx
284 void Changes::add(Change change, ChangeTable::size_type pos)
286 ChangeTable::iterator it = table_.begin();
287 ChangeTable::iterator end = table_.end();
291 for (; it != end; ++it) {
292 Range & range(it->range);
294 if (!found && range.loose_contains(pos)) {
296 if (lyxerr.debugging(Debug::CHANGES)) {
297 lyxerr[Debug::CHANGES] << "Found range of "
298 << range.start << "," << range.end << endl;
313 Change const Changes::lookupFull(pos_type pos) const
315 if (!table_.size()) {
316 if (lyxerr.debugging(Debug::CHANGES))
317 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
318 return Change(empty_type_);
321 ChangeTable::const_iterator it = table_.begin();
322 ChangeTable::const_iterator end = table_.end();
324 for (; it != end; ++it) {
325 if (it->range.contains(pos))
331 return Change(Change::UNCHANGED);
335 Change::Type Changes::lookup(pos_type pos) const
337 if (!table_.size()) {
338 if (lyxerr.debugging(Debug::CHANGES))
339 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
343 ChangeTable::const_iterator it = table_.begin();
344 ChangeTable::const_iterator end = table_.end();
346 for (; it != end; ++it) {
347 if (it->range.contains(pos))
348 return it->change.type;
353 return Change::UNCHANGED;
357 bool Changes::isChange(pos_type start, pos_type end) const
359 if (!table_.size()) {
360 if (lyxerr.debugging(Debug::CHANGES))
361 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
362 return empty_type_ != Change::UNCHANGED;
365 ChangeTable::const_iterator it = table_.begin();
366 ChangeTable::const_iterator itend = table_.end();
368 for (; it != itend; ++it) {
369 if (lyxerr.debugging(Debug::CHANGES)) {
370 lyxerr[Debug::CHANGES] << "Looking for " << start << ","
371 << end << " in " << it->range.start << ","
372 << it->range.end << "of type " << it->change.type << endl;
375 if (it->range.intersects(Range(start, end))
376 && it->change.type != Change::UNCHANGED) {
377 if (lyxerr.debugging(Debug::CHANGES)) {
378 lyxerr[Debug::CHANGES] << "Found intersection of "
379 << start << "," << end << " with "
380 << it->range.start << "," << it->range.end
381 << " of type " << it->change.type << endl;
391 bool Changes::isChangeEdited(lyx::pos_type start, lyx::pos_type end) const
393 if (!table_.size()) {
394 if (lyxerr.debugging(Debug::CHANGES))
395 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
396 return empty_type_ != Change::INSERTED;
399 ChangeTable::const_iterator it = table_.begin();
400 ChangeTable::const_iterator itend = table_.end();
402 for (; it != itend; ++it) {
403 if (it->range.intersects(Range(start, end ? end - 1 : 0))
404 && it->change.type != Change::INSERTED) {
412 void Changes::merge()
414 if (lyxerr.debugging(Debug::CHANGES))
415 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
417 ChangeTable::iterator it = table_.begin();
419 while (it != table_.end()) {
420 if (lyxerr.debugging(Debug::CHANGES)) {
421 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
422 << it->range.start << "," << it->range.end << endl;
425 if (it->range.start == it->range.end) {
426 if (lyxerr.debugging(Debug::CHANGES)) {
427 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
428 << it->range.start << endl;
437 if (it + 1 == table_.end())
440 if (it->change == (it + 1)->change) {
441 if (lyxerr.debugging(Debug::CHANGES)) {
442 lyxerr[Debug::CHANGES] << "Merging equal ranges "
443 << it->range.start << "," << it->range.end
444 << " and " << (it + 1)->range.start << ","
445 << (it + 1)->range.end << endl;
448 (it + 1)->range.start = it->range.start;
458 lyxerr[Debug::CHANGES] << "Merge ended" << endl;
463 void Changes::check() const
465 ChangeTable::const_iterator it = table_.begin();
466 ChangeTable::const_iterator end = table_.end();
468 bool dont_assert(true);
470 lyxerr[Debug::CHANGES] << "Changelist:" << endl;
471 for (; it != end; ++it) {
472 if (lyxerr.debugging(Debug::CHANGES)) {
473 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
474 << it->range.start << "," << it->range.end << " author "
475 << it->change.author << " time " << it->change.changetime << endl;
481 Range const & range(it->range);
482 Range const & next((it + 1)->range);
483 if (range.end != next.start)
487 if (lyxerr.debugging(Debug::CHANGES))
488 lyxerr[Debug::CHANGES] << "End" << endl;
494 int Changes::latexMarkChange(std::ostream & os, Change::Type old, Change::Type change)
499 string const start("\\changestart{}");
500 string const end("\\changeend{}");
501 string const son("\\overstrikeon{}");
502 string const soff("\\overstrikeoff{}");
506 if (old == Change::DELETED) {
508 column += soff.length();
512 case Change::UNCHANGED:
514 column += end.length();
517 case Change::DELETED:
518 if (old == Change::UNCHANGED) {
520 column += start.length();
523 column += son.length();
526 case Change::INSERTED:
527 if (old == Change::UNCHANGED) {
529 column += start.length();
538 void Changes::lyxMarkChange(std::ostream & os, int & column, lyx::time_type curtime,
539 Change const & old, Change const & change)
546 switch (change.type) {
547 case Change::UNCHANGED:
548 os << "\n\\change_unchanged\n";
551 case Change::DELETED: {
552 lyx::time_type t(change.changetime);
555 os << "\n\\change_deleted " << change.author
561 case Change::INSERTED:
562 lyx::time_type t(change.changetime);
565 os << "\n\\change_inserted " << change.author