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 "BufferParams.h"
20 #include "LaTeXFeatures.h"
22 #include <boost/assert.hpp>
33 * Class Change has a changetime field that specifies the exact time at which
34 * a specific change was made. The change time is used as a guidance for the
35 * user while editing his document. Presently, it is not considered for LaTeX
37 * When merging two adjacent changes, the changetime is not considered,
38 * only the equality of the change type and author is checked (in method
39 * isSimilarTo(...)). If two changes are in fact merged (in method merge()),
40 * the later change time is preserved.
43 bool Change::isSimilarTo(Change const & change)
45 if (type != change.type) {
49 if (type == Change::UNCHANGED) {
53 return author == change.author;
57 bool operator==(Change const & l, Change const & r)
59 if (l.type != r.type) {
63 // two changes of type UNCHANGED are always equal
64 if (l.type == Change::UNCHANGED) {
68 return l.author == r.author &&
69 l.changetime == r.changetime;
73 bool operator!=(Change const & l, Change const & r)
79 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
81 return r1.start == r2.start && r1.end == r2.end;
85 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
91 bool Changes::Range::intersects(Range const & r) const
93 return r.start < end && r.end > start; // end itself is not in the range!
97 void Changes::set(Change const & change, pos_type const pos)
99 set(change, pos, pos + 1);
103 void Changes::set(Change const & change, pos_type const start, pos_type const end)
105 if (change.type != Change::UNCHANGED) {
106 LYXERR(Debug::CHANGES) << "setting change (type: " << change.type
107 << ", author: " << change.author << ", time: " << change.changetime
108 << ") in range (" << start << ", " << end << ")" << endl;
111 Range const newRange(start, end);
113 ChangeTable::iterator it = table_.begin();
115 for (; it != table_.end(); ) {
116 // current change starts like or follows new change
117 if (it->range.start >= start) {
121 // new change intersects with existing change
122 if (it->range.end > start) {
123 pos_type oldEnd = it->range.end;
124 it->range.end = start;
126 LYXERR(Debug::CHANGES) << " cutting tail of type " << it->change.type
127 << " resulting in range (" << it->range.start << ", "
128 << it->range.end << ")" << endl;
132 LYXERR(Debug::CHANGES) << " inserting tail in range ("
133 << end << ", " << oldEnd << ")" << endl;
134 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
142 if (change.type != Change::UNCHANGED) {
143 LYXERR(Debug::CHANGES) << " inserting change" << endl;
144 it = table_.insert(it, ChangeRange(change, Range(start, end)));
148 for (; it != table_.end(); ) {
149 // new change 'contains' existing change
150 if (newRange.contains(it->range)) {
151 LYXERR(Debug::CHANGES) << " removing subrange ("
152 << it->range.start << ", " << it->range.end << ")" << endl;
153 it = table_.erase(it);
157 // new change precedes existing change
158 if (it->range.start >= end) {
162 // new change intersects with existing change
163 it->range.start = end;
164 LYXERR(Debug::CHANGES) << " cutting head of type "
165 << it->change.type << " resulting in range ("
166 << end << ", " << it->range.end << ")" << endl;
167 break; // no need for another iteration
174 void Changes::erase(pos_type const pos)
176 LYXERR(Debug::CHANGES) << "Erasing change at position " << pos << endl;
178 ChangeTable::iterator it = table_.begin();
179 ChangeTable::iterator end = table_.end();
181 for (; it != end; ++it) {
182 // range (pos,pos+x) becomes (pos,pos+x-1)
183 if (it->range.start > pos) {
186 // range (pos-x,pos) stays (pos-x,pos)
187 if (it->range.end > pos) {
196 void Changes::insert(Change const & change, lyx::pos_type pos)
198 if (change.type != Change::UNCHANGED) {
199 LYXERR(Debug::CHANGES) << "Inserting change of type " << change.type
200 << " at position " << pos << endl;
203 ChangeTable::iterator it = table_.begin();
204 ChangeTable::iterator end = table_.end();
206 for (; it != end; ++it) {
207 // range (pos,pos+x) becomes (pos+1,pos+x+1)
208 if (it->range.start >= pos) {
212 // range (pos-x,pos) stays as it is
213 if (it->range.end > pos) {
218 set(change, pos, pos + 1); // set will call merge
222 Change const & Changes::lookup(pos_type const pos) const
224 static Change const noChange = Change(Change::UNCHANGED);
226 ChangeTable::const_iterator it = table_.begin();
227 ChangeTable::const_iterator const end = table_.end();
229 for (; it != end; ++it) {
230 if (it->range.contains(pos))
238 bool Changes::isChanged(pos_type const start, pos_type const end) const
240 ChangeTable::const_iterator it = table_.begin();
241 ChangeTable::const_iterator const itend = table_.end();
243 for (; it != itend; ++it) {
244 if (it->range.intersects(Range(start, end))) {
245 LYXERR(Debug::CHANGES) << "found intersection of range ("
246 << start << ", " << end << ") with ("
247 << it->range.start << ", " << it->range.end
248 << ") of type " << it->change.type << endl;
256 void Changes::merge()
258 ChangeTable::iterator it = table_.begin();
260 while (it != table_.end()) {
261 LYXERR(Debug::CHANGES) << "found change of type " << it->change.type
262 << " and range (" << it->range.start << ", " << it->range.end
265 if (it->range.start == it->range.end) {
266 LYXERR(Debug::CHANGES) << "removing empty range for pos "
267 << it->range.start << endl;
275 if (it + 1 == table_.end())
278 if (it->change.isSimilarTo((it + 1)->change) && it->range.end == (it + 1)->range.start) {
279 LYXERR(Debug::CHANGES) << "merging ranges (" << it->range.start << ", "
280 << it->range.end << ") and (" << (it + 1)->range.start << ", "
281 << (it + 1)->range.end << ")" << endl;
283 (it + 1)->range.start = it->range.start;
284 (it + 1)->change.changetime = max(it->change.changetime,
285 (it + 1)->change.changetime);
297 int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
298 Change const & oldChange, Change const & change)
300 if (!bparams.outputChanges || oldChange == change)
305 if (oldChange.type != Change::UNCHANGED) {
306 os << '}'; // close \lyxinserted or \lyxdeleted
311 chgTime += ctime(&change.changetime);
312 chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
314 if (change.type == Change::DELETED) {
315 docstring str = "\\lyxdeleted{" +
316 bparams.authors().get(change.author).name() + "}{" +
319 column += str.size();
320 } else if (change.type == Change::INSERTED) {
321 docstring str = "\\lyxinserted{" +
322 bparams.authors().get(change.author).name() + "}{" +
325 column += str.size();
332 void Changes::lyxMarkChange(std::ostream & os, int & column,
333 Change const & old, Change const & change)
340 switch (change.type) {
341 case Change::UNCHANGED:
342 os << "\n\\change_unchanged\n";
345 case Change::DELETED: {
346 os << "\n\\change_deleted " << change.author
347 << " " << change.changetime << "\n";
351 case Change::INSERTED: {
352 os << "\n\\change_inserted " << change.author
353 << " " << change.changetime << "\n";