]> git.lyx.org Git - features.git/commitdiff
More comments and minor code cleanup
authorRichard Kimberly Heck <rikiheck@lyx.org>
Mon, 9 Nov 2020 21:59:04 +0000 (16:59 -0500)
committerRichard Kimberly Heck <rikiheck@lyx.org>
Mon, 9 Nov 2020 21:59:04 +0000 (16:59 -0500)
src/Buffer.cpp
src/Buffer.h

index 600d81090cc6e3761e8f4891bf1116aba2e7aaed..91602dad6f61d003d40a77c1cbab914e4d6605e5 100644 (file)
@@ -220,7 +220,10 @@ public:
        /// We also have a map from the positions where Buffers are included
        /// to those Buffers and their scope (whose point RKH does not yet 
        /// understand).
-       // FIXME Surely one of these is enough?
+       // FIXME Surely one of these is enough? The difference is that
+       // children_positions has one entry for each child Buffer, whereas
+       // position_to_children has one entry for each InsetInclude (and the
+       // same Buffer could be included twice).
        struct ScopeBuffer {
                ScopeBuffer() = default;
                ScopeBuffer(DocIterator const & s, Buffer const * b)
@@ -614,6 +617,10 @@ void Buffer::cloneWithChildren(BufferMap & bufmap, CloneList_ptr clones) const
                return;
 
        Buffer * buffer_clone = new Buffer(fileName().absFileName(), false, this);
+       if (!buffer_clone) {
+               LYXERR0("Buffer creation failed!");
+               LBUFERR(false);
+       }
 
        // The clone needs its own DocumentClass, since running updateBuffer() will
        // modify it, and we would otherwise be sharing it with the original Buffer.
@@ -629,27 +636,32 @@ void Buffer::cloneWithChildren(BufferMap & bufmap, CloneList_ptr clones) const
        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
-       // math macro caches need to be rethought and simplified.
-       // I am not sure wether we should handle Buffer cloning here or in BufferList.
-       // Right now BufferList knows nothing about buffer clones.
+       // clone the children and update the information in the InsetIncludes
        for (auto const & p : d->position_to_children) {
-               DocIterator dit = p.first.clone(buffer_clone);
+               // location of child in old Buffer
+               DocIterator const & olddit = p.first;
+               // a DocIterator that points to the same location in this new Buffer
+               DocIterator dit = olddit.clone(buffer_clone);
                dit.setBuffer(buffer_clone);
+               // the child in the old Buffer
                Buffer * child = const_cast<Buffer *>(p.second.buffer);
-
+               // clone the child
                child->cloneWithChildren(bufmap, clones);
+               // we now need to find the child we just created (or the previously
+               // created one, if it was already cloned).
                BufferMap::iterator const bit = bufmap.find(child);
                LASSERT(bit != bufmap.end(), continue);
                Buffer * child_clone = bit->second;
 
+               // the Buffer for this include inset is now the cloned child
                Inset * inset = dit.nextInset();
                LASSERT(inset && inset->lyxCode() == INCLUDE_CODE, continue);
                InsetInclude * inset_inc = static_cast<InsetInclude *>(inset);
                inset_inc->setChildBuffer(child_clone);
+               // set us as its parent
                child_clone->d->setParent(buffer_clone);
                // FIXME Do we need to do this now, or can we wait until we run updateMacros()?
-               buffer_clone->setChild(dit, child_clone);
+               d->children_positions[child_clone] = dit;
        }
        buffer_clone->d->macro_lock = false;
 }
@@ -795,12 +807,6 @@ Undo & Buffer::undo()
 }
 
 
-void Buffer::setChild(DocIterator const & dit, Buffer * child)
-{
-       d->children_positions[child] = dit;
-}
-
-
 string Buffer::latexName(bool const no_path) const
 {
        FileName latex_name =
index a917c6e9f74792d8185b6de18c18b08c4b9d4547..2dbc16ca4f150ffbd664f80d314bda3ce9fa85e9 100644 (file)
@@ -223,7 +223,7 @@ public:
        double fontScalingFactor() const;
 
 private:
-       ///
+       /// map from Buffers to their clones
        typedef std::map<Buffer const *, Buffer *> BufferMap;
        ///
        void cloneWithChildren(BufferMap &, CloneList_ptr) const;
@@ -648,8 +648,6 @@ public:
        /// This function is called when the buffer is changed.
        void changed(bool update_metrics) const;
        ///
-       void setChild(DocIterator const & dit, Buffer * child);
-       ///
        void updateTocItem(std::string const &, DocIterator const &) const;
        /// This function is called when the buffer structure is changed.
        void structureChanged() const;