1999-09-27 18:44:28 +00:00
|
|
|
|
/* This file is part of
|
1999-11-15 12:01:38 +00:00
|
|
|
|
* ======================================================
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*
|
|
|
|
|
* LyX, The Document Word Processor
|
|
|
|
|
*
|
2000-02-23 16:39:03 +00:00
|
|
|
|
* Copyright 1995 Matthias Ettrich
|
2001-05-30 13:53:44 +00:00
|
|
|
|
* Copyright 1995-2001 The LyX Team.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*
|
2001-05-30 13:53:44 +00:00
|
|
|
|
* This file is Copyright 1996-2001
|
1999-09-27 18:44:28 +00:00
|
|
|
|
* Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
*
|
1999-11-15 12:01:38 +00:00
|
|
|
|
* ======================================================
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2001-12-10 20:06:59 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#ifdef __GNUG__
|
|
|
|
|
#pragma implementation
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "bufferlist.h"
|
|
|
|
|
#include "lyx_main.h"
|
|
|
|
|
#include "lastfiles.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
|
#include "debug.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#include "lyxrc.h"
|
|
|
|
|
#include "lyxtext.h"
|
|
|
|
|
#include "lyx_cb.h"
|
2000-04-12 14:20:08 +00:00
|
|
|
|
#include "bufferview_funcs.h"
|
2001-06-25 00:06:33 +00:00
|
|
|
|
#include "BufferView.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#include "gettext.h"
|
1999-12-19 22:35:36 +00:00
|
|
|
|
#include "LyXView.h"
|
2000-01-11 08:23:07 +00:00
|
|
|
|
#include "vc-backend.h"
|
2000-02-04 09:38:32 +00:00
|
|
|
|
#include "TextCache.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-12-10 20:06:59 +00:00
|
|
|
|
#include "frontends/Alert.h"
|
|
|
|
|
|
|
|
|
|
#include "support/FileInfo.h"
|
|
|
|
|
#include "support/filetools.h"
|
|
|
|
|
#include "support/lyxmanip.h"
|
|
|
|
|
#include "support/lyxfunctional.h"
|
|
|
|
|
#include "support/LAssert.h"
|
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
#include <cassert>
|
2001-12-10 20:06:59 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
|
using std::vector;
|
2000-02-03 17:09:33 +00:00
|
|
|
|
using std::find;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
using std::endl;
|
2000-10-12 15:17:42 +00:00
|
|
|
|
using std::find_if;
|
|
|
|
|
using std::for_each;
|
2000-10-13 14:10:35 +00:00
|
|
|
|
using std::mem_fun;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
|
2000-11-14 02:01:57 +00:00
|
|
|
|
extern BufferView * current_view;
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
//
|
|
|
|
|
// Class BufferStorage
|
|
|
|
|
//
|
|
|
|
|
|
1999-11-05 06:02:34 +00:00
|
|
|
|
void BufferStorage::release(Buffer * buf)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
|
lyx::Assert(buf);
|
2000-02-03 17:09:33 +00:00
|
|
|
|
Container::iterator it = find(container.begin(), container.end(), buf);
|
|
|
|
|
if (it != container.end()) {
|
2000-02-04 09:38:32 +00:00
|
|
|
|
// Make sure that we don't store a LyXText in
|
|
|
|
|
// the textcache that points to the buffer
|
|
|
|
|
// we just deleted.
|
2000-02-03 17:09:33 +00:00
|
|
|
|
Buffer * tmp = (*it);
|
|
|
|
|
container.erase(it);
|
2000-02-04 09:38:32 +00:00
|
|
|
|
textcache.removeAllWithBuffer(tmp);
|
2000-02-03 17:09:33 +00:00
|
|
|
|
delete tmp;
|
1999-11-05 06:02:34 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-03-12 10:35:05 +00:00
|
|
|
|
Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2000-03-12 10:35:05 +00:00
|
|
|
|
Buffer * tmpbuf = new Buffer(s, ronly);
|
1999-11-05 06:02:34 +00:00
|
|
|
|
tmpbuf->params.useClassDefaults();
|
2000-11-15 03:22:08 +00:00
|
|
|
|
lyxerr[Debug::INFO] << "Assigning to buffer "
|
|
|
|
|
<< container.size() << endl;
|
1999-11-05 06:02:34 +00:00
|
|
|
|
container.push_back(tmpbuf);
|
|
|
|
|
return tmpbuf;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Class BufferList
|
|
|
|
|
//
|
1999-11-15 12:27:25 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
BufferList::BufferList()
|
2000-01-20 01:41:55 +00:00
|
|
|
|
: state_(BufferList::OK)
|
|
|
|
|
{}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
2000-02-03 17:09:33 +00:00
|
|
|
|
bool BufferList::empty() const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-11-05 06:02:34 +00:00
|
|
|
|
return bstore.empty();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
bool BufferList::qwriteOne(Buffer * buf, string const & fname,
|
|
|
|
|
string & unsaved_list)
|
2001-11-26 10:19:58 +00:00
|
|
|
|
{
|
|
|
|
|
bool reask = true;
|
|
|
|
|
while (reask) {
|
|
|
|
|
switch (Alert::askConfirmation(_("Changes in document:"),
|
|
|
|
|
fname,
|
|
|
|
|
_("Save document?"))) {
|
|
|
|
|
case 1: // Yes
|
|
|
|
|
// FIXME: WriteAs can be asynch !
|
|
|
|
|
if (buf->isUnnamed())
|
|
|
|
|
reask = !WriteAs(current_view, buf);
|
|
|
|
|
else {
|
|
|
|
|
reask = !MenuWrite(current_view, buf);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2: // No
|
|
|
|
|
// if we crash after this we could
|
|
|
|
|
// have no autosave file but I guess
|
|
|
|
|
// this is really inprobable (Jug)
|
|
|
|
|
if (buf->isUnnamed()) {
|
|
|
|
|
removeAutosaveFile(buf->fileName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsaved_list += MakeDisplayPath(fname, 50) + "\n";
|
|
|
|
|
return true;
|
|
|
|
|
case 3: // Cancel
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-11-28 16:09:25 +00:00
|
|
|
|
return true;
|
2001-11-26 10:19:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-06-28 10:25:20 +00:00
|
|
|
|
bool BufferList::qwriteAll()
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-11-28 16:09:25 +00:00
|
|
|
|
string unsaved;
|
2001-12-28 13:26:54 +00:00
|
|
|
|
BufferStorage::iterator it = bstore.begin();
|
|
|
|
|
BufferStorage::iterator end = bstore.end();
|
|
|
|
|
for (; it != end; ++it) {
|
1999-11-05 06:02:34 +00:00
|
|
|
|
if (!(*it)->isLyxClean()) {
|
2000-07-26 13:43:16 +00:00
|
|
|
|
string fname;
|
|
|
|
|
if ((*it)->isUnnamed())
|
|
|
|
|
fname = OnlyFilename((*it)->fileName());
|
|
|
|
|
else
|
|
|
|
|
fname = MakeDisplayPath((*it)->fileName(), 50);
|
2001-11-28 16:09:25 +00:00
|
|
|
|
if (!qwriteOne(*it, fname, unsaved)) // cancel the request!
|
|
|
|
|
return false;
|
1999-11-05 06:02:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2001-11-26 10:19:58 +00:00
|
|
|
|
|
2001-11-28 16:09:25 +00:00
|
|
|
|
if (!unsaved.empty() && lyxrc.exit_confirmation) {
|
2001-11-26 10:19:58 +00:00
|
|
|
|
return Alert::askQuestion(_("Some documents were not saved:"),
|
2001-11-28 16:09:25 +00:00
|
|
|
|
unsaved, _("Exit anyway?"));
|
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-11-28 16:09:25 +00:00
|
|
|
|
return true;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BufferList::closeAll()
|
|
|
|
|
{
|
2000-01-20 01:41:55 +00:00
|
|
|
|
state_ = BufferList::CLOSING;
|
2000-02-04 09:38:32 +00:00
|
|
|
|
// Since we are closing we can just as well delete all
|
|
|
|
|
// in the textcache this will also speed the closing/quiting up a bit.
|
|
|
|
|
textcache.clear();
|
|
|
|
|
|
1999-11-05 06:02:34 +00:00
|
|
|
|
while (!bstore.empty()) {
|
|
|
|
|
close(bstore.front());
|
|
|
|
|
}
|
2000-01-20 01:41:55 +00:00
|
|
|
|
state_ = BufferList::OK;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-11-05 06:02:34 +00:00
|
|
|
|
bool BufferList::close(Buffer * buf)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
|
lyx::Assert(buf);
|
|
|
|
|
|
2000-06-05 19:36:14 +00:00
|
|
|
|
// CHECK
|
|
|
|
|
// Trace back why we need to use buf->getUser here.
|
|
|
|
|
// Perhaps slight rewrite is in order? (Lgb)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
if (buf->getUser())
|
|
|
|
|
buf->getUser()->insetUnlock();
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (buf->paragraph && !buf->isLyxClean() && !quitting) {
|
2000-07-26 13:43:16 +00:00
|
|
|
|
if (buf->getUser())
|
2001-07-03 15:19:04 +00:00
|
|
|
|
buf->getUser()->owner()->prohibitInput();
|
2000-07-26 13:43:16 +00:00
|
|
|
|
string fname;
|
|
|
|
|
if (buf->isUnnamed())
|
|
|
|
|
fname = OnlyFilename(buf->fileName());
|
|
|
|
|
else
|
|
|
|
|
fname = MakeDisplayPath(buf->fileName(), 50);
|
|
|
|
|
bool reask = true;
|
2000-09-26 13:54:57 +00:00
|
|
|
|
while (reask) {
|
2001-11-26 10:19:58 +00:00
|
|
|
|
switch (Alert::askConfirmation(_("Changes in document:"),
|
2000-07-26 13:43:16 +00:00
|
|
|
|
fname,
|
2002-02-16 15:59:55 +00:00
|
|
|
|
_("Save document?"))) {
|
2000-07-26 13:43:16 +00:00
|
|
|
|
case 1: // Yes
|
|
|
|
|
if (buf->isUnnamed())
|
2001-03-07 14:25:31 +00:00
|
|
|
|
reask = !WriteAs(current_view, buf);
|
2000-07-26 13:43:16 +00:00
|
|
|
|
else if (buf->save()) {
|
|
|
|
|
lastfiles->newFile(buf->fileName());
|
2000-09-26 15:25:14 +00:00
|
|
|
|
reask = false;
|
2000-07-26 13:43:16 +00:00
|
|
|
|
} else {
|
|
|
|
|
if (buf->getUser())
|
2001-07-03 15:19:04 +00:00
|
|
|
|
buf->getUser()->owner()->allowInput();
|
2000-07-26 13:43:16 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
2000-08-08 15:36:25 +00:00
|
|
|
|
if (buf->isUnnamed()) {
|
|
|
|
|
removeAutosaveFile(buf->fileName());
|
|
|
|
|
}
|
2000-07-26 13:43:16 +00:00
|
|
|
|
reask = false;
|
|
|
|
|
break;
|
|
|
|
|
case 3: // Cancel
|
|
|
|
|
if (buf->getUser())
|
2001-07-03 15:19:04 +00:00
|
|
|
|
buf->getUser()->owner()->allowInput();
|
2000-02-22 00:36:17 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2000-07-26 13:43:16 +00:00
|
|
|
|
}
|
|
|
|
|
if (buf->getUser())
|
2001-07-03 15:19:04 +00:00
|
|
|
|
buf->getUser()->owner()->allowInput();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bstore.release(buf);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
|
vector<string> const BufferList::getFileNames() const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2000-01-20 01:41:55 +00:00
|
|
|
|
vector<string> nvec;
|
2000-10-11 21:06:43 +00:00
|
|
|
|
std::copy(bstore.begin(), bstore.end(),
|
2001-04-24 15:25:26 +00:00
|
|
|
|
lyx::back_inserter_fun(nvec, &Buffer::fileName));
|
2000-01-20 01:41:55 +00:00
|
|
|
|
return nvec;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-11-05 06:02:34 +00:00
|
|
|
|
Buffer * BufferList::first()
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-11-05 06:02:34 +00:00
|
|
|
|
if (bstore.empty()) return 0;
|
|
|
|
|
return bstore.front();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-10-11 21:06:43 +00:00
|
|
|
|
Buffer * BufferList::getBuffer(unsigned int choice)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-11-05 06:02:34 +00:00
|
|
|
|
if (choice >= bstore.size()) return 0;
|
|
|
|
|
return bstore[choice];
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-11-05 06:02:34 +00:00
|
|
|
|
int BufferList::unlockInset(UpdatableInset * inset)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
|
lyx::Assert(inset);
|
|
|
|
|
|
2001-12-28 13:26:54 +00:00
|
|
|
|
BufferStorage::iterator it = bstore.begin();
|
|
|
|
|
BufferStorage::iterator end = bstore.end();
|
|
|
|
|
for (; it != end; ++it) {
|
2000-01-08 21:02:58 +00:00
|
|
|
|
if ((*it)->getUser()
|
2000-10-03 13:55:48 +00:00
|
|
|
|
&& (*it)->getUser()->theLockingInset() == inset) {
|
2000-01-07 03:42:16 +00:00
|
|
|
|
(*it)->getUser()->insetUnlock();
|
1999-11-05 06:02:34 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-12-28 13:26:54 +00:00
|
|
|
|
BufferStorage::iterator it = bstore.begin();
|
|
|
|
|
BufferStorage::iterator end = bstore.end();
|
|
|
|
|
for (; it != end; ++it) {
|
1999-11-05 06:02:34 +00:00
|
|
|
|
if (!(*it)->isDepClean(mastertmpdir)) {
|
|
|
|
|
string writefile = mastertmpdir;
|
|
|
|
|
writefile += '/';
|
2000-05-11 16:12:46 +00:00
|
|
|
|
writefile += (*it)->getLatexName();
|
1999-11-05 06:02:34 +00:00
|
|
|
|
(*it)->makeLaTeXFile(writefile, mastertmpdir,
|
|
|
|
|
false, true);
|
|
|
|
|
(*it)->markDepClean(mastertmpdir);
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BufferList::emergencyWriteAll()
|
|
|
|
|
{
|
2000-10-11 21:06:43 +00:00
|
|
|
|
for_each(bstore.begin(), bstore.end(),
|
2001-04-24 15:25:26 +00:00
|
|
|
|
lyx::class_fun(*this, &BufferList::emergencyWrite));
|
2000-10-11 21:06:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BufferList::emergencyWrite(Buffer * buf)
|
|
|
|
|
{
|
2001-05-09 15:20:58 +00:00
|
|
|
|
// assert(buf) // this is not good since C assert takes an int
|
|
|
|
|
// and a pointer is a long (JMarc)
|
|
|
|
|
assert(buf != 0); // use c assert to avoid a loop
|
|
|
|
|
|
2001-04-24 15:25:26 +00:00
|
|
|
|
|
2000-10-11 21:06:43 +00:00
|
|
|
|
// No need to save if the buffer has not changed.
|
2001-12-28 13:26:54 +00:00
|
|
|
|
if (buf->isLyxClean())
|
|
|
|
|
return;
|
2000-10-11 21:06:43 +00:00
|
|
|
|
|
|
|
|
|
lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
|
|
|
|
|
buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
|
|
|
|
|
: buf->fileName().c_str()) << endl;
|
|
|
|
|
|
|
|
|
|
// We try to save three places:
|
|
|
|
|
|
|
|
|
|
// 1) Same place as document. Unless it is an unnamed doc.
|
|
|
|
|
if (!buf->isUnnamed()) {
|
|
|
|
|
string s = buf->fileName();
|
|
|
|
|
s += ".emergency";
|
|
|
|
|
lyxerr << " " << s << endl;
|
|
|
|
|
if (buf->writeFile(s, true)) {
|
|
|
|
|
buf->markLyxClean();
|
|
|
|
|
lyxerr << _(" Save seems successful. Phew.") << endl;
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
lyxerr << _(" Save failed! Trying...") << endl;
|
1999-11-05 06:02:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2000-10-11 21:06:43 +00:00
|
|
|
|
|
|
|
|
|
// 2) In HOME directory.
|
|
|
|
|
string s = AddName(GetEnvPath("HOME"), buf->fileName());
|
|
|
|
|
s += ".emergency";
|
|
|
|
|
lyxerr << " " << s << endl;
|
|
|
|
|
if (buf->writeFile(s, true)) {
|
|
|
|
|
buf->markLyxClean();
|
|
|
|
|
lyxerr << _(" Save seems successful. Phew.") << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lyxerr << _(" Save failed! Trying...") << endl;
|
|
|
|
|
|
|
|
|
|
// 3) In "/tmp" directory.
|
|
|
|
|
// MakeAbsPath to prepend the current
|
|
|
|
|
// drive letter on OS/2
|
|
|
|
|
s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
|
|
|
|
|
s += ".emergency";
|
|
|
|
|
lyxerr << " " << s << endl;
|
|
|
|
|
if (buf->writeFile(s, true)) {
|
|
|
|
|
buf->markLyxClean();
|
|
|
|
|
lyxerr << _(" Save seems successful. Phew.") << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-10-11 21:06:43 +00:00
|
|
|
|
|
1999-11-15 12:27:25 +00:00
|
|
|
|
Buffer * BufferList::readFile(string const & s, bool ronly)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-12-28 13:26:54 +00:00
|
|
|
|
string ts(s);
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string e = OnlyPath(s);
|
|
|
|
|
string a = e;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// File information about normal file
|
|
|
|
|
FileInfo fileInfo2(s);
|
|
|
|
|
|
2001-08-16 10:19:59 +00:00
|
|
|
|
if (!fileInfo2.exist()) {
|
2001-11-26 10:19:58 +00:00
|
|
|
|
Alert::alert(_("Error!"), _("Cannot open file"),
|
2001-08-16 10:19:59 +00:00
|
|
|
|
MakeDisplayPath(s));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-01-13 21:35:50 +00:00
|
|
|
|
Buffer * b = bstore.newBuffer(s, ronly);
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Check if emergency save file exists and is newer.
|
|
|
|
|
e += OnlyFilename(s) + ".emergency";
|
|
|
|
|
FileInfo fileInfoE(e);
|
|
|
|
|
|
|
|
|
|
bool use_emergency = false;
|
|
|
|
|
|
|
|
|
|
if (fileInfoE.exist() && fileInfo2.exist()) {
|
|
|
|
|
if (fileInfoE.getModificationTime()
|
|
|
|
|
> fileInfo2.getModificationTime()) {
|
2001-11-26 10:19:58 +00:00
|
|
|
|
if (Alert::askQuestion(_("An emergency save of this document exists!"),
|
1999-11-15 12:01:38 +00:00
|
|
|
|
MakeDisplayPath(s, 50),
|
1999-09-27 18:44:28 +00:00
|
|
|
|
_("Try to load that instead?"))) {
|
|
|
|
|
ts = e;
|
|
|
|
|
// the file is not saved if we load the
|
|
|
|
|
// emergency file.
|
|
|
|
|
b->markDirty();
|
|
|
|
|
use_emergency = true;
|
|
|
|
|
} else {
|
|
|
|
|
// Here, we should delete the emergency save
|
2000-09-26 13:54:57 +00:00
|
|
|
|
lyx::unlink(e);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!use_emergency) {
|
|
|
|
|
// Now check if autosave file is newer.
|
|
|
|
|
a += '#';
|
|
|
|
|
a += OnlyFilename(s);
|
|
|
|
|
a += '#';
|
|
|
|
|
FileInfo fileInfoA(a);
|
|
|
|
|
if (fileInfoA.exist() && fileInfo2.exist()) {
|
|
|
|
|
if (fileInfoA.getModificationTime()
|
|
|
|
|
> fileInfo2.getModificationTime()) {
|
2001-11-26 10:19:58 +00:00
|
|
|
|
if (Alert::askQuestion(_("Autosave file is newer."),
|
1999-11-15 12:01:38 +00:00
|
|
|
|
MakeDisplayPath(s, 50),
|
1999-09-27 18:44:28 +00:00
|
|
|
|
_("Load that one instead?"))) {
|
|
|
|
|
ts = a;
|
|
|
|
|
// the file is not saved if we load the
|
|
|
|
|
// autosave file.
|
|
|
|
|
b->markDirty();
|
|
|
|
|
} else {
|
|
|
|
|
// Here, we should delete the autosave
|
2000-09-26 13:54:57 +00:00
|
|
|
|
lyx::unlink(a);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// not sure if this is the correct place to begin LyXLex
|
1999-10-02 16:21:10 +00:00
|
|
|
|
LyXLex lex(0, 0);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
lex.setFile(ts);
|
|
|
|
|
if (b->readFile(lex))
|
|
|
|
|
return b;
|
|
|
|
|
else {
|
|
|
|
|
bstore.release(b);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-02-03 17:09:33 +00:00
|
|
|
|
bool BufferList::exists(string const & s) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2000-10-11 21:06:43 +00:00
|
|
|
|
return find_if(bstore.begin(), bstore.end(),
|
2001-04-24 15:25:26 +00:00
|
|
|
|
lyx::compare_memfun(&Buffer::fileName, s))
|
|
|
|
|
!= bstore.end();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-02-03 17:09:33 +00:00
|
|
|
|
bool BufferList::isLoaded(Buffer const * b) const
|
|
|
|
|
{
|
2001-04-24 15:25:26 +00:00
|
|
|
|
lyx::Assert(b);
|
|
|
|
|
|
2000-02-03 17:09:33 +00:00
|
|
|
|
BufferStorage::const_iterator cit =
|
|
|
|
|
find(bstore.begin(), bstore.end(), b);
|
|
|
|
|
return cit != bstore.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-11-05 06:02:34 +00:00
|
|
|
|
Buffer * BufferList::getBuffer(string const & s)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2000-10-11 21:06:43 +00:00
|
|
|
|
BufferStorage::iterator it =
|
|
|
|
|
find_if(bstore.begin(), bstore.end(),
|
2001-04-24 15:25:26 +00:00
|
|
|
|
lyx::compare_memfun(&Buffer::fileName, s));
|
2000-10-11 21:06:43 +00:00
|
|
|
|
return it != bstore.end() ? (*it) : 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-09-13 15:07:06 +00:00
|
|
|
|
Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2000-02-04 09:38:32 +00:00
|
|
|
|
// get a free buffer
|
2000-03-12 10:35:05 +00:00
|
|
|
|
Buffer * b = bstore.newBuffer(name);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// use defaults.lyx as a default template if it exists.
|
|
|
|
|
if (tname.empty()) {
|
|
|
|
|
tname = LibFileSearch("templates", "defaults.lyx");
|
|
|
|
|
}
|
2001-06-27 14:10:35 +00:00
|
|
|
|
if (!tname.empty()) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
bool templateok = false;
|
1999-11-15 12:01:38 +00:00
|
|
|
|
LyXLex lex(0, 0);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
lex.setFile(tname);
|
2001-08-06 19:12:46 +00:00
|
|
|
|
if (lex.isOK()) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (b->readFile(lex)) {
|
|
|
|
|
templateok = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!templateok) {
|
2001-11-26 10:19:58 +00:00
|
|
|
|
Alert::alert(_("Error!"), _("Unable to open template"),
|
1999-09-27 18:44:28 +00:00
|
|
|
|
MakeDisplayPath(tname));
|
|
|
|
|
// no template, start with empty buffer
|
2001-06-25 00:06:33 +00:00
|
|
|
|
b->paragraph = new Paragraph;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2000-09-14 17:53:12 +00:00
|
|
|
|
} else { // start with empty buffer
|
2001-06-25 00:06:33 +00:00
|
|
|
|
b->paragraph = new Paragraph;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-11-10 17:29:47 +00:00
|
|
|
|
if (!lyxrc.new_ask_filename && !isNamed) {
|
|
|
|
|
b->setUnnamed();
|
|
|
|
|
b->setFileName(name);
|
2000-08-08 11:08:07 +00:00
|
|
|
|
}
|
2000-11-10 17:29:47 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
b->setReadonly(false);
|
|
|
|
|
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-11-05 06:02:34 +00:00
|
|
|
|
Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-08-01 13:28:45 +00:00
|
|
|
|
// get absolute path of file and add ".lyx" to the filename if
|
|
|
|
|
// necessary
|
|
|
|
|
string s = FileSearch(string(), filename, "lyx");
|
|
|
|
|
if (s.empty()) {
|
|
|
|
|
s = filename;
|
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// file already open?
|
|
|
|
|
if (exists(s)) {
|
2001-11-26 10:19:58 +00:00
|
|
|
|
if (Alert::askQuestion(_("Document is already open:"),
|
1999-11-15 12:01:38 +00:00
|
|
|
|
MakeDisplayPath(s, 50),
|
1999-09-27 18:44:28 +00:00
|
|
|
|
_("Do you want to reload that document?"))) {
|
|
|
|
|
// Reload is accomplished by closing and then loading
|
|
|
|
|
if (!close(getBuffer(s))) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// Fall through to new load. (Asger)
|
|
|
|
|
} else {
|
|
|
|
|
// Here, we pretend that we just loaded the
|
|
|
|
|
// open document
|
|
|
|
|
return getBuffer(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-11-05 06:02:34 +00:00
|
|
|
|
Buffer * b = 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
bool ro = false;
|
|
|
|
|
switch (IsFileWriteable(s)) {
|
|
|
|
|
case 0:
|
|
|
|
|
ro = true;
|
|
|
|
|
// Fall through
|
|
|
|
|
case 1:
|
1999-11-05 06:02:34 +00:00
|
|
|
|
b = readFile(s, ro);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (b) {
|
|
|
|
|
b->lyxvc.file_found_hook(s);
|
|
|
|
|
}
|
|
|
|
|
break; //fine- it's r/w
|
|
|
|
|
case -1:
|
|
|
|
|
// Here we probably should run
|
|
|
|
|
if (LyXVC::file_not_found_hook(s)) {
|
|
|
|
|
// Ask if the file should be checked out for
|
|
|
|
|
// viewing/editing, if so: load it.
|
2001-11-26 10:19:58 +00:00
|
|
|
|
if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
|
2000-01-11 08:23:07 +00:00
|
|
|
|
// How can we know _how_ to do the checkout?
|
|
|
|
|
// With the current VC support it has to be,
|
|
|
|
|
// a RCS file since CVS do not have special ,v files.
|
2002-02-26 10:50:48 +00:00
|
|
|
|
RCS::retrieve(s);
|
2000-01-11 08:23:07 +00:00
|
|
|
|
return loadLyXFile(filename, tolastfiles);
|
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2001-11-26 10:19:58 +00:00
|
|
|
|
if (Alert::askQuestion(_("Cannot open specified file:"),
|
1999-11-15 12:01:38 +00:00
|
|
|
|
MakeDisplayPath(s, 50),
|
1999-09-27 18:44:28 +00:00
|
|
|
|
_("Create new document with this name?")))
|
1999-12-16 06:43:25 +00:00
|
|
|
|
{
|
|
|
|
|
// Find a free buffer
|
2000-09-13 15:07:06 +00:00
|
|
|
|
b = newFile(s, string(), true);
|
1999-12-16 06:43:25 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (b && tolastfiles)
|
1999-12-10 00:07:59 +00:00
|
|
|
|
lastfiles->newFile(b->fileName());
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
return b;
|
|
|
|
|
}
|