3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
10 * Record changes in a paragraph.
18 #include <boost/assert.hpp>
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 const pos) const
65 return pos >= start && pos < end;
69 bool Changes::Range::containsOrPrecedes(pos_type const 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 const type)
93 Changes::Changes(Changes const & c)
99 void Changes::record(Change const change, pos_type const 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 const change, pos_type const pos)
123 set(change, pos, pos + 1);
127 void Changes::set(Change::Type const type, pos_type const pos)
129 set(type, pos, pos + 1);
133 void Changes::set(Change::Type const type,
134 pos_type const start, pos_type const end)
136 set(Change(type), start, end);
140 void Changes::set(Change const change,
141 pos_type const start, pos_type const end)
143 ChangeTable::iterator it = table_.begin();
145 if (lyxerr.debugging(Debug::CHANGES)) {
146 lyxerr[Debug::CHANGES] << "changeset of " << change.type
147 << " author " << change.author << " time " << change.changetime
148 << " in range " << start << "," << end << endl;
151 Range const new_range(start, end);
153 // remove all sub-ranges
154 for (; it != table_.end();) {
155 if (new_range != it->range && it->range.contained(new_range)) {
156 if (lyxerr.debugging(Debug::CHANGES)) {
157 lyxerr[Debug::CHANGES] << "Removing subrange "
158 << it->range.start << "," << it->range.end << endl;
160 it = table_.erase(it);
167 ChangeTable::iterator const itend = table_.end();
169 // find a super-range
170 for (; it != itend; ++it) {
171 if (it->range.contains(new_range))
176 lyxerr[Debug::CHANGES] << "Inserting change at end" << endl;
177 table_.push_back(ChangeRange(start, end, change));
182 if (change.type == it->change.type) {
183 lyxerr[Debug::CHANGES] << "Change set already." << endl;
190 if (lyxerr.debugging(Debug::CHANGES)) {
191 lyxerr[Debug::CHANGES] << "Using change of type " << c.change.type
192 << " over " << c.range.start << "," << c.range.end << endl;
196 if (c.range.start < start) {
197 it = table_.insert(it, ChangeRange(c.range.start, start, c.change));
198 if (lyxerr.debugging(Debug::CHANGES)) {
199 lyxerr[Debug::CHANGES] << "Splitting head of type " << c.change.type
200 << " over " << c.range.start << "," << start << endl;
205 // reset this as new type
206 it->range.start = start;
209 lyxerr[Debug::CHANGES] << "Resetting to new change" << endl;
212 if (c.range.end > end) {
214 table_.insert(it, ChangeRange(end, c.range.end, c.change));
215 if (lyxerr.debugging(Debug::CHANGES)) {
216 lyxerr[Debug::CHANGES] << "Splitting tail of type " << c.change.type
217 << " over " << end << "," << c.range.end << endl;
226 void Changes::erase(pos_type const pos)
228 ChangeTable::iterator it = table_.begin();
229 ChangeTable::iterator end = table_.end();
233 for (; it != end; ++it) {
234 Range & range(it->range);
236 if (lyxerr.debugging(Debug::CHANGES)) {
237 lyxerr[Debug::CHANGES] << "era:Range of type " << it->change.type << " is "
238 << it->range.start << "," << it->range.end << endl;
241 if (range.contains(pos)) {
257 void Changes::del(Change const change, ChangeTable::size_type const pos)
259 // this case happens when building from .lyx
260 if (table_.empty()) {
265 ChangeTable::iterator it = table_.begin();
267 for (; it != table_.end(); ++it) {
268 Range & range(it->range);
270 if (range.contains(pos)) {
271 if (it->change.type != Change::INSERTED) {
277 } else if (range.containsOrPrecedes(pos) && it + 1 == table_.end()) {
278 // this case happens when building from .lyx
286 void Changes::add(Change const change, ChangeTable::size_type const pos)
288 ChangeTable::iterator it = table_.begin();
289 ChangeTable::iterator end = table_.end();
293 for (; it != end; ++it) {
294 Range & range(it->range);
296 if (!found && range.containsOrPrecedes(pos)) {
298 if (lyxerr.debugging(Debug::CHANGES)) {
299 lyxerr[Debug::CHANGES] << "Found range of "
300 << range.start << "," << range.end << endl;
315 Change const Changes::lookup(pos_type const pos) const
317 if (!table_.size()) {
318 if (lyxerr.debugging(Debug::CHANGES))
319 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
320 return Change(empty_type_);
323 ChangeTable::const_iterator it = table_.begin();
324 ChangeTable::const_iterator const end = table_.end();
326 for (; it != end; ++it) {
327 if (it->range.contains(pos))
332 BOOST_ASSERT(false && "missing changes for pos");
333 return Change(Change::UNCHANGED);
337 bool Changes::isChange(pos_type const start, pos_type const end) const
339 if (!table_.size()) {
340 if (lyxerr.debugging(Debug::CHANGES))
341 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
342 return empty_type_ != Change::UNCHANGED;
345 ChangeTable::const_iterator it = table_.begin();
346 ChangeTable::const_iterator const itend = table_.end();
348 for (; it != itend; ++it) {
349 if (lyxerr.debugging(Debug::CHANGES)) {
350 lyxerr[Debug::CHANGES] << "Looking for " << start << ","
351 << end << " in " << it->range.start << ","
352 << it->range.end << "of type " << it->change.type << endl;
355 if (it->range.intersects(Range(start, end))
356 && it->change.type != Change::UNCHANGED) {
357 if (lyxerr.debugging(Debug::CHANGES)) {
358 lyxerr[Debug::CHANGES] << "Found intersection of "
359 << start << "," << end << " with "
360 << it->range.start << "," << it->range.end
361 << " of type " << it->change.type << endl;
371 bool Changes::isChangeEdited(lyx::pos_type const start,
372 lyx::pos_type const end) const
374 if (!table_.size()) {
375 if (lyxerr.debugging(Debug::CHANGES))
376 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
377 return empty_type_ != Change::INSERTED;
380 ChangeTable::const_iterator it = table_.begin();
381 ChangeTable::const_iterator const itend = table_.end();
383 for (; it != itend; ++it) {
384 if (it->range.intersects(Range(start, end ? end - 1 : 0))
385 && it->change.type != Change::INSERTED) {
393 void Changes::merge()
395 if (lyxerr.debugging(Debug::CHANGES))
396 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
398 ChangeTable::iterator it = table_.begin();
400 while (it != table_.end()) {
401 if (lyxerr.debugging(Debug::CHANGES)) {
402 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
403 << it->range.start << "," << it->range.end << endl;
406 if (it->range.start == it->range.end) {
407 if (lyxerr.debugging(Debug::CHANGES)) {
408 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
409 << it->range.start << endl;
418 if (it + 1 == table_.end())
421 if (it->change == (it + 1)->change) {
422 if (lyxerr.debugging(Debug::CHANGES)) {
423 lyxerr[Debug::CHANGES] << "Merging equal ranges "
424 << it->range.start << "," << it->range.end
425 << " and " << (it + 1)->range.start << ","
426 << (it + 1)->range.end << endl;
429 (it + 1)->range.start = it->range.start;
439 lyxerr[Debug::CHANGES] << "Merge ended" << endl;
444 void Changes::check() const
446 ChangeTable::const_iterator it = table_.begin();
447 ChangeTable::const_iterator end = table_.end();
449 bool dont_assert = true;
451 lyxerr[Debug::CHANGES] << "Changelist:" << endl;
452 for (; it != end; ++it) {
453 if (lyxerr.debugging(Debug::CHANGES)) {
454 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
455 << it->range.start << "," << it->range.end << " author "
456 << it->change.author << " time " << it->change.changetime << endl;
462 Range const & range(it->range);
463 Range const & next((it + 1)->range);
464 if (range.end != next.start)
468 if (lyxerr.debugging(Debug::CHANGES))
469 lyxerr[Debug::CHANGES] << "End" << endl;
471 BOOST_ASSERT(dont_assert);
475 int Changes::latexMarkChange(std::ostream & os,
476 Change::Type const old, Change::Type const change,
479 if (!output || old == change)
482 string const start("\\changestart{}");
483 string const end("\\changeend{}");
484 string const son("\\overstrikeon{}");
485 string const soff("\\overstrikeoff{}");
489 if (old == Change::DELETED) {
491 column += soff.length();
495 case Change::UNCHANGED:
497 column += end.length();
500 case Change::DELETED:
501 if (old == Change::UNCHANGED) {
503 column += start.length();
506 column += son.length();
509 case Change::INSERTED:
510 if (old == Change::UNCHANGED) {
512 column += start.length();
521 void Changes::lyxMarkChange(std::ostream & os, int & column,
522 lyx::time_type const curtime,
523 Change const & old, Change const & change)
530 switch (change.type) {
531 case Change::UNCHANGED:
532 os << "\n\\change_unchanged\n";
535 case Change::DELETED: {
536 lyx::time_type t = change.changetime;
539 os << "\n\\change_deleted " << change.author
545 case Change::INSERTED: {
546 lyx::time_type t = change.changetime;
549 os << "\n\\change_inserted " << change.author