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"
21 #include "bufferview_funcs.h"
22 #include "BufferView.h"
24 #include "frontends/LyXView.h"
25 #include "vc-backend.h"
26 #include "TextCache.h"
29 #include "frontends/Alert.h"
31 #include "support/FileInfo.h"
32 #include "support/filetools.h"
33 #include "support/lyxmanip.h"
34 #include "support/lyxfunctional.h"
35 #include "support/LAssert.h"
37 #include <boost/bind.hpp>
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);
72 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
73 "Do you want to save the document?"), file);
74 int const ret = Alert::prompt(_("Save changed document?"),
75 text, 0, 2, _("&Save Changes"), _("&Discard Changes"), _("&Cancel"));
78 // FIXME: WriteAs can be asynch !
79 // but not right now...maybe we should remove that
84 succeeded = WriteAs(current_view, buf);
86 succeeded = MenuWrite(current_view, buf);
90 } else if (ret == 1) {
91 // if we crash after this we could
92 // have no autosave file but I guess
93 // this is really inprobable (Jug)
95 removeAutosaveFile(buf->fileName());
105 bool BufferList::quitWriteAll()
107 BufferStorage::iterator it = bstore.begin();
108 BufferStorage::iterator end = bstore.end();
109 for (; it != end; ++it) {
110 if ((*it)->isClean())
113 if (!quitWriteBuffer(*it))
121 void BufferList::release(Buffer * buf)
124 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
125 if (it != bstore.end()) {
126 // Make sure that we don't store a LyXText in
127 // the textcache that points to the buffer
129 Buffer * tmp = (*it);
131 textcache.removeAllWithBuffer(tmp);
137 Buffer * BufferList::newBuffer(string const & s, bool ronly)
139 Buffer * tmpbuf = new Buffer(s, ronly);
140 tmpbuf->params.useClassDefaults();
141 lyxerr[Debug::INFO] << "Assigning to buffer "
142 << bstore.size() << endl;
143 bstore.push_back(tmpbuf);
148 void BufferList::closeAll()
150 // Since we are closing we can just as well delete all
151 // in the textcache this will also speed the closing/quiting up a bit.
154 while (!bstore.empty()) {
155 close(bstore.front(), false);
160 bool BufferList::close(Buffer * buf, bool ask)
164 // FIXME: is the quitting check still necessary ?
165 if (!ask || buf->isClean() || quitting || buf->paragraphs.empty()) {
171 if (buf->isUnnamed())
172 fname = OnlyFilename(buf->fileName());
174 fname = MakeDisplayPath(buf->fileName(), 30);
176 string text = bformat(_("The document %1$s has unsaved changes.\n\n"
177 "Do you want to save the document?"), fname);
178 int const ret = Alert::prompt(_("Save changed document?"),
179 text, 0, 2, _("&Save Changes"), _("&Discard Changes"), _("&Cancel"));
182 if (buf->isUnnamed()) {
183 if (!WriteAs(current_view, buf))
185 } else if (buf->save()) {
186 lastfiles->newFile(buf->fileName());
190 } else if (ret == 2) {
194 if (buf->isUnnamed()) {
195 removeAutosaveFile(buf->fileName());
203 vector<string> const BufferList::getFileNames() const
206 std::copy(bstore.begin(), bstore.end(),
207 lyx::back_inserter_fun(nvec, &Buffer::fileName));
212 Buffer * BufferList::first()
216 return bstore.front();
220 Buffer * BufferList::getBuffer(unsigned int choice)
222 if (choice >= bstore.size())
224 return bstore[choice];
228 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir,
229 LatexRunParams const & runparams)
231 BufferStorage::iterator it = bstore.begin();
232 BufferStorage::iterator end = bstore.end();
233 for (; it != end; ++it) {
234 if (!(*it)->isDepClean(mastertmpdir)) {
235 string writefile = mastertmpdir;
237 writefile += (*it)->getLatexName();
238 (*it)->makeLaTeXFile(writefile, mastertmpdir,
240 (*it)->markDepClean(mastertmpdir);
246 void BufferList::emergencyWriteAll()
248 for_each(bstore.begin(), bstore.end(),
249 boost::bind(&BufferList::emergencyWrite, this, _1));
253 void BufferList::emergencyWrite(Buffer * buf)
255 // assert(buf) // this is not good since C assert takes an int
256 // and a pointer is a long (JMarc)
257 assert(buf != 0); // use c assert to avoid a loop
260 // No need to save if the buffer has not changed.
264 string const doc = buf->isUnnamed()
265 ? OnlyFilename(buf->fileName()) : buf->fileName();
267 lyxerr << bformat(_("LyX: Attempting to save document %1$s"), doc) << endl;
269 // We try to save three places:
270 // 1) Same place as document. Unless it is an unnamed doc.
271 if (!buf->isUnnamed()) {
272 string s = buf->fileName();
274 lyxerr << " " << s << endl;
275 if (buf->writeFile(s)) {
277 lyxerr << _(" Save seems successful. Phew.") << endl;
280 lyxerr << _(" Save failed! Trying...") << endl;
284 // 2) In HOME directory.
285 string s = AddName(GetEnvPath("HOME"), buf->fileName());
287 lyxerr << ' ' << s << endl;
288 if (buf->writeFile(s)) {
290 lyxerr << _(" Save seems successful. Phew.") << endl;
294 lyxerr << _(" Save failed! Trying...") << endl;
296 // 3) In "/tmp" directory.
297 // MakeAbsPath to prepend the current
298 // drive letter on OS/2
299 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
301 lyxerr << ' ' << s << endl;
302 if (buf->writeFile(s)) {
304 lyxerr << _(" Save seems successful. Phew.") << endl;
307 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
312 Buffer * BufferList::readFile(string const & s, bool ronly)
315 string e = OnlyPath(s);
317 // File information about normal file
318 FileInfo fileInfo2(s);
320 if (!fileInfo2.exist()) {
321 string const file = MakeDisplayPath(s, 50);
322 string text = bformat(_("The specified document\n%1$s"
323 "\ncould not be read."), file);
324 Alert::error(_("Could not read document"), text);
328 Buffer * b = newBuffer(s, ronly);
330 // Check if emergency save file exists and is newer.
331 e += OnlyFilename(s) + ".emergency";
332 FileInfo fileInfoE(e);
334 bool use_emergency = false;
336 if (fileInfoE.exist() && fileInfo2.exist()) {
337 if (fileInfoE.getModificationTime()
338 > fileInfo2.getModificationTime()) {
339 string const file = MakeDisplayPath(s, 20);
340 string text = bformat(_("An emergency save of the document %1$s exists.\n"
341 "\nRecover emergency save?"), file);
342 int const ret = Alert::prompt(_("Load emergency save?"),
343 text, 0, 1, _("&Recover"), _("&Load Original"));
347 // the file is not saved if we load the
350 use_emergency = true;
355 if (!use_emergency) {
356 // Now check if autosave file is newer.
358 a += OnlyFilename(s);
360 FileInfo fileInfoA(a);
361 if (fileInfoA.exist() && fileInfo2.exist()) {
362 if (fileInfoA.getModificationTime()
363 > fileInfo2.getModificationTime()) {
364 string const file = MakeDisplayPath(s, 20);
365 string text = bformat(_("The backup of the document %1$s is newer.\n\n"
366 "Load the backup instead?"), file);
367 int const ret = Alert::prompt(_("Load backup?"),
368 text, 0, 1, _("&Load backup"), _("Load &original"));
372 // the file is not saved if we load the
376 // Here, we should delete the autosave
382 // not sure if this is the correct place to begin LyXLex
385 if (b->readFile(lex, ts))
394 bool BufferList::exists(string const & s) const
396 return find_if(bstore.begin(), bstore.end(),
397 lyx::compare_memfun(&Buffer::fileName, s))
402 bool BufferList::isLoaded(Buffer const * b) const
406 BufferStorage::const_iterator cit =
407 find(bstore.begin(), bstore.end(), b);
408 return cit != bstore.end();
412 Buffer * BufferList::getBuffer(string const & s)
414 BufferStorage::iterator it =
415 find_if(bstore.begin(), bstore.end(),
416 lyx::compare_memfun(&Buffer::fileName, s));
417 return it != bstore.end() ? (*it) : 0;
421 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
424 Buffer * b = newBuffer(name);
426 // use defaults.lyx as a default template if it exists.
428 tname = LibFileSearch("templates", "defaults.lyx");
430 if (!tname.empty()) {
431 bool templateok = false;
435 if (b->readFile(lex, tname)) {
440 string const file = MakeDisplayPath(tname, 50);
441 string text = bformat(_("The specified document template\n%1$s\n"
442 "could not be read."), file);
443 Alert::error(_("Could not read template"), text);
444 // no template, start with empty buffer
445 b->paragraphs.push_back(Paragraph());
446 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
448 } else { // start with empty buffer
449 b->paragraphs.push_back(Paragraph());
450 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
455 b->setFileName(name);
458 b->setReadonly(false);
459 b->updateDocLang(b->params.language);
465 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
467 // get absolute path of file and add ".lyx" to the filename if
469 string s = FileSearch(string(), filename, "lyx");
474 // file already open?
476 string const file = MakeDisplayPath(s, 20);
477 string text = bformat(_("The document %1$s is already loaded.\n\n"
478 "Do you want to revert to the saved version?"), file);
479 int const ret = Alert::prompt(_("Revert to saved document?"),
480 text, 0, 1, _("&Revert"), _("&Switch to document"));
483 // FIXME: should be LFUN_REVERT
484 if (!close(getBuffer(s), false)) {
487 // Fall through to new load. (Asger)
489 // Here, we pretend that we just loaded the
497 switch (IsFileWriteable(s)) {
504 b->lyxvc.file_found_hook(s);
506 break; //fine- it's r/w
508 string const file = MakeDisplayPath(s, 20);
509 // Here we probably should run
510 if (LyXVC::file_not_found_hook(s)) {
511 string text = bformat(_("Do you want to retrieve the document"
512 " %1$s from version control?"), file);
513 int const ret = Alert::prompt(_("Retrieve from version control?"),
514 text, 0, 1, _("&Retrieve"), _("&Cancel"));
517 // How can we know _how_ to do the checkout?
518 // With the current VC support it has to be,
519 // a RCS file since CVS do not have special ,v files.
521 return loadLyXFile(filename, tolastfiles);
525 string text = bformat(_("The document %1$s does not yet exist.\n\n"
526 "Do you want to create a new document?"), file);
527 int const ret = Alert::prompt(_("Create new document?"),
528 text, 0, 1, _("&Create"), _("Cancel"));
531 b = newFile(s, string(), true);
537 if (b && tolastfiles)
538 lastfiles->newFile(b->fileName());
544 void BufferList::setCurrentAuthor(string const & name, string const & email)
546 BufferStorage::iterator it = bstore.begin();
547 BufferStorage::iterator end = bstore.end();
548 for (; it != end; ++it) {
549 (*it)->authors().record(0, Author(name, email));