]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
small simplification
[lyx.git] / src / Buffer.cpp
index 75fd9e024ae33a42367724ab2d7e96c304f7d4a4..26d171c6b88c81747468fb7386e10c180a340b99 100644 (file)
@@ -33,6 +33,7 @@
 #include "Language.h"
 #include "LaTeX.h"
 #include "LaTeXFeatures.h"
+#include "Layout.h"
 #include "LyXAction.h"
 #include "Lexer.h"
 #include "Text.h"
@@ -55,6 +56,7 @@
 #include "Undo.h"
 #include "version.h"
 #include "EmbeddedFiles.h"
+#include "PDFOptions.h"
 
 #include "insets/InsetBibitem.h"
 #include "insets/InsetBibtex.h"
@@ -66,6 +68,8 @@
 #include "mathed/MathSupport.h"
 
 #include "frontends/alert.h"
+#include "frontends/Delegates.h"
+#include "frontends/WorkAreaManager.h"
 
 #include "graphics/Previews.h"
 
@@ -134,7 +138,7 @@ using support::subst;
 using support::tempName;
 using support::trim;
 using support::sum;
-using support::unzipToDir;
+using support::suffixIs;
 
 namespace Alert = frontend::Alert;
 namespace os = support::os;
@@ -142,7 +146,7 @@ namespace fs = boost::filesystem;
 
 namespace {
 
-int const LYX_FORMAT = 282;
+int const LYX_FORMAT = 288; //RGH, command insets
 
 } // namespace anon
 
@@ -204,13 +208,17 @@ public:
        /// modified. (Used to properly enable 'File->Revert to saved', bug 4114).
        time_t timestamp_;
        unsigned long checksum_;
+
+       ///
+       frontend::WorkAreaManager * wa_;
 };
 
 
 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),
-         toc_backend(&parent), embedded_files(&parent), timestamp_(0), checksum_(0)
+         toc_backend(&parent), embedded_files(&parent), timestamp_(0),
+         checksum_(0), wa_(0)
 {
        inset.setAutoBreakRows(true);
        lyxvc.buffer(&parent);
@@ -219,11 +227,14 @@ Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_)
        // FIXME: And now do something if temppath == string(), because we
        // assume from now on that temppath points to a valid temp dir.
        // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg67406.html
+
+       if (use_gui)
+               wa_ = new frontend::WorkAreaManager;
 }
 
 
 Buffer::Buffer(string const & file, bool readonly)
-       : pimpl_(new Impl(*this, FileName(file), readonly))
+       : pimpl_(new Impl(*this, FileName(file), readonly)), gui_(0)
 {
        LYXERR(Debug::INFO) << "Buffer::Buffer()" << endl;
 }
@@ -251,7 +262,25 @@ Buffer::~Buffer()
        // Remove any previewed LaTeX snippets associated with this buffer.
        graphics::Previews::get().removeLoader(*this);
 
-       closing(this);
+       if (pimpl_->wa_) {
+               pimpl_->wa_->closeAll();
+               delete pimpl_->wa_;
+       }
+       delete pimpl_;
+}
+
+
+void Buffer::changed() const
+{
+       if (pimpl_->wa_)
+               pimpl_->wa_->redrawAll();
+}
+
+
+frontend::WorkAreaManager & Buffer::workAreaManager() const
+{
+       BOOST_ASSERT(pimpl_->wa_);
+       return *pimpl_->wa_;
 }
 
 
@@ -459,6 +488,7 @@ int Buffer::readHeader(Lexer & lex)
        params().footskip.erase();
        params().listings_params.clear();
        params().clearLayoutModules();
+       params().pdfoptions().clear();
        
        for (int i = 0; i < 4; ++i) {
                params().user_defined_bullet(i) = ITEMIZE_DEFAULTS[i];
@@ -531,8 +561,9 @@ bool Buffer::readDocument(Lexer & lex)
        BOOST_ASSERT(paragraphs().empty());
 
        readHeader(lex);
-       if (!params().getTextClass().load(filePath())) {
-               string theclass = params().getTextClass().name();
+       TextClass const & baseClass = textclasslist[params().getBaseClass()];
+       if (!baseClass.load(filePath())) {
+               string theclass = baseClass.name();
                Alert::error(_("Can't load document class"), bformat(
                        _("Using the default document class, because the "
                                     "class %1$s could not be loaded."), from_utf8(theclass)));
@@ -558,7 +589,10 @@ bool Buffer::readDocument(Lexer & lex)
                                         "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
                }
        }
+       // read manifest after header
+       embeddedFiles().readManifest(lex, errorList);   
 
+       // read main text
        bool const res = text().read(*this, lex, errorList);
        for_each(text().paragraphs().begin(),
                 text().paragraphs().end(),
@@ -658,22 +692,13 @@ bool Buffer::readFile(FileName const & filename)
        if (format == "zip") {
                // decompress to a temp directory
                LYXERR(Debug::FILES) << filename << " is in zip format. Unzip to " << temppath() << endl;
-               unzipToDir(filename.toFilesystemEncoding(), temppath());
+               ::unzipToDir(filename.toFilesystemEncoding(), temppath());
                //
-               FileName manifest(addName(temppath(), "manifest.txt"));
-               FileName lyxfile(addName(temppath(), 
-                       onlyFilename(filename.toFilesystemEncoding())));
+               FileName lyxfile(addName(temppath(), "content.lyx"));
                // if both manifest.txt and file.lyx exist, this is am embedded file
-               if (fs::exists(manifest.toFilesystemEncoding()) &&
-                       fs::exists(lyxfile.toFilesystemEncoding())) {
+               if (fs::exists(lyxfile.toFilesystemEncoding())) {
                        params().embedded = true;
                        fname = lyxfile;
-                       // read manifest file
-                       ifstream is(manifest.toFilesystemEncoding().c_str());
-                       is >> pimpl_->embedded_files;
-                       is.close();
-                       LYXERR(Debug::FILES) << filename << " is a embedded file. Its manifest is:\n"
-                                       << pimpl_->embedded_files;
                }
        }
        // The embedded lyx file can also be compressed, for backward compatibility
@@ -750,8 +775,13 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
        // to not overwrite them with those of the file created in the tempdir
        // when it has to be converted to the current format.
        if (!pimpl_->checksum_) {
-               pimpl_->timestamp_ = fs::last_write_time(filename.toFilesystemEncoding());
-               pimpl_->checksum_ = sum(filename);
+               // Save the timestamp and checksum of disk file. If filename is an
+               // emergency file, save the timestamp and sum of the original lyx file
+               // because isExternallyModified will check for this file. (BUG4193)
+               string diskfile = filename.toFilesystemEncoding();
+               if (suffixIs(diskfile, ".emergency"))
+                       diskfile = diskfile.substr(0, diskfile.size() - 10);
+               saveCheckSum(diskfile);
        }
 
        if (file_format != LYX_FORMAT) {
@@ -868,8 +898,7 @@ bool Buffer::save() const
        if (writeFile(pimpl_->filename)) {
                markClean();
                removeAutosaveFile(fileName());
-               pimpl_->timestamp_ = fs::last_write_time(pimpl_->filename.toFilesystemEncoding());
-               pimpl_->checksum_ = sum(pimpl_->filename);
+               saveCheckSum(pimpl_->filename.toFilesystemEncoding());
                return true;
        } else {
                // Saving failed, so backup is not backup
@@ -890,8 +919,7 @@ bool Buffer::writeFile(FileName const & fname) const
        FileName content;
        if (params().embedded)
                // first write the .lyx file to the temporary directory
-               content = FileName(addName(temppath(), 
-                       onlyFilename(fname.toFilesystemEncoding())));
+               content = FileName(addName(temppath(), "content.lyx"));
        else
                content = fname;
        
@@ -911,9 +939,8 @@ bool Buffer::writeFile(FileName const & fname) const
 
        if (retval && params().embedded) {
                // write file.lyx and all the embedded files to the zip file fname
-               // if embedding is enabled, and there is any embedded file
-               pimpl_->embedded_files.update();
-               return pimpl_->embedded_files.write(fname);
+               // if embedding is enabled
+               return pimpl_->embedded_files.writeFile(fname);
        }
        return retval;
 }
@@ -952,6 +979,12 @@ bool Buffer::write(ostream & ofs) const
        params().writeFile(ofs);
        ofs << "\\end_header\n";
 
+       // write the manifest after header
+       ofs << "\n\\begin_manifest\n";
+       pimpl_->embedded_files.update();
+       embeddedFiles().writeManifest(ofs);
+       ofs << "\\end_manifest\n";
+
        // write the text
        ofs << "\n\\begin_body\n";
        text().write(*this, ofs);
@@ -1530,9 +1563,6 @@ void Buffer::changeLanguage(Language const * from, Language const * to)
        for_each(par_iterator_begin(),
                 par_iterator_end(),
                 bind(&Paragraph::changeLanguage, _1, params(), from, to));
-
-       text().current_font.setLanguage(to);
-       text().real_current_font.setLanguage(to);
 }
 
 
@@ -1631,6 +1661,19 @@ bool Buffer::isExternallyModified(CheckMethod method) const
 }
 
 
+void Buffer::saveCheckSum(string const & file) const
+{
+       if (fs::exists(file)) {
+               pimpl_->timestamp_ = fs::last_write_time(file);
+               pimpl_->checksum_ = sum(FileName(file));
+       } else {
+               // in the case of save to a new file.
+               pimpl_->timestamp_ = 0;
+               pimpl_->checksum_ = 0;
+       }
+}
+
+
 void Buffer::markClean() const
 {
        if (!pimpl_->lyx_clean) {
@@ -1727,7 +1770,10 @@ Buffer * Buffer::getMasterBuffer()
        if (!params().parentname.empty()
            && theBufferList().exists(params().parentname)) {
                Buffer * buf = theBufferList().getBuffer(params().parentname);
-               if (buf)
+               //We need to check if the parent is us...
+               //FIXME RECURSIVE INCLUDE
+               //This is not sufficient, since recursive includes could be downstream.
+               if (buf && buf != this)
                        return buf->getMasterBuffer();
        }
 
@@ -1874,4 +1920,65 @@ ErrorList & Buffer::errorList(string const & type)
 }
 
 
+void Buffer::structureChanged() const
+{
+       if (gui_)
+               gui_->structureChanged();
+}
+
+
+void Buffer::embeddingChanged() const
+{
+       if (gui_)
+               gui_->embeddingChanged();
+}
+
+
+void Buffer::errors(std::string const & err) const
+{
+       if (gui_)
+               gui_->errors(err);
+}
+
+
+void Buffer::message(docstring const & msg) const
+{
+       if (gui_)
+               gui_->message(msg);
+}
+
+
+void Buffer::busy(bool on) const
+{
+       if (gui_)
+               gui_->busy(on);
+}
+
+
+void Buffer::readonly(bool on) const
+{
+       if (gui_)
+               gui_->readonly(on);
+}
+
+
+void Buffer::updateTitles() const
+{
+       if (gui_)
+               gui_->updateTitles();
+}
+
+
+void Buffer::resetAutosaveTimers() const
+{
+       if (gui_)
+               gui_->resetAutosaveTimers();
+}
+
+
+void Buffer::setGuiDelegate(frontend::GuiBufferDelegate * gui)
+{
+       gui_ = gui;
+}
+
 } // namespace lyx