X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fbufferlist.C;h=3f4477636478f3fe8f20590692be00d5112c1ee7;hb=e72ef278b8c4b4bc1b0009cb00a1a56c7b02d2e8;hp=0d4d6602504cc36a5891c3bfd65c196bcf4be86f;hpb=d6665cba427b04ae37f42c846398cad518d2be0f;p=lyx.git diff --git a/src/bufferlist.C b/src/bufferlist.C index 0d4d660250..3f44776364 100644 --- a/src/bufferlist.C +++ b/src/bufferlist.C @@ -19,12 +19,15 @@ #include #include +#include #include "bufferlist.h" #include "lyx_main.h" #include "minibuffer.h" #include "support/FileInfo.h" #include "support/filetools.h" +#include "support/lyxmanip.h" +#include "support/lyxfunctional.h" #include "lyx_gui_misc.h" #include "lastfiles.h" #include "debug.h" @@ -37,11 +40,14 @@ #include "vc-backend.h" #include "TextCache.h" -extern BufferView * current_view; // called too many times in this file... - using std::vector; using std::find; using std::endl; +using std::find_if; +using std::for_each; +using std::mem_fun; + +extern BufferView * current_view; // // Class BufferStorage @@ -66,8 +72,8 @@ Buffer * BufferStorage::newBuffer(string const & s, bool ronly) { Buffer * tmpbuf = new Buffer(s, ronly); tmpbuf->params.useClassDefaults(); - lyxerr.debug() << "Assigning to buffer " - << container.size() << endl; + lyxerr[Debug::INFO] << "Assigning to buffer " + << container.size() << endl; container.push_back(tmpbuf); return tmpbuf; } @@ -88,14 +94,11 @@ bool BufferList::empty() const } -extern bool MenuWrite(Buffer *); -extern bool MenuWriteAs(Buffer *); - bool BufferList::QwriteAll() { bool askMoreConfirmation = false; string unsaved; - for(BufferStorage::iterator it = bstore.begin(); + for (BufferStorage::iterator it = bstore.begin(); it != bstore.end(); ++it) { if (!(*it)->isLyxClean()) { string fname; @@ -104,15 +107,15 @@ bool BufferList::QwriteAll() else fname = MakeDisplayPath((*it)->fileName(), 50); bool reask = true; - while(reask) { - switch(AskConfirmation(_("Changes in document:"), + while (reask) { + switch (AskConfirmation(_("Changes in document:"), fname, _("Save document?"))) { case 1: // Yes if ((*it)->isUnnamed()) - reask = !MenuWriteAs((*it)); + reask = !MenuWriteAs(current_view, (*it)); else { - reask = !MenuWrite((*it)); + reask = !MenuWrite(current_view, (*it)); } break; case 2: // No @@ -160,10 +163,7 @@ void BufferList::closeAll() void BufferList::resize() { - for(BufferStorage::iterator it = bstore.begin(); - it != bstore.end(); ++it) { - (*it)->resize(); - } + for_each(bstore.begin(), bstore.end(), mem_fun(&Buffer::resize)); } @@ -184,14 +184,15 @@ bool BufferList::close(Buffer * buf) fname = MakeDisplayPath(buf->fileName(), 50); bool reask = true; while (reask) { - switch(AskConfirmation(_("Changes in document:"), + switch (AskConfirmation(_("Changes in document:"), fname, _("Save document?"))){ case 1: // Yes if (buf->isUnnamed()) - reask = !MenuWriteAs(buf); + reask = !MenuWriteAs(current_view, buf); else if (buf->save()) { lastfiles->newFile(buf->fileName()); + reask = false; } else { if (buf->getUser()) AllowInput(buf->getUser()); @@ -222,10 +223,8 @@ bool BufferList::close(Buffer * buf) vector const BufferList::getFileNames() const { vector nvec; - for(BufferStorage::const_iterator cit = bstore.begin(); - cit != bstore.end(); ++cit) { - nvec.push_back((*cit)->fileName()); - } + std::copy(bstore.begin(), bstore.end(), + back_inserter_fun(nvec, &Buffer::fileName)); return nvec; } @@ -237,7 +236,7 @@ Buffer * BufferList::first() } -Buffer * BufferList::getBuffer(int choice) +Buffer * BufferList::getBuffer(unsigned int choice) { if (choice >= bstore.size()) return 0; return bstore[choice]; @@ -247,10 +246,10 @@ Buffer * BufferList::getBuffer(int choice) int BufferList::unlockInset(UpdatableInset * inset) { if (!inset) return 1; - for(BufferStorage::iterator it = bstore.begin(); - it != bstore.end(); ++it) { + for (BufferStorage::iterator it = bstore.begin(); + it != bstore.end(); ++it) { if ((*it)->getUser() - && (*it)->getUser()->the_locking_inset == inset) { + && (*it)->getUser()->theLockingInset() == inset) { (*it)->getUser()->insetUnlock(); return 0; } @@ -261,8 +260,8 @@ int BufferList::unlockInset(UpdatableInset * inset) void BufferList::updateIncludedTeXfiles(string const & mastertmpdir) { - for(BufferStorage::iterator it = bstore.begin(); - it != bstore.end(); ++it) { + for (BufferStorage::iterator it = bstore.begin(); + it != bstore.end(); ++it) { if (!(*it)->isDepClean(mastertmpdir)) { string writefile = mastertmpdir; writefile += '/'; @@ -277,62 +276,64 @@ void BufferList::updateIncludedTeXfiles(string const & mastertmpdir) void BufferList::emergencyWriteAll() { - for (BufferStorage::iterator it = bstore.begin(); - it != bstore.end(); ++it) { - if (!(*it)->isLyxClean()) { - bool madeit = false; - - lyxerr <<_("lyx: Attempting to save" - " document "); - if ((*it)->isUnnamed()) - lyxerr << OnlyFilename((*it)->fileName()); - else - lyxerr << (*it)->fileName(); - lyxerr << _(" as...") << endl; - - for (int i = 0; i < 3 && !madeit; ++i) { - string s; - - // We try to save three places: - // 1) Same place as document. - // 2) In HOME directory. - // 3) In "/tmp" directory. - if (i == 0) { - if ((*it)->isUnnamed()) - continue; - s = (*it)->fileName(); - } else if (i == 1) { - s = AddName(GetEnvPath("HOME"), - (*it)->fileName()); - } else { - // MakeAbsPath to prepend the current - // drive letter on OS/2 - s = AddName(MakeAbsPath("/tmp/"), - (*it)->fileName()); - } - s += ".emergency"; - - lyxerr << " " << i + 1 << ") " << s << endl; - - if ((*it)->writeFile(s, true)) { - (*it)->markLyxClean(); - lyxerr << _(" Save seems successful. " - "Phew.") << endl; - madeit = true; - } else if (i != 2) { - lyxerr << _(" Save failed! Trying...") - << endl; - } else { - lyxerr << _(" Save failed! Bummer. " - "Document is lost.") - << endl; - } - } + for_each(bstore.begin(), bstore.end(), + class_fun(*this, &BufferList::emergencyWrite)); +} + + +void BufferList::emergencyWrite(Buffer * buf) +{ + // No need to save if the buffer has not changed. + if (buf->isLyxClean()) return; + + lyxerr << fmt(_("lyx: Attempting to save document %s as..."), + buf->isUnnamed() ? OnlyFilename(buf->fileName()).c_str() + : buf->fileName().c_str()) << endl; + + // We try to save three places: + + // 1) Same place as document. Unless it is an unnamed doc. + if (!buf->isUnnamed()) { + string s = buf->fileName(); + s += ".emergency"; + lyxerr << " " << s << endl; + if (buf->writeFile(s, true)) { + buf->markLyxClean(); + lyxerr << _(" Save seems successful. Phew.") << endl; + return; + } else { + lyxerr << _(" Save failed! Trying...") << endl; } } + + // 2) In HOME directory. + string s = AddName(GetEnvPath("HOME"), buf->fileName()); + s += ".emergency"; + lyxerr << " " << s << endl; + if (buf->writeFile(s, true)) { + buf->markLyxClean(); + lyxerr << _(" Save seems successful. Phew.") << endl; + return; + } + + lyxerr << _(" Save failed! Trying...") << endl; + + // 3) In "/tmp" directory. + // MakeAbsPath to prepend the current + // drive letter on OS/2 + s = AddName(MakeAbsPath("/tmp/"), buf->fileName()); + s += ".emergency"; + lyxerr << " " << s << endl; + if (buf->writeFile(s, true)) { + buf->markLyxClean(); + lyxerr << _(" Save seems successful. Phew.") << endl; + return; + } + lyxerr << _(" Save failed! Bummer. Document is lost.") << endl; } + Buffer * BufferList::readFile(string const & s, bool ronly) { Buffer * b = bstore.newBuffer(s, ronly); @@ -404,12 +405,8 @@ Buffer * BufferList::readFile(string const & s, bool ronly) bool BufferList::exists(string const & s) const { - for (BufferStorage::const_iterator cit = bstore.begin(); - cit != bstore.end(); ++cit) { - if ((*cit)->fileName() == s) - return true; - } - return false; + return find_if(bstore.begin(), bstore.end(), + compare_memfun(&Buffer::fileName, s)) != bstore.end(); } @@ -423,12 +420,10 @@ bool BufferList::isLoaded(Buffer const * b) const Buffer * BufferList::getBuffer(string const & s) { - for(BufferStorage::iterator it = bstore.begin(); - it != bstore.end(); ++it) { - if ((*it)->fileName() == s) - return (*it); - } - return 0; + BufferStorage::iterator it = + find_if(bstore.begin(), bstore.end(), + compare_memfun(&Buffer::fileName, s)); + return it != bstore.end() ? (*it) : 0; } @@ -460,10 +455,11 @@ Buffer * BufferList::newFile(string const & name, string tname, bool isNamed) b->paragraph = new LyXParagraph; } - if (!lyxrc.new_ask_filename) { - if (!isNamed) - b->setUnnamed(); + if (!lyxrc.new_ask_filename && !isNamed) { + b->setUnnamed(); + b->setFileName(name); } + b->setReadonly(false); return b;