1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Word Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * This file is Copyright 1996-2001
12 * ======================================================
18 #pragma implementation
21 #include "bufferlist.h"
23 #include "lastfiles.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
31 #include "frontends/LyXView.h"
32 #include "vc-backend.h"
33 #include "TextCache.h"
36 #include "frontends/Alert.h"
38 #include "support/FileInfo.h"
39 #include "support/filetools.h"
40 #include "support/lyxmanip.h"
41 #include "support/lyxfunctional.h"
42 #include "support/LAssert.h"
44 #include <boost/bind.hpp>
45 #include "BoostFormat.h"
59 extern BufferView * current_view;
62 // Class BufferStorage
65 void BufferStorage::release(Buffer * buf)
68 Container::iterator it = find(container.begin(), container.end(), buf);
69 if (it != container.end()) {
70 // Make sure that we don't store a LyXText in
71 // the textcache that points to the buffer
75 textcache.removeAllWithBuffer(tmp);
81 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
83 Buffer * tmpbuf = new Buffer(s, ronly);
84 tmpbuf->params.useClassDefaults();
85 lyxerr[Debug::INFO] << "Assigning to buffer "
86 << container.size() << endl;
87 container.push_back(tmpbuf);
96 BufferList::BufferList()
97 : state_(BufferList::OK)
101 bool BufferList::empty() const
103 return bstore.empty();
107 bool BufferList::qwriteOne(Buffer * buf, string const & fname,
108 string & unsaved_list)
112 switch (Alert::askConfirmation(_("Changes in document:"),
114 _("Save document?"))) {
116 // FIXME: WriteAs can be asynch !
117 if (buf->isUnnamed())
118 reask = !WriteAs(current_view, buf);
120 reask = !MenuWrite(current_view, buf);
124 // if we crash after this we could
125 // have no autosave file but I guess
126 // this is really inprobable (Jug)
127 if (buf->isUnnamed()) {
128 removeAutosaveFile(buf->fileName());
131 unsaved_list += MakeDisplayPath(fname, 50) + "\n";
141 bool BufferList::qwriteAll()
144 BufferStorage::iterator it = bstore.begin();
145 BufferStorage::iterator end = bstore.end();
146 for (; it != end; ++it) {
147 if (!(*it)->isClean()) {
149 if ((*it)->isUnnamed())
150 fname = OnlyFilename((*it)->fileName());
152 fname = MakeDisplayPath((*it)->fileName(), 50);
153 if (!qwriteOne(*it, fname, unsaved)) // cancel the request!
162 void BufferList::closeAll()
164 state_ = BufferList::CLOSING;
165 // Since we are closing we can just as well delete all
166 // in the textcache this will also speed the closing/quiting up a bit.
169 while (!bstore.empty()) {
170 close(bstore.front());
172 state_ = BufferList::OK;
176 bool BufferList::close(Buffer * buf)
181 // Trace back why we need to use buf->getUser here.
182 // Perhaps slight rewrite is in order? (Lgb)
185 buf->getUser()->insetUnlock();
187 if (!buf->paragraphs.empty() && !buf->isClean() && !quitting) {
189 buf->getUser()->owner()->prohibitInput();
191 if (buf->isUnnamed())
192 fname = OnlyFilename(buf->fileName());
194 fname = MakeDisplayPath(buf->fileName(), 50);
197 switch (Alert::askConfirmation(_("Changes in document:"),
199 _("Save document?"))) {
201 if (buf->isUnnamed())
202 reask = !WriteAs(current_view, buf);
203 else if (buf->save()) {
204 lastfiles->newFile(buf->fileName());
208 buf->getUser()->owner()->allowInput();
213 if (buf->isUnnamed()) {
214 removeAutosaveFile(buf->fileName());
220 buf->getUser()->owner()->allowInput();
225 buf->getUser()->owner()->allowInput();
233 vector<string> const BufferList::getFileNames() const
236 std::copy(bstore.begin(), bstore.end(),
237 lyx::back_inserter_fun(nvec, &Buffer::fileName));
242 Buffer * BufferList::first()
246 return bstore.front();
250 Buffer * BufferList::getBuffer(unsigned int choice)
252 if (choice >= bstore.size())
254 return bstore[choice];
258 int BufferList::unlockInset(UpdatableInset * inset)
262 BufferStorage::iterator it = bstore.begin();
263 BufferStorage::iterator end = bstore.end();
264 for (; it != end; ++it) {
266 && (*it)->getUser()->theLockingInset() == inset) {
267 (*it)->getUser()->insetUnlock();
275 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
277 BufferStorage::iterator it = bstore.begin();
278 BufferStorage::iterator end = bstore.end();
279 for (; it != end; ++it) {
280 if (!(*it)->isDepClean(mastertmpdir)) {
281 string writefile = mastertmpdir;
283 writefile += (*it)->getLatexName();
284 (*it)->makeLaTeXFile(writefile, mastertmpdir,
286 (*it)->markDepClean(mastertmpdir);
292 void BufferList::emergencyWriteAll()
294 for_each(bstore.begin(), bstore.end(),
295 boost::bind(&BufferList::emergencyWrite, this, _1));
299 void BufferList::emergencyWrite(Buffer * buf)
301 // assert(buf) // this is not good since C assert takes an int
302 // and a pointer is a long (JMarc)
303 assert(buf != 0); // use c assert to avoid a loop
306 // No need to save if the buffer has not changed.
310 string const doc = buf->isUnnamed()
311 ? OnlyFilename(buf->fileName()) : buf->fileName();
314 lyxerr << boost::format(_("LyX: Attempting to save document %1$s"))
318 lyxerr << _("LyX: Attempting to save document ") << doc << endl;
320 // We try to save three places:
322 // 1) Same place as document. Unless it is an unnamed doc.
323 if (!buf->isUnnamed()) {
324 string s = buf->fileName();
326 lyxerr << " " << s << endl;
327 if (buf->writeFile(s)) {
329 lyxerr << _(" Save seems successful. Phew.") << endl;
332 lyxerr << _(" Save failed! Trying...") << endl;
336 // 2) In HOME directory.
337 string s = AddName(GetEnvPath("HOME"), buf->fileName());
339 lyxerr << ' ' << s << endl;
340 if (buf->writeFile(s)) {
342 lyxerr << _(" Save seems successful. Phew.") << endl;
346 lyxerr << _(" Save failed! Trying...") << endl;
348 // 3) In "/tmp" directory.
349 // MakeAbsPath to prepend the current
350 // drive letter on OS/2
351 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
353 lyxerr << ' ' << s << endl;
354 if (buf->writeFile(s)) {
356 lyxerr << _(" Save seems successful. Phew.") << endl;
359 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
364 Buffer * BufferList::readFile(string const & s, bool ronly)
367 string e = OnlyPath(s);
369 // File information about normal file
370 FileInfo fileInfo2(s);
372 if (!fileInfo2.exist()) {
373 Alert::alert(_("Error!"), _("Cannot open file"),
378 Buffer * b = bstore.newBuffer(s, ronly);
380 // Check if emergency save file exists and is newer.
381 e += OnlyFilename(s) + ".emergency";
382 FileInfo fileInfoE(e);
384 bool use_emergency = false;
386 if (fileInfoE.exist() && fileInfo2.exist()) {
387 if (fileInfoE.getModificationTime()
388 > fileInfo2.getModificationTime()) {
389 if (Alert::askQuestion(_("An emergency save of this document exists!"),
390 MakeDisplayPath(s, 50),
391 _("Try to load that instead?"))) {
393 // the file is not saved if we load the
396 use_emergency = true;
398 // Here, we should delete the emergency save
404 if (!use_emergency) {
405 // Now check if autosave file is newer.
407 a += OnlyFilename(s);
409 FileInfo fileInfoA(a);
410 if (fileInfoA.exist() && fileInfo2.exist()) {
411 if (fileInfoA.getModificationTime()
412 > fileInfo2.getModificationTime()) {
413 if (Alert::askQuestion(_("Autosave file is newer."),
414 MakeDisplayPath(s, 50),
415 _("Load that one instead?"))) {
417 // the file is not saved if we load the
421 // Here, we should delete the autosave
427 // not sure if this is the correct place to begin LyXLex
430 if (b->readFile(lex, ts))
439 bool BufferList::exists(string const & s) const
441 return find_if(bstore.begin(), bstore.end(),
442 lyx::compare_memfun(&Buffer::fileName, s))
447 bool BufferList::isLoaded(Buffer const * b) const
451 BufferStorage::const_iterator cit =
452 find(bstore.begin(), bstore.end(), b);
453 return cit != bstore.end();
457 Buffer * BufferList::getBuffer(string const & s)
459 BufferStorage::iterator it =
460 find_if(bstore.begin(), bstore.end(),
461 lyx::compare_memfun(&Buffer::fileName, s));
462 return it != bstore.end() ? (*it) : 0;
466 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
469 Buffer * b = bstore.newBuffer(name);
471 // use defaults.lyx as a default template if it exists.
473 tname = LibFileSearch("templates", "defaults.lyx");
475 if (!tname.empty()) {
476 bool templateok = false;
480 if (b->readFile(lex, tname)) {
485 Alert::alert(_("Error!"), _("Unable to open template"),
486 MakeDisplayPath(tname));
487 // no template, start with empty buffer
488 b->paragraphs.set(new Paragraph);
489 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
491 } else { // start with empty buffer
492 b->paragraphs.set(new Paragraph);
493 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
498 b->setFileName(name);
501 b->setReadonly(false);
507 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
509 // get absolute path of file and add ".lyx" to the filename if
511 string s = FileSearch(string(), filename, "lyx");
516 // file already open?
518 if (Alert::askQuestion(_("Document is already open:"),
519 MakeDisplayPath(s, 50),
520 _("Do you want to reload that document?"))) {
521 // Reload is accomplished by closing and then loading
522 if (!close(getBuffer(s))) {
525 // Fall through to new load. (Asger)
527 // Here, we pretend that we just loaded the
534 switch (IsFileWriteable(s)) {
541 b->lyxvc.file_found_hook(s);
543 break; //fine- it's r/w
545 // Here we probably should run
546 if (LyXVC::file_not_found_hook(s)) {
547 // Ask if the file should be checked out for
548 // viewing/editing, if so: load it.
549 if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
550 // How can we know _how_ to do the checkout?
551 // With the current VC support it has to be,
552 // a RCS file since CVS do not have special ,v files.
554 return loadLyXFile(filename, tolastfiles);
557 if (Alert::askQuestion(_("Cannot open specified file:"),
558 MakeDisplayPath(s, 50),
559 _("Create new document with this name?")))
561 // Find a free buffer
562 b = newFile(s, string(), true);
567 if (b && tolastfiles)
568 lastfiles->newFile(b->fileName());