]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
Fixed some lines that were too long. It compiled afterwards.
[lyx.git] / src / Buffer.cpp
index 15246e680d45e7838bdc048e9f72f7ff896280a3..5e9972138cb09dce281f4ccf29087e79fadbfcc1 100644 (file)
@@ -47,6 +47,7 @@
 #include "ParIterator.h"
 #include "sgml.h"
 #include "TexRow.h"
+#include "TexStream.h"
 #include "TocBackend.h"
 #include "Undo.h"
 #include "version.h"
 #include "support/lyxalgo.h"
 #include "support/filetools.h"
 #include "support/fs_extras.h"
+#include "support/gzstream.h"
 #include "support/lyxlib.h"
 #include "support/os.h"
 #include "support/Path.h"
 #include "support/textutils.h"
 #include "support/convert.h"
 
-#include <boost/iostreams/filtering_stream.hpp>
-#include <boost/iostreams/filter/gzip.hpp>
-#include <boost/iostreams/device/file.hpp>
 #include <boost/bind.hpp>
 #include <boost/filesystem/exception.hpp>
 #include <boost/filesystem/operations.hpp>
 
-#if defined (HAVE_UTIME_H)
-#include <utime.h>
-#elif defined (HAVE_SYS_UTIME_H)
-#include <sys/utime.h>
-#endif
-
+#include <algorithm>
 #include <iomanip>
 #include <stack>
 #include <sstream>
 #include <fstream>
 
+using std::endl;
+using std::for_each;
+using std::make_pair;
+
+using std::ios;
+using std::map;
+using std::ostream;
+using std::ostringstream;
+using std::ofstream;
+using std::pair;
+using std::stack;
+using std::vector;
+using std::string;
+using std::time_t;
+
 
 namespace lyx {
 
@@ -119,30 +128,15 @@ using support::split;
 using support::subst;
 using support::tempName;
 using support::trim;
+using support::sum;
 
 namespace Alert = frontend::Alert;
 namespace os = support::os;
 namespace fs = boost::filesystem;
-namespace io = boost::iostreams;
-
-using std::endl;
-using std::for_each;
-using std::make_pair;
-
-using std::ios;
-using std::map;
-using std::ostream;
-using std::ostringstream;
-using std::ofstream;
-using std::pair;
-using std::stack;
-using std::vector;
-using std::string;
-
 
 namespace {
 
-int const LYX_FORMAT = 271;
+int const LYX_FORMAT = 278;
 
 } // namespace anon
 
@@ -193,13 +187,21 @@ public:
 
        ///
        TocBackend toc_backend;
+
+       /// Container for all sort of Buffer dependant errors.
+       map<string, ErrorList> errorLists;
+
+       /// 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_;
+       unsigned long checksum_;
 };
 
 
 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)
+         toc_backend(&parent), timestamp_(0), checksum_(0)
 {
        inset.setAutoBreakRows(true);
        lyxvc.buffer(&parent);
@@ -433,7 +435,7 @@ int Buffer::readHeader(Lexer & lex)
                params().temp_bullet(i) = ITEMIZE_DEFAULTS[i];
        }
 
-       ErrorList & errorList = errorLists_["Parse"];
+       ErrorList & errorList = pimpl_->errorLists["Parse"];
 
        while (lex.isOK()) {
                lex.next();
@@ -484,7 +486,7 @@ int Buffer::readHeader(Lexer & lex)
 // Returns false if "\end_document" is not read (Asger)
 bool Buffer::readDocument(Lexer & lex)
 {
-       ErrorList & errorList = errorLists_["Parse"];
+       ErrorList & errorList = pimpl_->errorLists["Parse"];
        errorList.clear();
 
        lex.next();
@@ -756,6 +758,9 @@ 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;
 }
 
@@ -790,9 +795,22 @@ bool Buffer::save() const
                }
        }
 
+       // ask if the disk file has been externally modified (use checksum method)
+       if (fs::exists(encodedFilename) && isExternallyModified(checksum_method)) {
+               docstring const file = makeDisplayPath(fileName(), 20);
+               docstring text = bformat(_("Document %1$s has been externally modified. Are you sure "
+                                                            "you want to overwrite this file?"), file);
+               int const ret = Alert::prompt(_("Overwrite modified file?"),
+                       text, 1, 1, _("&Overwrite"), _("&Cancel"));
+               if (ret == 1)
+                       return false;
+       }
+
        if (writeFile(pimpl_->filename)) {
                markClean();
                removeAutosaveFile(fileName());
+               pimpl_->timestamp_ = fs::last_write_time(pimpl_->filename.toFilesystemEncoding());
+               pimpl_->checksum_ = sum(pimpl_->filename);
                return true;
        } else {
                // Saving failed, so backup is not backup
@@ -811,7 +829,7 @@ bool Buffer::writeFile(FileName const & fname) const
        bool retval = false;
 
        if (params().compressed) {
-               io::filtering_ostream ofs(io::gzip_compressor() | io::file_sink(fname.toFilesystemEncoding()));
+               gz::ogzstream ofs(fname.toFilesystemEncoding().c_str(), ios::out|ios::trunc);
                if (!ofs)
                        return false;
 
@@ -843,6 +861,19 @@ bool Buffer::write(ostream & ofs) const
            << "\\lyxformat " << LYX_FORMAT << "\n"
            << "\\begin_document\n";
 
+
+       /// For each author, set 'used' to true if there is a change
+       /// by this author in the document; otherwise set it to 'false'.
+       AuthorList::Authors::const_iterator a_it = params().authors().begin();
+       AuthorList::Authors::const_iterator a_end = params().authors().end();
+       for (; a_it != a_end; ++a_it)
+               a_it->second.used(false);
+
+       ParIterator const end = par_iterator_end();
+       ParIterator it = par_iterator_begin();
+       for ( ; it != end; ++it)
+               it->checkAuthors(params().authors());
+
        // now write out the buffer parameters.
        ofs << "\\begin_header\n";
        params().writeFile(ofs);
@@ -890,6 +921,8 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
        if (!openFileWrite(ofs, fname))
                return false;
 
+       //TexStream ts(ofs.rdbuf(), &texrow());
+
        bool failed_export = false;
        try {
                writeLaTeXSource(ofs, original_path,
@@ -899,7 +932,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;
        }
@@ -1211,7 +1244,7 @@ int Buffer::runChktex()
                Alert::error(_("chktex failure"),
                             _("Could not run chktex successfully."));
        } else if (res > 0) {
-               ErrorList & errorList = errorLists_["ChkTeX"];
+               ErrorList & errorList = pimpl_->errorLists["ChkTeX"];
                // Clear out old errors
                errorList.clear();
                // Fill-in the error list with the TeX errors
@@ -1544,6 +1577,16 @@ bool Buffer::isBakClean() const
 }
 
 
+bool Buffer::isExternallyModified(CheckMethod method) const
+{
+       BOOST_ASSERT(fs::exists(pimpl_->filename.toFilesystemEncoding()));
+       // if method == timestamp, check timestamp before checksum
+       return (method == checksum_method 
+               || pimpl_->timestamp_ != fs::last_write_time(pimpl_->filename.toFilesystemEncoding()))
+               && pimpl_->checksum_ != sum(pimpl_->filename);
+}
+
+
 void Buffer::markClean() const
 {
        if (!pimpl_->lyx_clean) {
@@ -1574,9 +1617,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) {
@@ -1613,7 +1654,11 @@ bool Buffer::isReadonly() const
 
 void Buffer::setParentName(string const & name)
 {
-       params().parentname = name;
+       if (name == pimpl_->filename.absFilename())
+               // Avoids recursive include.
+               params().parentname.clear();
+       else
+               params().parentname = name;
 }
 
 
@@ -1717,7 +1762,7 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
        } 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) {
@@ -1751,7 +1796,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 << "% "
@@ -1774,8 +1819,8 @@ void Buffer::getSourceCode(odocstream & os, pit_type par_begin,
 ErrorList const & Buffer::errorList(string const & type) const
 {
        static ErrorList const emptyErrorList;
-       std::map<string, ErrorList>::const_iterator I = errorLists_.find(type);
-       if (I == errorLists_.end())
+       std::map<string, ErrorList>::const_iterator I = pimpl_->errorLists.find(type);
+       if (I == pimpl_->errorLists.end())
                return emptyErrorList;
 
        return I->second;
@@ -1784,7 +1829,7 @@ ErrorList const & Buffer::errorList(string const & type) const
 
 ErrorList & Buffer::errorList(string const & type)
 {
-       return errorLists_[type];
+       return pimpl_->errorLists[type];
 }