]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
Fix bug 5859.
[lyx.git] / src / Buffer.cpp
index 04ec31b035ce8b67d519c10c54578cc9c8cfcfa1..e708db794f11bc8845fc372b73f8ce30b705f350 100644 (file)
@@ -145,7 +145,6 @@ public:
        LyXVC lyxvc;
        FileName temppath;
        mutable TexRow texrow;
-       Buffer const * parent_buffer;
 
        /// need to regenerate .tex?
        DepClean dep_clean;
@@ -225,6 +224,19 @@ public:
 
        /// our Text that should be wrapped in an InsetText
        InsetText * inset;
+
+       /// This is here to force the test to be done whenever parent_buffer
+       /// is accessed.
+       Buffer const * parent() const { 
+               if (!theBufferList().isLoaded(parent_buffer))
+                       parent_buffer = 0;
+               return parent_buffer; 
+       }
+       ///
+       void setParent(Buffer const * pb) { parent_buffer = pb; }
+private:
+       /// So we can force access via the accessors.
+       mutable Buffer const * parent_buffer;
 };
 
 
@@ -248,10 +260,11 @@ static FileName createBufferTmpDir()
 
 
 Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_)
-       : parent_buffer(0), lyx_clean(true), bak_clean(true), unnamed(false),
+       : lyx_clean(true), bak_clean(true), unnamed(false),
          read_only(readonly_), filename(file), file_fully_loaded(false),
          toc_backend(&parent), macro_lock(false), timestamp_(0),
-         checksum_(0), wa_(0), undo_(parent), bibinfoCacheValid_(false)
+         checksum_(0), wa_(0), undo_(parent), bibinfoCacheValid_(false),
+         parent_buffer(0)
 {
        temppath = createBufferTmpDir();
        lyxvc.setBuffer(&parent);
@@ -1150,8 +1163,8 @@ void Buffer::writeLaTeXSource(odocstream & os,
        // This happens for example if only a child document is printed.
        Buffer const * save_parent = 0;
        if (output_preamble) {
-               save_parent = d->parent_buffer;
-               d->parent_buffer = 0;
+               save_parent = d->parent();
+               d->setParent(0);
        }
 
        // the real stuff
@@ -1159,7 +1172,7 @@ void Buffer::writeLaTeXSource(odocstream & os,
 
        // Restore the parenthood if needed
        if (output_preamble)
-               d->parent_buffer = save_parent;
+               d->setParent(save_parent);
 
        // add this just in case after all the paragraphs
        os << endl;
@@ -1352,8 +1365,9 @@ void Buffer::validate(LaTeXFeatures & features) const
 void Buffer::getLabelList(vector<docstring> & list) const
 {
        // If this is a child document, use the parent's list instead.
-       if (d->parent_buffer) {
-               d->parent_buffer->getLabelList(list);
+       Buffer const * const pbuf = d->parent();
+       if (pbuf) {
+               pbuf->getLabelList(list);
                return;
        }
 
@@ -1371,8 +1385,9 @@ void Buffer::getLabelList(vector<docstring> & list) const
 void Buffer::updateBibfilesCache(UpdateScope scope) const
 {
        // If this is a child document, use the parent's cache instead.
-       if (d->parent_buffer && scope != UpdateChildOnly) {
-               d->parent_buffer->updateBibfilesCache();
+       Buffer const * const pbuf = d->parent();
+       if (pbuf && scope != UpdateChildOnly) {
+               pbuf->updateBibfilesCache();
                return;
        }
 
@@ -1410,8 +1425,9 @@ void Buffer::invalidateBibinfoCache()
 support::FileNameList const & Buffer::getBibfilesCache(UpdateScope scope) const
 {
        // If this is a child document, use the parent's cache instead.
-       if (d->parent_buffer && scope != UpdateChildOnly)
-               return d->parent_buffer->getBibfilesCache();
+       Buffer const * const pbuf = d->parent();
+       if (pbuf && scope != UpdateChildOnly)
+               return pbuf->getBibfilesCache();
 
        // We update the cache when first used instead of at loading time.
        if (d->bibfilesCache_.empty())
@@ -1497,6 +1513,11 @@ bool Buffer::dispatch(FuncRequest const & func, bool * result)
                case LFUN_BRANCH_DEACTIVATE: {
                        BranchList & branchList = params().branchlist();
                        docstring const branchName = func.argument();
+                       // the case without a branch name is handled elsewhere
+                       if (branchName.empty()) {
+                               dispatched = false;
+                               break;
+                       }
                        Branch * branch = branchList.find(branchName);
                        if (!branch)
                                LYXERR0("Branch " << branchName << " does not exist.");
@@ -1703,14 +1724,14 @@ bool Buffer::isReadonly() const
 void Buffer::setParent(Buffer const * buffer)
 {
        // Avoids recursive include.
-       d->parent_buffer = buffer == this ? 0 : buffer;
+       d->setParent(buffer == this ? 0 : buffer);
        updateMacros();
 }
 
 
 Buffer const * Buffer::parent() const
 {
-       return d->parent_buffer;
+       return d->parent();
 }
 
 
@@ -1742,10 +1763,11 @@ std::vector<Buffer const *> Buffer::allRelatives() const
 
 Buffer const * Buffer::masterBuffer() const
 {
-       if (!d->parent_buffer)
+       Buffer const * const pbuf = d->parent();
+       if (!pbuf)
                return this;
 
-       return d->parent_buffer->masterBuffer();
+       return pbuf->masterBuffer();
 }
 
 
@@ -1888,9 +1910,10 @@ MacroData const * Buffer::getMacro(docstring const & name,
                return data;
 
        // If there is a master buffer, query that
-       if (d->parent_buffer) {
+       Buffer const * const pbuf = d->parent();
+       if (pbuf) {
                d->macro_lock = true;
-               MacroData const * macro = d->parent_buffer->getMacro(
+               MacroData const * macro = pbuf->getMacro(
                        name, *this, false);
                d->macro_lock = false;
                if (macro)
@@ -2083,8 +2106,9 @@ void Buffer::listMacroNames(MacroNameSet & macros) const
                it->first->listMacroNames(macros);
 
        // call parent
-       if (d->parent_buffer)
-               d->parent_buffer->listMacroNames(macros);
+       Buffer const * const pbuf = d->parent();
+       if (pbuf)
+               pbuf->listMacroNames(macros);
 
        d->macro_lock = false;
 }
@@ -2092,11 +2116,12 @@ void Buffer::listMacroNames(MacroNameSet & macros) const
 
 void Buffer::listParentMacros(MacroSet & macros, LaTeXFeatures & features) const
 {
-       if (!d->parent_buffer)
+       Buffer const * const pbuf = d->parent();
+       if (!pbuf)
                return;
 
        MacroNameSet names;
-       d->parent_buffer->listMacroNames(names);
+       pbuf->listMacroNames(names);
 
        // resolve macros
        MacroNameSet::iterator it = names.begin();
@@ -2104,7 +2129,7 @@ void Buffer::listParentMacros(MacroSet & macros, LaTeXFeatures & features) const
        for (; it != end; ++it) {
                // defined?
                MacroData const * data =
-               d->parent_buffer->getMacro(*it, *this, false);
+               pbuf->getMacro(*it, *this, false);
                if (data) {
                        macros.insert(data);
 
@@ -2120,7 +2145,7 @@ void Buffer::listParentMacros(MacroSet & macros, LaTeXFeatures & features) const
 
 Buffer::References & Buffer::references(docstring const & label)
 {
-       if (d->parent_buffer)
+       if (d->parent())
                return const_cast<Buffer *>(masterBuffer())->references(label);
 
        RefCache::iterator it = d->ref_cache_.find(label);
@@ -2155,7 +2180,7 @@ InsetLabel const * Buffer::insetLabel(docstring const & label) const
 
 void Buffer::clearReferenceCache() const
 {
-       if (!d->parent_buffer)
+       if (!d->parent())
                d->ref_cache_.clear();
 }
 
@@ -3067,13 +3092,10 @@ bool Buffer::nextWord(DocIterator & from, DocIterator & to,
                                        ignoreword = true;
                        }
                } else { // !isLetter(cur)
-                       if (inword)
-                               if (!word.empty() && !ignoreword) {
-                                       return true;
-                               }
-                               inword = false;
+                       if (inword && !word.empty() && !ignoreword)
+                               return true;
+                       inword = false;
                }
-
                to.forwardPos();
        }