]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
cleanup some debug messages
[lyx.git] / src / buffer.C
index 0c379584fa5acd8acd086629b1659c8ebb9bae95..429907f18ac17076db2828ad70f51d5e4f806cce 100644 (file)
@@ -64,6 +64,7 @@
 
 #include "graphics/Previews.h"
 
+#include "support/lyxalgo.h"
 #include "support/filetools.h"
 #include "support/fs_extras.h"
 #ifdef USE_COMPRESSION
@@ -143,7 +144,7 @@ extern BufferList bufferlist;
 
 namespace {
 
-int const LYX_FORMAT = 243;
+int const LYX_FORMAT = 245;
 
 } // namespace anon
 
@@ -469,25 +470,23 @@ bool Buffer::readDocument(LyXLex & lex)
                error(ErrorItem(_("Document header error"), s, -1, 0, 0));
        }
 
-       if (paragraphs().empty()) {
-               readHeader(lex);
-               if (!params().getLyXTextClass().load()) {
-                       string theclass = params().getLyXTextClass().name();
-                       Alert::error(_("Can't load document class"), bformat(
-                                       "Using the default document class, because the "
-                                       " class %1$s could not be loaded.", theclass));
-                       params().textclass = 0;
-               }
-       } else {
-               // We don't want to adopt the parameters from the
-               // document we insert, so read them into a temporary buffer
-               // and then discard it
+       // we are reading in a brand new document
+       BOOST_ASSERT(paragraphs().empty());
 
-               Buffer tmpbuf("", false);
-               tmpbuf.readHeader(lex);
+       readHeader(lex);
+       if (!params().getLyXTextClass().load()) {
+               string theclass = params().getLyXTextClass().name();
+               Alert::error(_("Can't load document class"), bformat(
+                                    "Using the default document class, because the "
+                                    " class %1$s could not be loaded.", theclass));
+               params().textclass = 0;
        }
 
-       return text().read(*this, lex);
+       bool const res = text().read(*this, lex);
+       for_each(text().paragraphs().begin(),
+                text().paragraphs().end(),
+                bind(&Paragraph::setInsetOwner, _1, &inset()));
+       return res;
 }
 
 
@@ -555,7 +554,9 @@ bool Buffer::readFile(string const & filename)
 
        // remove dummy empty par
        paragraphs().clear();
-       bool ret = readFile(filename, paragraphs().size());
+       LyXLex lex(0, 0);
+       lex.setFile(filename);
+       bool ret = readFile(lex, filename);
 
        // After we have read a file, we must ensure that the buffer
        // language is set and used in the gui.
@@ -566,14 +567,6 @@ bool Buffer::readFile(string const & filename)
 }
 
 
-bool Buffer::readFile(string const & filename, pit_type const pit)
-{
-       LyXLex lex(0, 0);
-       lex.setFile(filename);
-       return readFile(lex, filename, pit);
-}
-
-
 bool Buffer::fully_loaded() const
 {
        return pimpl_->file_fully_loaded;
@@ -586,7 +579,7 @@ void Buffer::fully_loaded(bool const value)
 }
 
 
-bool Buffer::readFile(LyXLex & lex, string const & filename, pit_type const pit)
+bool Buffer::readFile(LyXLex & lex, string const & filename)
 {
        BOOST_ASSERT(!filename.empty());
 
@@ -667,7 +660,7 @@ bool Buffer::readFile(LyXLex & lex, string const & filename, pit_type const pit)
                                              filename));
                        return false;
                } else {
-                       bool const ret = readFile(tmpfile, pit);
+                       bool const ret = readFile(tmpfile);
                        // Do stuff with tmpfile name and buffer name here.
                        return ret;
                }
@@ -1526,25 +1519,58 @@ void Buffer::buildMacros()
        pimpl_->macros = MacroTable::globalMacros();
 
        // Now add our own.
-       ParagraphList & pars = text().paragraphs();
+       ParagraphList const & pars = text().paragraphs();
        for (size_t i = 0, n = pars.size(); i != n; ++i) {
                //lyxerr << "searching main par " << i
                //      << " for macro definitions" << std::endl;
-               InsetList::iterator it = pars[i].insetlist.begin();
-               InsetList::iterator end = pars[i].insetlist.end();
+               InsetList const & insets = pars[i].insetlist;
+               InsetList::const_iterator it = insets.begin();
+               InsetList::const_iterator end = insets.end();
                for ( ; it != end; ++it) {
                        //lyxerr << "found inset code " << it->inset->lyxCode() << std::endl;
                        if (it->inset->lyxCode() == InsetBase::MATHMACRO_CODE) {
-                               MathMacroTemplate & mac
-                                       = static_cast<MathMacroTemplate &>(*it->inset);
+                               MathMacroTemplate const & mac
+                                       = static_cast<MathMacroTemplate const &>(*it->inset);
                                insertMacro(mac.name(), mac.asMacroData());
                        }
                }
        }
 }
 
+
 void Buffer::saveCursor(StableDocIterator cur, StableDocIterator anc)
 {
        cursor_ = cur;
        anchor_ = anc;
 }
+
+
+void Buffer::changeRefsIfUnique(string const & from, string const & to)
+{
+       // Check if the label 'from' appears more than once
+       vector<string> labels;
+       getLabelList(labels);
+
+       if (lyx::count(labels.begin(), labels.end(), from) > 1)
+               return;
+
+       InsetBase::Code code = InsetBase::REF_CODE;
+
+       ParIterator it = par_iterator_begin();
+       ParIterator end = par_iterator_end();
+       for ( ; it != end; ++it) {
+               bool changed_inset = false;
+               for (InsetList::iterator it2 = it->insetlist.begin();
+                    it2 != it->insetlist.end(); ++it2) {
+                       if (it2->inset->lyxCode() == code) {
+                               InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
+                               if (inset->getContents() == from) {
+                                       inset->setContents(to);
+                                       //inset->setButtonLabel();
+                                       changed_inset = true;
+                               }
+                       }
+               }
+       }
+}
+