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 "lyx_gui_misc.h"
24 #include "lastfiles.h"
29 #include "bufferview_funcs.h"
30 #include "BufferView.h"
33 #include "vc-backend.h"
34 #include "TextCache.h"
36 #include "frontends/Alert.h"
38 #include "support/FileInfo.h"
39 #include "support/filetools.h"
40 #include "support/lyxmanip.h"
41 #include "support/lyxfunctional.h"
42 #include "support/LAssert.h"
56 extern BufferView * current_view;
59 // Class BufferStorage
62 void BufferStorage::release(Buffer * buf)
65 Container::iterator it = find(container.begin(), container.end(), buf);
66 if (it != container.end()) {
67 // Make sure that we don't store a LyXText in
68 // the textcache that points to the buffer
72 textcache.removeAllWithBuffer(tmp);
78 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
80 Buffer * tmpbuf = new Buffer(s, ronly);
81 tmpbuf->params.useClassDefaults();
82 lyxerr[Debug::INFO] << "Assigning to buffer "
83 << container.size() << endl;
84 container.push_back(tmpbuf);
93 BufferList::BufferList()
94 : state_(BufferList::OK)
98 bool BufferList::empty() const
100 return bstore.empty();
104 bool BufferList::qwriteOne(Buffer * buf, string const & fname, 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 for (BufferStorage::iterator it = bstore.begin();
141 it != bstore.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)
185 if (buf->getUser()) buf->getUser()->insetUnlock();
186 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
188 buf->getUser()->owner()->prohibitInput();
190 if (buf->isUnnamed())
191 fname = OnlyFilename(buf->fileName());
193 fname = MakeDisplayPath(buf->fileName(), 50);
196 switch (Alert::askConfirmation(_("Changes in document:"),
198 _("Save document?"))){
200 if (buf->isUnnamed())
201 reask = !WriteAs(current_view, buf);
202 else if (buf->save()) {
203 lastfiles->newFile(buf->fileName());
207 buf->getUser()->owner()->allowInput();
212 if (buf->isUnnamed()) {
213 removeAutosaveFile(buf->fileName());
219 buf->getUser()->owner()->allowInput();
224 buf->getUser()->owner()->allowInput();
232 vector<string> const BufferList::getFileNames() const
235 std::copy(bstore.begin(), bstore.end(),
236 lyx::back_inserter_fun(nvec, &Buffer::fileName));
241 Buffer * BufferList::first()
243 if (bstore.empty()) return 0;
244 return bstore.front();
248 Buffer * BufferList::getBuffer(unsigned int choice)
250 if (choice >= bstore.size()) return 0;
251 return bstore[choice];
255 int BufferList::unlockInset(UpdatableInset * inset)
259 //if (!inset) return 1;
260 for (BufferStorage::iterator it = bstore.begin();
261 it != bstore.end(); ++it) {
263 && (*it)->getUser()->theLockingInset() == inset) {
264 (*it)->getUser()->insetUnlock();
272 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
274 for (BufferStorage::iterator it = bstore.begin();
275 it != bstore.end(); ++it) {
276 if (!(*it)->isDepClean(mastertmpdir)) {
277 string writefile = mastertmpdir;
279 writefile += (*it)->getLatexName();
280 (*it)->makeLaTeXFile(writefile, mastertmpdir,
282 (*it)->markDepClean(mastertmpdir);
288 void BufferList::emergencyWriteAll()
290 for_each(bstore.begin(), bstore.end(),
291 lyx::class_fun(*this, &BufferList::emergencyWrite));
295 void BufferList::emergencyWrite(Buffer * buf)
297 // assert(buf) // this is not good since C assert takes an int
298 // and a pointer is a long (JMarc)
299 assert(buf != 0); // use c assert to avoid a loop
302 // No need to save if the buffer has not changed.
303 if (buf->isLyxClean()) return;
305 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
306 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
307 : buf->fileName().c_str()) << endl;
309 // We try to save three places:
311 // 1) Same place as document. Unless it is an unnamed doc.
312 if (!buf->isUnnamed()) {
313 string s = buf->fileName();
315 lyxerr << " " << s << endl;
316 if (buf->writeFile(s, true)) {
318 lyxerr << _(" Save seems successful. Phew.") << endl;
321 lyxerr << _(" Save failed! Trying...") << endl;
325 // 2) In HOME directory.
326 string s = AddName(GetEnvPath("HOME"), buf->fileName());
328 lyxerr << " " << s << endl;
329 if (buf->writeFile(s, true)) {
331 lyxerr << _(" Save seems successful. Phew.") << endl;
335 lyxerr << _(" Save failed! Trying...") << endl;
337 // 3) In "/tmp" directory.
338 // MakeAbsPath to prepend the current
339 // drive letter on OS/2
340 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
342 lyxerr << " " << s << endl;
343 if (buf->writeFile(s, true)) {
345 lyxerr << _(" Save seems successful. Phew.") << endl;
348 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
353 Buffer * BufferList::readFile(string const & s, bool ronly)
355 Buffer * b = bstore.newBuffer(s, ronly);
358 string e = OnlyPath(s);
360 // File information about normal file
361 FileInfo fileInfo2(s);
363 if (!fileInfo2.exist()) {
364 Alert::alert(_("Error!"), _("Cannot open file"),
369 // Check if emergency save file exists and is newer.
370 e += OnlyFilename(s) + ".emergency";
371 FileInfo fileInfoE(e);
373 bool use_emergency = false;
375 if (fileInfoE.exist() && fileInfo2.exist()) {
376 if (fileInfoE.getModificationTime()
377 > fileInfo2.getModificationTime()) {
378 if (Alert::askQuestion(_("An emergency save of this document exists!"),
379 MakeDisplayPath(s, 50),
380 _("Try to load that instead?"))) {
382 // the file is not saved if we load the
385 use_emergency = true;
387 // Here, we should delete the emergency save
393 if (!use_emergency) {
394 // Now check if autosave file is newer.
396 a += OnlyFilename(s);
398 FileInfo fileInfoA(a);
399 if (fileInfoA.exist() && fileInfo2.exist()) {
400 if (fileInfoA.getModificationTime()
401 > fileInfo2.getModificationTime()) {
402 if (Alert::askQuestion(_("Autosave file is newer."),
403 MakeDisplayPath(s, 50),
404 _("Load that one instead?"))) {
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))
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 = bstore.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)) {
474 Alert::alert(_("Error!"), _("Unable to open template"),
475 MakeDisplayPath(tname));
476 // no template, start with empty buffer
477 b->paragraph = new Paragraph;
479 } else { // start with empty buffer
480 b->paragraph = new Paragraph;
483 if (!lyxrc.new_ask_filename && !isNamed) {
485 b->setFileName(name);
488 b->setReadonly(false);
494 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
496 // get absolute path of file and add ".lyx" to the filename if
498 string s = FileSearch(string(), filename, "lyx");
503 // file already open?
505 if (Alert::askQuestion(_("Document is already open:"),
506 MakeDisplayPath(s, 50),
507 _("Do you want to reload that document?"))) {
508 // Reload is accomplished by closing and then loading
509 if (!close(getBuffer(s))) {
512 // Fall through to new load. (Asger)
514 // Here, we pretend that we just loaded the
521 switch (IsFileWriteable(s)) {
528 b->lyxvc.file_found_hook(s);
530 break; //fine- it's r/w
532 // Here we probably should run
533 if (LyXVC::file_not_found_hook(s)) {
534 // Ask if the file should be checked out for
535 // viewing/editing, if so: load it.
536 if (Alert::askQuestion(_("Do you want to retrieve file under version control?"))) {
537 // How can we know _how_ to do the checkout?
538 // With the current VC support it has to be,
539 // a RCS file since CVS do not have special ,v files.
541 return loadLyXFile(filename, tolastfiles);
544 if (Alert::askQuestion(_("Cannot open specified file:"),
545 MakeDisplayPath(s, 50),
546 _("Create new document with this name?")))
548 // Find a free buffer
549 b = newFile(s, string(), true);
554 if (b && tolastfiles)
555 lastfiles->newFile(b->fileName());