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 if (table_.empty()) {
236 return Change(Change::UNCHANGED);
238 ChangeTable::const_iterator it = table_.begin();
239 ChangeTable::const_iterator const end = table_.end();
241 for (; it != end; ++it) {
242 if (it->range.contains(pos))
246 return Change(Change::UNCHANGED);
250 bool Changes::isChanged(pos_type const start, pos_type const end) const
252 ChangeTable::const_iterator it = table_.begin();
253 ChangeTable::const_iterator const itend = table_.end();
255 for (; it != itend; ++it) {
256 if (it->range.intersects(Range(start, end))) {
257 if (lyxerr.debugging(Debug::CHANGES)) {
258 lyxerr[Debug::CHANGES] << "found intersection of range ("
259 << start << ", " << end << ") with ("
260 << it->range.start << ", " << it->range.end
261 << ") of type " << it->change.type << endl;
270 void Changes::merge()
272 if (lyxerr.debugging(Debug::CHANGES)) {
273 lyxerr[Debug::CHANGES] << "merging changes..." << endl;
276 ChangeTable::iterator it = table_.begin();
278 while (it != table_.end()) {
279 if (lyxerr.debugging(Debug::CHANGES)) {
280 lyxerr[Debug::CHANGES] << " found change of type " << it->change.type
281 << " and range (" << it->range.start << ", " << it->range.end
285 if (it->range.start == it->range.end) {
286 if (lyxerr.debugging(Debug::CHANGES)) {
287 lyxerr[Debug::CHANGES] << " removing empty range for pos "
288 << it->range.start << endl;
297 if (it + 1 == table_.end())
300 if (it->change.isSimilarTo((it + 1)->change) && it->range.end == (it + 1)->range.start) {
301 if (lyxerr.debugging(Debug::CHANGES)) {
302 lyxerr[Debug::CHANGES] << " merging ranges (" << it->range.start << ", "
303 << it->range.end << ") and (" << (it + 1)->range.start << ", "
304 << (it + 1)->range.end << ")" << endl;
306 (it + 1)->range.start = it->range.start;
307 (it + 1)->change.changetime = max(it->change.changetime,
308 (it + 1)->change.changetime);
320 int Changes::latexMarkChange(odocstream & os,
321 Change::Type const oldChangeType, Change::Type const changeType,
324 if (!output || oldChangeType == changeType)
327 static docstring const start(from_ascii("\\changestart{}"));
328 static docstring const end(from_ascii("\\changeend{}"));
329 static docstring const son(from_ascii("\\overstrikeon{}"));
330 static docstring const soff(from_ascii("\\overstrikeoff{}"));
334 if (oldChangeType == Change::DELETED) {
336 column += soff.length();
339 switch (changeType) {
340 case Change::UNCHANGED:
342 column += end.length();
345 case Change::DELETED:
346 if (oldChangeType == Change::UNCHANGED) {
348 column += start.length();
351 column += son.length();
354 case Change::INSERTED:
355 if (oldChangeType == Change::UNCHANGED) {
357 column += start.length();
366 void Changes::lyxMarkChange(std::ostream & os, int & column,
367 Change const & old, Change const & change)
374 switch (change.type) {
375 case Change::UNCHANGED:
376 os << "\n\\change_unchanged\n";
379 case Change::DELETED: {
380 os << "\n\\change_deleted " << change.author
381 << " " << change.changetime << "\n";
385 case Change::INSERTED: {
386 os << "\n\\change_inserted " << change.author
387 << " " << change.changetime << "\n";