]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
SCons: msvc does not need this /TP option any more after we rename .C => .cpp. Also...
[lyx.git] / src / Buffer.cpp
index 8a52b65bc730657231a56477c4c49697f63e1d00..5f73e27f6bec1f9ce4eb07510839b996138d3ad4 100644 (file)
@@ -13,6 +13,7 @@
 #include "Buffer.h"
 
 #include "Author.h"
+#include "BiblioInfo.h"
 #include "BranchList.h"
 #include "buffer_funcs.h"
 #include "BufferList.h"
@@ -21,6 +22,7 @@
 #include "Bullet.h"
 #include "Chktex.h"
 #include "debug.h"
+#include "DocIterator.h"
 #include "Encoding.h"
 #include "ErrorList.h"
 #include "Exporter.h"
@@ -47,6 +49,7 @@
 #include "ParIterator.h"
 #include "sgml.h"
 #include "TexRow.h"
+#include "TexStream.h"
 #include "TocBackend.h"
 #include "Undo.h"
 #include "version.h"
@@ -79,6 +82,7 @@
 #include <boost/filesystem/exception.hpp>
 #include <boost/filesystem/operations.hpp>
 
+#include <algorithm>
 #include <iomanip>
 #include <stack>
 #include <sstream>
@@ -134,7 +138,7 @@ namespace fs = boost::filesystem;
 
 namespace {
 
-int const LYX_FORMAT = 276;
+int const LYX_FORMAT = 280;
 
 } // namespace anon
 
@@ -199,7 +203,7 @@ public:
 Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_)
        : lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
          filename(file), file_fully_loaded(false), inset(params),
-         timestamp_(0), checksum_(0), toc_backend(&parent)
+         toc_backend(&parent), timestamp_(0), checksum_(0)
 {
        inset.setAutoBreakRows(true);
        lyxvc.buffer(&parent);
@@ -224,7 +228,12 @@ Buffer::~Buffer()
        // here the buffer should take care that it is
        // saved properly, before it goes into the void.
 
-       closing();
+       Buffer * master = getMasterBuffer();
+       if (master != this && use_gui)
+               // We are closing buf which was a child document so we
+               // must update the labels and section numbering of its master
+               // Buffer.
+               updateLabels(*master);
 
        if (!temppath().empty() && !destroyDir(FileName(temppath()))) {
                Alert::warning(_("Could not remove temporary directory"),
@@ -234,6 +243,8 @@ Buffer::~Buffer()
 
        // Remove any previewed LaTeX snippets associated with this buffer.
        graphics::Previews::get().removeLoader(*this);
+
+       closing(this);
 }
 
 
@@ -919,8 +930,11 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
        if (!openFileWrite(ofs, fname))
                return false;
 
+       //TexStream ts(ofs.rdbuf(), &texrow());
+
        bool failed_export = false;
        try {
+               texrow().reset();
                writeLaTeXSource(ofs, original_path,
                      runparams, output_preamble, output_body);
        }
@@ -928,7 +942,7 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
                lyxerr << "Caught iconv exception: " << e.what() << endl;
                failed_export = true;
        }
-       catch (std::exception  const & e) {
+       catch (std::exception const & e) {
                lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
                failed_export = true;
        }
@@ -968,12 +982,8 @@ void Buffer::writeLaTeXSource(odocstream & os,
        validate(features);
        LYXERR(Debug::LATEX) << "  Buffer validation done." << endl;
 
-       texrow().reset();
-
        // The starting paragraph of the coming rows is the
        // first paragraph of the document. (Asger)
-       texrow().start(paragraphs().begin()->id(), 0);
-
        if (output_preamble && runparams.nice) {
                os << "%% LyX " << lyx_version << " created this file.  "
                        "For more info, see http://www.lyx.org/.\n"
@@ -1023,6 +1033,9 @@ void Buffer::writeLaTeXSource(odocstream & os,
                os << "\\begin{document}\n";
                texrow().newline();
        } // output_preamble
+
+       texrow().start(paragraphs().begin()->id(), 0);
+       
        LYXERR(Debug::INFO) << "preamble finished, now the body." << endl;
 
        if (!lyxrc.language_auto_begin &&
@@ -1054,6 +1067,8 @@ void Buffer::writeLaTeXSource(odocstream & os,
                params().parentname.erase();
        }
 
+       loadChildDocuments(*this);
+
        // the real stuff
        latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
 
@@ -1205,6 +1220,8 @@ void Buffer::writeDocBookSource(odocstream & os, string const & fname,
 
        params().getTextClass().counters().reset();
 
+       loadChildDocuments(*this);
+
        sgml::openTag(os, top);
        os << '\n';
        docbookParagraphs(paragraphs(), *this, os, runparams);
@@ -1294,6 +1311,8 @@ void Buffer::validate(LaTeXFeatures & features) const
        if (params().use_esint == BufferParams::package_on)
                features.require("esint");
 
+       loadChildDocuments(*this);
+
        for_each(paragraphs().begin(), paragraphs().end(),
                 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
 
@@ -1341,48 +1360,13 @@ void Buffer::getLabelList(vector<docstring> & list) const
                return;
        }
 
+       loadChildDocuments(*this);
+
        for (InsetIterator it = inset_iterator_begin(inset()); it; ++it)
                it.nextInset()->getLabelList(*this, list);
 }
 
 
-// This is also a buffer property (ale)
-void Buffer::fillWithBibKeys(vector<pair<string, docstring> > & keys)
-       const
-{
-       /// if this is a child document and the parent is already loaded
-       /// use the parent's list instead  [ale990412]
-       Buffer const * tmp = getMasterBuffer();
-       BOOST_ASSERT(tmp);
-       if (tmp != this) {
-               tmp->fillWithBibKeys(keys);
-               return;
-       }
-
-       for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
-               if (it->lyxCode() == Inset::BIBTEX_CODE) {
-                       InsetBibtex const & inset =
-                               static_cast<InsetBibtex const &>(*it);
-                       inset.fillWithBibKeys(*this, keys);
-               } else if (it->lyxCode() == Inset::INCLUDE_CODE) {
-                       InsetInclude const & inset =
-                               static_cast<InsetInclude const &>(*it);
-                       inset.fillWithBibKeys(*this, keys);
-               } else if (it->lyxCode() == Inset::BIBITEM_CODE) {
-                       InsetBibitem const & inset =
-                               static_cast<InsetBibitem const &>(*it);
-                       // FIXME UNICODE
-                       string const key = to_utf8(inset.getParam("key"));
-                       docstring const label = inset.getParam("label");
-                       DocIterator doc_it(it); doc_it.forwardPos();
-                       docstring const ref = doc_it.paragraph().asString(*this, false);
-                       docstring const info = label + "TheBibliographyRef" + ref;
-                       keys.push_back(pair<string, docstring>(key, info));
-               }
-       }
-}
-
-
 void Buffer::updateBibfilesCache()
 {
        // if this is a child document and the parent is already loaded
@@ -1613,9 +1597,7 @@ bool Buffer::isUnnamed() const
 }
 
 
-#ifdef WITH_WARNINGS
-#warning this function should be moved to buffer_pimpl.C
-#endif
+// FIXME: this function should be moved to buffer_pimpl.C
 void Buffer::markDirty()
 {
        if (pimpl_->lyx_clean) {
@@ -1733,13 +1715,6 @@ void Buffer::buildMacros()
 }
 
 
-void Buffer::saveCursor(StableDocIterator cur, StableDocIterator anc)
-{
-       cursor_ = cur;
-       anchor_ = anc;
-}
-
-
 void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
        Inset::Code code)
 {
@@ -1749,18 +1724,18 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
        vector<docstring> labels;
 
        if (code == Inset::CITE_CODE) {
-               vector<pair<string, docstring> > keys;
-               fillWithBibKeys(keys);
-               vector<pair<string, docstring> >::const_iterator bit  = keys.begin();
-               vector<pair<string, docstring> >::const_iterator bend = keys.end();
+               BiblioInfo keys;
+               keys.fillWithBibKeys(this);
+               BiblioInfo::const_iterator bit  = keys.begin();
+               BiblioInfo::const_iterator bend = keys.end();
 
                for (; bit != bend; ++bit)
                        // FIXME UNICODE
-                       labels.push_back(from_utf8(bit->first));
+                       labels.push_back(bit->first);
        } else
                getLabelList(labels);
 
-       if (lyx::count(labels.begin(), labels.end(), from) > 1)
+       if (std::count(labels.begin(), labels.end(), from) > 1)
                return;
 
        for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
@@ -1782,8 +1757,11 @@ void Buffer::getSourceCode(odocstream & os, pit_type par_begin,
        // No side effect of file copying and image conversion
        runparams.dryrun = true;
 
+       texrow().reset();
        if (full_source) {
                os << "% " << _("Preview source code") << "\n\n";
+               texrow().newline();
+               texrow().newline();
                if (isLatex())
                        writeLaTeXSource(os, filePath(), runparams, true, true);
                else {
@@ -1794,7 +1772,7 @@ void Buffer::getSourceCode(odocstream & os, pit_type par_begin,
                runparams.par_end = par_end;
                if (par_begin + 1 == par_end)
                        os << "% "
-                          << bformat(_("Preview source code for paragraph %1$s"), par_begin)
+                          << bformat(_("Preview source code for paragraph %1$d"), par_begin)
                           << "\n\n";
                else
                        os << "% "
@@ -1802,9 +1780,10 @@ void Buffer::getSourceCode(odocstream & os, pit_type par_begin,
                                        convert<docstring>(par_begin),
                                        convert<docstring>(par_end - 1))
                           << "\n\n";
+               texrow().newline();
+               texrow().newline();
                // output paragraphs
                if (isLatex()) {
-                       texrow().reset();
                        latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
                } else {
                        // DocBook