]> git.lyx.org Git - lyx.git/blob - src/changes.C
hopefully fix tex2lyx linking.
[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         if (table_.empty()) {
236                 return Change(Change::UNCHANGED);
237         }
238         ChangeTable::const_iterator it = table_.begin();
239         ChangeTable::const_iterator const end = table_.end();
240
241         for (; it != end; ++it) {
242                 if (it->range.contains(pos))
243                         return it->change;
244         }
245
246         return Change(Change::UNCHANGED);
247 }
248
249
250 bool Changes::isChanged(pos_type const start, pos_type const end) const
251 {
252         ChangeTable::const_iterator it = table_.begin();
253         ChangeTable::const_iterator const itend = table_.end();
254
255         for (; it != itend; ++it) {
256                 if (it->range.intersects(Range(start, end))) {
257                         if (lyxerr.debugging(Debug::CHANGES)) {
258                                 lyxerr[Debug::CHANGES] << "found intersection of range ("
259                                         << start << ", " << end << ") with ("
260                                         << it->range.start << ", " << it->range.end
261                                         << ") of type " << it->change.type << endl;
262                         }
263                         return true;
264                 }
265         }
266         return false;
267 }
268
269
270 void Changes::merge()
271 {
272         if (lyxerr.debugging(Debug::CHANGES)) {
273                 lyxerr[Debug::CHANGES] << "merging changes..." << endl;
274         }
275
276         ChangeTable::iterator it = table_.begin();
277
278         while (it != table_.end()) {
279                 if (lyxerr.debugging(Debug::CHANGES)) {
280                         lyxerr[Debug::CHANGES] << "  found change of type " << it->change.type
281                                 << " and range (" << it->range.start << ", " << it->range.end
282                                 << ")" << endl;
283                 }
284
285                 if (it->range.start == it->range.end) {
286                         if (lyxerr.debugging(Debug::CHANGES)) {
287                                 lyxerr[Debug::CHANGES] << "  removing empty range for pos "
288                                         << it->range.start << endl;
289                         }
290
291                         table_.erase(it);
292                         // start again
293                         it = table_.begin();
294                         continue;
295                 }
296
297                 if (it + 1 == table_.end())
298                         break;
299
300                 if (it->change.isSimilarTo((it + 1)->change) && it->range.end == (it + 1)->range.start) {
301                         if (lyxerr.debugging(Debug::CHANGES)) {
302                                 lyxerr[Debug::CHANGES] << "  merging ranges (" << it->range.start << ", "
303                                         << it->range.end << ") and (" << (it + 1)->range.start << ", "
304                                         << (it + 1)->range.end << ")" << endl;
305                         }
306                         (it + 1)->range.start = it->range.start;
307                         (it + 1)->change.changetime = max(it->change.changetime,
308                                                           (it + 1)->change.changetime);
309                         table_.erase(it);
310                         // start again
311                         it = table_.begin();
312                         continue;
313                 }
314
315                 ++it;
316         }
317 }
318
319
320 int Changes::latexMarkChange(odocstream & os,
321                              Change::Type const oldChangeType, Change::Type const changeType,
322                              bool const & output)
323 {
324         if (!output || oldChangeType == changeType)
325                 return 0;
326
327         static docstring const start(from_ascii("\\changestart{}"));
328         static docstring const end(from_ascii("\\changeend{}"));
329         static docstring const son(from_ascii("\\overstrikeon{}"));
330         static docstring const soff(from_ascii("\\overstrikeoff{}"));
331
332         int column = 0;
333
334         if (oldChangeType == Change::DELETED) {
335                 os << soff;
336                 column += soff.length();
337         }
338
339         switch (changeType) {
340                 case Change::UNCHANGED:
341                         os << end;
342                         column += end.length();
343                         break;
344
345                 case Change::DELETED:
346                         if (oldChangeType == Change::UNCHANGED) {
347                                 os << start;
348                                 column += start.length();
349                         }
350                         os << son;
351                         column += son.length();
352                         break;
353
354                 case Change::INSERTED:
355                         if (oldChangeType == Change::UNCHANGED) {
356                                 os << start;
357                                 column += start.length();
358                         }
359                         break;
360         }
361
362         return column;
363 }
364
365
366 void Changes::lyxMarkChange(std::ostream & os, int & column,
367                             Change const & old, Change const & change)
368 {
369         if (old == change)
370                 return;
371
372         column = 0;
373
374         switch (change.type) {
375                 case Change::UNCHANGED:
376                         os << "\n\\change_unchanged\n";
377                         break;
378
379                 case Change::DELETED: {
380                         os << "\n\\change_deleted " << change.author
381                                 << " " << change.changetime << "\n";
382                         break;
383                 }
384
385                 case Change::INSERTED: {
386                         os << "\n\\change_inserted " << change.author
387                                 << " " << change.changetime << "\n";
388                         break;
389                 }
390         }
391 }
392
393
394 } // namespace lyx