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