]> git.lyx.org Git - features.git/blob - src/Changes.cpp
MSVC compile fix.
[features.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 "Author.h"
18 #include "BufferParams.h"
19 #include "LaTeXFeatures.h"
20
21 #include "support/debug.h"
22
23 #include <boost/assert.hpp>
24
25 #include <ostream>
26
27 namespace lyx {
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         if (type == Change::UNCHANGED)
46                 return true;
47
48         return author == change.author;
49 }
50
51
52 bool operator==(Change const & l, Change const & r)
53 {
54         if (l.type != r.type)
55                 return false;
56
57         // two changes of type UNCHANGED are always equal
58         if (l.type == Change::UNCHANGED)
59                 return true;
60
61         return l.author == r.author && l.changetime == r.changetime;
62 }
63
64
65 bool operator!=(Change const & l, Change const & r)
66 {
67         return !(l == r);
68 }
69
70
71 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
72 {
73         return r1.start == r2.start && r1.end == r2.end;
74 }
75
76
77 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
78 {
79         return !(r1 == r2);
80 }
81
82
83 bool Changes::Range::intersects(Range const & r) const
84 {
85         return r.start < end && r.end > start; // end itself is not in the range!
86 }
87
88
89 void Changes::set(Change const & change, pos_type const pos)
90 {
91         set(change, pos, pos + 1);
92 }
93
94
95 void Changes::set(Change const & change, pos_type const start, pos_type const end)
96 {
97         if (change.type != Change::UNCHANGED) {
98                 LYXERR(Debug::CHANGES, "setting change (type: " << change.type
99                         << ", author: " << change.author 
100                         << ", time: " << long(change.changetime)
101                         << ") in range (" << start << ", " << end << ")");
102         }
103
104         Range const newRange(start, end);
105
106         ChangeTable::iterator it = table_.begin();
107
108         for (; it != table_.end(); ) {
109                 // current change starts like or follows new change
110                 if (it->range.start >= start) {
111                         break;
112                 }
113
114                 // new change intersects with existing change
115                 if (it->range.end > start) {
116                         pos_type oldEnd = it->range.end;
117                         it->range.end = start;
118
119                         LYXERR(Debug::CHANGES, "  cutting tail of type " << it->change.type
120                                 << " resulting in range (" << it->range.start << ", "
121                                 << it->range.end << ")");
122
123                         ++it;
124                         if (oldEnd >= end) {
125                                 LYXERR(Debug::CHANGES, "  inserting tail in range ("
126                                         << end << ", " << oldEnd << ")");
127                                 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
128                         }
129                         continue;
130                 }
131
132                 ++it;
133         }
134
135         if (change.type != Change::UNCHANGED) {
136                 LYXERR(Debug::CHANGES, "  inserting change");
137                 it = table_.insert(it, ChangeRange(change, Range(start, end)));
138                 ++it;
139         }
140
141         for (; it != table_.end(); ) {
142                 // new change 'contains' existing change
143                 if (newRange.contains(it->range)) {
144                         LYXERR(Debug::CHANGES, "  removing subrange ("
145                                 << it->range.start << ", " << it->range.end << ")");
146                         it = table_.erase(it);
147                         continue;
148                 }
149
150                 // new change precedes existing change
151                 if (it->range.start >= end)
152                         break;
153
154                 // new change intersects with existing change
155                 it->range.start = end;
156                 LYXERR(Debug::CHANGES, "  cutting head of type "
157                         << it->change.type << " resulting in range ("
158                         << end << ", " << it->range.end << ")");
159                 break; // no need for another iteration
160         }
161
162         merge();
163 }
164
165
166 void Changes::erase(pos_type const pos)
167 {
168         LYXERR(Debug::CHANGES, "Erasing change at position " << pos);
169
170         ChangeTable::iterator it = table_.begin();
171         ChangeTable::iterator end = table_.end();
172
173         for (; it != end; ++it) {
174                 // range (pos,pos+x) becomes (pos,pos+x-1)
175                 if (it->range.start > pos)
176                         --(it->range.start);
177                 // range (pos-x,pos) stays (pos-x,pos)
178                 if (it->range.end > pos)
179                         --(it->range.end);
180         }
181
182         merge();
183 }
184
185
186 void Changes::insert(Change const & change, lyx::pos_type pos)
187 {
188         if (change.type != Change::UNCHANGED) {
189                 LYXERR(Debug::CHANGES, "Inserting change of type " << change.type
190                         << " at position " << pos);
191         }
192
193         ChangeTable::iterator it = table_.begin();
194         ChangeTable::iterator end = table_.end();
195
196         for (; it != end; ++it) {
197                 // range (pos,pos+x) becomes (pos+1,pos+x+1)
198                 if (it->range.start >= pos)
199                         ++(it->range.start);
200
201                 // range (pos-x,pos) stays as it is
202                 if (it->range.end > pos)
203                         ++(it->range.end);
204         }
205
206         set(change, pos, pos + 1); // set will call merge
207 }
208
209
210 Change const & Changes::lookup(pos_type const pos) const
211 {
212         static Change const noChange = Change(Change::UNCHANGED);
213
214         ChangeTable::const_iterator it = table_.begin();
215         ChangeTable::const_iterator const end = table_.end();
216
217         for (; it != end; ++it) {
218                 if (it->range.contains(pos))
219                         return it->change;
220         }
221
222         return noChange;
223 }
224
225
226 bool Changes::isChanged(pos_type const start, pos_type const end) const
227 {
228         ChangeTable::const_iterator it = table_.begin();
229         ChangeTable::const_iterator const itend = table_.end();
230
231         for (; it != itend; ++it) {
232                 if (it->range.intersects(Range(start, end))) {
233                         LYXERR(Debug::CHANGES, "found intersection of range ("
234                                 << start << ", " << end << ") with ("
235                                 << it->range.start << ", " << it->range.end
236                                 << ") of type " << it->change.type);
237                         return true;
238                 }
239         }
240         return false;
241 }
242
243
244 void Changes::merge()
245 {
246         ChangeTable::iterator it = table_.begin();
247
248         while (it != table_.end()) {
249                 LYXERR(Debug::CHANGES, "found change of type " << it->change.type
250                         << " and range (" << it->range.start << ", " << it->range.end
251                         << ")");
252
253                 if (it->range.start == it->range.end) {
254                         LYXERR(Debug::CHANGES, "removing empty range for pos "
255                                 << it->range.start);
256
257                         table_.erase(it);
258                         // start again
259                         it = table_.begin();
260                         continue;
261                 }
262
263                 if (it + 1 == table_.end())
264                         break;
265
266                 if (it->change.isSimilarTo((it + 1)->change)
267                     && it->range.end == (it + 1)->range.start) {
268                         LYXERR(Debug::CHANGES, "merging ranges (" << it->range.start << ", "
269                                 << it->range.end << ") and (" << (it + 1)->range.start << ", "
270                                 << (it + 1)->range.end << ")");
271
272                         (it + 1)->range.start = it->range.start;
273                         (it + 1)->change.changetime = std::max(it->change.changetime,
274                                                           (it + 1)->change.changetime);
275                         table_.erase(it);
276                         // start again
277                         it = table_.begin();
278                         continue;
279                 }
280
281                 ++it;
282         }
283 }
284
285
286 int Changes::latexMarkChange(odocstream & os, BufferParams const & bparams,
287                              Change const & oldChange, Change const & change)
288 {
289         if (!bparams.outputChanges || oldChange == change)
290                 return 0;
291
292         int column = 0;
293
294         if (oldChange.type != Change::UNCHANGED) {
295                 os << '}'; // close \lyxadded or \lyxdeleted
296                 column++;
297         }
298
299         docstring chgTime;
300         chgTime += ctime(&change.changetime);
301         chgTime.erase(chgTime.end() - 1); // remove trailing '\n'
302
303         if (change.type == Change::DELETED) {
304                 docstring str = "\\lyxdeleted{" +
305                         bparams.authors().get(change.author).name() + "}{" +
306                         chgTime + "}{";
307                 os << str;
308                 column += str.size();
309         } else if (change.type == Change::INSERTED) {
310                 docstring str = "\\lyxadded{" +
311                         bparams.authors().get(change.author).name() + "}{" +
312                         chgTime + "}{";
313                 os << str;
314                 column += str.size();
315         }
316
317         return column;
318 }
319
320
321 void Changes::lyxMarkChange(std::ostream & os, int & column,
322                             Change const & old, Change const & change)
323 {
324         if (old == change)
325                 return;
326
327         column = 0;
328
329         switch (change.type) {
330                 case Change::UNCHANGED:
331                         os << "\n\\change_unchanged\n";
332                         break;
333
334                 case Change::DELETED: {
335                         os << "\n\\change_deleted " << change.author
336                                 << " " << change.changetime << "\n";
337                         break;
338                 }
339
340                 case Change::INSERTED: {
341                         os << "\n\\change_inserted " << change.author
342                                 << " " << change.changetime << "\n";
343                         break;
344                 }
345         }
346 }
347
348
349 void Changes::checkAuthors(AuthorList const & authorList)
350 {
351         ChangeTable::const_iterator it = table_.begin();
352         ChangeTable::const_iterator endit = table_.end();
353         for ( ; it != endit ; ++it) 
354                 if (it->change.type != Change::UNCHANGED)
355                         authorList.get(it->change.author).setUsed(true);
356 }
357
358 } // namespace lyx