]> git.lyx.org Git - lyx.git/blob - src/Changes.cpp
a7cd525dde3bf608f31b1d82861415aac26faae1
[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 "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 (change.type != Change::UNCHANGED) {
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
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                                 LYXERR(Debug::CHANGES) << "  inserting tail in range ("
130                                         << end << ", " << oldEnd << ")" << endl;
131                                 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
132                         }
133                         continue;
134                 }
135
136                 ++it;
137         }
138
139         if (change.type != Change::UNCHANGED) {
140                 LYXERR(Debug::CHANGES) << "  inserting change" << endl;
141                 it = table_.insert(it, ChangeRange(change, Range(start, end)));
142                 ++it;
143         }
144
145         for (; it != table_.end(); ) {
146                 // new change 'contains' existing change
147                 if (newRange.contains(it->range)) {
148                         LYXERR(Debug::CHANGES) << "  removing subrange ("
149                                 << it->range.start << ", " << it->range.end << ")" << endl;
150                         it = table_.erase(it);
151                         continue;
152                 }
153
154                 // new change precedes existing change
155                 if (it->range.start >= end) {
156                         break;
157                 }
158
159                 // new change intersects with existing change
160                 it->range.start = end;
161                 LYXERR(Debug::CHANGES) << "  cutting head of type "
162                         << it->change.type << " resulting in range ("
163                         << end << ", " << it->range.end << ")" << endl;
164                 break; // no need for another iteration
165         }
166
167         merge();
168 }
169
170
171 void Changes::erase(pos_type const pos)
172 {
173         LYXERR(Debug::CHANGES) << "Erasing change at position " << pos << endl;
174
175         ChangeTable::iterator it = table_.begin();
176         ChangeTable::iterator end = table_.end();
177
178         for (; it != end; ++it) {
179                 // range (pos,pos+x) becomes (pos,pos+x-1)
180                 if (it->range.start > pos) {
181                         --(it->range.start);
182                 }
183                 // range (pos-x,pos) stays (pos-x,pos)
184                 if (it->range.end > pos) {
185                         --(it->range.end);
186                 }
187         }
188
189         merge();
190 }
191
192
193 void Changes::insert(Change const & change, lyx::pos_type pos)
194 {
195         if (change.type != Change::UNCHANGED) {
196                 LYXERR(Debug::CHANGES) << "Inserting change of type " << change.type
197                         << " at position " << pos << endl;
198         }
199
200         ChangeTable::iterator it = table_.begin();
201         ChangeTable::iterator end = table_.end();
202
203         for (; it != end; ++it) {
204                 // range (pos,pos+x) becomes (pos+1,pos+x+1)
205                 if (it->range.start >= pos) {
206                         ++(it->range.start);
207                 }
208
209                 // range (pos-x,pos) stays as it is
210                 if (it->range.end > pos) {
211                         ++(it->range.end);
212                 }
213         }
214
215         set(change, pos, pos + 1); // set will call merge
216 }
217
218
219 Change const & Changes::lookup(pos_type const pos) const
220 {
221         static Change const noChange = Change(Change::UNCHANGED);
222                 
223         ChangeTable::const_iterator it = table_.begin();
224         ChangeTable::const_iterator const end = table_.end();
225
226         for (; it != end; ++it) {
227                 if (it->range.contains(pos))
228                         return it->change;
229         }
230
231         return noChange;
232 }
233
234
235 bool Changes::isChanged(pos_type const start, pos_type const end) const
236 {
237         ChangeTable::const_iterator it = table_.begin();
238         ChangeTable::const_iterator const itend = table_.end();
239
240         for (; it != itend; ++it) {
241                 if (it->range.intersects(Range(start, end))) {
242                         LYXERR(Debug::CHANGES) << "found intersection of range ("
243                                 << start << ", " << end << ") with ("
244                                 << it->range.start << ", " << it->range.end
245                                 << ") of type " << it->change.type << endl;
246                         return true;
247                 }
248         }
249         return false;
250 }
251
252
253 void Changes::merge()
254 {
255         ChangeTable::iterator it = table_.begin();
256
257         while (it != table_.end()) {
258                 LYXERR(Debug::CHANGES) << "found change of type " << it->change.type
259                         << " and range (" << it->range.start << ", " << it->range.end
260                         << ")" << endl;
261
262                 if (it->range.start == it->range.end) {
263                         LYXERR(Debug::CHANGES) << "removing empty range for pos "
264                                 << it->range.start << endl;
265
266                         table_.erase(it);
267                         // start again
268                         it = table_.begin();
269                         continue;
270                 }
271
272                 if (it + 1 == table_.end())
273                         break;
274
275                 if (it->change.isSimilarTo((it + 1)->change) && it->range.end == (it + 1)->range.start) {
276                         LYXERR(Debug::CHANGES) << "merging ranges (" << it->range.start << ", "
277                                 << it->range.end << ") and (" << (it + 1)->range.start << ", "
278                                 << (it + 1)->range.end << ")" << endl;
279
280                         (it + 1)->range.start = it->range.start;
281                         (it + 1)->change.changetime = max(it->change.changetime,
282                                                           (it + 1)->change.changetime);
283                         table_.erase(it);
284                         // start again
285                         it = table_.begin();
286                         continue;
287                 }
288
289                 ++it;
290         }
291 }
292
293
294 int Changes::latexMarkChange(odocstream & os,
295                              Change::Type const oldChangeType, Change::Type const changeType,
296                              bool const & output)
297 {
298         if (!output || oldChangeType == changeType)
299                 return 0;
300
301         static docstring const start(from_ascii("\\changestart{}"));
302         static docstring const end(from_ascii("\\changeend{}"));
303         static docstring const son(from_ascii("\\overstrikeon{}"));
304         static docstring const soff(from_ascii("\\overstrikeoff{}"));
305
306         int column = 0;
307
308         if (oldChangeType == Change::DELETED) {
309                 os << soff;
310                 column += soff.length();
311         }
312
313         switch (changeType) {
314                 case Change::UNCHANGED:
315                         os << end;
316                         column += end.length();
317                         break;
318
319                 case Change::DELETED:
320                         if (oldChangeType == Change::UNCHANGED) {
321                                 os << start;
322                                 column += start.length();
323                         }
324                         os << son;
325                         column += son.length();
326                         break;
327
328                 case Change::INSERTED:
329                         if (oldChangeType == Change::UNCHANGED) {
330                                 os << start;
331                                 column += start.length();
332                         }
333                         break;
334         }
335
336         return column;
337 }
338
339
340 void Changes::lyxMarkChange(std::ostream & os, int & column,
341                             Change const & old, Change const & change)
342 {
343         if (old == change)
344                 return;
345
346         column = 0;
347
348         switch (change.type) {
349                 case Change::UNCHANGED:
350                         os << "\n\\change_unchanged\n";
351                         break;
352
353                 case Change::DELETED: {
354                         os << "\n\\change_deleted " << change.author
355                                 << " " << change.changetime << "\n";
356                         break;
357                 }
358
359                 case Change::INSERTED: {
360                         os << "\n\\change_inserted " << change.author
361                                 << " " << change.changetime << "\n";
362                         break;
363                 }
364         }
365 }
366
367
368 } // namespace lyx