]> git.lyx.org Git - lyx.git/blob - src/Compare.cpp
compilation fir with qt 4.2
[lyx.git] / src / Compare.cpp
1 /**
2  * \file Compare.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Vincent van Ravesteijn
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Compare.h"
14
15 #include "BufferParams.h"
16 #include "Changes.h"
17
18 #include "insets/InsetText.h"
19
20 #include "support/lassert.h"    
21 #include "support/qstring_helpers.h"
22
23 #include <boost/next_prior.hpp>
24
25 #include <cmath>
26
27 using namespace std;
28 using namespace lyx::support;
29
30
31 namespace lyx {
32
33
34 enum Direction {
35         Forward = 0,
36         Backward
37 };
38
39
40 static void step(DocIterator & dit, Direction direction)
41 {
42         if (direction == Forward)
43                 dit.top().forwardPos();
44         else
45                 dit.top().backwardPos();
46 }
47
48
49 static void step(DocIterator & dit, DocIterator const & end, Direction direction)
50 {
51         if (dit != end)
52                 step(dit, direction);
53 }
54
55
56 /**
57  * A pair of two DocIterators that form a range.
58  */
59 class DocRange {
60 public:
61         DocRange(DocIterator from_, DocIterator to_)
62                 : from(from_), to(to_)
63         {}
64
65         DocRange(Buffer const * buf)
66         {
67                 from = doc_iterator_begin(buf);
68                 to = doc_iterator_end(buf);
69                 to.backwardPos();
70         }
71
72         ///
73         Text * text() const { return from.text(); }
74         ///
75         bool empty() const { return to <= from; }
76         ///
77         size_t length() const;
78
79         /// The begin of the range
80         DocIterator from;
81         /// The end of the range
82         DocIterator to;
83 };
84
85
86 size_t DocRange::length() const
87 {
88         ParagraphList const & ps = from.text()->paragraphs();
89         size_t length = 0;
90         pit_type pit = from.pit();
91         pit_type const endpit = to.pit();
92         for (; pit < endpit; ++pit)
93                 length += ps[pit].size() + 1;
94         length += to.pos() - from.pos();
95         return length;
96 }
97
98
99 class DocPair {
100 public:
101         DocPair() {}
102
103         DocPair(DocIterator o_, DocIterator n_)
104                 : o(o_), n(n_)
105         {}
106
107         bool operator!=(DocPair const & rhs) {
108                 // this might not be intuitive but correct for our purpose
109                 return o != rhs.o && n != rhs.n;
110         }
111         
112
113         DocPair & operator++()
114         {
115                 step(o, Forward);
116                 step(n, Forward);
117                 return *this;
118         }
119
120         DocPair & operator--()
121         {
122                 step(o, Backward);
123                 step(n, Backward);
124                 return *this;
125         }
126         ///
127         DocIterator o;
128         ///
129         DocIterator n;
130 };
131         
132 /**
133  * A pair of two DocRanges.
134  */
135 class DocRangePair {
136 public:
137         DocRangePair(DocRange o_, DocRange n_)
138                 : o(o_), n(n_)
139         {}
140         
141         DocRangePair(DocPair from, DocPair to)
142                 : o(from.o, to.o), n(from.n, to.n)
143         {}
144
145         DocRangePair(Buffer const * o_buf, Buffer const * n_buf)
146                 : o(o_buf), n(n_buf)
147         {}
148
149         /// Returns the from pair
150         DocPair from() const { return DocPair(o.from, n.from); }
151
152         /// Returns the to pair
153         DocPair to() const { return DocPair(o.to, n.to); }
154
155         DocRange o;
156         DocRange n;
157 };
158
159
160 static DocRangePair stepIntoInset(DocPair const & inset_location)
161 {
162         DocRangePair rp(inset_location, inset_location);
163         rp.o.from.forwardPos();
164         rp.n.from.forwardPos();
165         step(rp.o.to, Forward);
166         step(rp.n.to, Forward);
167         rp.o.to.backwardPos();
168         rp.n.to.backwardPos();
169         return rp;
170 }
171
172
173 /**
174  *  This class is designed to hold a vector that has both positive as
175  *  negative indices. It is internally represented as two vectors, one
176  *  for non-zero indices and one for negative indices. In this way, the
177  *  vector can grow in both directions.
178  *    If an index is not available in the vector, the default value is
179  *  returned. If an object is put in the vector beyond its size, the
180  *  empty spots in between are also filled with the default value.
181  */
182 template<class T>
183 class compl_vector {
184 public:
185         compl_vector() {}
186
187         void reset(T const & def)
188         {
189                 default_ = def;
190                 Vp_.clear();
191                 Vn_.clear();
192         }
193
194         /// Gets the value at index. If it is not in the vector
195         /// the default value is inserted and returned.
196         T & operator[](int index) {
197                 vector<T> & V = index >= 0 ? Vp_ : Vn_;
198                 unsigned int const ii = index >= 0 ? index : -index - 1;
199                 while (ii >= V.size())
200                         V.push_back(default_);
201                 return V[ii];
202         }
203
204 private:
205         /// The vector for positive indices
206         vector<T> Vp_;
207         /// The vector for negative indices
208         vector<T> Vn_;
209         /// The default value that is inserted in the vector
210         /// if more space is needed
211         T default_;
212 };
213
214
215 /**
216  * The implementation of the algorithm that does the comparison
217  * between two documents.
218  */
219 class Compare::Impl {
220 public:
221         ///
222         Impl(Compare const & compare) 
223                 : abort_(false), compare_(compare), recursion_level_(0), D_(0)
224         {}
225
226         ///
227         ~Impl() {}
228
229         // Algorithm to find the shortest edit string. This algorithm 
230         // only needs a linear amount of memory (linear with the sum
231         // of the number of characters in the two paragraph-lists).
232         bool diff(Buffer const * new_buf, Buffer const * old_buf,
233                 Buffer const * dest_buf);
234
235         /// Set to true to cancel the algorithm
236         bool abort_;
237
238         ///
239         QString status() {
240                 QString status;
241                 status += toqstr("recursion level:") + " " + QString::number(recursion_level_)
242                         + " " + toqstr("differences:") + " " + QString::number(D_);
243                 return status;
244         }
245
246 private:
247         /// Finds the middle snake and returns the length of the
248         /// shortest edit script.
249         int findMiddleSnake(DocRangePair const & rp, DocPair & middle_snake);
250
251         enum SnakeResult {
252                 NoSnake,
253                 SingleSnake,
254                 NormalSnake
255         };
256
257         /// Retrieve the middle snake when there is overlap between
258         /// the forward and backward path.
259         SnakeResult retrieveMiddleSnake(int k, int D, Direction direction,
260                 DocPair & middle_snake);
261         
262         /// Find the the furthest reaching D-path (number of horizontal
263         /// and vertical steps; differences between the old and new
264         /// document) in the k-diagonal (vertical minus horizontal steps).
265         void furthestDpathKdiagonal(int D, int k,
266                 DocRangePair const & rp, Direction direction);
267
268         /// Is there overlap between the forward and backward path
269         bool overlap(int k, int D);
270         
271         /// This function is called recursively by a divide and conquer
272         /// algorithm. Each time, the string is divided into two split
273         /// around the middle snake.
274         void diff_i(DocRangePair const & rp);
275
276         /// Processes the splitted chunks. It either adds them as deleted,
277         /// as added, or call diff_i for further processing.
278         void diffPart(DocRangePair const & rp);
279
280         /// Runs the algorithm for the inset located at /c it and /c it_n 
281         /// and adds the result to /c pars.
282         void diffInset(Inset * inset, DocPair const & p);
283
284         /// Adds the snake to the destination buffer. The algorithm will
285         /// recursively be applied to any InsetTexts that are within the snake.
286         void processSnake(DocRangePair const & rp);
287
288         /// Writes the range to the destination buffer
289         void writeToDestBuffer(DocRange const & range,
290                 Change::Type type = Change::UNCHANGED);
291         
292         /// Writes the paragraph list to the destination buffer
293         void writeToDestBuffer(ParagraphList const & copy_pars) const;
294
295         /// The length of the old chunk currently processed
296         int N_;
297         /// The length of the new chunk currently processed
298         int M_;
299         /// The offset diagonal of the reverse path of the
300         /// currently processed chunk
301         int offset_reverse_diagonal_;
302         /// Is the offset odd or even ?
303         bool odd_offset_;
304
305         /// The thread object, used to emit signals to the GUI
306         Compare const & compare_;
307
308         /// The buffer containing text that will be marked as old
309         Buffer const * old_buf_;
310         /// The buffer containing text that will be marked as new
311         Buffer const * new_buf_;
312         /// The buffer containing text that will be marked as new
313         Buffer const * dest_buf_;
314
315         /// The paragraph list of the destination buffer
316         ParagraphList * dest_pars_;
317
318         /// The level of recursion
319         int recursion_level_;
320
321         /// The number of nested insets at this level
322         int nested_inset_level_;
323
324         /// The position/snake in the old/new document
325         /// of the forward/reverse search
326         compl_vector<DocIterator> ofp;
327         compl_vector<DocIterator> nfp;
328         compl_vector<DocIterator> ofs;
329         compl_vector<DocIterator> nfs;
330         compl_vector<DocIterator> orp;
331         compl_vector<DocIterator> nrp;
332         compl_vector<DocIterator> ors;
333         compl_vector<DocIterator> nrs;
334         
335         /// The number of differences in the path the algorithm
336         /// is currently processing.
337         int D_;
338 };
339
340 /////////////////////////////////////////////////////////////////////
341 //
342 // Compare
343 //
344 /////////////////////////////////////////////////////////////////////
345
346 Compare::Compare(Buffer const * new_buf, Buffer const * old_buf,
347         Buffer * const dest_buf, CompareOptions const & options)
348         : new_buffer(new_buf), old_buffer(old_buf), dest_buffer(dest_buf),
349           options_(options), pimpl_(new Impl(*this))
350 {
351         connect(&status_timer_, SIGNAL(timeout()),
352                 this, SLOT(doStatusMessage()));
353         status_timer_.start(1000);
354 }
355
356
357 void Compare::doStatusMessage()
358 {
359         statusMessage(pimpl_->status());
360 }
361
362
363 void Compare::run()
364 {
365         if (!dest_buffer || !new_buffer || !old_buffer)
366                 return;
367
368         // Copy the buffer params to the new buffer
369         dest_buffer->params() = options_.settings_from_new
370                 ? new_buffer->params() : old_buffer->params();
371         
372         doStatusMessage();
373
374         // do the real work
375         if (!doCompare())
376                 return;
377         
378         finished(pimpl_->abort_);
379         return;
380 }
381
382
383 int Compare::doCompare()
384 {
385         return pimpl_->diff(new_buffer, old_buffer, dest_buffer);
386 }
387
388
389 void Compare::abort()
390 {
391         pimpl_->abort_ = true;
392         condition_.wakeOne();
393         wait();
394         pimpl_->abort_ = false;
395 }
396
397
398 static void getParagraphList(DocRange const & range,
399         ParagraphList & pars)
400 {
401         // Clone the paragraphs within the selection.
402         pit_type startpit = range.from.pit();
403         pit_type endpit = range.to.pit();
404         ParagraphList const & ps_ = range.text()->paragraphs();
405         ParagraphList tmp_pars(boost::next(ps_.begin(), startpit),
406                 boost::next(ps_.begin(), endpit + 1));
407
408         // Remove the end of the last paragraph; afterwards, remove the
409         // beginning of the first paragraph. Keep this order - there may only
410         // be one paragraph!
411         Paragraph & back = tmp_pars.back();
412         back.eraseChars(range.to.pos(), back.size(), false);
413         Paragraph & front = tmp_pars.front();
414         front.eraseChars(0, range.from.pos(), false);
415
416         pars.insert(pars.begin(), tmp_pars.begin(), tmp_pars.end());
417 }
418
419
420 static bool equal(Inset const * i_o, Inset const * i_n)
421 {
422         if (!i_o || !i_n)
423                 return false;
424
425         // Different types of insets
426         if (i_o->lyxCode() != i_n->lyxCode())
427                 return false;
428
429         // Editable insets are assumed to be the same as they are of the 
430         // same type. If we later on decide that we insert them in the
431         // document as being unchanged, we will run the algorithm on the
432         // contents of the two insets.
433         // FIXME: This fails if the parameters of the insets differ.
434         // FIXME: We do not recurse into InsetTabulars.
435         // FIXME: We need methods inset->equivalent(inset).
436         if (i_o->editable() && !i_o->asInsetMath()
437                   && i_o->asInsetText())
438                 return true;
439
440         ostringstream o_os;
441         ostringstream n_os;
442         i_o->write(o_os);
443         i_n->write(n_os);
444         return o_os.str() == n_os.str();
445 }
446
447
448 static bool equal(DocIterator & o, DocIterator & n) {
449         Paragraph const & old_par = o.text()->getPar(o.pit());
450         Paragraph const & new_par = n.text()->getPar(n.pit());
451
452         char_type const c_o = old_par.getChar(o.pos());
453         char_type const c_n = new_par.getChar(n.pos());
454         if (c_o != c_n)
455                 return false;
456
457         if (old_par.isInset(o.pos())) {
458                 Inset const * i_o = old_par.getInset(o.pos());
459                 Inset const * i_n = new_par.getInset(n.pos());
460
461                 if (i_o && i_n)
462                         return equal(i_o, i_n);
463         }       
464
465         Font fo = old_par.getFontSettings(o.buffer()->params(), o.pos());
466         Font fn = new_par.getFontSettings(n.buffer()->params(), n.pos());
467         return fo == fn;
468 }
469
470
471 /// Traverses a snake in a certain direction. p points to a 
472 /// position in the old and new file and they are synchronously
473 /// moved along the snake. The function returns true if a snake
474 /// was found.
475 static bool traverseSnake(DocPair & p, DocRangePair const & range,
476         Direction direction)
477 {
478         bool ret = false;
479         DocPair const & p_end = 
480                 direction == Forward ? range.to() : range.from();
481
482         while (p != p_end) {
483                 if (direction == Backward)
484                         --p;
485                 if (!equal(p.o, p.n)) {
486                         if (direction == Backward)
487                                 ++p;
488                         return ret;
489                 }
490                 if (direction == Forward)
491                         ++p;
492                 ret = true;
493         }
494         return ret;
495 }
496
497
498 /////////////////////////////////////////////////////////////////////
499 //
500 // Compare::Impl
501 //
502 /////////////////////////////////////////////////////////////////////
503
504
505 void Compare::Impl::furthestDpathKdiagonal(int D, int k,
506          DocRangePair const & rp, Direction direction)
507 {
508         compl_vector<DocIterator> & op = direction == Forward ? ofp : orp;
509         compl_vector<DocIterator> & np = direction == Forward ? nfp : nrp;
510         compl_vector<DocIterator> & os = direction == Forward ? ofs : ors;
511         compl_vector<DocIterator> & ns = direction == Forward ? nfs : nrs;
512
513         // A vertical step means stepping one character in the new document.
514         bool vertical_step = k == -D;
515         if (!vertical_step && k != D) {
516                 vertical_step = direction == Forward
517                         ? op[k - 1] < op[k + 1] : op[k - 1] > op[k + 1];
518         }
519
520         // Where do we take the step from ?
521         int const kk = vertical_step ? k + 1 : k - 1;
522         DocPair p(op[kk], np[kk]);
523
524         // If D==0 we simulate a vertical step from (0,-1) by doing nothing.
525         if (D != 0) {
526                 // Take a step
527                 if (vertical_step && direction == Forward)
528                         step(p.n, rp.n.to, direction);
529                 else if (vertical_step && direction == Backward)
530                         step(p.n, rp.n.from, direction);
531                 else if (!vertical_step && direction == Forward)
532                         step(p.o, rp.o.to, direction);
533                 else if (!vertical_step && direction == Backward)
534                         step(p.o, rp.o.from, direction);
535         }       
536         
537         // Traverse snake
538         if (traverseSnake(p, rp, direction)) {
539                 // Record last snake
540                 os[k] = p.o;
541                 ns[k] = p.n;
542         } else {
543                 // Copy last snake from the previous step
544                 os[k] = os[kk];
545                 ns[k] = ns[kk];
546         }
547
548         //Record new position
549         op[k] = p.o;
550         np[k] = p.n;
551 }
552
553
554 bool Compare::Impl::overlap(int k, int D)
555 {
556         // To generalize for the forward and reverse checks
557         int kk = offset_reverse_diagonal_ - k;
558
559         // Can we have overlap ?
560         if (kk <= D && kk >= -D) {
561                 // Do we have overlap ?
562                 if (odd_offset_)
563                         return ofp[k] >= orp[kk] && nfp[k] >= nrp[kk];
564                 else
565                         return ofp[kk] >= orp[k] && nfp[kk] >= nrp[k];
566         }
567         return false;
568 }
569
570
571 Compare::Impl::SnakeResult Compare::Impl::retrieveMiddleSnake(
572         int k, int D, Direction direction, DocPair & middle_snake)
573 {
574         compl_vector<DocIterator> & os = direction == Forward ? ofs : ors;
575         compl_vector<DocIterator> & ns = direction == Forward ? nfs : nrs;
576         compl_vector<DocIterator> & os_r = direction == Forward ? ors : ofs;
577         compl_vector<DocIterator> & ns_r = direction == Forward ? nrs : nfs;
578
579         // The diagonal while doing the backward search
580         int kk = -k + offset_reverse_diagonal_;
581
582         // Did we find a snake ?
583         if (os[k].empty() && os_r[kk].empty()) {
584                 // No, there is no snake at all, in which case
585                 // the length of the shortest edit script is M+N.
586                 LASSERT(2 * D - odd_offset_ == M_ + N_, /**/);
587                 return NoSnake;
588         } 
589         
590         if (os[k].empty()) {
591                 // Yes, but there is only 1 snake and we found it in the
592                 // reverse path.
593                 middle_snake.o = os_r[kk];
594                 middle_snake.n = ns_r[kk];
595                 return SingleSnake;
596         }
597
598         middle_snake.o = os[k];
599         middle_snake.n = ns[k];
600         return NormalSnake;
601 }
602
603
604 int Compare::Impl::findMiddleSnake(DocRangePair const & rp,
605         DocPair & middle_snake)
606 {
607         // The lengths of the old and new chunks.
608         N_ = rp.o.length();
609         M_ = rp.n.length();
610
611         // Forward paths are centered around the 0-diagonal; reverse paths
612         // are centered around the diagonal N - M. (Delta in the article)
613         offset_reverse_diagonal_ = N_ - M_;
614
615         // If the offset is odd, only check for overlap while extending forward
616     // paths, otherwise only check while extending reverse paths.
617         odd_offset_ = (offset_reverse_diagonal_ % 2 != 0);
618
619         ofp.reset(rp.o.from);
620         nfp.reset(rp.n.from);
621         ofs.reset(DocIterator());
622         nfs.reset(DocIterator());
623         orp.reset(rp.o.to);
624         nrp.reset(rp.n.to);
625         ors.reset(DocIterator());
626         nrs.reset(DocIterator());
627
628         // D is the number of horizontal and vertical steps, i.e.
629         // different characters in the old and new chunk.
630         int const D_max = ceil(((double)M_ + N_)/2);
631         for (int D = 0; D <= D_max; ++D) {
632                 // to be used in the status messages
633                 D_ = D; 
634
635                 // Forward and reverse paths
636                 for (int f = 0; f < 2; ++f) {
637                         Direction direction = f == 0 ? Forward : Backward;
638
639                         // Diagonals between -D and D can be reached by a D-path
640                         for (int k = -D; k <= D; k += 2) {                      
641                                 // Find the furthest reaching D-path on this diagonal
642                                 furthestDpathKdiagonal(D, k, rp, direction);
643
644                                 // Only check for overlap for forward paths if the offset is odd
645                                 // and only for reverse paths if the offset is even.
646                                 if (odd_offset_ == (direction == Forward)) {
647
648                                         // Do the forward and backward paths overlap ?
649                                         if (overlap(k, D - odd_offset_)) {
650                                                 retrieveMiddleSnake(k, D, direction, middle_snake);
651                                                 return 2 * D - odd_offset_;
652                                         }
653                                 }
654                                 if (abort_)
655                                         return 0;
656                         }
657                 }
658         }
659         // This should never be reached
660         return -2;
661 }
662
663
664 bool Compare::Impl::diff(Buffer const * new_buf, Buffer const * old_buf,
665         Buffer const * dest_buf)
666 {
667         if (!new_buf || !old_buf || !dest_buf)
668                 return false;
669
670         old_buf_ = old_buf;
671         new_buf_ = new_buf;
672         dest_buf_ = dest_buf;
673         dest_pars_ = &dest_buf->inset().asInsetText()->paragraphs();
674         dest_pars_->clear();
675
676         recursion_level_ = 0;
677         nested_inset_level_ = 0;
678
679         DocRangePair rp(old_buf_, new_buf_);
680
681         DocPair from = rp.from();
682         traverseSnake(from, rp, Forward);
683         DocRangePair const snake(rp.from(), from);
684         processSnake(snake);
685         
686         // Start the recursive algorithm
687         diff_i(rp);
688
689         for (pit_type p = 0; p < (pit_type)dest_pars_->size(); ++p) {
690                 (*dest_pars_)[p].setBuffer(const_cast<Buffer &>(*dest_buf));
691                 (*dest_pars_)[p].setInsetOwner(&dest_buf_->inset());
692         }
693
694         return true;
695 }
696
697
698 void Compare::Impl::diff_i(DocRangePair const & rp)
699 {
700         if (abort_)
701                 return;
702
703         // The middle snake
704         DocPair middle_snake;
705
706         // Divides the problem into two smaller problems, split around
707         // the snake in the middle.
708         int const L_ses = findMiddleSnake(rp, middle_snake);
709
710         // Set maximum of progress bar
711         if (++recursion_level_ == 1)
712                 compare_.progressMax(L_ses);
713
714         // There are now three possibilities: the strings were the same,
715         // the strings were completely different, or we found a middle
716         // snake and we can split the string into two parts to process.
717         if (L_ses == 0)
718                 // Two the same strings (this must be a very rare case, because
719                 // usually this will be part of a snake adjacent to these strings).
720                 writeToDestBuffer(rp.o);
721
722         else if (middle_snake.o.empty()) {
723                 // Two totally different strings
724                 writeToDestBuffer(rp.o, Change::DELETED);
725                 writeToDestBuffer(rp.n, Change::INSERTED);
726
727         } else {
728                 // Retrieve the complete snake
729                 DocPair first_part_end = middle_snake;
730                 traverseSnake(first_part_end, rp, Backward);
731                 DocRangePair first_part(rp.from(), first_part_end);
732                         
733                 DocPair second_part_begin = middle_snake;
734                 traverseSnake(second_part_begin, rp, Forward);
735                 DocRangePair second_part(second_part_begin, rp.to());
736                                 
737                 // Split the string in three parts:
738                 // 1. in front of the snake
739                 diffPart(first_part);
740
741                 // 2. the snake itself, and
742                 DocRangePair const snake(first_part.to(), second_part.from());
743                 processSnake(snake);
744
745                 // 3. behind the snake.
746                 diffPart(second_part);
747         }
748         --recursion_level_;
749 }
750
751
752 void Compare::Impl::diffPart(DocRangePair const & rp)
753 {
754         // Is there a finite length string in both buffers, if not there
755         // is an empty string and we write the other one to the buffer.
756         if (!rp.o.empty() && !rp.n.empty())
757                 diff_i(rp);
758         
759         else if (!rp.o.empty())
760                 writeToDestBuffer(rp.o, Change::DELETED);
761
762         else if (!rp.n.empty())
763                 writeToDestBuffer(rp.n, Change::INSERTED);
764 }
765
766
767 void Compare::Impl::diffInset(Inset * inset, DocPair const & p)
768 {
769         // Find the dociterators for the beginning and the
770         // end of the inset, for the old and new document.
771         DocRangePair const rp = stepIntoInset(p);
772
773         // Recurse into the inset. Temporarily replace the dest_pars
774         // paragraph list by the paragraph list of the nested inset.
775         ParagraphList * backup_dest_pars = dest_pars_;
776         dest_pars_ = &inset->asInsetText()->text().paragraphs();
777         dest_pars_->clear();
778         
779         ++nested_inset_level_;
780         diff_i(rp);
781         --nested_inset_level_;
782
783         dest_pars_ = backup_dest_pars;
784 }
785
786
787 void Compare::Impl::processSnake(DocRangePair const & rp)
788 {
789         ParagraphList pars;
790         getParagraphList(rp.o, pars);
791
792         // Find insets in this paragaph list
793         DocPair it = rp.from();
794         for (; it.o < rp.o.to; ++it) {
795                 Inset * inset = it.o.text()->getPar(it.o.pit()).getInset(it.o.pos());
796                 if (inset && inset->editable() && inset->asInsetText()) {
797                         // Find the inset in the paragraph list that will be pasted into
798                         // the final document. The contents of the inset will be replaced
799                         // by the output of the algorithm below.
800                         pit_type const pit = it.o.pit() - rp.o.from.pit();
801                         pos_type const pos = pit ? it.o.pos() : it.o.pos() - rp.o.from.pos();
802                         inset = pars[pit].getInset(pos);
803                         LASSERT(inset, /**/);
804                         diffInset(inset, it);
805                 }
806         }
807         writeToDestBuffer(pars);
808 }
809
810
811 void Compare::Impl::writeToDestBuffer(DocRange const & range,
812         Change::Type type)
813 {
814         ParagraphList pars;
815         getParagraphList(range, pars);
816
817         pos_type size = 0;
818
819         // Set the change
820         ParagraphList::iterator it = pars.begin();
821         for (; it != pars.end(); ++it) {
822                 it->setChange(Change(type));
823                 size += it->size();
824         }
825
826         writeToDestBuffer(pars);
827
828         if (nested_inset_level_ == 0)
829                 compare_.progress(size);
830 }
831
832
833 void Compare::Impl::writeToDestBuffer(ParagraphList const & pars) const
834 {
835         pit_type const pit = dest_pars_->size() - 1;
836         dest_pars_->insert(dest_pars_->end(), pars.begin(), pars.end());
837         if (pit >= 0)
838                 mergeParagraph(dest_buf_->params(), *dest_pars_, pit);
839 }
840
841
842 #include "moc_Compare.cpp"
843
844 } // namespace lyx