]> git.lyx.org Git - lyx.git/blob - src/changes.C
change tracking:
[lyx.git] / src / changes.C
1 /**
2  * \file changes.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Michael Gerz
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * Record changes in a paragraph.
12  */
13
14 #include <config.h>
15
16 #include "changes.h"
17 #include "debug.h"
18
19 #include <boost/assert.hpp>
20
21
22 namespace lyx {
23
24 using std::endl;
25 using std::string;
26
27
28 bool operator==(Change const & l, Change const & r)
29 {
30         return l.type == r.type && l.author == r.author
31                 && l.changetime == r.changetime;
32 }
33
34
35 bool operator!=(Change const & l, Change const & r)
36 {
37         return !(l == r);
38 }
39
40
41 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
42 {
43         return r1.start == r2.start && r1.end == r2.end;
44 }
45
46
47 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
48 {
49         return !(r1 == r2);
50 }
51
52
53 bool Changes::Range::contains(Range const & r) const
54 {
55         return r.start >= start && r.end <= end;
56 }
57
58
59 bool Changes::Range::contains(pos_type const pos) const
60 {
61         return pos >= start && pos < end;
62 }
63
64
65 bool Changes::Range::intersects(Range const & r) const
66 {
67         return r.start < end && r.end > start; // end itself is not in the range!
68 }
69
70
71 void Changes::set(Change const & change, pos_type const pos)
72 {
73         set(change, pos, pos + 1);
74 }
75
76
77 void Changes::set(Change const & change, pos_type const start, pos_type const end)
78 {
79         if (lyxerr.debugging(Debug::CHANGES)) {
80                 lyxerr[Debug::CHANGES] << "setting change (type: " << change.type
81                         << ", author: " << change.author << ", time: " << change.changetime
82                         << ") in range (" << start << ", " << end << ")" << endl;
83         }
84
85         Range const newRange(start, end);
86
87         ChangeTable::iterator it = table_.begin();
88
89         for (; it != table_.end(); ) {
90                 // find super change, check for equal types, and do nothing
91                 if (it->range.contains(newRange) && it->change.type == change.type) {
92                         return; 
93                 }
94
95                 // current change starts like or follows new change
96                 if (it->range.start >= start) {
97                         break;
98                 }
99
100                 // new change intersects with existing change
101                 if (it->range.end > start) {
102                         pos_type oldEnd = it->range.end;
103                         it->range.end = start;
104                         if (lyxerr.debugging(Debug::CHANGES)) {
105                                 lyxerr[Debug::CHANGES] << "  cutting tail of type " << it->change.type
106                                         << " resulting in range (" << it->range.start << ", "
107                                         << it->range.end << ")" << endl;
108                         }
109                         ++it;
110                         if (oldEnd >= end) {
111                                 if (lyxerr.debugging(Debug::CHANGES)) {
112                                         lyxerr[Debug::CHANGES] << "  inserting tail in range ("
113                                                 << end << ", " << oldEnd << ")" << endl;
114                                 }
115                                 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
116                         }
117                         continue;
118                 }
119
120                 ++it;
121         }
122
123         if (change.type != Change::UNCHANGED) {
124                 if (lyxerr.debugging(Debug::CHANGES)) {
125                         lyxerr[Debug::CHANGES] << "  inserting change" << endl;
126                 }
127                 it = table_.insert(it, ChangeRange(change, Range(start, end)));
128                 ++it;
129         }
130
131         for (; it != table_.end(); ) {
132                 // new change 'contains' existing change
133                 if (newRange.contains(it->range)) {
134                         if (lyxerr.debugging(Debug::CHANGES)) {
135                                 lyxerr[Debug::CHANGES] << "  removing subrange ("
136                                         << it->range.start << ", " << it->range.end << ")" << endl;
137                         }
138                         it = table_.erase(it);
139                         continue;
140                 }
141
142                 // new change precedes existing change
143                 if (it->range.start >= end) {
144                         break;
145                 }
146
147                 // new change intersects with existing change
148                 it->range.start = end;
149                 if (lyxerr.debugging(Debug::CHANGES)) {
150                         lyxerr[Debug::CHANGES] << "  cutting head of type "
151                                 << it->change.type << " resulting in range ("
152                                 << end << ", " << it->range.end << ")" << endl;
153                 }
154                 break; // no need for another iteration
155         }
156
157         merge();
158 }
159
160
161 void Changes::erase(pos_type const pos)
162 {
163         if (lyxerr.debugging(Debug::CHANGES)) {
164                 lyxerr[Debug::CHANGES] << "Erasing change at position " << pos << endl;
165         }
166
167         ChangeTable::iterator it = table_.begin();
168         ChangeTable::iterator end = table_.end();
169
170         for (; it != end; ++it) {
171                 // range (pos,pos+x) becomes (pos,pos+x-1)
172                 if (it->range.start > pos) {
173                         --(it->range.start);
174                 }
175                 // range (pos-x,pos) stays (pos-x,pos)
176                 if (it->range.end > pos) {
177                         --(it->range.end);
178                 }
179         }
180
181         merge();
182 }
183
184
185 void Changes::insert(Change const & change, lyx::pos_type pos)
186 {
187         if (lyxerr.debugging(Debug::CHANGES)) {
188                 lyxerr[Debug::CHANGES] << "Inserting change of type " << change.type
189                         << " at position " << pos << endl;
190         }
191
192         ChangeTable::iterator it = table_.begin();
193         ChangeTable::iterator end = table_.end();
194
195         for (; it != end; ++it) {
196                 // range (pos,pos+x) becomes (pos+1,pos+x+1)
197                 if (it->range.start >= pos) {
198                         ++(it->range.start);
199                 }
200
201                 // range (pos-x,pos) stays as it is
202                 if (it->range.end > pos) {
203                         ++(it->range.end);
204                 }
205         }
206
207         set(change, pos, pos + 1); // set will call merge
208 }
209
210
211 Change const Changes::lookup(pos_type const pos) const
212 {
213         ChangeTable::const_iterator it = table_.begin();
214         ChangeTable::const_iterator const end = table_.end();
215
216         for (; it != end; ++it) {
217                 if (it->range.contains(pos))
218                         return it->change;
219         }
220
221         BOOST_ASSERT(false && "missing changes for pos");
222         return Change(Change::UNCHANGED);
223 }
224
225
226 bool Changes::isChanged(pos_type const start, pos_type const end) const
227 {
228         ChangeTable::const_iterator it = table_.begin();
229         ChangeTable::const_iterator const itend = table_.end();
230
231         for (; it != itend; ++it) {
232                 if (lyxerr.debugging(Debug::CHANGES)) {
233                         lyxerr[Debug::CHANGES] << "Looking for " << start << ","
234                                 << end << " in " << it->range.start << ","
235                                 << it->range.end << "of type " << it->change.type << endl;
236                 }
237
238                 if (it->range.intersects(Range(start, end))
239                         && it->change.type != Change::UNCHANGED) {
240                         if (lyxerr.debugging(Debug::CHANGES)) {
241                                 lyxerr[Debug::CHANGES] << "Found intersection of "
242                                         << start << "," << end << " with "
243                                         << it->range.start << "," << it->range.end
244                                         << " of type " << it->change.type << endl;
245                         }
246                         return true;
247                 }
248         }
249
250         return false;
251 }
252
253
254 void Changes::merge()
255 {
256         if (lyxerr.debugging(Debug::CHANGES))
257                 lyxerr[Debug::CHANGES] << "Starting merge" << endl;
258
259         ChangeTable::iterator it = table_.begin();
260
261         while (it != table_.end()) {
262                 if (lyxerr.debugging(Debug::CHANGES)) {
263                         lyxerr[Debug::CHANGES] << "Range of type " << it->change.type << " is "
264                                 << it->range.start << "," << it->range.end << endl;
265                 }
266
267                 if (it->range.start == it->range.end) {
268                         if (lyxerr.debugging(Debug::CHANGES)) {
269                                 lyxerr[Debug::CHANGES] << "Removing empty range for pos "
270                                         << it->range.start << endl;
271                         }
272
273                         table_.erase(it);
274                         // start again
275                         it = table_.begin();
276                         continue;
277                 }
278
279                 if (it + 1 == table_.end())
280                         break;
281
282                 if (it->change == (it + 1)->change) {
283                         if (lyxerr.debugging(Debug::CHANGES)) {
284                                 lyxerr[Debug::CHANGES] << "Merging equal ranges "
285                                         << it->range.start << "," << it->range.end
286                                         << " and " << (it + 1)->range.start << ","
287                                         << (it + 1)->range.end << endl;
288                         }
289
290                         (it + 1)->range.start = it->range.start;
291                         table_.erase(it);
292                         // start again
293                         it = table_.begin();
294                         continue;
295                 }
296
297                 ++it;
298         }
299
300         lyxerr[Debug::CHANGES] << "Merge ended" << endl;
301 }
302
303
304 int Changes::latexMarkChange(odocstream & os,
305                              Change::Type const old, Change::Type const change,
306                              bool const & output)
307 {
308         if (!output || old == change)
309                 return 0;
310
311         static docstring const start(from_ascii("\\changestart{}"));
312         static docstring const end(from_ascii("\\changeend{}"));
313         static docstring const son(from_ascii("\\overstrikeon{}"));
314         static docstring const soff(from_ascii("\\overstrikeoff{}"));
315
316         int column = 0;
317
318         if (old == Change::DELETED) {
319                 os << soff;
320                 column += soff.length();
321         }
322
323         switch (change) {
324                 case Change::UNCHANGED:
325                         os << end;
326                         column += end.length();
327                         break;
328
329                 case Change::DELETED:
330                         if (old == Change::UNCHANGED) {
331                                 os << start;
332                                 column += start.length();
333                         }
334                         os << son;
335                         column += son.length();
336                         break;
337
338                 case Change::INSERTED:
339                         if (old == Change::UNCHANGED) {
340                                 os << start;
341                                 column += start.length();
342                         }
343                         break;
344         }
345
346         return column;
347 }
348
349
350 void Changes::lyxMarkChange(std::ostream & os, int & column,
351                             time_type const curtime,
352                             Change const & old, Change const & change)
353 {
354         if (old == change)
355                 return;
356
357         column = 0;
358
359         switch (change.type) {
360                 case Change::UNCHANGED:
361                         os << "\n\\change_unchanged\n";
362                         break;
363
364                 case Change::DELETED: {
365                         time_type t = change.changetime;
366                         if (!t)
367                                 t = curtime;
368                         os << "\n\\change_deleted " << change.author
369                                 << " " << t << "\n";
370
371                         break;
372                 }
373
374         case Change::INSERTED: {
375                         time_type t = change.changetime;
376                         if (!t)
377                                 t = curtime;
378                         os << "\n\\change_inserted " << change.author
379                                 << " " << t << "\n";
380                         break;
381         }
382         }
383 }
384
385
386 } // namespace lyx