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 * ======================================================
17 #include "bufferlist.h"
19 #include "lastfiles.h"
24 #include "bufferview_funcs.h"
25 #include "BufferView.h"
27 #include "frontends/LyXView.h"
28 #include "vc-backend.h"
29 #include "TextCache.h"
32 #include "frontends/Alert.h"
34 #include "support/FileInfo.h"
35 #include "support/filetools.h"
36 #include "support/lyxmanip.h"
37 #include "support/lyxfunctional.h"
38 #include "support/LAssert.h"
40 #include <boost/bind.hpp>
41 #include "BoostFormat.h"
55 extern BufferView * current_view;
58 // Class BufferStorage
61 void BufferStorage::release(Buffer * buf)
64 Container::iterator it = find(container.begin(), container.end(), buf);
65 if (it != container.end()) {
66 // Make sure that we don't store a LyXText in
67 // the textcache that points to the buffer
71 textcache.removeAllWithBuffer(tmp);
77 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
79 Buffer * tmpbuf = new Buffer(s, ronly);
80 tmpbuf->params.useClassDefaults();
81 lyxerr[Debug::INFO] << "Assigning to buffer "
82 << container.size() << endl;
83 container.push_back(tmpbuf);
92 BufferList::BufferList()
93 : state_(BufferList::OK)
97 bool BufferList::empty() const
99 return bstore.empty();
103 bool BufferList::qwriteOne(Buffer * buf, string const & fname,
104 string & unsaved_list)
108 switch (Alert::askConfirmation(_("Changes in document:"),
110 _("Save document?"))) {
112 // FIXME: WriteAs can be asynch !
113 if (buf->isUnnamed())
114 reask = !WriteAs(current_view, buf);
116 reask = !MenuWrite(current_view, buf);
120 // if we crash after this we could
121 // have no autosave file but I guess
122 // this is really inprobable (Jug)
123 if (buf->isUnnamed()) {
124 removeAutosaveFile(buf->fileName());
127 unsaved_list += MakeDisplayPath(fname, 50) + "\n";
137 bool BufferList::qwriteAll()
140 BufferStorage::iterator it = bstore.begin();
141 BufferStorage::iterator end = bstore.end();
142 for (; it != end; ++it) {
143 if (!(*it)->isClean()) {
145 if ((*it)->isUnnamed())
146 fname = OnlyFilename((*it)->fileName());
148 fname = MakeDisplayPath((*it)->fileName(), 50);
149 if (!qwriteOne(*it, fname, unsaved)) // cancel the request!
158 void BufferList::closeAll()
160 state_ = BufferList::CLOSING;
161 // Since we are closing we can just as well delete all
162 // in the textcache this will also speed the closing/quiting up a bit.
165 while (!bstore.empty()) {
166 close(bstore.front());
168 state_ = BufferList::OK;
172 bool BufferList::close(Buffer * buf)
177 // Trace back why we need to use buf->getUser here.
178 // Perhaps slight rewrite is in order? (Lgb)
181 buf->getUser()->insetUnlock();
183 if (!buf->paragraphs.empty() && !buf->isClean() && !quitting) {
185 buf->getUser()->owner()->prohibitInput();
187 if (buf->isUnnamed())
188 fname = OnlyFilename(buf->fileName());
190 fname = MakeDisplayPath(buf->fileName(), 50);
193 switch (Alert::askConfirmation(_("Changes in document:"),
195 _("Save document?"))) {
197 if (buf->isUnnamed())
198 reask = !WriteAs(current_view, buf);
199 else if (buf->save()) {
200 lastfiles->newFile(buf->fileName());
204 buf->getUser()->owner()->allowInput();
209 if (buf->isUnnamed()) {
210 removeAutosaveFile(buf->fileName());
216 buf->getUser()->owner()->allowInput();
221 buf->getUser()->owner()->allowInput();
229 vector<string> const BufferList::getFileNames() const
232 std::copy(bstore.begin(), bstore.end(),
233 lyx::back_inserter_fun(nvec, &Buffer::fileName));
238 Buffer * BufferList::first()
242 return bstore.front();
246 Buffer * BufferList::getBuffer(unsigned int choice)
248 if (choice >= bstore.size())
250 return bstore[choice];
254 int BufferList::unlockInset(UpdatableInset * inset)
258 BufferStorage::iterator it = bstore.begin();
259 BufferStorage::iterator end = bstore.end();
260 for (; it != end; ++it) {
262 && (*it)->getUser()->theLockingInset() == inset) {
263 (*it)->getUser()->insetUnlock();
271 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
273 BufferStorage::iterator it = bstore.begin();
274 BufferStorage::iterator end = bstore.end();
275 for (; it != end; ++it) {
276 if (!(*it)->isDepClean(mastertmpdir)) {
277 string writefile = mastertmpdir;
279 writefile += (*it)->getLatexName();
280 (*it)->makeLaTeXFile(writefile, mastertmpdir,
282 (*it)->markDepClean(mastertmpdir);
288 void BufferList::emergencyWriteAll()
290 for_each(bstore.begin(), bstore.end(),
291 boost::bind(&BufferList::emergencyWrite, this, _1));
295 void BufferList::emergencyWrite(Buffer * buf)
297 // assert(buf) // this is not good since C assert takes an int
298 // and a pointer is a long (JMarc)
299 assert(buf != 0); // use c assert to avoid a loop
302 // No need to save if the buffer has not changed.
306 string const doc = buf->isUnnamed()
307 ? OnlyFilename(buf->fileName()) : buf->fileName();
310 lyxerr << boost::format(_("LyX: Attempting to save document %1$s"))
314 lyxerr << _("LyX: Attempting to save document ") << doc << endl;
316 // We try to save three places:
318 // 1) Same place as document. Unless it is an unnamed doc.
319 if (!buf->isUnnamed()) {
320 string s = buf->fileName();
322 lyxerr << " " << s << endl;
323 if (buf->writeFile(s)) {
325 lyxerr << _(" Save seems successful. Phew.") << endl;
328 lyxerr << _(" Save failed! Trying...") << endl;
332 // 2) In HOME directory.
333 string s = AddName(GetEnvPath("HOME"), buf->fileName());
335 lyxerr << ' ' << s << endl;
336 if (buf->writeFile(s)) {
338 lyxerr << _(" Save seems successful. Phew.") << endl;
342 lyxerr << _(" Save failed! Trying...") << endl;
344 // 3) In "/tmp" directory.
345 // MakeAbsPath to prepend the current
346 // drive letter on OS/2
347 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
349 lyxerr << ' ' << s << endl;
350 if (buf->writeFile(s)) {
352 lyxerr << _(" Save seems successful. Phew.") << endl;
355 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
360 Buffer * BufferList::readFile(string const & s, bool ronly)
363 string e = OnlyPath(s);
365 // File information about normal file
366 FileInfo fileInfo2(s);
368 if (!fileInfo2.exist()) {
369 Alert::alert(_("Error!"), _("Cannot open file"),
374 Buffer * b = bstore.newBuffer(s, ronly);
376 // Check if emergency save file exists and is newer.
377 e += OnlyFilename(s) + ".emergency";
378 FileInfo fileInfoE(e);
380 bool use_emergency = false;
382 if (fileInfoE.exist() && fileInfo2.exist()) {
383 if (fileInfoE.getModificationTime()
384 > fileInfo2.getModificationTime()) {
385 if (Alert::askQuestion(_("An emergency save of this document exists!"),
386 MakeDisplayPath(s, 50),
387 _("Try to load that instead?"))) {
389 // the file is not saved if we load the
392 use_emergency = true;
394 // Here, we should delete the emergency save
400 if (!use_emergency) {
401 // Now check if autosave file is newer.
403 a += OnlyFilename(s);
405 FileInfo fileInfoA(a);
406 if (fileInfoA.exist() && fileInfo2.exist()) {
407 if (fileInfoA.getModificationTime()
408 > fileInfo2.getModificationTime()) {
409 if (Alert::askQuestion(_("Autosave file is newer."),
410 MakeDisplayPath(s, 50),
411 _("Load that one instead?"))) {
413 // the file is not saved if we load the
417 // Here, we should delete the autosave
423 // not sure if this is the correct place to begin LyXLex
426 if (b->readFile(lex, ts))
435 bool BufferList::exists(string const & s) const
437 return find_if(bstore.begin(), bstore.end(),
438 lyx::compare_memfun(&Buffer::fileName, s))
443 bool BufferList::isLoaded(Buffer const * b) const
447 BufferStorage::const_iterator cit =
448 find(bstore.begin(), bstore.end(), b);
449 return cit != bstore.end();
453 Buffer * BufferList::getBuffer(string const & s)
455 BufferStorage::iterator it =
456 find_if(bstore.begin(), bstore.end(),
457 lyx::compare_memfun(&Buffer::fileName, s));
458 return it != bstore.end() ? (*it) : 0;
462 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
465 Buffer * b = bstore.newBuffer(name);
467 // use defaults.lyx as a default template if it exists.
469 tname = LibFileSearch("templates", "defaults.lyx");
471 if (!tname.empty()) {
472 bool templateok = false;
476 if (b->readFile(lex, tname)) {
481 Alert::alert(_("Error!"), _("Unable to open template"),
482 MakeDisplayPath(tname));
483 // no template, start with empty buffer
484 b->paragraphs.set(new Paragraph);
485 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
487 } else { // start with empty buffer
488 b->paragraphs.set(new Paragraph);
489 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
494 b->setFileName(name);
497 b->setReadonly(false);
503 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
505 // get absolute path of file and add ".lyx" to the filename if
507 string s = FileSearch(string(), filename, "lyx");
512 // file already open?
514 if (Alert::askQuestion(_("Document is already open:"),
515 MakeDisplayPath(s, 50),
516 _("Do you want to reload that document?"))) {
517 // Reload is accomplished by closing and then loading
518 if (!close(getBuffer(s))) {
521 // Fall through to new load. (Asger)
523 // Here, we pretend that we just loaded the
530 switch (IsFileWriteable(s)) {
537 b->lyxvc.file_found_hook(s);
539 break; //fine- it's r/w
541 // Here we probably should run
542 if (LyXVC::file_not_found_hook(s)) {
543 // Ask if the file should be checked out for
544 // viewing/editing, if so: load it.
545 if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
546 // How can we know _how_ to do the checkout?
547 // With the current VC support it has to be,
548 // a RCS file since CVS do not have special ,v files.
550 return loadLyXFile(filename, tolastfiles);
553 if (Alert::askQuestion(_("Cannot open specified file:"),
554 MakeDisplayPath(s, 50),
555 _("Create new document with this name?")))
557 // Find a free buffer
558 b = newFile(s, string(), true);
563 if (b && tolastfiles)
564 lastfiles->newFile(b->fileName());
570 void BufferList::setCurrentAuthor(string const & name, string const & email)
572 BufferStorage::iterator it = bstore.begin();
573 BufferStorage::iterator end = bstore.end();
574 for (; it != end; ++it) {
575 (*it)->authors().record(0, Author(name, email));