#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 {
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
///
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);
params().temp_bullet(i) = ITEMIZE_DEFAULTS[i];
}
- ErrorList & errorList = errorLists_["Parse"];
+ ErrorList & errorList = pimpl_->errorLists["Parse"];
while (lex.isOK()) {
lex.next();
// 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();
//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;
}
}
}
+ // 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
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;
<< "\\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);
if (!openFileWrite(ofs, fname))
return false;
+ //TexStream ts(ofs.rdbuf(), &texrow());
+
bool failed_export = false;
try {
writeLaTeXSource(ofs, original_path,
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;
}
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
}
+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) {
}
-#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) {
void Buffer::setParentName(string const & name)
{
- params().parentname = name;
+ if (name == pimpl_->filename.absFilename())
+ // Avoids recursive include.
+ params().parentname.clear();
+ else
+ params().parentname = name;
}
} 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) {
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 << "% "
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;
ErrorList & Buffer::errorList(string const & type)
{
- return errorLists_[type];
+ return pimpl_->errorLists[type];
}