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