]> git.lyx.org Git - lyx.git/blob - src/Changes.cpp
* src/LyXRC.{cpp,h}:
[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 #include "Author.h"
19 #include "BufferParams.h"
20 #include "LaTeXFeatures.h"
21
22 #include <boost/assert.hpp>
23
24 using std::abs;
25 using std::endl;
26 using std::string;
27 using std::max;
28
29 namespace lyx {
30
31 /*
32  * Class Change has a changetime field that specifies the exact time at which
33  * a specific change was made. The change time is used as a guidance for the
34  * user while editing his document. Presently, it is not considered for LaTeX
35  * export.
36  * When merging two adjacent changes, the changetime is not considered,
37  * only the equality of the change type and author is checked (in method
38  * isSimilarTo(...)). If two changes are in fact merged (in method merge()),
39  * the later change time is preserved.
40  */
41
42 bool Change::isSimilarTo(Change const & change)
43 {
44         if (type != change.type)
45                 return false;
46
47         if (type == Change::UNCHANGED)
48                 return true;
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         // two changes of type UNCHANGED are always equal
60         if (l.type == Change::UNCHANGED)
61                 return true;
62
63         return l.author == r.author && l.changetime == r.changetime;
64 }
65
66
67 bool operator!=(Change const & l, Change const & r)
68 {
69         return !(l == r);
70 }
71
72
73 bool operator==(Changes::Range const & r1, Changes::Range const & r2)
74 {
75         return r1.start == r2.start && r1.end == r2.end;
76 }
77
78
79 bool operator!=(Changes::Range const & r1, Changes::Range const & r2)
80 {
81         return !(r1 == r2);
82 }
83
84
85 bool Changes::Range::intersects(Range const & r) const
86 {
87         return r.start < end && r.end > start; // end itself is not in the range!
88 }
89
90
91 void Changes::set(Change const & change, pos_type const pos)
92 {
93         set(change, pos, pos + 1);
94 }
95
96
97 void Changes::set(Change const & change, pos_type const start, pos_type const end)
98 {
99         if (change.type != Change::UNCHANGED) {
100                 LYXERR(Debug::CHANGES) << "setting change (type: " << change.type
101                         << ", author: " << change.author << ", time: " << change.changetime
102                         << ") in range (" << start << ", " << end << ")" << endl;
103         }
104
105         Range const newRange(start, end);
106
107         ChangeTable::iterator it = table_.begin();
108
109         for (; it != table_.end(); ) {
110                 // current change starts like or follows new change
111                 if (it->range.start >= start) {
112                         break;
113                 }
114
115                 // new change intersects with existing change
116                 if (it->range.end > start) {
117                         pos_type oldEnd = it->range.end;
118                         it->range.end = start;
119
120                         LYXERR(Debug::CHANGES) << "  cutting tail of type " << it->change.type
121                                 << " resulting in range (" << it->range.start << ", "
122                                 << it->range.end << ")" << endl;
123
124                         ++it;
125                         if (oldEnd >= end) {
126                                 LYXERR(Debug::CHANGES) << "  inserting tail in range ("
127                                         << end << ", " << oldEnd << ")" << endl;
128                                 it = table_.insert(it, ChangeRange((it-1)->change, Range(end, oldEnd)));
129                         }
130                         continue;
131                 }
132
133                 ++it;
134         }
135
136         if (change.type != Change::UNCHANGED) {
137                 LYXERR(Debug::CHANGES) << "  inserting change" << endl;
138                 it = table_.insert(it, ChangeRange(change, Range(start, end)));
139                 ++it;
140         }
141
142         for (; it != table_.end(); ) {
143                 // new change 'contains' existing change
144                 if (newRange.contains(it->range)) {
145                         LYXERR(Debug::CHANGES) << "  removing subrange ("
146                                 << it->range.start << ", " << it->range.end << ")" << endl;
147                         it = table_.erase(it);
148                         continue;
149                 }
150
151                 // new change precedes existing change
152                 if (it->range.start >= end)
153                         break;
154
155                 // new change intersects with existing change
156                 it->range.start = end;
157                 LYXERR(Debug::CHANGES) << "  cutting head of type "
158                         << it->change.type << " resulting in range ("
159                         << end << ", " << it->range.end << ")" << endl;
160                 break; // no need for another iteration
161         }
162
163         merge();
164 }
165
166
167 void Changes::erase(pos_type const pos)
168 {
169         LYXERR(Debug::CHANGES) << "Erasing change at position " << pos << endl;
170
171         ChangeTable::iterator it = table_.begin();
172         ChangeTable::iterator end = table_.end();
173
174         for (; it != end; ++it) {
175                 // range (pos,pos+x) becomes (pos,pos+x-1)
176                 if (it->range.start > pos)
177                         --(it->range.start);
178                 // range (pos-x,pos) stays (pos-x,pos)
179                 if (it->range.end > pos)
180                         --(it->range.end);
181         }
182
183         merge();
184 }
185
186
187 void Changes::insert(Change const & change, lyx::pos_type pos)
188 {
189         if (change.type != Change::UNCHANGED) {
190                 LYXERR(Debug::CHANGES) << "Inserting change of type " << change.type
191                         << " at position " << pos << endl;
192         }
193
194         ChangeTable::iterator it = table_.begin();
195         ChangeTable::iterator end = table_.end();
196
197         for (; it != end; ++it) {
198                 // range (pos,pos+x) becomes (pos+1,pos+x+1)
199                 if (it->range.start >= pos)
200                         ++(it->range.start);
201
202                 // range (pos-x,pos) stays as it is
203                 if (it->range.end > pos)
204                         ++(it->range.end);
205         }
206
207         set(change, pos, pos + 1); // set will call merge
208 }
209
210
211 Change const & Changes::lookup(pos_type const pos) const
212 {
213         static Change const noChange = Change(Change::UNCHANGED);
214
215         ChangeTable::const_iterator it = table_.begin();
216         ChangeTable::const_iterator const end = table_.end();
217
218         for (; it != end; ++it) {
219                 if (it->range.contains(pos))
220                         return it->change;
221         }
222
223         return noChange;
224 }
225
226
227 bool Changes::isChanged(pos_type const start, pos_type const end) const
228 {
229         ChangeTable::const_iterator it = table_.begin();
230         ChangeTable::const_iterator const itend = table_.end();
231
232         for (; it != itend; ++it) {
233                 if (it->range.intersects(Range(start, end))) {
234                         LYXERR(Debug::CHANGES) << "found intersection of range ("
235                                 << start << ", " << end << ") with ("
236                                 << it->range.start << ", " << it->range.end
237                                 << ") of type " << it->change.type << endl;
238                         return true;
239                 }
240         }
241         return false;
242 }
243
244
245 void Changes::merge()
246 {
247         ChangeTable::iterator it = table_.begin();
248
249         while (it != table_.end()) {
250                 LYXERR(Debug::CHANGES) << "found change of type " << it->change.type
251                         << " and range (" << it->range.start << ", " << it->range.end
252                         << ")" << endl;
253
254                 if (it->range.start == it->range.end) {
255                         LYXERR(Debug::CHANGES) << "removing empty range for pos "
256                                 << it->range.start << endl;
257
258                         table_.erase(it);
259                         // start again
260                         it = table_.begin();
261                         continue;
262                 }
263
264                 if (it + 1 == table_.end())
265                         break;
266
267                 if (it->change.isSimilarTo((it + 1)->change) && 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 << ")" << endl;
271
272                         (it + 1)->range.start = it->range.start;
273                         (it + 1)->change.changetime = 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