]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
Transfer current_font and real_current_font from Text to Cursor.
[lyx.git] / src / Buffer.cpp
index 5f73e27f6bec1f9ce4eb07510839b996138d3ad4..42a602b169a3edc5582742be6c4e7157cfb67dea 100644 (file)
 #include "ParIterator.h"
 #include "sgml.h"
 #include "TexRow.h"
+#include "TextClassList.h"
 #include "TexStream.h"
 #include "TocBackend.h"
 #include "Undo.h"
 #include "version.h"
+#include "EmbeddedFiles.h"
 
 #include "insets/InsetBibitem.h"
 #include "insets/InsetBibtex.h"
@@ -97,6 +99,7 @@ using std::map;
 using std::ostream;
 using std::ostringstream;
 using std::ofstream;
+using std::ifstream;
 using std::pair;
 using std::stack;
 using std::vector;
@@ -131,6 +134,7 @@ using support::subst;
 using support::tempName;
 using support::trim;
 using support::sum;
+using support::unzipToDir;
 
 namespace Alert = frontend::Alert;
 namespace os = support::os;
@@ -138,7 +142,7 @@ namespace fs = boost::filesystem;
 
 namespace {
 
-int const LYX_FORMAT = 280;
+int const LYX_FORMAT = 282;
 
 } // namespace anon
 
@@ -193,6 +197,9 @@ public:
        /// Container for all sort of Buffer dependant errors.
        map<string, ErrorList> errorLists;
 
+       /// all embedded files of this buffer
+       EmbeddedFiles embedded_files;
+
        /// timestamp and checksum used to test if the file has been externally
        /// modified. (Used to properly enable 'File->Revert to saved', bug 4114).
        time_t timestamp_;
@@ -203,7 +210,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),
-         toc_backend(&parent), timestamp_(0), checksum_(0)
+         toc_backend(&parent), embedded_files(&parent), timestamp_(0), checksum_(0)
 {
        inset.setAutoBreakRows(true);
        lyxvc.buffer(&parent);
@@ -350,6 +357,18 @@ TocBackend const & Buffer::tocBackend() const
 }
 
 
+EmbeddedFiles & Buffer::embeddedFiles()
+{
+       return pimpl_->embedded_files;
+}
+
+
+EmbeddedFiles const & Buffer::embeddedFiles() const
+{
+       return pimpl_->embedded_files;
+}
+
+
 string const Buffer::getLatexName(bool const no_path) const
 {
        string const name = changeExtension(makeLatexName(fileName()), ".tex");
@@ -439,6 +458,8 @@ int Buffer::readHeader(Lexer & lex)
        params().headsep.erase();
        params().footskip.erase();
        params().listings_params.clear();
+       params().clearLayoutModules();
+       
        for (int i = 0; i < 4; ++i) {
                params().user_defined_bullet(i) = ITEMIZE_DEFAULTS[i];
                params().temp_bullet(i) = ITEMIZE_DEFAULTS[i];
@@ -515,7 +536,7 @@ bool Buffer::readDocument(Lexer & lex)
                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)));
-               params().textclass = 0;
+               params().setBaseClass(defaultTextclass());
        }
 
        if (params().outputChanges) {
@@ -631,8 +652,32 @@ bool Buffer::readString(std::string const & s)
 
 bool Buffer::readFile(FileName const & filename)
 {
+       FileName fname(filename);
        // Check if the file is compressed.
-       string const format = getFormatFromContents(filename);
+       string format = getFormatFromContents(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());
+               //
+               FileName manifest(addName(temppath(), "manifest.txt"));
+               FileName lyxfile(addName(temppath(), 
+                       onlyFilename(filename.toFilesystemEncoding())));
+               // if both manifest.txt and file.lyx exist, this is am embedded file
+               if (fs::exists(manifest.toFilesystemEncoding()) &&
+                       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
+       format = getFormatFromContents(fname);
        if (format == "gzip" || format == "zip" || format == "compress") {
                params().compressed = true;
        }
@@ -640,8 +685,8 @@ bool Buffer::readFile(FileName const & filename)
        // remove dummy empty par
        paragraphs().clear();
        Lexer lex(0, 0);
-       lex.setFile(filename);
-       if (readFile(lex, filename) != success)
+       lex.setFile(fname);
+       if (readFile(lex, fname) != success)
                return false;
 
        return true;
@@ -701,6 +746,14 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
        int const file_format = convert<int>(tmp_format);
        //lyxerr << "format: " << file_format << endl;
 
+       // save timestamp and checksum of the original disk file, making sure
+       // 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);
+       }
+
        if (file_format != LYX_FORMAT) {
 
                if (fromstring)
@@ -767,9 +820,6 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
        //MacroTable::localMacros().clear();
 
        pimpl_->file_fully_loaded = true;
-       // save the timestamp and checksum of disk file
-       pimpl_->timestamp_ = fs::last_write_time(filename.toFilesystemEncoding());
-       pimpl_->checksum_ = sum(filename);
        return success;
 }
 
@@ -837,20 +887,34 @@ bool Buffer::writeFile(FileName const & fname) const
 
        bool retval = false;
 
+       FileName content;
+       if (params().embedded)
+               // first write the .lyx file to the temporary directory
+               content = FileName(addName(temppath(), 
+                       onlyFilename(fname.toFilesystemEncoding())));
+       else
+               content = fname;
+       
        if (params().compressed) {
-               gz::ogzstream ofs(fname.toFilesystemEncoding().c_str(), ios::out|ios::trunc);
+               gz::ogzstream ofs(content.toFilesystemEncoding().c_str(), ios::out|ios::trunc);
                if (!ofs)
                        return false;
 
                retval = write(ofs);
        } else {
-               ofstream ofs(fname.toFilesystemEncoding().c_str(), ios::out|ios::trunc);
+               ofstream ofs(content.toFilesystemEncoding().c_str(), ios::out|ios::trunc);
                if (!ofs)
                        return false;
 
                retval = write(ofs);
        }
 
+       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);
+       }
        return retval;
 }
 
@@ -1466,9 +1530,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);
 }