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>
58 extern BufferView * current_view;
61 // Class BufferStorage
64 void BufferStorage::release(Buffer * buf)
67 Container::iterator it = find(container.begin(), container.end(), buf);
68 if (it != container.end()) {
69 // Make sure that we don't store a LyXText in
70 // the textcache that points to the buffer
74 textcache.removeAllWithBuffer(tmp);
80 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
82 Buffer * tmpbuf = new Buffer(s, ronly);
83 tmpbuf->params.useClassDefaults();
84 lyxerr[Debug::INFO] << "Assigning to buffer "
85 << container.size() << endl;
86 container.push_back(tmpbuf);
95 BufferList::BufferList()
96 : state_(BufferList::OK)
100 bool BufferList::empty() const
102 return bstore.empty();
106 bool BufferList::qwriteOne(Buffer * buf, string const & fname,
107 string & unsaved_list)
111 switch (Alert::askConfirmation(_("Changes in document:"),
113 _("Save document?"))) {
115 // FIXME: WriteAs can be asynch !
116 if (buf->isUnnamed())
117 reask = !WriteAs(current_view, buf);
119 reask = !MenuWrite(current_view, buf);
123 // if we crash after this we could
124 // have no autosave file but I guess
125 // this is really inprobable (Jug)
126 if (buf->isUnnamed()) {
127 removeAutosaveFile(buf->fileName());
130 unsaved_list += MakeDisplayPath(fname, 50) + "\n";
140 bool BufferList::qwriteAll()
143 BufferStorage::iterator it = bstore.begin();
144 BufferStorage::iterator end = bstore.end();
145 for (; it != end; ++it) {
146 if (!(*it)->isClean()) {
148 if ((*it)->isUnnamed())
149 fname = OnlyFilename((*it)->fileName());
151 fname = MakeDisplayPath((*it)->fileName(), 50);
152 if (!qwriteOne(*it, fname, unsaved)) // cancel the request!
161 void BufferList::closeAll()
163 state_ = BufferList::CLOSING;
164 // Since we are closing we can just as well delete all
165 // in the textcache this will also speed the closing/quiting up a bit.
168 while (!bstore.empty()) {
169 close(bstore.front());
171 state_ = BufferList::OK;
175 bool BufferList::close(Buffer * buf)
180 // Trace back why we need to use buf->getUser here.
181 // Perhaps slight rewrite is in order? (Lgb)
184 buf->getUser()->insetUnlock();
186 if (!buf->paragraphs.empty() && !buf->isClean() && !quitting) {
188 buf->getUser()->owner()->prohibitInput();
190 if (buf->isUnnamed())
191 fname = OnlyFilename(buf->fileName());
193 fname = MakeDisplayPath(buf->fileName(), 50);
196 switch (Alert::askConfirmation(_("Changes in document:"),
198 _("Save document?"))) {
200 if (buf->isUnnamed())
201 reask = !WriteAs(current_view, buf);
202 else if (buf->save()) {
203 lastfiles->newFile(buf->fileName());
207 buf->getUser()->owner()->allowInput();
212 if (buf->isUnnamed()) {
213 removeAutosaveFile(buf->fileName());
219 buf->getUser()->owner()->allowInput();
224 buf->getUser()->owner()->allowInput();
232 vector<string> const BufferList::getFileNames() const
235 std::copy(bstore.begin(), bstore.end(),
236 lyx::back_inserter_fun(nvec, &Buffer::fileName));
241 Buffer * BufferList::first()
245 return bstore.front();
249 Buffer * BufferList::getBuffer(unsigned int choice)
251 if (choice >= bstore.size())
253 return bstore[choice];
257 int BufferList::unlockInset(UpdatableInset * inset)
261 BufferStorage::iterator it = bstore.begin();
262 BufferStorage::iterator end = bstore.end();
263 for (; it != end; ++it) {
265 && (*it)->getUser()->theLockingInset() == inset) {
266 (*it)->getUser()->insetUnlock();
274 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
276 BufferStorage::iterator it = bstore.begin();
277 BufferStorage::iterator end = bstore.end();
278 for (; it != end; ++it) {
279 if (!(*it)->isDepClean(mastertmpdir)) {
280 string writefile = mastertmpdir;
282 writefile += (*it)->getLatexName();
283 (*it)->makeLaTeXFile(writefile, mastertmpdir,
285 (*it)->markDepClean(mastertmpdir);
291 void BufferList::emergencyWriteAll()
293 for_each(bstore.begin(), bstore.end(),
294 boost::bind(&BufferList::emergencyWrite, this, _1));
298 void BufferList::emergencyWrite(Buffer * buf)
300 // assert(buf) // this is not good since C assert takes an int
301 // and a pointer is a long (JMarc)
302 assert(buf != 0); // use c assert to avoid a loop
305 // No need to save if the buffer has not changed.
309 lyxerr << _("lyx: Attempting to save document ")
310 << (buf->isUnnamed() ? OnlyFilename(buf->fileName())
312 << _(" as...") << endl;
314 // We try to save three places:
316 // 1) Same place as document. Unless it is an unnamed doc.
317 if (!buf->isUnnamed()) {
318 string s = buf->fileName();
320 lyxerr << " " << s << endl;
321 if (buf->writeFile(s)) {
323 lyxerr << _(" Save seems successful. Phew.") << endl;
326 lyxerr << _(" Save failed! Trying...") << endl;
330 // 2) In HOME directory.
331 string s = AddName(GetEnvPath("HOME"), buf->fileName());
333 lyxerr << " " << s << endl;
334 if (buf->writeFile(s)) {
336 lyxerr << _(" Save seems successful. Phew.") << endl;
340 lyxerr << _(" Save failed! Trying...") << endl;
342 // 3) In "/tmp" directory.
343 // MakeAbsPath to prepend the current
344 // drive letter on OS/2
345 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
347 lyxerr << " " << s << endl;
348 if (buf->writeFile(s)) {
350 lyxerr << _(" Save seems successful. Phew.") << endl;
353 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
358 Buffer * BufferList::readFile(string const & s, bool ronly)
361 string e = OnlyPath(s);
363 // File information about normal file
364 FileInfo fileInfo2(s);
366 if (!fileInfo2.exist()) {
367 Alert::alert(_("Error!"), _("Cannot open file"),
372 Buffer * b = bstore.newBuffer(s, ronly);
374 // Check if emergency save file exists and is newer.
375 e += OnlyFilename(s) + ".emergency";
376 FileInfo fileInfoE(e);
378 bool use_emergency = false;
380 if (fileInfoE.exist() && fileInfo2.exist()) {
381 if (fileInfoE.getModificationTime()
382 > fileInfo2.getModificationTime()) {
383 if (Alert::askQuestion(_("An emergency save of this document exists!"),
384 MakeDisplayPath(s, 50),
385 _("Try to load that instead?"))) {
387 // the file is not saved if we load the
390 use_emergency = true;
392 // Here, we should delete the emergency save
398 if (!use_emergency) {
399 // Now check if autosave file is newer.
401 a += OnlyFilename(s);
403 FileInfo fileInfoA(a);
404 if (fileInfoA.exist() && fileInfo2.exist()) {
405 if (fileInfoA.getModificationTime()
406 > fileInfo2.getModificationTime()) {
407 if (Alert::askQuestion(_("Autosave file is newer."),
408 MakeDisplayPath(s, 50),
409 _("Load that one instead?"))) {
411 // the file is not saved if we load the
415 // Here, we should delete the autosave
421 // not sure if this is the correct place to begin LyXLex
424 if (b->readFile(lex))
433 bool BufferList::exists(string const & s) const
435 return find_if(bstore.begin(), bstore.end(),
436 lyx::compare_memfun(&Buffer::fileName, s))
441 bool BufferList::isLoaded(Buffer const * b) const
445 BufferStorage::const_iterator cit =
446 find(bstore.begin(), bstore.end(), b);
447 return cit != bstore.end();
451 Buffer * BufferList::getBuffer(string const & s)
453 BufferStorage::iterator it =
454 find_if(bstore.begin(), bstore.end(),
455 lyx::compare_memfun(&Buffer::fileName, s));
456 return it != bstore.end() ? (*it) : 0;
460 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
463 Buffer * b = bstore.newBuffer(name);
465 // use defaults.lyx as a default template if it exists.
467 tname = LibFileSearch("templates", "defaults.lyx");
469 if (!tname.empty()) {
470 bool templateok = false;
474 if (b->readFile(lex)) {
479 Alert::alert(_("Error!"), _("Unable to open template"),
480 MakeDisplayPath(tname));
481 // no template, start with empty buffer
482 b->paragraphs.set(new Paragraph);
483 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
485 } else { // start with empty buffer
486 b->paragraphs.set(new Paragraph);
487 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
492 b->setFileName(name);
495 b->setReadonly(false);
501 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
503 // get absolute path of file and add ".lyx" to the filename if
505 string s = FileSearch(string(), filename, "lyx");
510 // file already open?
512 if (Alert::askQuestion(_("Document is already open:"),
513 MakeDisplayPath(s, 50),
514 _("Do you want to reload that document?"))) {
515 // Reload is accomplished by closing and then loading
516 if (!close(getBuffer(s))) {
519 // Fall through to new load. (Asger)
521 // Here, we pretend that we just loaded the
528 switch (IsFileWriteable(s)) {
535 b->lyxvc.file_found_hook(s);
537 break; //fine- it's r/w
539 // Here we probably should run
540 if (LyXVC::file_not_found_hook(s)) {
541 // Ask if the file should be checked out for
542 // viewing/editing, if so: load it.
543 if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
544 // How can we know _how_ to do the checkout?
545 // With the current VC support it has to be,
546 // a RCS file since CVS do not have special ,v files.
548 return loadLyXFile(filename, tolastfiles);
551 if (Alert::askQuestion(_("Cannot open specified file:"),
552 MakeDisplayPath(s, 50),
553 _("Create new document with this name?")))
555 // Find a free buffer
556 b = newFile(s, string(), true);
561 if (b && tolastfiles)
562 lastfiles->newFile(b->fileName());