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::contains(pos_type const pos) const
61 return pos >= start && pos < end;
65 bool Changes::Range::intersects(Range const & r) const
67 return r.start < end && r.end > start; // end itself is not in the range!
71 Changes::Changes(Change::Type const type)
82 Changes::Changes(Changes const & c)
88 void Changes::record(Change const & change, pos_type const pos)
90 if (lyxerr.debugging(Debug::CHANGES)) {
91 lyxerr[Debug::CHANGES] << "record " << change.type
92 << " at pos " << pos << " with total "
93 << table_.size() << " changes." << endl;
96 switch (change.type) {
97 case Change::INSERTED:
100 case Change::DELETED:
103 case Change::UNCHANGED:
104 // FIXME: change tracking (MG)
105 // set(Change::UNCHANGED, pos);
111 void Changes::set(Change const & change, pos_type const pos)
113 set(change, pos, pos + 1);
117 void Changes::set(Change const & change,
118 pos_type const start, pos_type const end)
120 ChangeTable::iterator it = table_.begin();
122 if (lyxerr.debugging(Debug::CHANGES)) {
123 lyxerr[Debug::CHANGES] << "changeset of " << change.type
124 << " author " << change.author << " time " << change.changetime
125 << " in range " << start << "," << end << endl;
128 Range const new_range(start, end);
130 // remove all sub-ranges
131 for (; it != table_.end();) {
132 if (new_range != it->range /*&& it->range.contained(new_range)*/) { // FIXME: change tracking (MG)
133 if (lyxerr.debugging(Debug::CHANGES)) {
134 lyxerr[Debug::CHANGES] << "Removing subrange "
135 << it->range.start << "," << it->range.end << endl;
137 it = table_.erase(it);
144 ChangeTable::iterator const itend = table_.end();
146 // find a super-range
147 for (; it != itend; ++it) {
148 if (it->range.contains(new_range))
153 lyxerr[Debug::CHANGES] << "Inserting change at end" << endl;
154 table_.push_back(ChangeRange(start, end, change));
159 if (change.type == it->change.type) {
160 lyxerr[Debug::CHANGES] << "Change set already." << endl;
167 if (lyxerr.debugging(Debug::CHANGES)) {
168 lyxerr[Debug::CHANGES] << "Using change of type " << c.change.type
169 << " over " << c.range.start << "," << c.range.end << endl;
173 if (c.range.start < start) {
174 it = table_.insert(it, ChangeRange(c.range.start, start, c.change));
175 if (lyxerr.debugging(Debug::CHANGES)) {
176 lyxerr[Debug::CHANGES] << "Splitting head of type " << c.change.type
177 << " over " << c.range.start << "," << start << endl;
182 // reset this as new type
183 it->range.start = start;
186 lyxerr[Debug::CHANGES] << "Resetting to new change" << endl;
189 if (c.range.end > end) {
191 table_.insert(it, ChangeRange(end, c.range.end, c.change));
192 if (lyxerr.debugging(Debug::CHANGES)) {
193 lyxerr[Debug::CHANGES] << "Splitting tail of type " << c.change.type
194 << " over " << end << "," << c.range.end << endl;
202 void Changes::erase(pos_type const pos)
204 ChangeTable::iterator it = table_.begin();
205 ChangeTable::iterator end = table_.end();
209 for (; it != end; ++it) {
210 Range & range(it->range);
212 if (lyxerr.debugging(Debug::CHANGES)) {
213 lyxerr[Debug::CHANGES] << "era:Range of type " << it->change.type << " is "
214 << it->range.start << "," << it->range.end << endl;
217 if (range.contains(pos)) {
232 void Changes::del(Change const & change, ChangeTable::size_type const pos)
234 // this case happens when building from .lyx
235 if (table_.empty()) {
240 ChangeTable::iterator it = table_.begin();
242 for (; it != table_.end(); ++it) {
243 Range & range(it->range);
245 if (range.contains(pos)) {
246 if (it->change.type != Change::INSERTED) {
252 } else if (/*range.containsOrPrecedes(pos) && it + 1 == table_.end()*/ true) { // FIXME: change tracking (MG)
253 // this case happens when building from .lyx
261 void Changes::add(Change const & change, ChangeTable::size_type const pos)
263 ChangeTable::iterator it = table_.begin();
264 ChangeTable::iterator end = table_.end();
268 for (; it != end; ++it) {
269 Range & range(it->range);
271 if (!found /* && range.containsOrPrecedes(pos)*/) { // FIXME: change tracking (MG)
273 if (lyxerr.debugging(Debug::CHANGES)) {
274 lyxerr[Debug::CHANGES] << "Found range of "
275 << range.start << "," << range.end << endl;
290 Change const Changes::lookup(pos_type const pos) const
292 if (!table_.size()) {
293 if (lyxerr.debugging(Debug::CHANGES))
294 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
295 return Change(empty_type_);
298 ChangeTable::const_iterator it = table_.begin();
299 ChangeTable::const_iterator const end = table_.end();
301 for (; it != end; ++it) {
302 if (it->range.contains(pos))
306 BOOST_ASSERT(false && "missing changes for pos");
307 return Change(Change::UNCHANGED);
311 bool Changes::isChange(pos_type const start, pos_type const end) const
313 if (!table_.size()) {
314 if (lyxerr.debugging(Debug::CHANGES))
315 lyxerr[Debug::CHANGES] << "Empty, type is " << empty_type_ << endl;
316 return empty_type_ != Change::UNCHANGED;
319 ChangeTable::const_iterator it = table_.begin();
320 ChangeTable::const_iterator const itend = table_.end();
322 for (; it != itend; ++it) {
323 if (lyxerr.debugging(Debug::CHANGES)) {
324 lyxerr[Debug::CHANGES] << "Looking for " << start << ","
325 << end << " in " << it->range.start << ","
326 << it->range.end << "of type " << it->change.type << endl;
329 if (it->range.intersects(Range(start, end))
330 && it->change.type != Change::UNCHANGED) {
331 if (lyxerr.debugging(Debug::CHANGES)) {
332 lyxerr[Debug::CHANGES] << "Found intersection of "
333 << start << "," << end << " with "
334 << it->range.start << "," << it->range.end
335 << " of type " << it->change.type << endl;
345 void Changes::merge()
347 if (lyxerr.debugging(Debug::CHANGES))
348 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
350 ChangeTable::iterator it = table_.begin();
352 while (it != table_.end()) {
353 if (lyxerr.debugging(Debug::CHANGES)) {
354 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
355 << it->range.start << "," << it->range.end << endl;
358 if (it->range.start == it->range.end) {
359 if (lyxerr.debugging(Debug::CHANGES)) {
360 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
361 << it->range.start << endl;
370 if (it + 1 == table_.end())
373 if (it->change == (it + 1)->change) {
374 if (lyxerr.debugging(Debug::CHANGES)) {
375 lyxerr[Debug::CHANGES] << "Merging equal ranges "
376 << it->range.start << "," << it->range.end
377 << " and " << (it + 1)->range.start << ","
378 << (it + 1)->range.end << endl;
381 (it + 1)->range.start = it->range.start;
391 lyxerr[Debug::CHANGES] << "Merge ended" << endl;
395 int Changes::latexMarkChange(odocstream & os,
396 Change::Type const old, Change::Type const change,
399 if (!output || old == change)
402 static docstring const start(lyx::from_ascii("\\changestart{}"));
403 static docstring const end(lyx::from_ascii("\\changeend{}"));
404 static docstring const son(lyx::from_ascii("\\overstrikeon{}"));
405 static docstring const soff(lyx::from_ascii("\\overstrikeoff{}"));
409 if (old == Change::DELETED) {
411 column += soff.length();
415 case Change::UNCHANGED:
417 column += end.length();
420 case Change::DELETED:
421 if (old == Change::UNCHANGED) {
423 column += start.length();
426 column += son.length();
429 case Change::INSERTED:
430 if (old == Change::UNCHANGED) {
432 column += start.length();
441 void Changes::lyxMarkChange(std::ostream & os, int & column,
442 lyx::time_type const curtime,
443 Change const & old, Change const & change)
450 switch (change.type) {
451 case Change::UNCHANGED:
452 os << "\n\\change_unchanged\n";
455 case Change::DELETED: {
456 lyx::time_type t = change.changetime;
459 os << "\n\\change_deleted " << change.author
465 case Change::INSERTED: {
466 lyx::time_type t = change.changetime;
469 os << "\n\\change_inserted " << change.author