]> git.lyx.org Git - lyx.git/blob - src/changes.C
Scons: update_po should now work (missing dependency though)
[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 (change.type != Change::UNCHANGED && 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 (change.type != Change::UNCHANGED && 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         ChangeTable::iterator it = table_.begin();
268
269         while (it != table_.end()) {
270                 if (lyxerr.debugging(Debug::CHANGES)) {
271                         lyxerr[Debug::CHANGES] << "found change of type " << it->change.type
272                                 << " and range (" << it->range.start << ", " << it->range.end
273                                 << ")" << endl;
274                 }
275
276                 if (it->range.start == it->range.end) {
277                         if (lyxerr.debugging(Debug::CHANGES)) {
278                                 lyxerr[Debug::CHANGES] << "removing empty range for pos "
279                                         << it->range.start << endl;
280                         }
281
282                         table_.erase(it);
283                         // start again
284                         it = table_.begin();
285                         continue;
286                 }
287
288                 if (it + 1 == table_.end())
289                         break;
290
291                 if (it->change.isSimilarTo((it + 1)->change) && it->range.end == (it + 1)->range.start) {
292                         if (lyxerr.debugging(Debug::CHANGES)) {
293                                 lyxerr[Debug::CHANGES] << "merging ranges (" << it->range.start << ", "
294                                         << it->range.end << ") and (" << (it + 1)->range.start << ", "
295                                         << (it + 1)->range.end << ")" << endl;
296                         }
297                         (it + 1)->range.start = it->range.start;
298                         (it + 1)->change.changetime = max(it->change.changetime,
299                                                           (it + 1)->change.changetime);
300                         table_.erase(it);
301                         // start again
302                         it = table_.begin();
303                         continue;
304                 }
305
306                 ++it;
307         }
308 }
309
310
311 int Changes::latexMarkChange(odocstream & os,
312                              Change::Type const oldChangeType, Change::Type const changeType,
313                              bool const & output)
314 {
315         if (!output || oldChangeType == changeType)
316                 return 0;
317
318         static docstring const start(from_ascii("\\changestart{}"));
319         static docstring const end(from_ascii("\\changeend{}"));
320         static docstring const son(from_ascii("\\overstrikeon{}"));
321         static docstring const soff(from_ascii("\\overstrikeoff{}"));
322
323         int column = 0;
324
325         if (oldChangeType == Change::DELETED) {
326                 os << soff;
327                 column += soff.length();
328         }
329
330         switch (changeType) {
331                 case Change::UNCHANGED:
332                         os << end;
333                         column += end.length();
334                         break;
335
336                 case Change::DELETED:
337                         if (oldChangeType == Change::UNCHANGED) {
338                                 os << start;
339                                 column += start.length();
340                         }
341                         os << son;
342                         column += son.length();
343                         break;
344
345                 case Change::INSERTED:
346                         if (oldChangeType == Change::UNCHANGED) {
347                                 os << start;
348                                 column += start.length();
349                         }
350                         break;
351         }
352
353         return column;
354 }
355
356
357 void Changes::lyxMarkChange(std::ostream & os, int & column,
358                             Change const & old, Change const & change)
359 {
360         if (old == change)
361                 return;
362
363         column = 0;
364
365         switch (change.type) {
366                 case Change::UNCHANGED:
367                         os << "\n\\change_unchanged\n";
368                         break;
369
370                 case Change::DELETED: {
371                         os << "\n\\change_deleted " << change.author
372                                 << " " << change.changetime << "\n";
373                         break;
374                 }
375
376                 case Change::INSERTED: {
377                         os << "\n\\change_inserted " << change.author
378                                 << " " << change.changetime << "\n";
379                         break;
380                 }
381         }
382 }
383
384
385 } // namespace lyx