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"
51 // Class BufferStorage
54 void BufferStorage::release(Buffer * buf)
56 Container::iterator it = find(container.begin(), container.end(), buf);
57 if (it != container.end()) {
58 // Make sure that we don't store a LyXText in
59 // the textcache that points to the buffer
63 textcache.removeAllWithBuffer(tmp);
69 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
71 Buffer * tmpbuf = new Buffer(s, ronly);
72 tmpbuf->params.useClassDefaults();
73 lyxerr.debug() << "Assigning to buffer "
74 << container.size() << endl;
75 container.push_back(tmpbuf);
84 BufferList::BufferList()
85 : state_(BufferList::OK)
89 bool BufferList::empty() const
91 return bstore.empty();
95 extern bool MenuWrite(Buffer *);
96 extern bool MenuWriteAs(Buffer *);
98 bool BufferList::QwriteAll()
100 bool askMoreConfirmation = false;
102 for (BufferStorage::iterator it = bstore.begin();
103 it != bstore.end(); ++it) {
104 if (!(*it)->isLyxClean()) {
106 if ((*it)->isUnnamed())
107 fname = OnlyFilename((*it)->fileName());
109 fname = MakeDisplayPath((*it)->fileName(), 50);
112 switch(AskConfirmation(_("Changes in document:"),
114 _("Save document?"))) {
116 if ((*it)->isUnnamed())
117 reask = !MenuWriteAs((*it));
119 reask = !MenuWrite((*it));
123 // if we crash after this we could
124 // have no autosave file but I guess
125 // this is really inprobable (Jug)
126 if ((*it)->isUnnamed()) {
127 removeAutosaveFile((*it)->fileName());
129 askMoreConfirmation = true;
130 unsaved += MakeDisplayPath(fname, 50);
140 if (askMoreConfirmation &&
141 lyxrc.exit_confirmation &&
142 !AskQuestion(_("Some documents were not saved:"),
143 unsaved, _("Exit anyway?"))) {
151 void BufferList::closeAll()
153 state_ = BufferList::CLOSING;
154 // Since we are closing we can just as well delete all
155 // in the textcache this will also speed the closing/quiting up a bit.
158 while (!bstore.empty()) {
159 close(bstore.front());
161 state_ = BufferList::OK;
165 void BufferList::resize()
167 for_each(bstore.begin(), bstore.end(), mem_fun(&Buffer::resize));
171 bool BufferList::close(Buffer * buf)
174 // Trace back why we need to use buf->getUser here.
175 // Perhaps slight rewrite is in order? (Lgb)
177 if (buf->getUser()) buf->getUser()->insetUnlock();
178 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
180 ProhibitInput(buf->getUser());
182 if (buf->isUnnamed())
183 fname = OnlyFilename(buf->fileName());
185 fname = MakeDisplayPath(buf->fileName(), 50);
188 switch(AskConfirmation(_("Changes in document:"),
190 _("Save document?"))){
192 if (buf->isUnnamed())
193 reask = !MenuWriteAs(buf);
194 else if (buf->save()) {
195 lastfiles->newFile(buf->fileName());
199 AllowInput(buf->getUser());
204 if (buf->isUnnamed()) {
205 removeAutosaveFile(buf->fileName());
211 AllowInput(buf->getUser());
216 AllowInput(buf->getUser());
224 vector<string> const BufferList::getFileNames() const
227 std::copy(bstore.begin(), bstore.end(),
228 back_inserter_fun(nvec, &Buffer::fileName));
233 Buffer * BufferList::first()
235 if (bstore.empty()) return 0;
236 return bstore.front();
240 Buffer * BufferList::getBuffer(unsigned int choice)
242 if (choice >= bstore.size()) return 0;
243 return bstore[choice];
247 int BufferList::unlockInset(UpdatableInset * inset)
249 if (!inset) return 1;
250 for (BufferStorage::iterator it = bstore.begin();
251 it != bstore.end(); ++it) {
253 && (*it)->getUser()->theLockingInset() == inset) {
254 (*it)->getUser()->insetUnlock();
262 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
264 for (BufferStorage::iterator it = bstore.begin();
265 it != bstore.end(); ++it) {
266 if (!(*it)->isDepClean(mastertmpdir)) {
267 string writefile = mastertmpdir;
269 writefile += (*it)->getLatexName();
270 (*it)->makeLaTeXFile(writefile, mastertmpdir,
272 (*it)->markDepClean(mastertmpdir);
278 void BufferList::emergencyWriteAll()
280 for_each(bstore.begin(), bstore.end(),
281 class_fun(*this, &BufferList::emergencyWrite));
285 void BufferList::emergencyWrite(Buffer * buf)
287 // No need to save if the buffer has not changed.
288 if (buf->isLyxClean()) return;
290 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
291 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
292 : buf->fileName().c_str()) << endl;
294 //if (buf->isUnnamed())
295 // lyxerr << OnlyFilename(buf->fileName());
297 // lyxerr << buf->fileName();
298 //lyxerr << _(" as...") << endl;
300 // Let's unroll this loop (Lgb)
304 for (int i = 0; i < 3 && !madeit; ++i) {
307 // We try to save three places:
308 // 1) Same place as document.
309 // 2) In HOME directory.
310 // 3) In "/tmp" directory.
312 if (buf->isUnnamed())
316 s = AddName(GetEnvPath("HOME"), buf->fileName());
318 // MakeAbsPath to prepend the current
319 // drive letter on OS/2
320 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
324 lyxerr << " " << i + 1 << ") " << s << endl;
326 if (buf->writeFile(s, true)) {
328 lyxerr << _(" Save seems successful. Phew.") << endl;
331 lyxerr << _(" Save failed! Trying...") << endl;
333 lyxerr << _(" Save failed! Bummer. Document is lost.")
338 // We try to save three places:
340 // 1) Same place as document. Unless it is an unnamed doc.
341 if (!buf->isUnnamed()) {
342 string s = buf->fileName();
344 lyxerr << " " << s << endl;
345 if (buf->writeFile(s, true)) {
347 lyxerr << _(" Save seems successful. Phew.") << endl;
350 lyxerr << _(" Save failed! Trying...") << endl;
354 // 2) In HOME directory.
355 string s = AddName(GetEnvPath("HOME"), buf->fileName());
357 lyxerr << " " << s << endl;
358 if (buf->writeFile(s, true)) {
360 lyxerr << _(" Save seems successful. Phew.") << endl;
364 lyxerr << _(" Save failed! Trying...") << endl;
366 // 3) In "/tmp" directory.
367 // MakeAbsPath to prepend the current
368 // drive letter on OS/2
369 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
371 lyxerr << " " << s << endl;
372 if (buf->writeFile(s, true)) {
374 lyxerr << _(" Save seems successful. Phew.") << endl;
377 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
383 Buffer * BufferList::readFile(string const & s, bool ronly)
385 Buffer * b = bstore.newBuffer(s, ronly);
388 string e = OnlyPath(s);
390 // File information about normal file
391 FileInfo fileInfo2(s);
393 // Check if emergency save file exists and is newer.
394 e += OnlyFilename(s) + ".emergency";
395 FileInfo fileInfoE(e);
397 bool use_emergency = false;
399 if (fileInfoE.exist() && fileInfo2.exist()) {
400 if (fileInfoE.getModificationTime()
401 > fileInfo2.getModificationTime()) {
402 if (AskQuestion(_("An emergency save of this document exists!"),
403 MakeDisplayPath(s, 50),
404 _("Try to load that instead?"))) {
406 // the file is not saved if we load the
409 use_emergency = true;
411 // Here, we should delete the emergency save
417 if (!use_emergency) {
418 // Now check if autosave file is newer.
420 a += OnlyFilename(s);
422 FileInfo fileInfoA(a);
423 if (fileInfoA.exist() && fileInfo2.exist()) {
424 if (fileInfoA.getModificationTime()
425 > fileInfo2.getModificationTime()) {
426 if (AskQuestion(_("Autosave file is newer."),
427 MakeDisplayPath(s, 50),
428 _("Load that one instead?"))) {
430 // the file is not saved if we load the
434 // Here, we should delete the autosave
440 // not sure if this is the correct place to begin LyXLex
443 if (b->readFile(lex))
452 bool BufferList::exists(string const & s) const
454 return find_if(bstore.begin(), bstore.end(),
455 compare_memfun(&Buffer::fileName, s)) != bstore.end();
459 bool BufferList::isLoaded(Buffer const * b) const
461 BufferStorage::const_iterator cit =
462 find(bstore.begin(), bstore.end(), b);
463 return cit != bstore.end();
467 Buffer * BufferList::getBuffer(string const & s)
469 BufferStorage::iterator it =
470 find_if(bstore.begin(), bstore.end(),
471 compare_memfun(&Buffer::fileName, s));
472 return it != bstore.end() ? (*it) : 0;
476 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
479 Buffer * b = bstore.newBuffer(name);
481 // use defaults.lyx as a default template if it exists.
483 tname = LibFileSearch("templates", "defaults.lyx");
485 if (!tname.empty() && IsLyXFilename(tname)) {
486 bool templateok = false;
490 if (b->readFile(lex)) {
495 WriteAlert(_("Error!"), _("Unable to open template"),
496 MakeDisplayPath(tname));
497 // no template, start with empty buffer
498 b->paragraph = new LyXParagraph;
500 } else { // start with empty buffer
501 b->paragraph = new LyXParagraph;
504 if (!lyxrc.new_ask_filename) {
508 b->setReadonly(false);
514 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
516 // make sure our path is absolute
517 string const s = MakeAbsPath(filename);
519 // file already open?
521 if (AskQuestion(_("Document is already open:"),
522 MakeDisplayPath(s, 50),
523 _("Do you want to reload that document?"))) {
524 // Reload is accomplished by closing and then loading
525 if (!close(getBuffer(s))) {
528 // Fall through to new load. (Asger)
530 // Here, we pretend that we just loaded the
537 switch (IsFileWriteable(s)) {
540 current_view->owner()->getMiniBuffer()->
541 Set(_("File `") + MakeDisplayPath(s, 50) +
542 _("' is read-only."));
549 b->lyxvc.file_found_hook(s);
551 break; //fine- it's r/w
553 // Here we probably should run
554 if (LyXVC::file_not_found_hook(s)) {
555 // Ask if the file should be checked out for
556 // viewing/editing, if so: load it.
557 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
558 // How can we know _how_ to do the checkout?
559 // With the current VC support it has to be,
560 // a RCS file since CVS do not have special ,v files.
562 return loadLyXFile(filename, tolastfiles);
565 if (AskQuestion(_("Cannot open specified file:"),
566 MakeDisplayPath(s, 50),
567 _("Create new document with this name?")))
569 // Find a free buffer
570 b = newFile(s, string(), true);
575 if (b && tolastfiles)
576 lastfiles->newFile(b->fileName());