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 * ======================================================
18 #pragma implementation
21 #include "bufferlist.h"
23 #include "lastfiles.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
32 #include "vc-backend.h"
33 #include "TextCache.h"
35 #include "frontends/Alert.h"
37 #include "support/FileInfo.h"
38 #include "support/filetools.h"
39 #include "support/lyxmanip.h"
40 #include "support/lyxfunctional.h"
41 #include "support/LAssert.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)->isLyxClean()) {
145 if ((*it)->isUnnamed())
146 fname = OnlyFilename((*it)->fileName());
148 fname = MakeDisplayPath((*it)->fileName(), 50);
149 if (!qwriteOne(*it, fname, unsaved)) // cancel the request!
154 if (!unsaved.empty() && lyxrc.exit_confirmation) {
155 return Alert::askQuestion(_("Some documents were not saved:"),
156 unsaved, _("Exit anyway?"));
163 void BufferList::closeAll()
165 state_ = BufferList::CLOSING;
166 // Since we are closing we can just as well delete all
167 // in the textcache this will also speed the closing/quiting up a bit.
170 while (!bstore.empty()) {
171 close(bstore.front());
173 state_ = BufferList::OK;
177 bool BufferList::close(Buffer * buf)
182 // Trace back why we need to use buf->getUser here.
183 // Perhaps slight rewrite is in order? (Lgb)
186 buf->getUser()->insetUnlock();
188 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
190 buf->getUser()->owner()->prohibitInput();
192 if (buf->isUnnamed())
193 fname = OnlyFilename(buf->fileName());
195 fname = MakeDisplayPath(buf->fileName(), 50);
198 switch (Alert::askConfirmation(_("Changes in document:"),
200 _("Save document?"))) {
202 if (buf->isUnnamed())
203 reask = !WriteAs(current_view, buf);
204 else if (buf->save()) {
205 lastfiles->newFile(buf->fileName());
209 buf->getUser()->owner()->allowInput();
214 if (buf->isUnnamed()) {
215 removeAutosaveFile(buf->fileName());
221 buf->getUser()->owner()->allowInput();
226 buf->getUser()->owner()->allowInput();
234 vector<string> const BufferList::getFileNames() const
237 std::copy(bstore.begin(), bstore.end(),
238 lyx::back_inserter_fun(nvec, &Buffer::fileName));
243 Buffer * BufferList::first()
245 if (bstore.empty()) return 0;
246 return bstore.front();
250 Buffer * BufferList::getBuffer(unsigned int choice)
252 if (choice >= bstore.size()) return 0;
253 return bstore[choice];
257 int BufferList::unlockInset(UpdatableInset * inset)
261 BufferStorage::iterator it = bstore.begin();
262 BufferStorage::iterator end = bstore.end();
263 for (; it != end; ++it) {
265 && (*it)->getUser()->theLockingInset() == inset) {
266 (*it)->getUser()->insetUnlock();
274 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
276 BufferStorage::iterator it = bstore.begin();
277 BufferStorage::iterator end = bstore.end();
278 for (; it != end; ++it) {
279 if (!(*it)->isDepClean(mastertmpdir)) {
280 string writefile = mastertmpdir;
282 writefile += (*it)->getLatexName();
283 (*it)->makeLaTeXFile(writefile, mastertmpdir,
285 (*it)->markDepClean(mastertmpdir);
291 void BufferList::emergencyWriteAll()
293 for_each(bstore.begin(), bstore.end(),
294 lyx::class_fun(*this, &BufferList::emergencyWrite));
298 void BufferList::emergencyWrite(Buffer * buf)
300 // assert(buf) // this is not good since C assert takes an int
301 // and a pointer is a long (JMarc)
302 assert(buf != 0); // use c assert to avoid a loop
305 // No need to save if the buffer has not changed.
306 if (buf->isLyxClean())
309 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
310 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
311 : buf->fileName().c_str()) << endl;
313 // We try to save three places:
315 // 1) Same place as document. Unless it is an unnamed doc.
316 if (!buf->isUnnamed()) {
317 string s = buf->fileName();
319 lyxerr << " " << s << endl;
320 if (buf->writeFile(s, true)) {
322 lyxerr << _(" Save seems successful. Phew.") << endl;
325 lyxerr << _(" Save failed! Trying...") << endl;
329 // 2) In HOME directory.
330 string s = AddName(GetEnvPath("HOME"), buf->fileName());
332 lyxerr << " " << s << endl;
333 if (buf->writeFile(s, true)) {
335 lyxerr << _(" Save seems successful. Phew.") << endl;
339 lyxerr << _(" Save failed! Trying...") << endl;
341 // 3) In "/tmp" directory.
342 // MakeAbsPath to prepend the current
343 // drive letter on OS/2
344 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
346 lyxerr << " " << s << endl;
347 if (buf->writeFile(s, true)) {
349 lyxerr << _(" Save seems successful. Phew.") << endl;
352 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
357 Buffer * BufferList::readFile(string const & s, bool ronly)
360 string e = OnlyPath(s);
362 // File information about normal file
363 FileInfo fileInfo2(s);
365 if (!fileInfo2.exist()) {
366 Alert::alert(_("Error!"), _("Cannot open file"),
371 Buffer * b = bstore.newBuffer(s, ronly);
373 // Check if emergency save file exists and is newer.
374 e += OnlyFilename(s) + ".emergency";
375 FileInfo fileInfoE(e);
377 bool use_emergency = false;
379 if (fileInfoE.exist() && fileInfo2.exist()) {
380 if (fileInfoE.getModificationTime()
381 > fileInfo2.getModificationTime()) {
382 if (Alert::askQuestion(_("An emergency save of this document exists!"),
383 MakeDisplayPath(s, 50),
384 _("Try to load that instead?"))) {
386 // the file is not saved if we load the
389 use_emergency = true;
391 // Here, we should delete the emergency save
397 if (!use_emergency) {
398 // Now check if autosave file is newer.
400 a += OnlyFilename(s);
402 FileInfo fileInfoA(a);
403 if (fileInfoA.exist() && fileInfo2.exist()) {
404 if (fileInfoA.getModificationTime()
405 > fileInfo2.getModificationTime()) {
406 if (Alert::askQuestion(_("Autosave file is newer."),
407 MakeDisplayPath(s, 50),
408 _("Load that one instead?"))) {
410 // the file is not saved if we load the
414 // Here, we should delete the autosave
420 // not sure if this is the correct place to begin LyXLex
423 if (b->readFile(lex))
432 bool BufferList::exists(string const & s) const
434 return find_if(bstore.begin(), bstore.end(),
435 lyx::compare_memfun(&Buffer::fileName, s))
440 bool BufferList::isLoaded(Buffer const * b) const
444 BufferStorage::const_iterator cit =
445 find(bstore.begin(), bstore.end(), b);
446 return cit != bstore.end();
450 Buffer * BufferList::getBuffer(string const & s)
452 BufferStorage::iterator it =
453 find_if(bstore.begin(), bstore.end(),
454 lyx::compare_memfun(&Buffer::fileName, s));
455 return it != bstore.end() ? (*it) : 0;
459 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
462 Buffer * b = bstore.newBuffer(name);
464 // use defaults.lyx as a default template if it exists.
466 tname = LibFileSearch("templates", "defaults.lyx");
468 if (!tname.empty()) {
469 bool templateok = false;
473 if (b->readFile(lex)) {
478 Alert::alert(_("Error!"), _("Unable to open template"),
479 MakeDisplayPath(tname));
480 // no template, start with empty buffer
481 b->paragraph = new Paragraph;
483 } else { // start with empty buffer
484 b->paragraph = new Paragraph;
487 if (!lyxrc.new_ask_filename && !isNamed) {
489 b->setFileName(name);
492 b->setReadonly(false);
498 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
500 // get absolute path of file and add ".lyx" to the filename if
502 string s = FileSearch(string(), filename, "lyx");
507 // file already open?
509 if (Alert::askQuestion(_("Document is already open:"),
510 MakeDisplayPath(s, 50),
511 _("Do you want to reload that document?"))) {
512 // Reload is accomplished by closing and then loading
513 if (!close(getBuffer(s))) {
516 // Fall through to new load. (Asger)
518 // Here, we pretend that we just loaded the
525 switch (IsFileWriteable(s)) {
532 b->lyxvc.file_found_hook(s);
534 break; //fine- it's r/w
536 // Here we probably should run
537 if (LyXVC::file_not_found_hook(s)) {
538 // Ask if the file should be checked out for
539 // viewing/editing, if so: load it.
540 if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
541 // How can we know _how_ to do the checkout?
542 // With the current VC support it has to be,
543 // a RCS file since CVS do not have special ,v files.
545 return loadLyXFile(filename, tolastfiles);
548 if (Alert::askQuestion(_("Cannot open specified file:"),
549 MakeDisplayPath(s, 50),
550 _("Create new document with this name?")))
552 // Find a free buffer
553 b = newFile(s, string(), true);
558 if (b && tolastfiles)
559 lastfiles->newFile(b->fileName());