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();
313 lyxerr << boost::format(_("LyX: Attempting to save document %1$s"))
317 // We try to save three places:
319 // 1) Same place as document. Unless it is an unnamed doc.
320 if (!buf->isUnnamed()) {
321 string s = buf->fileName();
323 lyxerr << " " << s << endl;
324 if (buf->writeFile(s)) {
326 lyxerr << _(" Save seems successful. Phew.") << endl;
329 lyxerr << _(" Save failed! Trying...") << endl;
333 // 2) In HOME directory.
334 string s = AddName(GetEnvPath("HOME"), buf->fileName());
336 lyxerr << " " << s << endl;
337 if (buf->writeFile(s)) {
339 lyxerr << _(" Save seems successful. Phew.") << endl;
343 lyxerr << _(" Save failed! Trying...") << endl;
345 // 3) In "/tmp" directory.
346 // MakeAbsPath to prepend the current
347 // drive letter on OS/2
348 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
350 lyxerr << " " << s << endl;
351 if (buf->writeFile(s)) {
353 lyxerr << _(" Save seems successful. Phew.") << endl;
356 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
361 Buffer * BufferList::readFile(string const & s, bool ronly)
364 string e = OnlyPath(s);
366 // File information about normal file
367 FileInfo fileInfo2(s);
369 if (!fileInfo2.exist()) {
370 Alert::alert(_("Error!"), _("Cannot open file"),
375 Buffer * b = bstore.newBuffer(s, ronly);
377 // Check if emergency save file exists and is newer.
378 e += OnlyFilename(s) + ".emergency";
379 FileInfo fileInfoE(e);
381 bool use_emergency = false;
383 if (fileInfoE.exist() && fileInfo2.exist()) {
384 if (fileInfoE.getModificationTime()
385 > fileInfo2.getModificationTime()) {
386 if (Alert::askQuestion(_("An emergency save of this document exists!"),
387 MakeDisplayPath(s, 50),
388 _("Try to load that instead?"))) {
390 // the file is not saved if we load the
393 use_emergency = true;
395 // Here, we should delete the emergency save
401 if (!use_emergency) {
402 // Now check if autosave file is newer.
404 a += OnlyFilename(s);
406 FileInfo fileInfoA(a);
407 if (fileInfoA.exist() && fileInfo2.exist()) {
408 if (fileInfoA.getModificationTime()
409 > fileInfo2.getModificationTime()) {
410 if (Alert::askQuestion(_("Autosave file is newer."),
411 MakeDisplayPath(s, 50),
412 _("Load that one instead?"))) {
414 // the file is not saved if we load the
418 // Here, we should delete the autosave
424 // not sure if this is the correct place to begin LyXLex
427 if (b->readFile(lex, ts))
436 bool BufferList::exists(string const & s) const
438 return find_if(bstore.begin(), bstore.end(),
439 lyx::compare_memfun(&Buffer::fileName, s))
444 bool BufferList::isLoaded(Buffer const * b) const
448 BufferStorage::const_iterator cit =
449 find(bstore.begin(), bstore.end(), b);
450 return cit != bstore.end();
454 Buffer * BufferList::getBuffer(string const & s)
456 BufferStorage::iterator it =
457 find_if(bstore.begin(), bstore.end(),
458 lyx::compare_memfun(&Buffer::fileName, s));
459 return it != bstore.end() ? (*it) : 0;
463 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
466 Buffer * b = bstore.newBuffer(name);
468 // use defaults.lyx as a default template if it exists.
470 tname = LibFileSearch("templates", "defaults.lyx");
472 if (!tname.empty()) {
473 bool templateok = false;
477 if (b->readFile(lex, tname)) {
482 Alert::alert(_("Error!"), _("Unable to open template"),
483 MakeDisplayPath(tname));
484 // no template, start with empty buffer
485 b->paragraphs.set(new Paragraph);
486 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
488 } else { // start with empty buffer
489 b->paragraphs.set(new Paragraph);
490 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
495 b->setFileName(name);
498 b->setReadonly(false);
504 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
506 // get absolute path of file and add ".lyx" to the filename if
508 string s = FileSearch(string(), filename, "lyx");
513 // file already open?
515 if (Alert::askQuestion(_("Document is already open:"),
516 MakeDisplayPath(s, 50),
517 _("Do you want to reload that document?"))) {
518 // Reload is accomplished by closing and then loading
519 if (!close(getBuffer(s))) {
522 // Fall through to new load. (Asger)
524 // Here, we pretend that we just loaded the
531 switch (IsFileWriteable(s)) {
538 b->lyxvc.file_found_hook(s);
540 break; //fine- it's r/w
542 // Here we probably should run
543 if (LyXVC::file_not_found_hook(s)) {
544 // Ask if the file should be checked out for
545 // viewing/editing, if so: load it.
546 if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
547 // How can we know _how_ to do the checkout?
548 // With the current VC support it has to be,
549 // a RCS file since CVS do not have special ,v files.
551 return loadLyXFile(filename, tolastfiles);
554 if (Alert::askQuestion(_("Cannot open specified file:"),
555 MakeDisplayPath(s, 50),
556 _("Create new document with this name?")))
558 // Find a free buffer
559 b = newFile(s, string(), true);
564 if (b && tolastfiles)
565 lastfiles->newFile(b->fileName());