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 // Class BufferStorage
61 void BufferStorage::release(Buffer * buf)
64 Container::iterator it = find(container.begin(), container.end(), buf);
65 if (it != container.end()) {
66 // Make sure that we don't store a LyXText in
67 // the textcache that points to the buffer
71 textcache.removeAllWithBuffer(tmp);
77 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
79 Buffer * tmpbuf = new Buffer(s, ronly);
80 tmpbuf->params.useClassDefaults();
81 lyxerr[Debug::INFO] << "Assigning to buffer "
82 << container.size() << endl;
83 container.push_back(tmpbuf);
92 BufferList::BufferList()
93 : state_(BufferList::OK)
97 bool BufferList::empty() const
99 return bstore.empty();
103 bool BufferList::qwriteOne(Buffer * buf, string const & fname,
104 string & unsaved_list)
108 switch (Alert::askConfirmation(_("Changes in document:"),
110 _("Save document?"))) {
112 // FIXME: WriteAs can be asynch !
113 if (buf->isUnnamed())
114 reask = !WriteAs(current_view, buf);
116 reask = !MenuWrite(current_view, buf);
120 // if we crash after this we could
121 // have no autosave file but I guess
122 // this is really inprobable (Jug)
123 if (buf->isUnnamed()) {
124 removeAutosaveFile(buf->fileName());
127 unsaved_list += MakeDisplayPath(fname, 50) + "\n";
137 bool BufferList::qwriteAll()
140 BufferStorage::iterator it = bstore.begin();
141 BufferStorage::iterator end = bstore.end();
142 for (; it != end; ++it) {
143 if (!(*it)->isClean()) {
145 if ((*it)->isUnnamed())
146 fname = OnlyFilename((*it)->fileName());
148 fname = MakeDisplayPath((*it)->fileName(), 50);
149 if (!qwriteOne(*it, fname, unsaved)) // cancel the request!
158 void BufferList::closeAll()
160 state_ = BufferList::CLOSING;
161 // Since we are closing we can just as well delete all
162 // in the textcache this will also speed the closing/quiting up a bit.
165 while (!bstore.empty()) {
166 close(bstore.front());
168 state_ = BufferList::OK;
172 bool BufferList::close(Buffer * buf)
177 // Trace back why we need to use buf->getUser here.
178 // Perhaps slight rewrite is in order? (Lgb)
181 buf->getUser()->insetUnlock();
183 if (!buf->paragraphs.empty() && !buf->isClean() && !quitting) {
185 if (buf->isUnnamed())
186 fname = OnlyFilename(buf->fileName());
188 fname = MakeDisplayPath(buf->fileName(), 50);
191 switch (Alert::askConfirmation(_("Changes in document:"),
193 _("Save document?"))) {
195 if (buf->isUnnamed())
196 reask = !WriteAs(current_view, buf);
197 else if (buf->save()) {
198 lastfiles->newFile(buf->fileName());
205 if (buf->isUnnamed()) {
206 removeAutosaveFile(buf->fileName());
221 vector<string> const BufferList::getFileNames() const
224 std::copy(bstore.begin(), bstore.end(),
225 lyx::back_inserter_fun(nvec, &Buffer::fileName));
230 Buffer * BufferList::first()
234 return bstore.front();
238 Buffer * BufferList::getBuffer(unsigned int choice)
240 if (choice >= bstore.size())
242 return bstore[choice];
246 int BufferList::unlockInset(UpdatableInset * inset)
250 BufferStorage::iterator it = bstore.begin();
251 BufferStorage::iterator end = bstore.end();
252 for (; it != end; ++it) {
254 && (*it)->getUser()->theLockingInset() == inset) {
255 (*it)->getUser()->insetUnlock();
263 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
265 BufferStorage::iterator it = bstore.begin();
266 BufferStorage::iterator end = bstore.end();
267 for (; it != end; ++it) {
268 if (!(*it)->isDepClean(mastertmpdir)) {
269 string writefile = mastertmpdir;
271 writefile += (*it)->getLatexName();
272 (*it)->makeLaTeXFile(writefile, mastertmpdir,
274 (*it)->markDepClean(mastertmpdir);
280 void BufferList::emergencyWriteAll()
282 for_each(bstore.begin(), bstore.end(),
283 boost::bind(&BufferList::emergencyWrite, this, _1));
287 void BufferList::emergencyWrite(Buffer * buf)
289 // assert(buf) // this is not good since C assert takes an int
290 // and a pointer is a long (JMarc)
291 assert(buf != 0); // use c assert to avoid a loop
294 // No need to save if the buffer has not changed.
298 string const doc = buf->isUnnamed()
299 ? OnlyFilename(buf->fileName()) : buf->fileName();
302 lyxerr << boost::format(_("LyX: Attempting to save document %1$s"))
306 lyxerr << _("LyX: Attempting to save document ") << doc << endl;
308 // We try to save three places:
310 // 1) Same place as document. Unless it is an unnamed doc.
311 if (!buf->isUnnamed()) {
312 string s = buf->fileName();
314 lyxerr << " " << s << endl;
315 if (buf->writeFile(s)) {
317 lyxerr << _(" Save seems successful. Phew.") << endl;
320 lyxerr << _(" Save failed! Trying...") << endl;
324 // 2) In HOME directory.
325 string s = AddName(GetEnvPath("HOME"), buf->fileName());
327 lyxerr << ' ' << s << endl;
328 if (buf->writeFile(s)) {
330 lyxerr << _(" Save seems successful. Phew.") << endl;
334 lyxerr << _(" Save failed! Trying...") << endl;
336 // 3) In "/tmp" directory.
337 // MakeAbsPath to prepend the current
338 // drive letter on OS/2
339 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
341 lyxerr << ' ' << s << endl;
342 if (buf->writeFile(s)) {
344 lyxerr << _(" Save seems successful. Phew.") << endl;
347 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
352 Buffer * BufferList::readFile(string const & s, bool ronly)
355 string e = OnlyPath(s);
357 // File information about normal file
358 FileInfo fileInfo2(s);
360 if (!fileInfo2.exist()) {
361 Alert::alert(_("Error!"), _("Cannot open file"),
366 Buffer * b = bstore.newBuffer(s, ronly);
368 // Check if emergency save file exists and is newer.
369 e += OnlyFilename(s) + ".emergency";
370 FileInfo fileInfoE(e);
372 bool use_emergency = false;
374 if (fileInfoE.exist() && fileInfo2.exist()) {
375 if (fileInfoE.getModificationTime()
376 > fileInfo2.getModificationTime()) {
377 if (Alert::askQuestion(_("An emergency save of this document exists!"),
378 MakeDisplayPath(s, 50),
379 _("Try to load that instead?"))) {
381 // the file is not saved if we load the
384 use_emergency = true;
386 // Here, we should delete the emergency save
392 if (!use_emergency) {
393 // Now check if autosave file is newer.
395 a += OnlyFilename(s);
397 FileInfo fileInfoA(a);
398 if (fileInfoA.exist() && fileInfo2.exist()) {
399 if (fileInfoA.getModificationTime()
400 > fileInfo2.getModificationTime()) {
401 if (Alert::askQuestion(_("Autosave file is newer."),
402 MakeDisplayPath(s, 50),
403 _("Load that one instead?"))) {
405 // the file is not saved if we load the
409 // Here, we should delete the autosave
415 // not sure if this is the correct place to begin LyXLex
418 if (b->readFile(lex, ts))
427 bool BufferList::exists(string const & s) const
429 return find_if(bstore.begin(), bstore.end(),
430 lyx::compare_memfun(&Buffer::fileName, s))
435 bool BufferList::isLoaded(Buffer const * b) const
439 BufferStorage::const_iterator cit =
440 find(bstore.begin(), bstore.end(), b);
441 return cit != bstore.end();
445 Buffer * BufferList::getBuffer(string const & s)
447 BufferStorage::iterator it =
448 find_if(bstore.begin(), bstore.end(),
449 lyx::compare_memfun(&Buffer::fileName, s));
450 return it != bstore.end() ? (*it) : 0;
454 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
457 Buffer * b = bstore.newBuffer(name);
459 // use defaults.lyx as a default template if it exists.
461 tname = LibFileSearch("templates", "defaults.lyx");
463 if (!tname.empty()) {
464 bool templateok = false;
468 if (b->readFile(lex, tname)) {
473 Alert::alert(_("Error!"), _("Unable to open template"),
474 MakeDisplayPath(tname));
475 // no template, start with empty buffer
476 b->paragraphs.set(new Paragraph);
477 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
479 } else { // start with empty buffer
480 b->paragraphs.set(new Paragraph);
481 b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
486 b->setFileName(name);
489 b->setReadonly(false);
495 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
497 // get absolute path of file and add ".lyx" to the filename if
499 string s = FileSearch(string(), filename, "lyx");
504 // file already open?
506 if (Alert::askQuestion(_("Document is already open:"),
507 MakeDisplayPath(s, 50),
508 _("Do you want to reload that document?"))) {
509 // Reload is accomplished by closing and then loading
510 if (!close(getBuffer(s))) {
513 // Fall through to new load. (Asger)
515 // Here, we pretend that we just loaded the
522 switch (IsFileWriteable(s)) {
529 b->lyxvc.file_found_hook(s);
531 break; //fine- it's r/w
533 // Here we probably should run
534 if (LyXVC::file_not_found_hook(s)) {
535 // Ask if the file should be checked out for
536 // viewing/editing, if so: load it.
537 if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
538 // How can we know _how_ to do the checkout?
539 // With the current VC support it has to be,
540 // a RCS file since CVS do not have special ,v files.
542 return loadLyXFile(filename, tolastfiles);
545 if (Alert::askQuestion(_("Cannot open specified file:"),
546 MakeDisplayPath(s, 50),
547 _("Create new document with this name?")))
549 // Find a free buffer
550 b = newFile(s, string(), true);
555 if (b && tolastfiles)
556 lastfiles->newFile(b->fileName());
562 void BufferList::setCurrentAuthor(string const & name, string const & email)
564 BufferStorage::iterator it = bstore.begin();
565 BufferStorage::iterator end = bstore.end();
566 for (; it != end; ++it) {
567 (*it)->authors().record(0, Author(name, email));