3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS
13 #include "bufferlist.h"
15 #include "lastfiles.h"
20 #include "bufferview_funcs.h"
21 #include "BufferView.h"
23 #include "frontends/LyXView.h"
24 #include "vc-backend.h"
25 #include "TextCache.h"
28 #include "frontends/Alert.h"
30 #include "support/FileInfo.h"
31 #include "support/filetools.h"
32 #include "support/lyxmanip.h"
33 #include "support/lyxfunctional.h"
34 #include "support/LAssert.h"
36 #include <boost/bind.hpp>
37 #include "support/BoostFormat.h"
51 extern BufferView * current_view;
54 BufferList::BufferList()
58 bool BufferList::empty() const
60 return bstore.empty();
64 bool BufferList::quitWriteBuffer(Buffer * buf)
68 file = OnlyFilename(buf->fileName());
70 file = MakeDisplayPath(buf->fileName(), 30);
73 boost::format fmt(_("The document %1$s has unsaved changes.\n\nDo you want to save the document?"));
75 string text = fmt.str();
77 string text = _("The document ");
78 text += file + _(" has unsaved changes.\n\nWhat do you want to do with it?");
80 int const ret = Alert::prompt(_("Save changed document?"),
81 text, 0, _("&Save"), _("&Discard"), _("&Cancel"));
84 // FIXME: WriteAs can be asynch !
85 // but not right now...maybe we should remove that
90 succeeded = WriteAs(current_view, buf);
92 succeeded = MenuWrite(current_view, buf);
96 } else if (ret == 1) {
97 // if we crash after this we could
98 // have no autosave file but I guess
99 // this is really inprobable (Jug)
100 if (buf->isUnnamed())
101 removeAutosaveFile(buf->fileName());
111 bool BufferList::quitWriteAll()
113 BufferStorage::iterator it = bstore.begin();
114 BufferStorage::iterator end = bstore.end();
115 for (; it != end; ++it) {
116 if ((*it)->isClean())
119 if (!quitWriteBuffer(*it))
127 void BufferList::release(Buffer * buf)
130 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
131 if (it != bstore.end()) {
132 // Make sure that we don't store a LyXText in
133 // the textcache that points to the buffer
135 Buffer * tmp = (*it);
137 textcache.removeAllWithBuffer(tmp);
143 Buffer * BufferList::newBuffer(string const & s, bool ronly)
145 Buffer * tmpbuf = new Buffer(s, ronly);
146 tmpbuf->params.useClassDefaults();
147 lyxerr[Debug::INFO] << "Assigning to buffer "
148 << bstore.size() << endl;
149 bstore.push_back(tmpbuf);
154 void BufferList::closeAll()
156 // Since we are closing we can just as well delete all
157 // in the textcache this will also speed the closing/quiting up a bit.
160 while (!bstore.empty()) {
161 close(bstore.front(), false);
166 bool BufferList::close(Buffer * buf, bool ask)
170 // FIXME: is the quitting check still necessary ?
171 if (!ask || buf->isClean() || quitting || buf->paragraphs.empty()) {
177 if (buf->isUnnamed())
178 fname = OnlyFilename(buf->fileName());
180 fname = MakeDisplayPath(buf->fileName(), 30);
182 boost::format fmt(_("The document %1$s has unsaved changes.\n\nDo you want to save the document?"));
184 string text = fmt.str();
186 string text = _("The document ");
187 text += fname + _(" has unsaved changes.\n\nWhat do you want to do with it?");
189 int const ret = Alert::prompt(_("Save changed document?"),
190 text, 0, _("&Save"), _("&Discard"), _("&Cancel"));
193 if (buf->isUnnamed()) {
194 if (!WriteAs(current_view, buf))
196 } else if (buf->save()) {
197 lastfiles->newFile(buf->fileName());
201 } else if (ret == 2) {
205 if (buf->isUnnamed()) {
206 removeAutosaveFile(buf->fileName());
214 vector<string> const BufferList::getFileNames() const
217 std::copy(bstore.begin(), bstore.end(),
218 lyx::back_inserter_fun(nvec, &Buffer::fileName));
223 Buffer * BufferList::first()
227 return bstore.front();
231 Buffer * BufferList::getBuffer(unsigned int choice)
233 if (choice >= bstore.size())
235 return bstore[choice];
239 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
241 BufferStorage::iterator it = bstore.begin();
242 BufferStorage::iterator end = bstore.end();
243 for (; it != end; ++it) {
244 if (!(*it)->isDepClean(mastertmpdir)) {
245 string writefile = mastertmpdir;
247 writefile += (*it)->getLatexName();
248 (*it)->makeLaTeXFile(writefile, mastertmpdir,
250 (*it)->markDepClean(mastertmpdir);
256 void BufferList::emergencyWriteAll()
258 for_each(bstore.begin(), bstore.end(),
259 boost::bind(&BufferList::emergencyWrite, this, _1));
263 void BufferList::emergencyWrite(Buffer * buf)
265 // assert(buf) // this is not good since C assert takes an int
266 // and a pointer is a long (JMarc)
267 assert(buf != 0); // use c assert to avoid a loop
270 // No need to save if the buffer has not changed.
274 string const doc = buf->isUnnamed()
275 ? OnlyFilename(buf->fileName()) : buf->fileName();
278 lyxerr << boost::format(_("LyX: Attempting to save document %1$s"))
282 lyxerr << _("LyX: Attempting to save document ") << doc << endl;
284 // We try to save three places:
286 // 1) Same place as document. Unless it is an unnamed doc.
287 if (!buf->isUnnamed()) {
288 string s = buf->fileName();
290 lyxerr << " " << s << endl;
291 if (buf->writeFile(s)) {
293 lyxerr << _(" Save seems successful. Phew.") << endl;
296 lyxerr << _(" Save failed! Trying...") << endl;
300 // 2) In HOME directory.
301 string s = AddName(GetEnvPath("HOME"), buf->fileName());
303 lyxerr << ' ' << s << endl;
304 if (buf->writeFile(s)) {
306 lyxerr << _(" Save seems successful. Phew.") << endl;
310 lyxerr << _(" Save failed! Trying...") << endl;
312 // 3) In "/tmp" directory.
313 // MakeAbsPath to prepend the current
314 // drive letter on OS/2
315 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
317 lyxerr << ' ' << s << endl;
318 if (buf->writeFile(s)) {
320 lyxerr << _(" Save seems successful. Phew.") << endl;
323 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
328 Buffer * BufferList::readFile(string const & s, bool ronly)
331 string e = OnlyPath(s);
333 // File information about normal file
334 FileInfo fileInfo2(s);
336 if (!fileInfo2.exist()) {
337 string const file = MakeDisplayPath(s, 50);
339 boost::format fmt(_("The specified document\n%1$s\ncould not be read."));
341 string text = fmt.str();
343 string text = _("The specified document\n");
344 text += file + _(" could not be read.");
346 Alert::error(_("Could not read document"), text);
350 Buffer * b = newBuffer(s, ronly);
352 // Check if emergency save file exists and is newer.
353 e += OnlyFilename(s) + ".emergency";
354 FileInfo fileInfoE(e);
356 bool use_emergency = false;
358 if (fileInfoE.exist() && fileInfo2.exist()) {
359 if (fileInfoE.getModificationTime()
360 > fileInfo2.getModificationTime()) {
361 string const file = MakeDisplayPath(s, 20);
363 boost::format fmt(_("An emergency save of the document %1$s exists.\n\nRecover emergency save?"));
365 string text = fmt.str();
367 string text = _("An emergency save of the document ");
368 text += file + _(" exists.\n\nRecover emergency save?");
370 int const ret = Alert::prompt(_("Load emergency save?"),
371 text, 0, _("&Recover"), _("&Load Original"));
375 // the file is not saved if we load the
378 use_emergency = true;
380 // Here, we should delete the emergency save
386 if (!use_emergency) {
387 // Now check if autosave file is newer.
389 a += OnlyFilename(s);
391 FileInfo fileInfoA(a);
392 if (fileInfoA.exist() && fileInfo2.exist()) {
393 if (fileInfoA.getModificationTime()
394 > fileInfo2.getModificationTime()) {
395 string const file = MakeDisplayPath(s, 20);
397 boost::format fmt(_("The backup of the document %1$s is newer.\n\nLoad the backup instead?"));
399 string text = fmt.str();
401 string text = _("The backup of the document ");
402 text += file + _(" is newer.\n\nLoad the backup instead?");
404 int const ret = Alert::prompt(_("Load backup?"),
405 text, 0, _("&Load backup"), _("Load &original"));
409 // the file is not saved if we load the
413 // Here, we should delete the autosave
419 // not sure if this is the correct place to begin LyXLex
422 if (b->readFile(lex, ts))
431 bool BufferList::exists(string const & s) const
433 return find_if(bstore.begin(), bstore.end(),
434 lyx::compare_memfun(&Buffer::fileName, s))
439 bool BufferList::isLoaded(Buffer const * b) const
443 BufferStorage::const_iterator cit =
444 find(bstore.begin(), bstore.end(), b);
445 return cit != bstore.end();
449 Buffer * BufferList::getBuffer(string const & s)
451 BufferStorage::iterator it =
452 find_if(bstore.begin(), bstore.end(),
453 lyx::compare_memfun(&Buffer::fileName, s));
454 return it != bstore.end() ? (*it) : 0;
458 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
461 Buffer * b = newBuffer(name);
463 // use defaults.lyx as a default template if it exists.
465 tname = LibFileSearch("templates", "defaults.lyx");
467 if (!tname.empty()) {
468 bool templateok = false;
472 if (b->readFile(lex, tname)) {
477 string const file = MakeDisplayPath(tname, 50);
479 boost::format fmt(_("The specified document template\n%1$s\ncould not be read."));
481 string text = fmt.str();
483 string text = _("The specified document template\n");
484 text += file + _(" could not be read.");
486 Alert::error(_("Could not read template"), text);
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 string const file = MakeDisplayPath(s, 20);
520 boost::format fmt(_("The document %1$s is already loaded.\n\nDo you want to revert to the saved version?"));
522 string text = fmt.str();
524 string text = _("The document ");
525 text += file + _(" is already loaded.\n\nDo you want to revert to the saved version?");
527 int const ret = Alert::prompt(_("Revert to saved document?"),
528 text, 1, _("&Revert"), _("&Switch to document"));
531 // FIXME: should be LFUN_REVERT
532 if (!close(getBuffer(s), false)) {
535 // Fall through to new load. (Asger)
537 // Here, we pretend that we just loaded the
545 switch (IsFileWriteable(s)) {
552 b->lyxvc.file_found_hook(s);
554 break; //fine- it's r/w
556 string const file = MakeDisplayPath(s, 20);
557 // Here we probably should run
558 if (LyXVC::file_not_found_hook(s)) {
560 boost::format fmt(_("Do you want to retrieve the document %1$s from version control?"));
562 string text = fmt.str();
564 string text = _("Do you want to retrieve the document ");
565 text += file + _(" from version control?");
567 int const ret = Alert::prompt(_("Retrieve from version control?"),
568 text, 0, _("&Retrieve"), _("&Cancel"));
571 // How can we know _how_ to do the checkout?
572 // With the current VC support it has to be,
573 // a RCS file since CVS do not have special ,v files.
575 return loadLyXFile(filename, tolastfiles);
580 boost::format fmt(_("The document %1$s does not yet exist.\n\nDo you want to create a new document?"));
582 string text = fmt.str();
584 string text = _("The document ");
585 text += file + _(" does not yet exist.\n\nDo you want to create a new document?");
587 int const ret = Alert::prompt(_("Create new document?"),
588 text, 0, _("&Create"), _("Cancel"));
591 b = newFile(s, string(), true);
597 if (b && tolastfiles)
598 lastfiles->newFile(b->fileName());
604 void BufferList::setCurrentAuthor(string const & name, string const & email)
606 BufferStorage::iterator it = bstore.begin();
607 BufferStorage::iterator end = bstore.end();
608 for (; it != end; ++it) {
609 (*it)->authors().record(0, Author(name, email));