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