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 void Changes::record(Change const & change, pos_type const pos)
73 if (lyxerr.debugging(Debug::CHANGES)) {
74 lyxerr[Debug::CHANGES] << "record " << change.type
75 << " at pos " << pos << " with total "
76 << table_.size() << " changes." << endl;
79 switch (change.type) {
80 case Change::INSERTED:
86 case Change::UNCHANGED:
87 // FIXME: change tracking (MG)
88 // set(Change::UNCHANGED, pos);
94 void Changes::set(Change const & change, pos_type const pos)
96 set(change, pos, pos + 1);
100 void Changes::set(Change const & change,
101 pos_type const start, pos_type const end)
103 ChangeTable::iterator it = table_.begin();
105 if (lyxerr.debugging(Debug::CHANGES)) {
106 lyxerr[Debug::CHANGES] << "changeset of " << change.type
107 << " author " << change.author << " time " << change.changetime
108 << " in range " << start << "," << end << endl;
111 Range const new_range(start, end);
113 // remove all sub-ranges
114 for (; it != table_.end();) {
115 if (new_range != it->range /*&& it->range.contained(new_range)*/) { // FIXME: change tracking (MG)
116 if (lyxerr.debugging(Debug::CHANGES)) {
117 lyxerr[Debug::CHANGES] << "Removing subrange "
118 << it->range.start << "," << it->range.end << endl;
120 it = table_.erase(it);
127 ChangeTable::iterator const itend = table_.end();
129 // find a super-range
130 for (; it != itend; ++it) {
131 if (it->range.contains(new_range))
136 lyxerr[Debug::CHANGES] << "Inserting change at end" << endl;
137 table_.push_back(ChangeRange(start, end, change));
142 if (change.type == it->change.type) {
143 lyxerr[Debug::CHANGES] << "Change set already." << endl;
150 if (lyxerr.debugging(Debug::CHANGES)) {
151 lyxerr[Debug::CHANGES] << "Using change of type " << c.change.type
152 << " over " << c.range.start << "," << c.range.end << endl;
156 if (c.range.start < start) {
157 it = table_.insert(it, ChangeRange(c.range.start, start, c.change));
158 if (lyxerr.debugging(Debug::CHANGES)) {
159 lyxerr[Debug::CHANGES] << "Splitting head of type " << c.change.type
160 << " over " << c.range.start << "," << start << endl;
165 // reset this as new type
166 it->range.start = start;
169 lyxerr[Debug::CHANGES] << "Resetting to new change" << endl;
172 if (c.range.end > end) {
174 table_.insert(it, ChangeRange(end, c.range.end, c.change));
175 if (lyxerr.debugging(Debug::CHANGES)) {
176 lyxerr[Debug::CHANGES] << "Splitting tail of type " << c.change.type
177 << " over " << end << "," << c.range.end << endl;
185 void Changes::erase(pos_type const pos)
187 ChangeTable::iterator it = table_.begin();
188 ChangeTable::iterator end = table_.end();
192 for (; it != end; ++it) {
193 Range & range(it->range);
195 if (lyxerr.debugging(Debug::CHANGES)) {
196 lyxerr[Debug::CHANGES] << "era:Range of type " << it->change.type << " is "
197 << it->range.start << "," << it->range.end << endl;
200 if (range.contains(pos)) {
215 void Changes::del(Change const & change, ChangeTable::size_type const pos)
217 // this case happens when building from .lyx
218 if (table_.empty()) {
223 ChangeTable::iterator it = table_.begin();
225 for (; it != table_.end(); ++it) {
226 Range & range(it->range);
228 if (range.contains(pos)) {
229 if (it->change.type != Change::INSERTED) {
235 } else if (/*range.containsOrPrecedes(pos) && it + 1 == table_.end()*/ true) { // FIXME: change tracking (MG)
236 // this case happens when building from .lyx
244 void Changes::add(Change const & change, ChangeTable::size_type const pos)
246 ChangeTable::iterator it = table_.begin();
247 ChangeTable::iterator end = table_.end();
251 for (; it != end; ++it) {
252 Range & range(it->range);
254 if (!found /* && range.containsOrPrecedes(pos)*/) { // FIXME: change tracking (MG)
256 if (lyxerr.debugging(Debug::CHANGES)) {
257 lyxerr[Debug::CHANGES] << "Found range of "
258 << range.start << "," << range.end << endl;
273 Change const Changes::lookup(pos_type const pos) const
275 ChangeTable::const_iterator it = table_.begin();
276 ChangeTable::const_iterator const end = table_.end();
278 for (; it != end; ++it) {
279 if (it->range.contains(pos))
283 BOOST_ASSERT(false && "missing changes for pos");
284 return Change(Change::UNCHANGED);
288 bool Changes::isChanged(pos_type const start, pos_type const end) const
290 ChangeTable::const_iterator it = table_.begin();
291 ChangeTable::const_iterator const itend = table_.end();
293 for (; it != itend; ++it) {
294 if (lyxerr.debugging(Debug::CHANGES)) {
295 lyxerr[Debug::CHANGES] << "Looking for " << start << ","
296 << end << " in " << it->range.start << ","
297 << it->range.end << "of type " << it->change.type << endl;
300 if (it->range.intersects(Range(start, end))
301 && it->change.type != Change::UNCHANGED) {
302 if (lyxerr.debugging(Debug::CHANGES)) {
303 lyxerr[Debug::CHANGES] << "Found intersection of "
304 << start << "," << end << " with "
305 << it->range.start << "," << it->range.end
306 << " of type " << it->change.type << endl;
316 void Changes::merge()
318 if (lyxerr.debugging(Debug::CHANGES))
319 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
321 ChangeTable::iterator it = table_.begin();
323 while (it != table_.end()) {
324 if (lyxerr.debugging(Debug::CHANGES)) {
325 lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
326 << it->range.start << "," << it->range.end << endl;
329 if (it->range.start == it->range.end) {
330 if (lyxerr.debugging(Debug::CHANGES)) {
331 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
332 << it->range.start << endl;
341 if (it + 1 == table_.end())
344 if (it->change == (it + 1)->change) {
345 if (lyxerr.debugging(Debug::CHANGES)) {
346 lyxerr[Debug::CHANGES] << "Merging equal ranges "
347 << it->range.start << "," << it->range.end
348 << " and " << (it + 1)->range.start << ","
349 << (it + 1)->range.end << endl;
352 (it + 1)->range.start = it->range.start;
362 lyxerr[Debug::CHANGES] << "Merge ended" << endl;
366 int Changes::latexMarkChange(odocstream & os,
367 Change::Type const old, Change::Type const change,
370 if (!output || old == change)
373 static docstring const start(lyx::from_ascii("\\changestart{}"));
374 static docstring const end(lyx::from_ascii("\\changeend{}"));
375 static docstring const son(lyx::from_ascii("\\overstrikeon{}"));
376 static docstring const soff(lyx::from_ascii("\\overstrikeoff{}"));
380 if (old == Change::DELETED) {
382 column += soff.length();
386 case Change::UNCHANGED:
388 column += end.length();
391 case Change::DELETED:
392 if (old == Change::UNCHANGED) {
394 column += start.length();
397 column += son.length();
400 case Change::INSERTED:
401 if (old == Change::UNCHANGED) {
403 column += start.length();
412 void Changes::lyxMarkChange(std::ostream & os, int & column,
413 lyx::time_type const curtime,
414 Change const & old, Change const & change)
421 switch (change.type) {
422 case Change::UNCHANGED:
423 os << "\n\\change_unchanged\n";
426 case Change::DELETED: {
427 lyx::time_type t = change.changetime;
430 os << "\n\\change_deleted " << change.author
436 case Change::INSERTED: {
437 lyx::time_type t = change.changetime;
440 os << "\n\\change_inserted " << change.author