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