1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Word Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * This file is Copyright 1996-2000
12 * ======================================================
16 #pragma implementation
24 #include "bufferlist.h"
26 #include "minibuffer.h"
27 #include "support/FileInfo.h"
28 #include "support/filetools.h"
29 #include "support/lyxmanip.h"
30 #include "support/lyxfunctional.h"
31 #include "lyx_gui_misc.h"
32 #include "lastfiles.h"
37 #include "bufferview_funcs.h"
40 #include "vc-backend.h"
41 #include "TextCache.h"
48 // Class BufferStorage
51 void BufferStorage::release(Buffer * buf)
53 Container::iterator it = find(container.begin(), container.end(), buf);
54 if (it != container.end()) {
55 // Make sure that we don't store a LyXText in
56 // the textcache that points to the buffer
60 textcache.removeAllWithBuffer(tmp);
66 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
68 Buffer * tmpbuf = new Buffer(s, ronly);
69 tmpbuf->params.useClassDefaults();
70 lyxerr.debug() << "Assigning to buffer "
71 << container.size() << endl;
72 container.push_back(tmpbuf);
81 BufferList::BufferList()
82 : state_(BufferList::OK)
86 bool BufferList::empty() const
88 return bstore.empty();
92 extern bool MenuWrite(Buffer *);
93 extern bool MenuWriteAs(Buffer *);
95 bool BufferList::QwriteAll()
97 bool askMoreConfirmation = false;
99 for (BufferStorage::iterator it = bstore.begin();
100 it != bstore.end(); ++it) {
101 if (!(*it)->isLyxClean()) {
103 if ((*it)->isUnnamed())
104 fname = OnlyFilename((*it)->fileName());
106 fname = MakeDisplayPath((*it)->fileName(), 50);
109 switch(AskConfirmation(_("Changes in document:"),
111 _("Save document?"))) {
113 if ((*it)->isUnnamed())
114 reask = !MenuWriteAs((*it));
116 reask = !MenuWrite((*it));
120 // if we crash after this we could
121 // have no autosave file but I guess
122 // this is really inprobable (Jug)
123 if ((*it)->isUnnamed()) {
124 removeAutosaveFile((*it)->fileName());
126 askMoreConfirmation = true;
127 unsaved += MakeDisplayPath(fname, 50);
137 if (askMoreConfirmation &&
138 lyxrc.exit_confirmation &&
139 !AskQuestion(_("Some documents were not saved:"),
140 unsaved, _("Exit anyway?"))) {
148 void BufferList::closeAll()
150 state_ = BufferList::CLOSING;
151 // Since we are closing we can just as well delete all
152 // in the textcache this will also speed the closing/quiting up a bit.
155 while (!bstore.empty()) {
156 close(bstore.front());
158 state_ = BufferList::OK;
162 void BufferList::resize()
164 for_each(bstore.begin(), bstore.end(), mem_fun(&Buffer::resize));
168 bool BufferList::close(Buffer * buf)
171 // Trace back why we need to use buf->getUser here.
172 // Perhaps slight rewrite is in order? (Lgb)
174 if (buf->getUser()) buf->getUser()->insetUnlock();
175 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
177 ProhibitInput(buf->getUser());
179 if (buf->isUnnamed())
180 fname = OnlyFilename(buf->fileName());
182 fname = MakeDisplayPath(buf->fileName(), 50);
185 switch(AskConfirmation(_("Changes in document:"),
187 _("Save document?"))){
189 if (buf->isUnnamed())
190 reask = !MenuWriteAs(buf);
191 else if (buf->save()) {
192 lastfiles->newFile(buf->fileName());
196 AllowInput(buf->getUser());
201 if (buf->isUnnamed()) {
202 removeAutosaveFile(buf->fileName());
208 AllowInput(buf->getUser());
213 AllowInput(buf->getUser());
221 vector<string> const BufferList::getFileNames() const
224 std::copy(bstore.begin(), bstore.end(),
225 back_inserter_fun(nvec, &Buffer::fileName));
230 Buffer * BufferList::first()
232 if (bstore.empty()) return 0;
233 return bstore.front();
237 Buffer * BufferList::getBuffer(unsigned int choice)
239 if (choice >= bstore.size()) return 0;
240 return bstore[choice];
244 int BufferList::unlockInset(UpdatableInset * inset)
246 if (!inset) return 1;
247 for (BufferStorage::iterator it = bstore.begin();
248 it != bstore.end(); ++it) {
250 && (*it)->getUser()->theLockingInset() == inset) {
251 (*it)->getUser()->insetUnlock();
259 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
261 for (BufferStorage::iterator it = bstore.begin();
262 it != bstore.end(); ++it) {
263 if (!(*it)->isDepClean(mastertmpdir)) {
264 string writefile = mastertmpdir;
266 writefile += (*it)->getLatexName();
267 (*it)->makeLaTeXFile(writefile, mastertmpdir,
269 (*it)->markDepClean(mastertmpdir);
275 void BufferList::emergencyWriteAll()
277 for_each(bstore.begin(), bstore.end(),
278 class_fun(*this, &BufferList::emergencyWrite));
282 void BufferList::emergencyWrite(Buffer * buf)
284 // No need to save if the buffer has not changed.
285 if (buf->isLyxClean()) return;
287 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
288 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
289 : buf->fileName().c_str()) << endl;
291 //if (buf->isUnnamed())
292 // lyxerr << OnlyFilename(buf->fileName());
294 // lyxerr << buf->fileName();
295 //lyxerr << _(" as...") << endl;
297 // Let's unroll this loop (Lgb)
301 for (int i = 0; i < 3 && !madeit; ++i) {
304 // We try to save three places:
305 // 1) Same place as document.
306 // 2) In HOME directory.
307 // 3) In "/tmp" directory.
309 if (buf->isUnnamed())
313 s = AddName(GetEnvPath("HOME"), buf->fileName());
315 // MakeAbsPath to prepend the current
316 // drive letter on OS/2
317 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
321 lyxerr << " " << i + 1 << ") " << s << endl;
323 if (buf->writeFile(s, true)) {
325 lyxerr << _(" Save seems successful. Phew.") << endl;
328 lyxerr << _(" Save failed! Trying...") << endl;
330 lyxerr << _(" Save failed! Bummer. Document is lost.")
335 // We try to save three places:
337 // 1) Same place as document. Unless it is an unnamed doc.
338 if (!buf->isUnnamed()) {
339 string s = buf->fileName();
341 lyxerr << " " << s << endl;
342 if (buf->writeFile(s, true)) {
344 lyxerr << _(" Save seems successful. Phew.") << endl;
347 lyxerr << _(" Save failed! Trying...") << endl;
351 // 2) In HOME directory.
352 string s = AddName(GetEnvPath("HOME"), buf->fileName());
354 lyxerr << " " << s << endl;
355 if (buf->writeFile(s, true)) {
357 lyxerr << _(" Save seems successful. Phew.") << endl;
361 lyxerr << _(" Save failed! Trying...") << endl;
363 // 3) In "/tmp" directory.
364 // MakeAbsPath to prepend the current
365 // drive letter on OS/2
366 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
368 lyxerr << " " << s << endl;
369 if (buf->writeFile(s, true)) {
371 lyxerr << _(" Save seems successful. Phew.") << endl;
374 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
380 Buffer * BufferList::readFile(string const & s, bool ronly)
382 Buffer * b = bstore.newBuffer(s, ronly);
385 string e = OnlyPath(s);
387 // File information about normal file
388 FileInfo fileInfo2(s);
390 // Check if emergency save file exists and is newer.
391 e += OnlyFilename(s) + ".emergency";
392 FileInfo fileInfoE(e);
394 bool use_emergency = false;
396 if (fileInfoE.exist() && fileInfo2.exist()) {
397 if (fileInfoE.getModificationTime()
398 > fileInfo2.getModificationTime()) {
399 if (AskQuestion(_("An emergency save of this document exists!"),
400 MakeDisplayPath(s, 50),
401 _("Try to load that instead?"))) {
403 // the file is not saved if we load the
406 use_emergency = true;
408 // Here, we should delete the emergency save
414 if (!use_emergency) {
415 // Now check if autosave file is newer.
417 a += OnlyFilename(s);
419 FileInfo fileInfoA(a);
420 if (fileInfoA.exist() && fileInfo2.exist()) {
421 if (fileInfoA.getModificationTime()
422 > fileInfo2.getModificationTime()) {
423 if (AskQuestion(_("Autosave file is newer."),
424 MakeDisplayPath(s, 50),
425 _("Load that one instead?"))) {
427 // the file is not saved if we load the
431 // Here, we should delete the autosave
437 // not sure if this is the correct place to begin LyXLex
440 if (b->readFile(lex))
449 bool BufferList::exists(string const & s) const
451 return find_if(bstore.begin(), bstore.end(),
452 compare_memfun(&Buffer::fileName, s)) != bstore.end();
456 bool BufferList::isLoaded(Buffer const * b) const
458 BufferStorage::const_iterator cit =
459 find(bstore.begin(), bstore.end(), b);
460 return cit != bstore.end();
464 Buffer * BufferList::getBuffer(string const & s)
466 BufferStorage::iterator it =
467 find_if(bstore.begin(), bstore.end(),
468 compare_memfun(&Buffer::fileName, s));
469 return it != bstore.end() ? (*it) : 0;
473 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
476 Buffer * b = bstore.newBuffer(name);
478 // use defaults.lyx as a default template if it exists.
480 tname = LibFileSearch("templates", "defaults.lyx");
482 if (!tname.empty() && IsLyXFilename(tname)) {
483 bool templateok = false;
487 if (b->readFile(lex)) {
492 WriteAlert(_("Error!"), _("Unable to open template"),
493 MakeDisplayPath(tname));
494 // no template, start with empty buffer
495 b->paragraph = new LyXParagraph;
497 } else { // start with empty buffer
498 b->paragraph = new LyXParagraph;
501 if (!lyxrc.new_ask_filename) {
505 b->setReadonly(false);
511 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
513 // make sure our path is absolute
514 string const s = MakeAbsPath(filename);
516 // file already open?
518 if (AskQuestion(_("Document is already open:"),
519 MakeDisplayPath(s, 50),
520 _("Do you want to reload that document?"))) {
521 // Reload is accomplished by closing and then loading
522 if (!close(getBuffer(s))) {
525 // Fall through to new load. (Asger)
527 // Here, we pretend that we just loaded the
534 switch (IsFileWriteable(s)) {
537 current_view->owner()->getMiniBuffer()->
538 Set(_("File `") + MakeDisplayPath(s, 50) +
539 _("' is read-only."));
546 b->lyxvc.file_found_hook(s);
548 break; //fine- it's r/w
550 // Here we probably should run
551 if (LyXVC::file_not_found_hook(s)) {
552 // Ask if the file should be checked out for
553 // viewing/editing, if so: load it.
554 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
555 // How can we know _how_ to do the checkout?
556 // With the current VC support it has to be,
557 // a RCS file since CVS do not have special ,v files.
559 return loadLyXFile(filename, tolastfiles);
562 if (AskQuestion(_("Cannot open specified file:"),
563 MakeDisplayPath(s, 50),
564 _("Create new document with this name?")))
566 // Find a free buffer
567 b = newFile(s, string(), true);
572 if (b && tolastfiles)
573 lastfiles->newFile(b->fileName());