From: Richard Kimberly Heck Date: Mon, 9 Nov 2020 21:59:04 +0000 (-0500) Subject: More comments and minor code cleanup X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=a9fc72242ff85f6030c8ad1637d70308d54cbcb3;p=features.git More comments and minor code cleanup --- diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 7e72619a4c..3d2729c15a 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -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(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(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 = diff --git a/src/Buffer.h b/src/Buffer.h index a917c6e9f7..2dbc16ca4f 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -223,7 +223,7 @@ public: double fontScalingFactor() const; private: - /// + /// map from Buffers to their clones typedef std::map 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;