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"
50 // Class BufferStorage
53 void BufferStorage::release(Buffer * buf)
55 Container::iterator it = find(container.begin(), container.end(), buf);
56 if (it != container.end()) {
57 // Make sure that we don't store a LyXText in
58 // the textcache that points to the buffer
62 textcache.removeAllWithBuffer(tmp);
68 Buffer * BufferStorage::newBuffer(string const & s, bool ronly)
70 Buffer * tmpbuf = new Buffer(s, ronly);
71 tmpbuf->params.useClassDefaults();
72 lyxerr.debug() << "Assigning to buffer "
73 << container.size() << endl;
74 container.push_back(tmpbuf);
83 BufferList::BufferList()
84 : state_(BufferList::OK)
88 bool BufferList::empty() const
90 return bstore.empty();
94 extern bool MenuWrite(Buffer *);
95 extern bool MenuWriteAs(Buffer *);
97 bool BufferList::QwriteAll()
99 bool askMoreConfirmation = false;
101 for (BufferStorage::iterator it = bstore.begin();
102 it != bstore.end(); ++it) {
103 if (!(*it)->isLyxClean()) {
105 if ((*it)->isUnnamed())
106 fname = OnlyFilename((*it)->fileName());
108 fname = MakeDisplayPath((*it)->fileName(), 50);
111 switch(AskConfirmation(_("Changes in document:"),
113 _("Save document?"))) {
115 if ((*it)->isUnnamed())
116 reask = !MenuWriteAs((*it));
118 reask = !MenuWrite((*it));
122 // if we crash after this we could
123 // have no autosave file but I guess
124 // this is really inprobable (Jug)
125 if ((*it)->isUnnamed()) {
126 removeAutosaveFile((*it)->fileName());
128 askMoreConfirmation = true;
129 unsaved += MakeDisplayPath(fname, 50);
139 if (askMoreConfirmation &&
140 lyxrc.exit_confirmation &&
141 !AskQuestion(_("Some documents were not saved:"),
142 unsaved, _("Exit anyway?"))) {
150 void BufferList::closeAll()
152 state_ = BufferList::CLOSING;
153 // Since we are closing we can just as well delete all
154 // in the textcache this will also speed the closing/quiting up a bit.
157 while (!bstore.empty()) {
158 close(bstore.front());
160 state_ = BufferList::OK;
164 void BufferList::resize()
166 for_each(bstore.begin(), bstore.end(), mem_fun(&Buffer::resize));
170 bool BufferList::close(Buffer * buf)
173 // Trace back why we need to use buf->getUser here.
174 // Perhaps slight rewrite is in order? (Lgb)
176 if (buf->getUser()) buf->getUser()->insetUnlock();
177 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
179 ProhibitInput(buf->getUser());
181 if (buf->isUnnamed())
182 fname = OnlyFilename(buf->fileName());
184 fname = MakeDisplayPath(buf->fileName(), 50);
187 switch(AskConfirmation(_("Changes in document:"),
189 _("Save document?"))){
191 if (buf->isUnnamed())
192 reask = !MenuWriteAs(buf);
193 else if (buf->save()) {
194 lastfiles->newFile(buf->fileName());
198 AllowInput(buf->getUser());
203 if (buf->isUnnamed()) {
204 removeAutosaveFile(buf->fileName());
210 AllowInput(buf->getUser());
215 AllowInput(buf->getUser());
223 vector<string> const BufferList::getFileNames() const
226 std::copy(bstore.begin(), bstore.end(),
227 back_inserter_fun(nvec, &Buffer::fileName));
232 Buffer * BufferList::first()
234 if (bstore.empty()) return 0;
235 return bstore.front();
239 Buffer * BufferList::getBuffer(unsigned int choice)
241 if (choice >= bstore.size()) return 0;
242 return bstore[choice];
246 int BufferList::unlockInset(UpdatableInset * inset)
248 if (!inset) return 1;
249 for (BufferStorage::iterator it = bstore.begin();
250 it != bstore.end(); ++it) {
252 && (*it)->getUser()->theLockingInset() == inset) {
253 (*it)->getUser()->insetUnlock();
261 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
263 for (BufferStorage::iterator it = bstore.begin();
264 it != bstore.end(); ++it) {
265 if (!(*it)->isDepClean(mastertmpdir)) {
266 string writefile = mastertmpdir;
268 writefile += (*it)->getLatexName();
269 (*it)->makeLaTeXFile(writefile, mastertmpdir,
271 (*it)->markDepClean(mastertmpdir);
277 void BufferList::emergencyWriteAll()
279 for_each(bstore.begin(), bstore.end(),
280 class_fun(*this, &BufferList::emergencyWrite));
284 void BufferList::emergencyWrite(Buffer * buf)
286 // No need to save if the buffer has not changed.
287 if (buf->isLyxClean()) return;
289 lyxerr << fmt(_("lyx: Attempting to save document %s as..."),
290 buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str()
291 : buf->fileName().c_str()) << endl;
293 //if (buf->isUnnamed())
294 // lyxerr << OnlyFilename(buf->fileName());
296 // lyxerr << buf->fileName();
297 //lyxerr << _(" as...") << endl;
299 // Let's unroll this loop (Lgb)
303 for (int i = 0; i < 3 && !madeit; ++i) {
306 // We try to save three places:
307 // 1) Same place as document.
308 // 2) In HOME directory.
309 // 3) In "/tmp" directory.
311 if (buf->isUnnamed())
315 s = AddName(GetEnvPath("HOME"), buf->fileName());
317 // MakeAbsPath to prepend the current
318 // drive letter on OS/2
319 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
323 lyxerr << " " << i + 1 << ") " << s << endl;
325 if (buf->writeFile(s, true)) {
327 lyxerr << _(" Save seems successful. Phew.") << endl;
330 lyxerr << _(" Save failed! Trying...") << endl;
332 lyxerr << _(" Save failed! Bummer. Document is lost.")
337 // We try to save three places:
339 // 1) Same place as document. Unless it is an unnamed doc.
340 if (!buf->isUnnamed()) {
341 string s = buf->fileName();
343 lyxerr << " " << s << endl;
344 if (buf->writeFile(s, true)) {
346 lyxerr << _(" Save seems successful. Phew.") << endl;
349 lyxerr << _(" Save failed! Trying...") << endl;
353 // 2) In HOME directory.
354 string s = AddName(GetEnvPath("HOME"), buf->fileName());
356 lyxerr << " " << s << endl;
357 if (buf->writeFile(s, true)) {
359 lyxerr << _(" Save seems successful. Phew.") << endl;
363 lyxerr << _(" Save failed! Trying...") << endl;
365 // 3) In "/tmp" directory.
366 // MakeAbsPath to prepend the current
367 // drive letter on OS/2
368 s = AddName(MakeAbsPath("/tmp/"), buf->fileName());
370 lyxerr << " " << s << endl;
371 if (buf->writeFile(s, true)) {
373 lyxerr << _(" Save seems successful. Phew.") << endl;
376 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
382 Buffer * BufferList::readFile(string const & s, bool ronly)
384 Buffer * b = bstore.newBuffer(s, ronly);
387 string e = OnlyPath(s);
389 // File information about normal file
390 FileInfo fileInfo2(s);
392 // Check if emergency save file exists and is newer.
393 e += OnlyFilename(s) + ".emergency";
394 FileInfo fileInfoE(e);
396 bool use_emergency = false;
398 if (fileInfoE.exist() && fileInfo2.exist()) {
399 if (fileInfoE.getModificationTime()
400 > fileInfo2.getModificationTime()) {
401 if (AskQuestion(_("An emergency save of this document exists!"),
402 MakeDisplayPath(s, 50),
403 _("Try to load that instead?"))) {
405 // the file is not saved if we load the
408 use_emergency = true;
410 // Here, we should delete the emergency save
416 if (!use_emergency) {
417 // Now check if autosave file is newer.
419 a += OnlyFilename(s);
421 FileInfo fileInfoA(a);
422 if (fileInfoA.exist() && fileInfo2.exist()) {
423 if (fileInfoA.getModificationTime()
424 > fileInfo2.getModificationTime()) {
425 if (AskQuestion(_("Autosave file is newer."),
426 MakeDisplayPath(s, 50),
427 _("Load that one instead?"))) {
429 // the file is not saved if we load the
433 // Here, we should delete the autosave
439 // not sure if this is the correct place to begin LyXLex
442 if (b->readFile(lex))
451 bool BufferList::exists(string const & s) const
453 return find_if(bstore.begin(), bstore.end(),
454 compare_memfun(&Buffer::fileName, s)) != bstore.end();
458 bool BufferList::isLoaded(Buffer const * b) const
460 BufferStorage::const_iterator cit =
461 find(bstore.begin(), bstore.end(), b);
462 return cit != bstore.end();
466 Buffer * BufferList::getBuffer(string const & s)
468 BufferStorage::iterator it =
469 find_if(bstore.begin(), bstore.end(),
470 compare_memfun(&Buffer::fileName, s));
471 return it != bstore.end() ? (*it) : 0;
475 Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
478 Buffer * b = bstore.newBuffer(name);
480 // use defaults.lyx as a default template if it exists.
482 tname = LibFileSearch("templates", "defaults.lyx");
484 if (!tname.empty() && IsLyXFilename(tname)) {
485 bool templateok = false;
489 if (b->readFile(lex)) {
494 WriteAlert(_("Error!"), _("Unable to open template"),
495 MakeDisplayPath(tname));
496 // no template, start with empty buffer
497 b->paragraph = new LyXParagraph;
499 } else { // start with empty buffer
500 b->paragraph = new LyXParagraph;
503 if (!lyxrc.new_ask_filename) {
507 b->setReadonly(false);
513 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
515 // make sure our path is absolute
516 string const s = MakeAbsPath(filename);
518 // file already open?
520 if (AskQuestion(_("Document is already open:"),
521 MakeDisplayPath(s, 50),
522 _("Do you want to reload that document?"))) {
523 // Reload is accomplished by closing and then loading
524 if (!close(getBuffer(s))) {
527 // Fall through to new load. (Asger)
529 // Here, we pretend that we just loaded the
536 switch (IsFileWriteable(s)) {
539 current_view->owner()->getMiniBuffer()->
540 Set(_("File `") + MakeDisplayPath(s, 50) +
541 _("' is read-only."));
548 b->lyxvc.file_found_hook(s);
550 break; //fine- it's r/w
552 // Here we probably should run
553 if (LyXVC::file_not_found_hook(s)) {
554 // Ask if the file should be checked out for
555 // viewing/editing, if so: load it.
556 if (AskQuestion(_("Do you want to retrieve file under version control?"))) {
557 // How can we know _how_ to do the checkout?
558 // With the current VC support it has to be,
559 // a RCS file since CVS do not have special ,v files.
561 return loadLyXFile(filename, tolastfiles);
564 if (AskQuestion(_("Cannot open specified file:"),
565 MakeDisplayPath(s, 50),
566 _("Create new document with this name?")))
568 // Find a free buffer
569 b = newFile(s, string(), true);
574 if (b && tolastfiles)
575 lastfiles->newFile(b->fileName());