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, 2, _("&Save Changes"), _("&Discard Changes"), _("&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, 2, _("&Save Changes"), _("&Discard Changes"), _("&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, 1, _("&Recover"), _("&Load Original"));
375 // the file is not saved if we load the
378 use_emergency = true;
383 if (!use_emergency) {
384 // Now check if autosave file is newer.
386 a += OnlyFilename(s);
388 FileInfo fileInfoA(a);
389 if (fileInfoA.exist() && fileInfo2.exist()) {
390 if (fileInfoA.getModificationTime()
391 > fileInfo2.getModificationTime()) {
392 string const file = MakeDisplayPath(s, 20);
394 boost::format fmt(_("The backup of the document %1$s is newer.\n\nLoad the backup instead?"));
396 string text = fmt.str();
398 string text = _("The backup of the document ");
399 text += file + _(" is newer.\n\nLoad the backup instead?");
401 int const ret = Alert::prompt(_("Load backup?"),
402 text, 0, 1, _("&Load backup"), _("Load &original"));
406 // the file is not saved if we load the
410 // Here, we should delete the autosave
416 // not sure if this is the correct place to begin LyXLex
419 if (b->readFile(lex, ts))
428 bool BufferList::exists(string const & s) const
430 return find_if(bstore.begin(), bstore.end(),
431 lyx::compare_memfun(&Buffer::fileName, s))
436 bool BufferList::isLoaded(Buffer const * b) const
440 BufferStorage::const_iterator cit =
441 find(bstore.begin(), bstore.end(), b);
442 return cit != bstore.end();
446 Buffer * BufferList::getBuffer(string const & s)
448 BufferStorage::iterator it =
449 find_if(bstore.begin(), bstore.end(),
450 lyx::compare_memfun(&Buffer::fileName, s));
451 return it != bstore.end() ? (*it) : 0;
455 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
458 Buffer * b = newBuffer(name);
460 // use defaults.lyx as a default template if it exists.
462 tname = LibFileSearch("templates", "defaults.lyx");
464 if (!tname.empty()) {
465 bool templateok = false;
469 if (b->readFile(lex, tname)) {
474 string const file = MakeDisplayPath(tname, 50);
476 boost::format fmt(_("The specified document template\n%1$s\ncould not be read."));
478 string text = fmt.str();
480 string text = _("The specified document template\n");
481 text += file + _(" could not be read.");
483 Alert::error(_("Could not read template"), text);
484 // no template, start with empty buffer
485 b->paragraphs.push_back(new Paragraph);
486 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
488 } else { // start with empty buffer
489 b->paragraphs.push_back(new Paragraph);
490 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
495 b->setFileName(name);
498 b->setReadonly(false);
499 b->updateDocLang(b->params.language);
505 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
507 // get absolute path of file and add ".lyx" to the filename if
509 string s = FileSearch(string(), filename, "lyx");
514 // file already open?
516 string const file = MakeDisplayPath(s, 20);
518 boost::format fmt(_("The document %1$s is already loaded.\n\nDo you want to revert to the saved version?"));
520 string text = fmt.str();
522 string text = _("The document ");
523 text += file + _(" is already loaded.\n\nDo you want to revert to the saved version?");
525 int const ret = Alert::prompt(_("Revert to saved document?"),
526 text, 0, 1, _("&Revert"), _("&Switch to document"));
529 // FIXME: should be LFUN_REVERT
530 if (!close(getBuffer(s), false)) {
533 // Fall through to new load. (Asger)
535 // Here, we pretend that we just loaded the
543 switch (IsFileWriteable(s)) {
550 b->lyxvc.file_found_hook(s);
552 break; //fine- it's r/w
554 string const file = MakeDisplayPath(s, 20);
555 // Here we probably should run
556 if (LyXVC::file_not_found_hook(s)) {
558 boost::format fmt(_("Do you want to retrieve the document %1$s from version control?"));
560 string text = fmt.str();
562 string text = _("Do you want to retrieve the document ");
563 text += file + _(" from version control?");
565 int const ret = Alert::prompt(_("Retrieve from version control?"),
566 text, 0, 1, _("&Retrieve"), _("&Cancel"));
569 // How can we know _how_ to do the checkout?
570 // With the current VC support it has to be,
571 // a RCS file since CVS do not have special ,v files.
573 return loadLyXFile(filename, tolastfiles);
578 boost::format fmt(_("The document %1$s does not yet exist.\n\nDo you want to create a new document?"));
580 string text = fmt.str();
582 string text = _("The document ");
583 text += file + _(" does not yet exist.\n\nDo you want to create a new document?");
585 int const ret = Alert::prompt(_("Create new document?"),
586 text, 0, 1, _("&Create"), _("Cancel"));
589 b = newFile(s, string(), true);
595 if (b && tolastfiles)
596 lastfiles->newFile(b->fileName());
602 void BufferList::setCurrentAuthor(string const & name, string const & email)
604 BufferStorage::iterator it = bstore.begin();
605 BufferStorage::iterator end = bstore.end();
606 for (; it != end; ++it) {
607 (*it)->authors().record(0, Author(name, email));