Author: rgheck
Date: Thu May 26 15:20:11 2011
New Revision: 38837
URL: http://www.lyx.org/trac/changeset/38837

Log:
Fix problem first reported by Diego: If you try to view a child document,
the macros declared in the parent do not work. This is because we were
cloning only the child, which then didn't have a parent.

The solution is to clone the whole structure, starting with the master
document. As a side effect, we only clone each child once.

Modified:
   lyx-devel/trunk/src/Buffer.cpp
   lyx-devel/trunk/src/Buffer.h
   lyx-devel/trunk/src/frontends/qt4/GuiView.cpp

Modified: lyx-devel/trunk/src/Buffer.cpp
==============================================================================
--- lyx-devel/trunk/src/Buffer.cpp      Wed May 25 17:22:00 2011        (r38836)
+++ lyx-devel/trunk/src/Buffer.cpp      Thu May 26 15:20:11 2011        (r38837)
@@ -427,7 +427,22 @@
 
 Buffer * Buffer::clone() const
 {
+       BufferMap bufmap;
+       masterBuffer()->clone(bufmap);
+       BufferMap::iterator it = bufmap.find(this);
+       LASSERT(it != bufmap.end(), return 0);
+       return it->second;
+}
+
+
+void Buffer::clone(BufferMap & bufmap) const
+{
+       // have we already been cloned?
+       if (bufmap.find(this) != bufmap.end())
+               return;
+
        Buffer * buffer_clone = new Buffer(fileName().absFileName(), false, 
this);
+       bufmap[this] = buffer_clone;
        buffer_clone->d->macro_lock = true;
        buffer_clone->d->children_positions.clear();
        // FIXME (Abdel 09/01/2010): this is too complicated. The whole 
children_positions and
@@ -440,7 +455,12 @@
                DocIterator dit = it->second.clone(buffer_clone);
                dit.setBuffer(buffer_clone);
                Buffer * child = const_cast<Buffer *>(it->first);
-               Buffer * child_clone = child->clone();
+
+               child->clone(bufmap);
+               BufferMap::iterator it = bufmap.find(child);
+               LASSERT(it != bufmap.end(), continue);
+               Buffer * child_clone = it->second;
+
                Inset * inset = dit.nextInset();
                LASSERT(inset && inset->lyxCode() == INCLUDE_CODE, continue);
                InsetInclude * inset_inc = static_cast<InsetInclude *>(inset);
@@ -449,7 +469,7 @@
                buffer_clone->setChild(dit, child_clone);
        }
        buffer_clone->d->macro_lock = false;
-       return buffer_clone;
+       return;
 }
 
 

Modified: lyx-devel/trunk/src/Buffer.h
==============================================================================
--- lyx-devel/trunk/src/Buffer.h        Wed May 25 17:22:00 2011        (r38836)
+++ lyx-devel/trunk/src/Buffer.h        Thu May 26 15:20:11 2011        (r38837)
@@ -211,6 +211,10 @@
        int readHeader(Lexer & lex);
 
 private:
+       ///
+       typedef std::map<Buffer const *, Buffer *> BufferMap;
+       ///
+       void clone(BufferMap &) const;
        /// save timestamp and checksum of the given file.
        void saveCheckSum() const;      
        /// read a new file

Modified: lyx-devel/trunk/src/frontends/qt4/GuiView.cpp
==============================================================================
--- lyx-devel/trunk/src/frontends/qt4/GuiView.cpp       Wed May 25 17:22:00 
2011        (r38836)
+++ lyx-devel/trunk/src/frontends/qt4/GuiView.cpp       Thu May 26 15:20:11 
2011        (r38837)
@@ -2944,7 +2944,11 @@
                                buffer->params().maintain_unincluded_children
                                && 
!buffer->params().getIncludedChildren().empty();
        bool const success = func(format, update_unincluded);
-       delete buffer;
+
+       // the cloning operation will have produced a clone of the entire set of
+       // documents, starting from the master. so we must delete those.
+       Buffer * mbuf = const_cast<Buffer *>(buffer->masterBuffer());
+       delete mbuf;
        busyBuffers.remove(orig);
        if (msg == "preview") {
                return success

Reply via email to