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>
21 using lyx::odocstream;
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::contained(Range const & r) const
61 return r.contains(*this);
65 bool Changes::Range::contains(pos_type const pos) const
67 return pos >= start && pos < end;
71 bool Changes::Range::containsOrPrecedes(pos_type const pos) const
73 return pos >= start && pos <= end;
77 bool Changes::Range::intersects(Range const & r) const
79 return contained(r) || contains(r)
80 || contains(r.start) || contains(r.end);
84 Changes::Changes(Change::Type const type)
95 Changes::Changes(Changes const & c)
101 void Changes::record(Change const & change, pos_type const pos)
103 if (lyxerr.debugging(Debug::CHANGES)) {
104 lyxerr[Debug::CHANGES] << "record " << change.type
105 << " at pos " << pos << " with total "
106 << table_.size() << " changes." << endl;
109 switch (change.type) {
110 case Change::INSERTED:
113 case Change::DELETED:
116 case Change::UNCHANGED:
117 set(Change::UNCHANGED, pos);
123 void Changes::set(Change const & change, pos_type const pos)
125 set(change, pos, pos + 1);
129 void Changes::set(Change::Type const type, pos_type const pos)
131 set(type, pos, pos + 1);
135 void Changes::set(Change::Type const type,
136 pos_type const start, pos_type const end)
138 set(Change(type), start, end);
142 void Changes::set(Change const & change,
143 pos_type const start, pos_type const end)
145 ChangeTable::iterator it = table_.begin();
147 if (lyxerr.debugging(Debug::CHANGES)) {
148 lyxerr[Debug::CHANGES] << "changeset of " << change.type
149 << " author " << change.author << " time " << change.changetime
150 << " in range " << start << "," << end << endl;
153 Range const new_range(start, end);
155 // remove all sub-ranges
156 for (; it != table_.end();) {
157 if (new_range != it->range && it->range.contained(new_range)) {
158 if (lyxerr.debugging(Debug::CHANGES)) {
159 lyxerr[Debug::CHANGES] << "Removing subrange "
160 << it->range.start << "," << it->range.end << endl;
162 it = table_.erase(it);
169 ChangeTable::iterator const itend = table_.end();
171 // find a super-range
172 for (; it != itend; ++it) {
173 if (it->range.contains(new_range))
178 lyxerr[Debug::CHANGES] << "Inserting change at end" << endl;
179 table_.push_back(ChangeRange(start, end, change));
184 if (change.type == it->change.type) {
185 lyxerr[Debug::CHANGES] << "Change set already." << endl;
192 if (lyxerr.debugging(Debug::CHANGES)) {
193 lyxerr[Debug::CHANGES] << "Using change of type " << c.change.type
194 << " over " << c.range.start << "," << c.range.end << endl;
198 if (c.range.start < start) {
199 it = table_.insert(it, ChangeRange(c.range.start, start, c.change));
200 if (lyxerr.debugging(Debug::CHANGES)) {
201 lyxerr[Debug::CHANGES] << "Splitting head of type " << c.change.type
202 << " over " << c.range.start << "," << start << endl;
207 // reset this as new type
208 it->range.start = start;
211 lyxerr[Debug::CHANGES] << "Resetting to new change" << endl;
214 if (c.range.end > end) {
216 table_.insert(it, ChangeRange(end, c.range.end, c.change));
217 if (lyxerr.debugging(Debug::CHANGES)) {
218 lyxerr[Debug::CHANGES] << "Splitting tail of type " << c.change.type
219 << " over " << end << "," << c.range.end << endl;
228 void Changes::erase(pos_type const pos)
230 ChangeTable::iterator it = table_.begin();
231 ChangeTable::iterator end = table_.end();
235 for (; it != end; ++it) {
236 Range & range(it->range);
238 if (lyxerr.debugging(Debug::CHANGES)) {
239 lyxerr[Debug::CHANGES] << "era:Range of type " << it->change.type << " is "
240 << it->range.start << "," << it->range.end << endl;
243 if (range.contains(pos)) {
259 void Changes::del(Change const & change, ChangeTable::size_type const pos)
261 // this case happens when building from .lyx
262 if (table_.empty()) {
267 ChangeTable::iterator it = table_.begin();
269 for (; it != table_.end(); ++it) {
270 Range & range(it->range);
272 if (range.contains(pos)) {
273 if (it->change.type != Change::INSERTED) {
279 } else if (range.containsOrPrecedes(pos) && it + 1 == table_.end()) {
280 // this case happens when building from .lyx
288 void Changes::add(Change const & change, ChangeTable::size_type const pos)
290 ChangeTable::iterator it = table_.begin();
291 ChangeTable::iterator end = table_.end();
295 for (; it != end; ++it) {
296 Range & range(it->range);
298 if (!found && range.containsOrPrecedes(pos)) {
300 if (lyxerr.debugging(Debug::CHANGES)) {
301 lyxerr[Debug::CHANGES] << "Found range of "
302 << range.start << "," << range.end << endl;
317 Change const Changes::lookup(pos_type const pos) const
319 if (!table_.size()) {
320 if (lyxerr.debugging(Debug::CHANGES))
321 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
322 return Change(empty_type_);
325 ChangeTable::const_iterator it = table_.begin();
326 ChangeTable::const_iterator const end = table_.end();
328 for (; it != end; ++it) {
329 if (it->range.contains(pos))
334 BOOST_ASSERT(false && "missing changes for pos");
335 return Change(Change::UNCHANGED);
339 bool Changes::isChange(pos_type const start, pos_type const end) const
341 if (!table_.size()) {
342 if (lyxerr.debugging(Debug::CHANGES))
343 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
344 return empty_type_ != Change::UNCHANGED;
347 ChangeTable::const_iterator it = table_.begin();
348 ChangeTable::const_iterator const itend = table_.end();
350 for (; it != itend; ++it) {
351 if (lyxerr.debugging(Debug::CHANGES)) {
352 lyxerr[Debug::CHANGES] << "Looking for " << start << ","
353 << end << " in " << it->range.start << ","
354 << it->range.end << "of type " << it->change.type << endl;
357 if (it->range.intersects(Range(start, end))
358 && it->change.type != Change::UNCHANGED) {
359 if (lyxerr.debugging(Debug::CHANGES)) {
360 lyxerr[Debug::CHANGES] << "Found intersection of "
361 << start << "," << end << " with "
362 << it->range.start << "," << it->range.end
363 << " of type " << it->change.type << endl;
373 void Changes::merge()
375 if (lyxerr.debugging(Debug::CHANGES))
376 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
378 ChangeTable::iterator it = table_.begin();
380 while (it != table_.end()) {
381 if (lyxerr.debugging(Debug::CHANGES)) {
382 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
383 << it->range.start << "," << it->range.end << endl;
386 if (it->range.start == it->range.end) {
387 if (lyxerr.debugging(Debug::CHANGES)) {
388 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
389 << it->range.start << endl;
398 if (it + 1 == table_.end())
401 if (it->change == (it + 1)->change) {
402 if (lyxerr.debugging(Debug::CHANGES)) {
403 lyxerr[Debug::CHANGES] << "Merging equal ranges "
404 << it->range.start << "," << it->range.end
405 << " and " << (it + 1)->range.start << ","
406 << (it + 1)->range.end << endl;
409 (it + 1)->range.start = it->range.start;
419 lyxerr[Debug::CHANGES] << "Merge ended" << endl;
424 void Changes::check() const
426 ChangeTable::const_iterator it = table_.begin();
427 ChangeTable::const_iterator end = table_.end();
429 bool dont_assert = true;
431 lyxerr[Debug::CHANGES] << "Changelist:" << endl;
432 for (; it != end; ++it) {
433 if (lyxerr.debugging(Debug::CHANGES)) {
434 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
435 << it->range.start << "," << it->range.end << " author "
436 << it->change.author << " time " << it->change.changetime << endl;
442 Range const & range(it->range);
443 Range const & next((it + 1)->range);
444 if (range.end != next.start)
448 if (lyxerr.debugging(Debug::CHANGES))
449 lyxerr[Debug::CHANGES] << "End" << endl;
451 BOOST_ASSERT(dont_assert);
455 int Changes::latexMarkChange(odocstream & os,
456 Change::Type const old, Change::Type const change,
459 if (!output || old == change)
462 static docstring const start(lyx::from_ascii("\\changestart{}"));
463 static docstring const end(lyx::from_ascii("\\changeend{}"));
464 static docstring const son(lyx::from_ascii("\\overstrikeon{}"));
465 static docstring const soff(lyx::from_ascii("\\overstrikeoff{}"));
469 if (old == Change::DELETED) {
471 column += soff.length();
475 case Change::UNCHANGED:
477 column += end.length();
480 case Change::DELETED:
481 if (old == Change::UNCHANGED) {
483 column += start.length();
486 column += son.length();
489 case Change::INSERTED:
490 if (old == Change::UNCHANGED) {
492 column += start.length();
501 void Changes::lyxMarkChange(std::ostream & os, int & column,
502 lyx::time_type const curtime,
503 Change const & old, Change const & change)
510 switch (change.type) {
511 case Change::UNCHANGED:
512 os << "\n\\change_unchanged\n";
515 case Change::DELETED: {
516 lyx::time_type t = change.changetime;
519 os << "\n\\change_deleted " << change.author
525 case Change::INSERTED: {
526 lyx::time_type t = change.changetime;
529 os << "\n\\change_inserted " << change.author