]> git.lyx.org Git - lyx.git/blob - src/changes.C
* src/LaTeX.C
[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.
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. 
38  */
39
40 bool Change::isSimilarTo(Change const & change)
41 {
42         if (type != change.type) {
43                 return false;
44         }
45
46         if (type == Change::UNCHANGED) {
47                 return true;
48         }
49
50         return author == change.author;
51 }
52
53
54 bool operator==(Change const & l, Change const & r)
55 {
56         if (l.type != r.type) {
57                 return false;
58         }
59
60         // two changes of type UNCHANGED are always equal
61         if (l.type == Change::UNCHANGED) {
62                 return true;
63         }
64         
65         return l.author == r.author &&
66                l.changetime == r.changetime;
67 }
68
69
70 bool operator!=(Change const & l, Change const & r)
71 {
72         return !(l == r);
73 }
74
75
76 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
77 {
78         return r1.start == r2.start && r1.end == r2.end;
79 }
80
81
82 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
83 {
84         return !(r1 == r2);
85 }
86
87
88 bool Changes::Range::intersects(Range const & r) const
89 {
90         return r.start < end && r.end > start; // end itself is not in the range!
91 }
92
93
94 void Changes::set(Change const & change, pos_type const pos)
95 {
96         set(change, pos, pos + 1);
97 }
98
99
100 void Changes::set(Change const & change, pos_type const start, pos_type const end)
101 {
102         if (lyxerr.debugging(Debug::CHANGES)) {
103                 lyxerr[Debug::CHANGES] << "setting change (type: " << change.type
104                         << ", author: " << change.author << ", time: " << change.changetime
105                         << ") in range (" << start << ", " << end << ")" << endl;
106         }
107
108         Range const newRange(start, end);
109
110         ChangeTable::iterator it = table_.begin();
111
112         for (; it != table_.end(); ) {
113                 // current change starts like or follows new change
114                 if (it->range.start >= start) {
115                         break;
116                 }
117
118                 // new change intersects with existing change
119                 if (it->range.end > start) {
120                         pos_type oldEnd = it->range.end;
121                         it->range.end = start;
122                         if (lyxerr.debugging(Debug::CHANGES)) {
123                                 lyxerr[Debug::CHANGES] << "  cutting tail of type " << it->change.type
124                                         << " resulting in range (" << it->range.start << ", "
125                                         << it->range.end << ")" << endl;
126                         }
127                         ++it;
128                         if (oldEnd >= end) {
129                                 if (lyxerr.debugging(Debug::CHANGES)) {
130                                         lyxerr[Debug::CHANGES] << "  inserting tail in range ("
131                                                 << end << ", " << oldEnd << ")" << endl;
132                                 }
133                                 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
134                         }
135                         continue;
136                 }
137
138                 ++it;
139         }
140
141         if (change.type != Change::UNCHANGED) {
142                 if (lyxerr.debugging(Debug::CHANGES)) {
143                         lyxerr[Debug::CHANGES] << "  inserting change" << endl;
144                 }
145                 it = table_.insert(it, ChangeRange(change, Range(start, end)));
146                 ++it;
147         }
148
149         for (; it != table_.end(); ) {
150                 // new change 'contains' existing change
151                 if (newRange.contains(it->range)) {
152                         if (lyxerr.debugging(Debug::CHANGES)) {
153                                 lyxerr[Debug::CHANGES] << "  removing subrange ("
154                                         << it->range.start << ", " << it->range.end << ")" << endl;
155                         }
156                         it = table_.erase(it);
157                         continue;
158                 }
159
160                 // new change precedes existing change
161                 if (it->range.start >= end) {
162                         break;
163                 }
164
165                 // new change intersects with existing change
166                 it->range.start = end;
167                 if (lyxerr.debugging(Debug::CHANGES)) {
168                         lyxerr[Debug::CHANGES] << "  cutting head of type "
169                                 << it->change.type << " resulting in range ("
170                                 << end << ", " << it->range.end << ")" << endl;
171                 }
172                 break; // no need for another iteration
173         }
174
175         merge();
176 }
177
178
179 void Changes::erase(pos_type const pos)
180 {
181         if (lyxerr.debugging(Debug::CHANGES)) {
182                 lyxerr[Debug::CHANGES] << "Erasing change at position " << pos << endl;
183         }
184
185         ChangeTable::iterator it = table_.begin();
186         ChangeTable::iterator end = table_.end();
187
188         for (; it != end; ++it) {
189                 // range (pos,pos+x) becomes (pos,pos+x-1)
190                 if (it->range.start > pos) {
191                         --(it->range.start);
192                 }
193                 // range (pos-x,pos) stays (pos-x,pos)
194                 if (it->range.end > pos) {
195                         --(it->range.end);
196                 }
197         }
198
199         merge();
200 }
201
202
203 void Changes::insert(Change const & change, lyx::pos_type pos)
204 {
205         if (lyxerr.debugging(Debug::CHANGES)) {
206                 lyxerr[Debug::CHANGES] << "Inserting change of type " << change.type
207                         << " at position " << pos << endl;
208         }
209
210         ChangeTable::iterator it = table_.begin();
211         ChangeTable::iterator end = table_.end();
212
213         for (; it != end; ++it) {
214                 // range (pos,pos+x) becomes (pos+1,pos+x+1)
215                 if (it->range.start >= pos) {
216                         ++(it->range.start);
217                 }
218
219                 // range (pos-x,pos) stays as it is
220                 if (it->range.end > pos) {
221                         ++(it->range.end);
222                 }
223         }
224
225         set(change, pos, pos + 1); // set will call merge
226 }
227
228
229 Change const & Changes::lookup(pos_type const pos) const
230 {
231         static Change const noChange = 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 noChange;
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.isSimilarTo((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