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 * ======================================================
17 #include "bufferlist.h"
19 #include "lastfiles.h"
24 #include "bufferview_funcs.h"
25 #include "BufferView.h"
27 #include "frontends/LyXView.h"
28 #include "vc-backend.h"
29 #include "TextCache.h"
32 #include "frontends/Alert.h"
34 #include "support/FileInfo.h"
35 #include "support/filetools.h"
36 #include "support/lyxmanip.h"
37 #include "support/lyxfunctional.h"
38 #include "support/LAssert.h"
40 #include <boost/bind.hpp>
41 #include "BoostFormat.h"
55 extern BufferView * current_view;
58 BufferList::BufferList()
62 bool BufferList::empty() const
64 return bstore.empty();
68 bool BufferList::qwriteOne(Buffer * buf, string const & fname,
69 string & unsaved_list)
73 switch (Alert::askConfirmation(_("Changes in document:"),
75 _("Save document?"))) {
77 // FIXME: WriteAs can be asynch !
79 reask = !WriteAs(current_view, buf);
81 reask = !MenuWrite(current_view, buf);
85 // if we crash after this we could
86 // have no autosave file but I guess
87 // this is really inprobable (Jug)
88 if (buf->isUnnamed()) {
89 removeAutosaveFile(buf->fileName());
92 unsaved_list += MakeDisplayPath(fname, 50) + "\n";
102 bool BufferList::qwriteAll()
105 BufferStorage::iterator it = bstore.begin();
106 BufferStorage::iterator end = bstore.end();
107 for (; it != end; ++it) {
108 if (!(*it)->isClean()) {
110 if ((*it)->isUnnamed())
111 fname = OnlyFilename((*it)->fileName());
113 fname = MakeDisplayPath((*it)->fileName(), 50);
114 if (!qwriteOne(*it, fname, unsaved)) // cancel the request!
123 void BufferList::release(Buffer * buf)
126 BufferStorage::iterator it = find(bstore.begin(), bstore.end(), buf);
127 if (it != bstore.end()) {
128 // Make sure that we don't store a LyXText in
129 // the textcache that points to the buffer
131 Buffer * tmp = (*it);
133 textcache.removeAllWithBuffer(tmp);
139 Buffer * BufferList::newBuffer(string const & s, bool ronly)
141 Buffer * tmpbuf = new Buffer(s, ronly);
142 tmpbuf->params.useClassDefaults();
143 lyxerr[Debug::INFO] << "Assigning to buffer "
144 << bstore.size() << endl;
145 bstore.push_back(tmpbuf);
150 void BufferList::closeAll()
152 // Since we are closing we can just as well delete all
153 // in the textcache this will also speed the closing/quiting up a bit.
156 while (!bstore.empty()) {
157 close(bstore.front());
162 bool BufferList::close(Buffer * buf)
166 if (!buf->paragraphs.empty() && !buf->isClean() && !quitting) {
168 if (buf->isUnnamed())
169 fname = OnlyFilename(buf->fileName());
171 fname = MakeDisplayPath(buf->fileName(), 50);
174 switch (Alert::askConfirmation(_("Changes in document:"),
176 _("Save document?"))) {
178 if (buf->isUnnamed())
179 reask = !WriteAs(current_view, buf);
180 else if (buf->save()) {
181 lastfiles->newFile(buf->fileName());
188 if (buf->isUnnamed()) {
189 removeAutosaveFile(buf->fileName());
204 vector<string> const BufferList::getFileNames() const
207 std::copy(bstore.begin(), bstore.end(),
208 lyx::back_inserter_fun(nvec, &Buffer::fileName));
213 Buffer * BufferList::first()
217 return bstore.front();
221 Buffer * BufferList::getBuffer(unsigned int choice)
223 if (choice >= bstore.size())
225 return bstore[choice];
229 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
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();
268 lyxerr << boost::format(_("LyX: Attempting to save document %1$s"))
272 lyxerr << _("LyX: Attempting to save document ") << doc << endl;
274 // We try to save three places:
276 // 1) Same place as document. Unless it is an unnamed doc.
277 if (!buf->isUnnamed()) {
278 string s = buf->fileName();
280 lyxerr << " " << s << endl;
281 if (buf->writeFile(s)) {
283 lyxerr << _(" Save seems successful. Phew.") << endl;
286 lyxerr << _(" Save failed! Trying...") << endl;
290 // 2) In HOME directory.
291 string s = AddName(GetEnvPath("HOME"), buf->fileName());
293 lyxerr << ' ' << s << endl;
294 if (buf->writeFile(s)) {
296 lyxerr << _(" Save seems successful. Phew.") << endl;
300 lyxerr << _(" Save failed! Trying...") << endl;
302 // 3) In "/tmp" directory.
303 // MakeAbsPath to prepend the current
304 // drive letter on OS/2
305 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
307 lyxerr << ' ' << s << endl;
308 if (buf->writeFile(s)) {
310 lyxerr << _(" Save seems successful. Phew.") << endl;
313 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
318 Buffer * BufferList::readFile(string const & s, bool ronly)
321 string e = OnlyPath(s);
323 // File information about normal file
324 FileInfo fileInfo2(s);
326 if (!fileInfo2.exist()) {
327 Alert::alert(_("Error!"), _("Cannot open file"),
332 Buffer * b = newBuffer(s, ronly);
334 // Check if emergency save file exists and is newer.
335 e += OnlyFilename(s) + ".emergency";
336 FileInfo fileInfoE(e);
338 bool use_emergency = false;
340 if (fileInfoE.exist() && fileInfo2.exist()) {
341 if (fileInfoE.getModificationTime()
342 > fileInfo2.getModificationTime()) {
343 if (Alert::askQuestion(_("An emergency save of this document exists!"),
344 MakeDisplayPath(s, 50),
345 _("Try to load that instead?"))) {
347 // the file is not saved if we load the
350 use_emergency = true;
352 // Here, we should delete the emergency save
358 if (!use_emergency) {
359 // Now check if autosave file is newer.
361 a += OnlyFilename(s);
363 FileInfo fileInfoA(a);
364 if (fileInfoA.exist() && fileInfo2.exist()) {
365 if (fileInfoA.getModificationTime()
366 > fileInfo2.getModificationTime()) {
367 if (Alert::askQuestion(_("Autosave file is newer."),
368 MakeDisplayPath(s, 50),
369 _("Load that one instead?"))) {
371 // the file is not saved if we load the
375 // Here, we should delete the autosave
381 // not sure if this is the correct place to begin LyXLex
384 if (b->readFile(lex, ts))
393 bool BufferList::exists(string const & s) const
395 return find_if(bstore.begin(), bstore.end(),
396 lyx::compare_memfun(&Buffer::fileName, s))
401 bool BufferList::isLoaded(Buffer const * b) const
405 BufferStorage::const_iterator cit =
406 find(bstore.begin(), bstore.end(), b);
407 return cit != bstore.end();
411 Buffer * BufferList::getBuffer(string const & s)
413 BufferStorage::iterator it =
414 find_if(bstore.begin(), bstore.end(),
415 lyx::compare_memfun(&Buffer::fileName, s));
416 return it != bstore.end() ? (*it) : 0;
420 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
423 Buffer * b = newBuffer(name);
425 // use defaults.lyx as a default template if it exists.
427 tname = LibFileSearch("templates", "defaults.lyx");
429 if (!tname.empty()) {
430 bool templateok = false;
434 if (b->readFile(lex, tname)) {
439 Alert::alert(_("Error!"), _("Unable to open template"),
440 MakeDisplayPath(tname));
441 // no template, start with empty buffer
442 b->paragraphs.set(new Paragraph);
443 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
445 } else { // start with empty buffer
446 b->paragraphs.set(new Paragraph);
447 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
452 b->setFileName(name);
455 b->setReadonly(false);
461 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
463 // get absolute path of file and add ".lyx" to the filename if
465 string s = FileSearch(string(), filename, "lyx");
470 // file already open?
472 if (Alert::askQuestion(_("Document is already open:"),
473 MakeDisplayPath(s, 50),
474 _("Do you want to reload that document?"))) {
475 // Reload is accomplished by closing and then loading
476 if (!close(getBuffer(s))) {
479 // Fall through to new load. (Asger)
481 // Here, we pretend that we just loaded the
488 switch (IsFileWriteable(s)) {
495 b->lyxvc.file_found_hook(s);
497 break; //fine- it's r/w
499 // Here we probably should run
500 if (LyXVC::file_not_found_hook(s)) {
501 // Ask if the file should be checked out for
502 // viewing/editing, if so: load it.
503 if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
504 // How can we know _how_ to do the checkout?
505 // With the current VC support it has to be,
506 // a RCS file since CVS do not have special ,v files.
508 return loadLyXFile(filename, tolastfiles);
511 if (Alert::askQuestion(_("Cannot open specified file:"),
512 MakeDisplayPath(s, 50),
513 _("Create new document with this name?")))
515 // Find a free buffer
516 b = newFile(s, string(), true);
521 if (b && tolastfiles)
522 lastfiles->newFile(b->fileName());
528 void BufferList::setCurrentAuthor(string const & name, string const & email)
530 BufferStorage::iterator it = bstore.begin();
531 BufferStorage::iterator end = bstore.end();
532 for (; it != end; ++it) {
533 (*it)->authors().record(0, Author(name, email));