3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
11 * Record changes in a paragraph.
19 #include <boost/assert.hpp>
30 * Class Change has a changetime field that specifies the exact time at which
31 * a specific change was made. The change time is used as a guidance for the
32 * user while editing his document. Presently, it is not considered for LaTeX
34 * When merging two adjacent changes, the changetime is not considered,
35 * only the equality of the change type and author is checked (in method
36 * isSimilarTo(...)). If two changes are in fact merged (in method merge()),
37 * the later change time is preserved.
40 bool Change::isSimilarTo(Change const & change)
42 if (type != change.type) {
46 if (type == Change::UNCHANGED) {
50 return author == change.author;
54 bool operator==(Change const & l, Change const & r)
56 return l.type == r.type &&
57 l.author == r.author &&
58 l.changetime == r.changetime;
62 bool operator!=(Change const & l, Change const & r)
68 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
70 return r1.start == r2.start && r1.end == r2.end;
74 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
80 bool Changes::Range::contains(Range const & r) const
82 return r.start >= start && r.end <= end;
86 bool Changes::Range::contains(pos_type const pos) const
88 return pos >= start && pos < end;
92 bool Changes::Range::intersects(Range const & r) const
94 return r.start < end && r.end > start; // end itself is not in the range!
98 void Changes::set(Change const & change, pos_type const pos)
100 set(change, pos, pos + 1);
104 void Changes::set(Change const & change, pos_type const start, pos_type const end)
106 if (lyxerr.debugging(Debug::CHANGES)) {
107 lyxerr[Debug::CHANGES] << "setting change (type: " << change.type
108 << ", author: " << change.author << ", time: " << change.changetime
109 << ") in range (" << start << ", " << end << ")" << endl;
112 Range const newRange(start, end);
114 ChangeTable::iterator it = table_.begin();
116 for (; it != table_.end(); ) {
117 // current change starts like or follows new change
118 if (it->range.start >= start) {
122 // new change intersects with existing change
123 if (it->range.end > start) {
124 pos_type oldEnd = it->range.end;
125 it->range.end = start;
126 if (lyxerr.debugging(Debug::CHANGES)) {
127 lyxerr[Debug::CHANGES] << " cutting tail of type " << it->change.type
128 << " resulting in range (" << it->range.start << ", "
129 << it->range.end << ")" << endl;
133 if (lyxerr.debugging(Debug::CHANGES)) {
134 lyxerr[Debug::CHANGES] << " inserting tail in range ("
135 << end << ", " << oldEnd << ")" << endl;
137 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
145 if (change.type != Change::UNCHANGED) {
146 if (lyxerr.debugging(Debug::CHANGES)) {
147 lyxerr[Debug::CHANGES] << " inserting change" << endl;
149 it = table_.insert(it, ChangeRange(change, Range(start, end)));
153 for (; it != table_.end(); ) {
154 // new change 'contains' existing change
155 if (newRange.contains(it->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);
164 // new change precedes existing change
165 if (it->range.start >= end) {
169 // new change intersects with existing change
170 it->range.start = end;
171 if (lyxerr.debugging(Debug::CHANGES)) {
172 lyxerr[Debug::CHANGES] << " cutting head of type "
173 << it->change.type << " resulting in range ("
174 << end << ", " << it->range.end << ")" << endl;
176 break; // no need for another iteration
183 void Changes::erase(pos_type const pos)
185 if (lyxerr.debugging(Debug::CHANGES)) {
186 lyxerr[Debug::CHANGES] << "Erasing change at position " << pos << endl;
189 ChangeTable::iterator it = table_.begin();
190 ChangeTable::iterator end = table_.end();
192 for (; it != end; ++it) {
193 // range (pos,pos+x) becomes (pos,pos+x-1)
194 if (it->range.start > pos) {
197 // range (pos-x,pos) stays (pos-x,pos)
198 if (it->range.end > pos) {
207 void Changes::insert(Change const & change, lyx::pos_type pos)
209 if (lyxerr.debugging(Debug::CHANGES)) {
210 lyxerr[Debug::CHANGES] << "Inserting change of type " << change.type
211 << " at position " << pos << endl;
214 ChangeTable::iterator it = table_.begin();
215 ChangeTable::iterator end = table_.end();
217 for (; it != end; ++it) {
218 // range (pos,pos+x) becomes (pos+1,pos+x+1)
219 if (it->range.start >= pos) {
223 // range (pos-x,pos) stays as it is
224 if (it->range.end > pos) {
229 set(change, pos, pos + 1); // set will call merge
233 Change const Changes::lookup(pos_type const pos) const
235 ChangeTable::const_iterator it = table_.begin();
236 ChangeTable::const_iterator const end = table_.end();
238 for (; it != end; ++it) {
239 if (it->range.contains(pos))
243 return Change(Change::UNCHANGED);
247 bool Changes::isChanged(pos_type const start, pos_type const end) const
249 ChangeTable::const_iterator it = table_.begin();
250 ChangeTable::const_iterator const itend = table_.end();
252 for (; it != itend; ++it) {
253 if (it->range.intersects(Range(start, end))) {
254 if (lyxerr.debugging(Debug::CHANGES)) {
255 lyxerr[Debug::CHANGES] << "found intersection of range ("
256 << start << ", " << end << ") with ("
257 << it->range.start << ", " << it->range.end
258 << ") of type " << it->change.type << endl;
267 void Changes::merge()
269 if (lyxerr.debugging(Debug::CHANGES)) {
270 lyxerr[Debug::CHANGES] << "merging changes..." << endl;
273 ChangeTable::iterator it = table_.begin();
275 while (it != table_.end()) {
276 if (lyxerr.debugging(Debug::CHANGES)) {
277 lyxerr[Debug::CHANGES] << " found change of type " << it->change.type
278 << " and range (" << it->range.start << ", " << it->range.end
282 if (it->range.start == it->range.end) {
283 if (lyxerr.debugging(Debug::CHANGES)) {
284 lyxerr[Debug::CHANGES] << " removing empty range for pos "
285 << it->range.start << endl;
294 if (it + 1 == table_.end())
297 if (it->change.isSimilarTo((it + 1)->change) && it->range.end == (it + 1)->range.start) {
298 if (lyxerr.debugging(Debug::CHANGES)) {
299 lyxerr[Debug::CHANGES] << " merging ranges (" << it->range.start << ", "
300 << it->range.end << ") and (" << (it + 1)->range.start << ", "
301 << (it + 1)->range.end << ")" << endl;
303 (it + 1)->range.start = it->range.start;
304 (it + 1)->change.changetime = max(it->change.changetime,
305 (it + 1)->change.changetime);
317 int Changes::latexMarkChange(odocstream & os,
318 Change::Type const oldChangeType, Change::Type const changeType,
321 if (!output || oldChangeType == changeType)
324 static docstring const start(from_ascii("\\changestart{}"));
325 static docstring const end(from_ascii("\\changeend{}"));
326 static docstring const son(from_ascii("\\overstrikeon{}"));
327 static docstring const soff(from_ascii("\\overstrikeoff{}"));
331 if (oldChangeType == Change::DELETED) {
333 column += soff.length();
336 switch (changeType) {
337 case Change::UNCHANGED:
339 column += end.length();
342 case Change::DELETED:
343 if (oldChangeType == Change::UNCHANGED) {
345 column += start.length();
348 column += son.length();
351 case Change::INSERTED:
352 if (oldChangeType == Change::UNCHANGED) {
354 column += start.length();
363 void Changes::lyxMarkChange(std::ostream & os, int & column,
364 Change const & old, Change const & change)
371 switch (change.type) {
372 case Change::UNCHANGED:
373 os << "\n\\change_unchanged\n";
376 case Change::DELETED: {
377 os << "\n\\change_deleted " << change.author
378 << " " << change.changetime << "\n";
382 case Change::INSERTED: {
383 os << "\n\\change_inserted " << change.author
384 << " " << change.changetime << "\n";