]> git.lyx.org Git - lyx.git/blob - src/Changes.cpp
LaTeXFeatures.cpp: fix part of http://bugzilla.lyx.org/show_bug.cgi?id=5294 also...
[lyx.git] / src / Changes.cpp
1 /**
2  * \file Changes.cpp
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 "Author.h"
18 #include "BufferParams.h"
19 #include "LaTeXFeatures.h"
20
21 #include "support/debug.h"
22
23 #include "support/lassert.h"
24
25 #include <ostream>
26
27 using namespace std;
28
29 namespace lyx {
30
31 /*
32  * Class Change has a changetime field that specifies the exact time at which
33  * a specific change was made. The change time is used as a guidance for the
34  * user while editing his document. Presently, it is not considered for LaTeX
35  * export.
36  * When merging two adjacent changes, the changetime is not considered,
37  * only the equality of the change type and author is checked (in method
38  * isSimilarTo(...)). If two changes are in fact merged (in method merge()),
39  * the later change time is preserved.
40  */
41
42 bool Change::isSimilarTo(Change const & change) const
43 {
44         if (type != change.type)
45                 return false;
46
47         if (type == Change::UNCHANGED)
48                 return true;
49
50         return author == change.author;
51 }
52
53
54 ColorCode Change::color() const
55 {
56         ColorCode color = Color_none;
57         switch (author % 5) {
58                 case 0:
59                         color = Color_changedtextauthor1;
60                         break;
61                 case 1:
62                         color = Color_changedtextauthor2;
63                         break;
64                 case 2:
65                         color = Color_changedtextauthor3;
66                         break;
67                 case 3:
68                         color = Color_changedtextauthor4;
69                         break;
70                 case 4:
71                         color = Color_changedtextauthor5;
72                         break;
73         }
74         return color;
75 }
76
77
78 bool operator==(Change const & l, Change const & r)
79 {
80         if (l.type != r.type)
81                 return false;
82
83         // two changes of type UNCHANGED are always equal
84         if (l.type == Change::UNCHANGED)
85                 return true;
86
87         return l.author == r.author && l.changetime == r.changetime;
88 }
89
90
91 bool operator!=(Change const & l, Change const & r)
92 {
93         return !(l == r);
94 }
95
96
97 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
98 {
99         return r1.start == r2.start && r1.end == r2.end;
100 }
101
102
103 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
104 {
105         return !(r1 == r2);
106 }
107
108
109 bool Changes::Range::intersects(Range const & r) const
110 {
111         return r.start < end && r.end > start; // end itself is not in the range!
112 }
113
114
115 void Changes::set(Change const & change, pos_type const pos)
116 {
117         set(change, pos, pos + 1);
118 }
119
120
121 void Changes::set(Change const & change, pos_type const start, pos_type const end)
122 {
123         if (change.type != Change::UNCHANGED) {
124                 LYXERR(Debug::CHANGES, "setting change (type: " << change.type
125                         << ", author: " << change.author 
126                         << ", time: " << long(change.changetime)
127                         << ") in range (" << start << ", " << end << ")");
128         }
129
130         Range const newRange(start, end);
131
132         ChangeTable::iterator it = table_.begin();
133
134         for (; it != table_.end(); ) {
135                 // current change starts like or follows new change
136                 if (it->range.start >= start) {
137                         break;
138                 }
139
140                 // new change intersects with existing change
141                 if (it->range.end > start) {
142                         pos_type oldEnd = it->range.end;
143                         it->range.end = start;
144
145                         LYXERR(Debug::CHANGES, "  cutting tail of type " << it->change.type
146                                 << " resulting in range (" << it->range.start << ", "
147                                 << it->range.end << ")");
148
149                         ++it;
150                         if (oldEnd >= end) {
151                                 LYXERR(Debug::CHANGES, "  inserting tail in range ("
152                                         << end << ", " << oldEnd << ")");
153                                 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
154                         }
155                         continue;
156                 }
157
158                 ++it;
159         }
160
161         if (change.type != Change::UNCHANGED) {
162                 LYXERR(Debug::CHANGES, "  inserting change");
163                 it = table_.insert(it, ChangeRange(change, Range(start, end)));
164                 ++it;
165         }
166
167         for (; it != table_.end(); ) {
168                 // new change 'contains' existing change
169                 if (newRange.contains(it->range)) {
170                         LYXERR(Debug::CHANGES, "  removing subrange ("
171                                 << it->range.start << ", " << it->range.end << ")");
172                         it = table_.erase(it);
173                         continue;
174                 }
175
176                 // new change precedes existing change
177                 if (it->range.start >= end)
178                         break;
179
180                 // new change intersects with existing change
181                 it->range.start = end;
182                 LYXERR(Debug::CHANGES, "  cutting head of type "
183                         << it->change.type << " resulting in range ("
184                         << end << ", " << it->range.end << ")");
185                 break; // no need for another iteration
186         }
187
188         merge();
189 }
190
191
192 void Changes::erase(pos_type const pos)
193 {
194         LYXERR(Debug::CHANGES, "Erasing change at position " << pos);
195
196         ChangeTable::iterator it = table_.begin();
197         ChangeTable::iterator end = table_.end();
198
199         for (; it != end; ++it) {
200                 // range (pos,pos+x) becomes (pos,pos+x-1)
201                 if (it->range.start > pos)
202                         --(it->range.start);
203                 // range (pos-x,pos) stays (pos-x,pos)
204                 if (it->range.end > pos)
205                         --(it->range.end);
206         }
207
208         merge();
209 }
210
211
212 void Changes::insert(Change const & change, lyx::pos_type pos)
213 {
214         if (change.type != Change::UNCHANGED) {
215                 LYXERR(Debug::CHANGES, "Inserting change of type " << change.type
216                         << " at position " << pos);
217         }
218
219         ChangeTable::iterator it = table_.begin();
220         ChangeTable::iterator end = table_.end();
221
222         for (; it != end; ++it) {
223                 // range (pos,pos+x) becomes (pos+1,pos+x+1)
224                 if (it->range.start >= pos)
225                         ++(it->range.start);
226
227                 // range (pos-x,pos) stays as it is
228                 if (it->range.end > pos)
229                         ++(it->range.end);
230         }
231
232         set(change, pos, pos + 1); // set will call merge
233 }
234
235
236 Change const & Changes::lookup(pos_type const pos) const
237 {
238         static Change const noChange = Change(Change::UNCHANGED);
239
240         ChangeTable::const_iterator it = table_.begin();
241         ChangeTable::const_iterator const end = table_.end();
242
243         for (; it != end; ++it) {
244                 if (it->range.contains(pos))
245                         return it->change;
246         }
247
248         return noChange;
249 }
250
251
252 bool Changes::isChanged(pos_type const start, pos_type const end) const
253 {
254         ChangeTable::const_iterator it = table_.begin();
255         ChangeTable::const_iterator const itend = table_.end();
256
257         for (; it != itend; ++it) {
258                 if (it->range.intersects(Range(start, end))) {
259                         LYXERR(Debug::CHANGES, "found intersection of range ("
260                                 << start << ", " << end << ") with ("
261                                 << it->range.start << ", " << it->range.end
262                                 << ") of type " << it->change.type);
263                         return true;
264                 }
265         }
266         return false;
267 }
268
269
270 void Changes::merge()
271 {
272         ChangeTable::iterator it = table_.begin();
273
274         while (it != table_.end()) {
275                 LYXERR(Debug::CHANGES, "found change of type " << it->change.type
276                         << " and range (" << it->range.start << ", " << it->range.end
277                         << ")");
278
279                 if (it->range.start == it->range.end) {
280                         LYXERR(Debug::CHANGES, "removing empty range for pos "
281                                 << it->range.start);
282
283                         table_.erase(it);
284                         // start again
285                         it = table_.begin();
286                         continue;
287                 }
288
289                 if (it + 1 == table_.end())
290                         break;
291
292                 if (it->change.isSimilarTo((it + 1)->change)
293                     && it->range.end == (it + 1)->range.start) {
294                         LYXERR(Debug::CHANGES, "merging ranges (" << it->range.start << ", "
295                                 << it->range.end << ") and (" << (it + 1)->range.start << ", "
296                                 << (it + 1)->range.end << ")");
297
298                         (it + 1)->range.start = it->range.start;
299                         (it + 1)->change.changetime = max(it->change.changetime,
300                                                           (it + 1)->change.changetime);
301                         table_.erase(it);
302                         // start again
303                         it = table_.begin();
304                         continue;
305                 }
306
307                 ++it;
308         }
309 }
310
311
312 int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
313                              Change const & oldChange, Change const & change)
314 {
315         if (!bparams.outputChanges || oldChange == change)
316                 return 0;
317
318         int column = 0;
319
320         if (oldChange.type != Change::UNCHANGED) {
321                 os << '}'; // close \lyxadded or \lyxdeleted
322                 column++;
323         }
324
325         docstring chgTime;
326         chgTime += ctime(&change.changetime);
327         chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
328
329         if (change.type == Change::DELETED) {
330                 docstring str = "\\lyxdeleted{" +
331                         bparams.authors().get(change.author).name() + "}{" +
332                         chgTime + "}{";
333                 os << str;
334                 column += str.size();
335         } else if (change.type == Change::INSERTED) {
336                 docstring str = "\\lyxadded{" +
337                         bparams.authors().get(change.author).name() + "}{" +
338                         chgTime + "}{";
339                 os << str;
340                 column += str.size();
341         }
342
343         return column;
344 }
345
346
347 void Changes::lyxMarkChange(ostream & os, int & column,
348                             Change const & old, Change const & change)
349 {
350         if (old == change)
351                 return;
352
353         column = 0;
354
355         switch (change.type) {
356                 case Change::UNCHANGED:
357                         os << "\n\\change_unchanged\n";
358                         break;
359
360                 case Change::DELETED: {
361                         os << "\n\\change_deleted " << change.author
362                                 << " " << change.changetime << "\n";
363                         break;
364                 }
365
366                 case Change::INSERTED: {
367                         os << "\n\\change_inserted " << change.author
368                                 << " " << change.changetime << "\n";
369                         break;
370                 }
371         }
372 }
373
374
375 void Changes::checkAuthors(AuthorList const & authorList)
376 {
377         ChangeTable::const_iterator it = table_.begin();
378         ChangeTable::const_iterator endit = table_.end();
379         for ( ; it != endit ; ++it) 
380                 if (it->change.type != Change::UNCHANGED)
381                         authorList.get(it->change.author).setUsed(true);
382 }
383
384 } // namespace lyx