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 "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\nDo you want to save the document?");
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(), true);
166 bool BufferList::close(Buffer * buf, bool ask)
170 if (!ask || buf->isClean() || quitting || buf->paragraphs.empty()) {
176 if (buf->isUnnamed())
177 fname = OnlyFilename(buf->fileName());
179 fname = MakeDisplayPath(buf->fileName(), 30);
181 boost::format fmt(_("The document %1$s has unsaved changes.\n\nDo you want to save the document?"));
183 string text = fmt.str();
185 string text = _("The document ");
186 text += fname + _(" has unsaved changes.\n\nDo you want to save the document?");
188 int const ret = Alert::prompt(_("Save changed document?"),
189 text, 0, _("&Save"), _("&Discard"));
192 if (buf->isUnnamed()) {
193 if (!WriteAs(current_view, buf))
195 } else if (buf->save()) {
196 lastfiles->newFile(buf->fileName());
202 if (buf->isUnnamed()) {
203 removeAutosaveFile(buf->fileName());
211 vector<string> const BufferList::getFileNames() const
214 std::copy(bstore.begin(), bstore.end(),
215 lyx::back_inserter_fun(nvec, &Buffer::fileName));
220 Buffer * BufferList::first()
224 return bstore.front();
228 Buffer * BufferList::getBuffer(unsigned int choice)
230 if (choice >= bstore.size())
232 return bstore[choice];
236 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
238 BufferStorage::iterator it = bstore.begin();
239 BufferStorage::iterator end = bstore.end();
240 for (; it != end; ++it) {
241 if (!(*it)->isDepClean(mastertmpdir)) {
242 string writefile = mastertmpdir;
244 writefile += (*it)->getLatexName();
245 (*it)->makeLaTeXFile(writefile, mastertmpdir,
247 (*it)->markDepClean(mastertmpdir);
253 void BufferList::emergencyWriteAll()
255 for_each(bstore.begin(), bstore.end(),
256 boost::bind(&BufferList::emergencyWrite, this, _1));
260 void BufferList::emergencyWrite(Buffer * buf)
262 // assert(buf) // this is not good since C assert takes an int
263 // and a pointer is a long (JMarc)
264 assert(buf != 0); // use c assert to avoid a loop
267 // No need to save if the buffer has not changed.
271 string const doc = buf->isUnnamed()
272 ? OnlyFilename(buf->fileName()) : buf->fileName();
275 lyxerr << boost::format(_("LyX: Attempting to save document %1$s"))
279 lyxerr << _("LyX: Attempting to save document ") << doc << endl;
281 // We try to save three places:
283 // 1) Same place as document. Unless it is an unnamed doc.
284 if (!buf->isUnnamed()) {
285 string s = buf->fileName();
287 lyxerr << " " << s << endl;
288 if (buf->writeFile(s)) {
290 lyxerr << _(" Save seems successful. Phew.") << endl;
293 lyxerr << _(" Save failed! Trying...") << endl;
297 // 2) In HOME directory.
298 string s = AddName(GetEnvPath("HOME"), buf->fileName());
300 lyxerr << ' ' << s << endl;
301 if (buf->writeFile(s)) {
303 lyxerr << _(" Save seems successful. Phew.") << endl;
307 lyxerr << _(" Save failed! Trying...") << endl;
309 // 3) In "/tmp" directory.
310 // MakeAbsPath to prepend the current
311 // drive letter on OS/2
312 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
314 lyxerr << ' ' << s << endl;
315 if (buf->writeFile(s)) {
317 lyxerr << _(" Save seems successful. Phew.") << endl;
320 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
325 Buffer * BufferList::readFile(string const & s, bool ronly)
328 string e = OnlyPath(s);
330 // File information about normal file
331 FileInfo fileInfo2(s);
333 if (!fileInfo2.exist()) {
334 string const file = MakeDisplayPath(s, 50);
336 boost::format fmt(_("The specified document\n%1$s\ncould not be read."));
338 string text = fmt.str();
340 string text = _("The specified document\n");
341 text += file + _(" could not be read.");
343 Alert::error(_("Could not read document"), text);
347 Buffer * b = newBuffer(s, ronly);
349 // Check if emergency save file exists and is newer.
350 e += OnlyFilename(s) + ".emergency";
351 FileInfo fileInfoE(e);
353 bool use_emergency = false;
355 if (fileInfoE.exist() && fileInfo2.exist()) {
356 if (fileInfoE.getModificationTime()
357 > fileInfo2.getModificationTime()) {
358 string const file = MakeDisplayPath(s, 20);
360 boost::format fmt(_("An emergency save of the document %1$s exists.\n\nRecover emergency save?"));
362 string text = fmt.str();
364 string text = _("An emergency save of the document ");
365 text += file + _(" exists.\n\nRecover emergency save?");
367 int const ret = Alert::prompt(_("Load emergency save?"),
368 text, 0, _("&Recover"), _("&Load original"));
372 // the file is not saved if we load the
375 use_emergency = true;
377 // Here, we should delete the emergency save
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, _("&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.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 string const file = MakeDisplayPath(s, 20);
517 boost::format fmt(_("The document %1$s is already loaded.\n\nDo you want to revert to the saved version?"));
519 string text = fmt.str();
521 string text = _("The document ");
522 text += file + _(" is already loaded.\n\nDo you want to revert to the saved version?");
524 int const ret = Alert::prompt(_("Revert to saved document?"),
525 text, 1, _("&Revert"), _("&Switch to document"));
528 // FIXME: should be LFUN_REVERT
529 if (!close(getBuffer(s), false)) {
532 // Fall through to new load. (Asger)
534 // Here, we pretend that we just loaded the
542 switch (IsFileWriteable(s)) {
549 b->lyxvc.file_found_hook(s);
551 break; //fine- it's r/w
553 string const file = MakeDisplayPath(s, 20);
554 // Here we probably should run
555 if (LyXVC::file_not_found_hook(s)) {
557 boost::format fmt(_("Do you want to retrieve the document %1$s from version control?"));
559 string text = fmt.str();
561 string text = _("Do you want to retrieve the document ");
562 text += file + _(" from version control?");
564 int const ret = Alert::prompt(_("Retrieve from version control?"),
565 text, 0, _("&Retrieve"), _("&Cancel"));
568 // How can we know _how_ to do the checkout?
569 // With the current VC support it has to be,
570 // a RCS file since CVS do not have special ,v files.
572 return loadLyXFile(filename, tolastfiles);
577 boost::format fmt(_("The document %1$s does not yet exist.\n\nDo you want to create a new document?"));
579 string text = fmt.str();
581 string text = _("The document ");
582 text += file + _(" does not yet exist.\n\nDo you want to create a new document?");
584 int const ret = Alert::prompt(_("Create new document?"),
585 text, 0, _("&Create"), _("Cancel"));
588 b = newFile(s, string(), true);
594 if (b && tolastfiles)
595 lastfiles->newFile(b->fileName());
601 void BufferList::setCurrentAuthor(string const & name, string const & email)
603 BufferStorage::iterator it = bstore.begin();
604 BufferStorage::iterator end = bstore.end();
605 for (; it != end; ++it) {
606 (*it)->authors().record(0, Author(name, email));